-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_env_var_addy.c
74 lines (60 loc) · 2.3 KB
/
find_env_var_addy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <mcheck.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define MAXBUFFERSIZE 4096
extern char **environ;
/* As I mentioned in my_setenv.c comments, writing my own
* implementations of those syscalls made me wonder where
* setenv() stores the actual new strings pointed to by
* char **environ. You can't make them local to the function,
* since the stack frame is torn down when it returns. Maybe
* it puts them in the same place as the ones it already
* points to, i.e. above the stack? I should be able to figure
* this out with a gdb session.
*
* This program is going to mess around with setenv() and see
* if I can find out where the char pointer pointers are
* pointing to.
*
* And it turns out, setenv() uses the heap for the strings
* it makes char **environ point to. This makes sense, since
* if the space above the stack was going to be used, it
* would have to be allocated for the largest possible
* strings that can be held as environment variables, which
* isn't a good use of virtual memory space. My next question
* is, how is that memory free'd at the end of the program?
*
* */
int main(int argc, char *argv[]) {
mtrace();
printf("\nsbrk(0) = %p\n", sbrk(0));
printf("Original environment variables:\n\n");
printf("\nsbrk(0) = %p\n", sbrk(0));
char **ep;
for (ep = environ; *ep != NULL; ep++) {
printf("Virtual memory address: %p\n", *ep);
puts(*ep);
printf("Next char pointer should be located at %p\n", *ep + strlen(*ep) + 1);
printf("\n");
}
printf("\nsbrk(0) = %p\n", sbrk(0));
printf("\n\n#####################################################\n\n");
printf("\n\n### Changing 'HOME' environment variable... ###\n\n");
setenv("HOME", "NEW_HOME", 1);
printf("\nsbrk(0) = %p\n", sbrk(0));
for (ep = environ; *ep != NULL; ep++) {
printf("Virtual memory address: %p\n", *ep);
puts(*ep);
printf("Next char pointer should be located at %p\n", *ep + strlen(*ep) + 1);
printf("\n");
}
printf("\nsbrk(0) = %p\n", sbrk(0));
return EXIT_SUCCESS;
}