Loadable libs Position Indepent code vs Unfixed base address

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
lopidas
Member
Member
Posts: 65
Joined: Sun May 26, 2013 10:12 am

Loadable libs Position Indepent code vs Unfixed base address

Post by lopidas »

What are pros and cons of them?
Some other shared libraries approaches?
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by dozniak »

runtime relocations?
Learn to read.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by thepowersgang »

I presume you're talking about the two philosophies used by ELF and PE. In short, Position independent code can be slightly slower to execute than statically linked code, but is faster to load and can always be shared between processes. On the flip side, runtime relocations (PE-style) mean that if the address chosen at compile time is unavailable, very expensive relocations need to be done (taking up time and memory), these also require far more relocation entries than PIC.

Nowdays, PIC is probably the best, as modern CPUs have pretty good PC-relative addressing and the like, but back in protected mode, static linking was quite a bit faster.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by Love4Boobies »

While x86-64, ARM, etc. have improved the PIC situation with the addition of RIP-relative addressing, PC-relative instructions, etc., respectively, the most efficient solution in terms of performance is to forget about shared objects and only use position-dependent code. The trade-off is pretty good considering the fact that data typically takes up a lot more memory.

However, if you do want shared objects, PIC is the recommended way to go as otherwise the text segment would require runtime modifications (leading to memory and performance overheads, since we're talking about a read-only segment) that would ironically result in it becoming non-sharable. On the other hand, PIC uses indirections via tables like the GOT and the PLT, which reside in the data segment.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by dozniak »

Patching relocations in absence of single-address-space memory has a drawback that no library code could be shared between two processes; unless they are loaded at fixed virtual addresses.
Learn to read.
greyOne
Member
Member
Posts: 58
Joined: Sun Feb 03, 2013 10:38 pm
Location: Canada

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by greyOne »

I'm going to also bring up that position independent code might be a little more practical to use in lower level environments (ie. kernel modules, etc.).
At least, that's the case in my experience.

For example, it's quite easy to relocate ELF files in memory to whatever address modules are loaded to.
(Potentially that being wherever it is that the boot loader put them, although that might cause issues later.)
They don't need much more than a simple move in memory, symbol address adjustment, bit of linking and perhaps a couple of page mappings.
lopidas
Member
Member
Posts: 65
Joined: Sun May 26, 2013 10:12 am

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by lopidas »

Thanks all for info. Are there some Cons of PE approach?
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by Owen »

Love4Boobies wrote:While x86-64, ARM, etc. have improved the PIC situation with the addition of RIP-relative addressing, PC-relative instructions, etc., respectively, the most efficient solution in terms of performance is to forget about shared objects and only use position-dependent code. The trade-off is pretty good considering the fact that data typically takes up a lot more memory.
Theres a good security argument (defense in depth) that code at fixed addresses should be deprecated, as it makes stack smashing attacks (and similar) significantly easier and more effective (particularly in combination with e.g. return-to-libc attacks). Not only does it mean that all functions are at fixed addresses, it means that the locations of the R/W data pages are fixed also (which may make attacks easier or harder, depending upon how difficult it is for the attacker to invoke e.g. mprotect)

(Another good argument for shared libraries is, of course, in ensuring that all library code is kept up to date)
lopidas
Member
Member
Posts: 65
Joined: Sun May 26, 2013 10:12 am

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by lopidas »

I said it bad I wanted good things about windows approach?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by Combuster »

lopidas wrote:good things about windows approach?
In that formulation, a religious "no", perhaps? :wink:

Relocatable or fixed position code is significantly faster (~10%) on 32-bit x86 at runtime. On 64-bit, that difference is neglegible. Security can still be fixed using ASLR, but memory overhead and load times are potentially an order of magnitude worse.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by Love4Boobies »

Personally, I feel the fault lies with programming languages or implementations thereof. The OS shouldn't try to protect vulnerable code from itself. With safe languages and/or language implementations, the code would avoid vulnerabilities like buffer overflows altogether instead of obfuscating them.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by Owen »

Love4Boobies wrote:Personally, I feel the fault lies with programming languages or implementations thereof. The OS shouldn't try to protect vulnerable code from itself. With safe languages and/or language implementations, the code would avoid vulnerabilities like buffer overflows altogether instead of obfuscating them.
There is a lot to be said for defense in depth. ASLR is a mitigation factor for buffer overflows. Obviously this doesn't obviate the issue of the buffer overflow, just reduces the exploitability of it.

Every line of defense you use is one additional hurdle your attacker has to jump over... and maybe, just maybe, the difference between a DOS and a privilege escalation. Or, if nothing else, the difference between a zero-day exploit and getting the patch out before the exploit is in the wild.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by Love4Boobies »

lopidas wrote:I said it bad I wanted good things about windows approach?
I thought the advantages and disadvantages of both formats were summarized fairly well in this thread. Here is the same summary in simpler terms:

In a nutshell, PE DLL's may have larger loading overhead and sharing them is not guaranteed; all due to rebasing. However, you may precalculate the base so as to drastically decrease the probability of rebasing being required in the first place. There is a catch, though: For this to work, you must give up ASLR, whose point is to reduce the likelihood of certain vulnerabilities (i.e., some buffer overflows) being exploited.

On the other hand, PIC with ELF shared objects is somewhat slower to run, albeit libraries are sometimes noticeably faster to load. Furthermore, sharing between different processes is always possible, even with ASLR.

Whether or not you design your loader to do ASLR is entirely up to you; as you can see, different people have different opinions on this.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
lopidas
Member
Member
Posts: 65
Joined: Sun May 26, 2013 10:12 am

Re: Loadable libs Position Indepent code vs Unfixed base add

Post by lopidas »

Thanks, nice for nice summary.
Post Reply