c – What is the need of randomizing memory addresses for loading libraries? – Education Career Blog

ldd displays the memory addresses where the shared libraries are linked at runtime

$ cat one.c 

int main() {
    printf ("%d", 45);
$ gcc one.c -o one -O3
$ ldd one
    linux-gate.so.1 =>  (0x00331000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00bc2000)
    /lib/ld-linux.so.2 (0x006dc000)

From this answer to another question,

… The addresses are basically random numbers. Before secure implementations were devised, ldd would consistently indicate the memory addresses where the program sections were loaded. Since about five years ago, many flavors of Linux now intentionally randomize load addresses to frustrate would-be virus writers, etc.

I do not fully understand how these memory addresses can be used for exploitations.

Is the problem something like “If the addresses are fixed, one can put some undesirable code at that address which would be linked as if it was a library” or is it something more than this?


“If the addresses are fixed, one can put some undesirable code at that address which would be linked as if it was a library”


Also. Buffer overflow exploits require a consistent memory model so that the bytes that overflow the buffer do known things to known parts of the code.

http://www.corewars.org/ A great illustration of the principle.


Some vulnerabilities allow overwriting some address (stack overflows allow overwriting return addresses, exploit for heap overflows typically overwrite SEH pointers on Win32 and addresses (GOT entries) of dynamically called functions on Linux, …). So the attacker needs to make the overwritten address point to something interesting. To make this more difficult, several counter-measures have been adopted:

  • Non-executable stacks prevents exploits from just jumping to some code the attacker has put on the stack.
  • W^X segments (segments which can never be writable and executable at the same time) prevents the same for other memory areas.
  • Randomized load addresses for libraries and position independent executables decrease the probabilities of succesful exploitation via return-into-libc and return-oriented-programming techniques, …
  • Randomized load addresses also prevent attackers from knowing in advance where to find some interesting function (e.g: imagine an attacker that can overwrite the GOT entry and part of the message for the next logging call, knowing the address of system would be “interesting”).

So, you have to view load address randomization as another counter-measure among many (several layers of defense and all that).

Also note that exploits aren’t restricted to arbitrary code execution. Getting a program to print some sensitive information instead of (or in addition to, think of string truncation bugs) some non-sensitive information also counts as an exploit; it would not be difficult to write some proof-of-concept program with this kind of vulnerability where knowing absolute addresses would make reliable exploits possible.

You should definitely take a look at return-into-libc and return-oriented-programming. These techniques make heavy use of knowledge of addresses in the executable and libraries.

And finally, I’ll note there are two ways to randomize library load addresses:

  • Do it on every load: this makes (some) exploits less reliable even if an attacker can obtain info about addresses on one run and try to use that info on another run.
  • Do it once per system: this is what prelink -R does. It avoids attackers using generic information for e.g: all Redhat 7.2 boxes. Obviously, its advantage is that it doesn’t interfere with prelink :).


A simple example:

If on a popular operating system the standard C library was always loaded at address 0x00100000 and a recent version of the standard C library had the system function at offset 0x00000100 then if someone were able to exploit a flaw in a program running on a computer with this operating system (such as a web server) causing it to write some data to the stack (via a buffer overrun) they would know that it was very likely that if they wrote 0x00100100 to the place on the stack where the current function expected its return address to be then they could make it so that upon returning from the current function the system function would be called. While they still haven’t done everything needed to cause system to execute something that they want it to, they are close, and there are some tricks writing more stuff to the stack aver the address mentioned above that have a high likelihood of resulting in a valid string pointer and a command (or series of commands) being run by this forced call to system.

By randomizing the addresses at which libraries are loaded the attacker is more likely to just crash the web server than gain control of the system.


The typical method is by a buffer overrun, where you put a particular address on the stack, and then return to it. You typically pick an address in the kernel where it assumes the parameters you’ve passed it on the stack have already been checked, so it just uses them without any further checking, allowing you to do things that normally wouldn’t be allowed.

Leave a Comment