Don't ask why I am not posting this at Stack. So I have this small C program. It returns the address of the environment variable specified in argv[1].

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    printf("%s is at %p\n", argv[1], getenv(argv[1]));

What's irritating is that on Ubuntu Linux 2.6.20-15 x86 it returns the same address every time the program is run.

enter image description here

However on Ubuntu Linux 3.2.0-37 x86 it returns a different address each time. So what's new with the new kernel?

enter image description here

  • You're leaving out crucial details. What do you mean "different address"? Examples? – Isaac Rabinovitch Feb 15 '13 at 23:07
  • 1
    This might be related to ASLR, which causes every new process to have different locations for their stack. – user1686 Feb 15 '13 at 23:23
  • @grawity as per Wikipedia: Linux has enabled a weak form of ASLR by default since kernel version 2.6.12 (released June 2005). – WikiWitz Feb 15 '13 at 23:33
  • To potential close voters: This is a question about the kernel's behavior. The C code is just used illustrate the "symptoms". I think this is perfectly on topic here. – Dennis Feb 16 '13 at 13:30
  • It was able to find your desired env variable and return an address for it... it works exactly as described on the tin... expecting the same address is an odd concept. If you add or remove env vars, the address would change, ASLR has been around a while too. What would be strange would be you not getting an address. It's all the same, regardless where the data is stored. (and turtles all the way down!) – lornix Jun 16 '14 at 9:14

The Linux kernel has a feature that does memory address randomization for security. Its purpose is to prevent reliable buffer overflow attacks. Possibly you have that enabled on one box, but not on the other. check sysctl for howto enable and disable it.

| improve this answer | |
  • 1
    Issue echo 0 > /proc/sys/kernel/randomize_va_space to disable ASLR. – Jingguo Yao Nov 8 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.