Since quite a while, I've been thinking about how to expand this concept further, not only in terms of dynamic libraries, but also all kind of other dynamically loaded resources.
Just do an "strace" on any application startup and conceptually, every single of sometimes thousands "ENOENT (No such file or directory)" is a wasted compute cycle where the resource's location could've been pre-determined.
One could argue that in the context of user applications (e.g. themes or plugins installed by a user) this does not apply, but even there something like home-manager could prevent this. Sure, this would require the usage of home-manager for every minor change, but what if there was a generic interface (e. g. with home-manager as a potential provider) applications could make use of to provision those kind of resources instead of having to manage them on their own?
>Static linking throws away sharing. Dynamic linking throws away immutability. On an immutable store, you shouldn’t have to pick.
This got me thinking: what if we gave each shared library a static virtual address? x86-64 has 256TB of virtual address space, which should comfortably accommodate any typical system's libraries (my NixOS host has 6999 libraries comprising about 3.9GB of data). Applications could then statically link against those addresses (in the sense of hard coding the addresses, not bringing the full library text into their assembly) but those pages could still be shared between processes. Linking at runtime then just becomes ordinary mapping of virtual memory with no patching. If you wanted to support multiple library sets (e.g. arising from "epochs") then the applications could specify their library set metadata via a per set linker that is essentially just a file-to-address map.
The main obstacles I see here are:
1. You need OS support (I assume) to fix the memory addresses, and you're giving up on ASLR.
2. It's not immediately obvious how to make this work with a Nix build. The most natural way to approach it is to assign an address to each library after all the libraries are known, but everything is already compiled by that point. Perhaps a two-phased approach could work: compile everything normally, build the dependency graph, then make another pass to patch (or rebuild) everything. Alternately, if you knew the build graph ahead of time, then you should be able to work out some way to give each build a unique address range upfront (at the cost of wasting some address space, since you won't know the exact size of each library).
(This is all a bit outside my core expertise, and I may be missing some obvious problems or prior art)
Addendum: it looks like this is exactly what prelink (https://linux.die.net/man/8/prelink) does except that it needs to defensively check for any changes to the libraries because it cannot assume immutability. Whether the ASLR provided by prelink is sufficient would depend on your threat model.
ASLR is generally preferred because it makes finding gadgets or other things harder for an attacker.
However, if we forgo ASLR of libraries we can do something like this without needing the OS to help. We just need a loader that always loads each shared library at the same location or a location that can be calculated at ahead of time for that library. That rule could probably be more efficient than just giving each unique library a portion of the address space since not every app needs all libraries.
Then when compiling the application we could have like a generated .S file or something that just defines all the symbols we need from the library at the correct relative position/address. Then in our linker script we treat that area like similar to .bss (e.g. a NOLOAD for the sections at that address).
Doing that gets rid of the extra layer of indirection since it behaves like it's statically linked.
I have also generally thought for dynamic linking something BASE + Address is better for dynamic linking. It pretty simple and quick as you show.
However, I think the reason the for the whole per symbol resolution is, like take a library and it is updated on the system now all your offsets potentially change. You also run into the issue that a symbol may move from one library to an other over time. Plus other things, but that kinda sums up why we have per symbol resolution.
Although, I do like what is being proposed. Although it seems to only work if the number of symbols per library stay the same or are not moved to other libraries. Although, it sounds like they are mainly targeting things considering environment is relatively stable.
Although I guess what is not clear is where the table they generate lives? Sounds like the executable or their own loader?
If a library changed all downstream dependencies of it (applications) in Nix would need to be rebuilt anyways -- that is the cost of the input-addressed (pessimistic) model.
Although I was thinking if it could maybe used more generally. If the table is separate or part of storeld loader that the application uses. Couldn't we also change/update just that table on a system update. The compiled application could still keep the same offsets without recompiling. That keeps updating the table separate from the application loading.
Basically, becomes an ahead of time symbol resolution. Although, you still have the issue if the symbol moved to some other lib I think, and definitely if it was removed.
That's what the thesis tried to generalize as "epochs". All modern day systems have them, whether you are using apt-get or dnf, there is a point in time to do the one-time calculation of offsets.
Since quite a while, I've been thinking about how to expand this concept further, not only in terms of dynamic libraries, but also all kind of other dynamically loaded resources.
Just do an "strace" on any application startup and conceptually, every single of sometimes thousands "ENOENT (No such file or directory)" is a wasted compute cycle where the resource's location could've been pre-determined.
One could argue that in the context of user applications (e.g. themes or plugins installed by a user) this does not apply, but even there something like home-manager could prevent this. Sure, this would require the usage of home-manager for every minor change, but what if there was a generic interface (e. g. with home-manager as a potential provider) applications could make use of to provision those kind of resources instead of having to manage them on their own?
>Static linking throws away sharing. Dynamic linking throws away immutability. On an immutable store, you shouldn’t have to pick.
This got me thinking: what if we gave each shared library a static virtual address? x86-64 has 256TB of virtual address space, which should comfortably accommodate any typical system's libraries (my NixOS host has 6999 libraries comprising about 3.9GB of data). Applications could then statically link against those addresses (in the sense of hard coding the addresses, not bringing the full library text into their assembly) but those pages could still be shared between processes. Linking at runtime then just becomes ordinary mapping of virtual memory with no patching. If you wanted to support multiple library sets (e.g. arising from "epochs") then the applications could specify their library set metadata via a per set linker that is essentially just a file-to-address map.
The main obstacles I see here are:
1. You need OS support (I assume) to fix the memory addresses, and you're giving up on ASLR.
2. It's not immediately obvious how to make this work with a Nix build. The most natural way to approach it is to assign an address to each library after all the libraries are known, but everything is already compiled by that point. Perhaps a two-phased approach could work: compile everything normally, build the dependency graph, then make another pass to patch (or rebuild) everything. Alternately, if you knew the build graph ahead of time, then you should be able to work out some way to give each build a unique address range upfront (at the cost of wasting some address space, since you won't know the exact size of each library).
(This is all a bit outside my core expertise, and I may be missing some obvious problems or prior art)
Addendum: it looks like this is exactly what prelink (https://linux.die.net/man/8/prelink) does except that it needs to defensively check for any changes to the libraries because it cannot assume immutability. Whether the ASLR provided by prelink is sufficient would depend on your threat model.
ASLR is generally preferred because it makes finding gadgets or other things harder for an attacker.
However, if we forgo ASLR of libraries we can do something like this without needing the OS to help. We just need a loader that always loads each shared library at the same location or a location that can be calculated at ahead of time for that library. That rule could probably be more efficient than just giving each unique library a portion of the address space since not every app needs all libraries.
Then when compiling the application we could have like a generated .S file or something that just defines all the symbols we need from the library at the correct relative position/address. Then in our linker script we treat that area like similar to .bss (e.g. a NOLOAD for the sections at that address).
Doing that gets rid of the extra layer of indirection since it behaves like it's statically linked.
This is how things prior to ASLR could work.
I have also generally thought for dynamic linking something BASE + Address is better for dynamic linking. It pretty simple and quick as you show.
However, I think the reason the for the whole per symbol resolution is, like take a library and it is updated on the system now all your offsets potentially change. You also run into the issue that a symbol may move from one library to an other over time. Plus other things, but that kinda sums up why we have per symbol resolution.
Although, I do like what is being proposed. Although it seems to only work if the number of symbols per library stay the same or are not moved to other libraries. Although, it sounds like they are mainly targeting things considering environment is relatively stable.
Although I guess what is not clear is where the table they generate lives? Sounds like the executable or their own loader?
If a library changed all downstream dependencies of it (applications) in Nix would need to be rebuilt anyways -- that is the cost of the input-addressed (pessimistic) model.
A perfect fit!
Although I was thinking if it could maybe used more generally. If the table is separate or part of storeld loader that the application uses. Couldn't we also change/update just that table on a system update. The compiled application could still keep the same offsets without recompiling. That keeps updating the table separate from the application loading.
Basically, becomes an ahead of time symbol resolution. Although, you still have the issue if the symbol moved to some other lib I think, and definitely if it was removed.
That's what the thesis tried to generalize as "epochs". All modern day systems have them, whether you are using apt-get or dnf, there is a point in time to do the one-time calculation of offsets.
I suggest going further and just using static linking. It removes so much pain and may improve result performance and decrease program size.