You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By convention, the elements of char ** argv and char ** environ point at a contiguous block of memory that is divided up into C-style nul terminated strings. By definition, the pointer char ** environ can be reassigned and subsequent functions such as getenv() will respect the new location.
At program start, you can copy the memory block starting at enrivon[0] and ending at environ[num_envs-1] + strlen(environ[num_envs-1]) + 1 to a new location, and reassign the environ[...] pointers to locations in the new block. You could also strdup() each string.
To find out the number of environment variables:
size_tnum_envs=0;
while (environ[num_envs]) num_envs++;
Or strdup each string:
for (size_ti=0; environ[i]; i++) {
environ[i] =strdup(environ[i]);
}
Once you have copied away the contents of environ, you have the entire block of memory starting at argv[0] and ending at the original environ[num_envs-1] + strlen(environ[num_envs-1]) + 1. This is the safe memory space that you can overwrite, and have it picked up by ps, etc.
Finally, do not fill with spaces. Fill with nul bytes. You might have a solid 1K of argv + environ space, and you wouldn't want that many blank spaces in your ps output!
The text was updated successfully, but these errors were encountered:
Your approach here is flat out wrong and dangerous. Please read other people's code first to see how to approach this problem safely: https://www.google.com/search?q=setproctitle.c
By convention, the elements of
char ** argv
andchar ** environ
point at a contiguous block of memory that is divided up into C-style nul terminated strings. By definition, the pointerchar ** environ
can be reassigned and subsequent functions such as getenv() will respect the new location.At program start, you can copy the memory block starting at
enrivon[0]
and ending atenviron[num_envs-1] + strlen(environ[num_envs-1]) + 1
to a new location, and reassign theenviron[...]
pointers to locations in the new block. You could also strdup() each string.To find out the number of environment variables:
Or strdup each string:
Once you have copied away the contents of environ, you have the entire block of memory starting at
argv[0]
and ending at the originalenviron[num_envs-1] + strlen(environ[num_envs-1]) + 1
. This is the safe memory space that you can overwrite, and have it picked up by ps, etc.Finally, do not fill with spaces. Fill with nul bytes. You might have a solid 1K of argv + environ space, and you wouldn't want that many blank spaces in your ps output!
The text was updated successfully, but these errors were encountered: