[LINUX] ENV VAR addresses

P

pater

Following a book, i read that the address of the environment variables
depends on the program name... and it won't change until u change it.

Making some tries, on my ubuntu, i found out that it changes every
time i execute the program. So, there's no way to predict what
addresses the env vars would take?

I need these infos cuz i need to put datas in the stack to be easily
picked up by the program itself, without using getenv, only with an
address.
 
A

Andrew Smallshaw

Following a book, i read that the address of the environment variables
depends on the program name... and it won't change until u change it.

Making some tries, on my ubuntu, i found out that it changes every
time i execute the program. So, there's no way to predict what
addresses the env vars would take?

I need these infos cuz i need to put datas in the stack to be easily
picked up by the program itself, without using getenv, only with an
address.

Traditionally with Unix the environment is passed to the main
function as a usually ignored third parameter, conventionally known
as envp if memory serves. The code below is a simple 'env' command:

#include <stdio.h>

int main(int argc, char *argv[], char *envp[])
{
int i;

for (i = 0; envp != NULL; i++)
printf("%s\n", envp);

return 0;
}

No idea if envp made it into POSIX, but the above works fine on
this NetBSD system with gcc 3.3.3. None of this is covered by ANSI
C of course.
 
F

Flash Gordon

pater wrote, On 14/12/08 21:10:
Following a book, i read that the address of the environment variables
depends on the program name... and it won't change until u change it.

You read wrong.
Making some tries, on my ubuntu, i found out that it changes every
time i execute the program. So, there's no way to predict what
addresses the env vars would take?

There is no standard way to do it.
I need these infos cuz i need to put datas in the stack to be easily
picked up by the program itself, without using getenv, only with an
address.

There is no standard way to do it, and there may well be no non-standard
way to do it either. I suggest that instead of trying to do things in
some strange and non-standard way you use a standard mechanism, such as
command-line parameters or environment variables.
 
R

Richard Tobin

pater said:
Making some tries, on my ubuntu, i found out that it changes every
time i execute the program. So, there's no way to predict what
addresses the env vars would take?

Some modern operating systems, including Linux, deliberately randomise
the stack address to make buffer-overflow and similar attacks more
difficult. See

http://en.wikipedia.org/wiki/Address_space_layout_randomization
I need these infos cuz i need to put datas in the stack to be easily
picked up by the program itself, without using getenv, only with an
address.

I can't see any good reason to do that. It makes your program
unportable and, as you see, is rather difficult to achieve.

-- Richard
 
C

Chris Dollin

pater said:
Following a book, i read that the address of the environment variables
depends on the program name... and it won't change until u change it.

Making some tries, on my ubuntu, i found out that it changes every
time i execute the program. So, there's no way to predict what
addresses the env vars would take?

I need these infos cuz i need to put datas in the stack to be easily
picked up by the program itself, without using getenv, only with an
address.

You're making life difficult for yourself. If they're environment
variables, use the environment variable mechanisms; don't mess around
trying to shortcut things, because it will almost certainly cost you,
later if not sooner.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top