Environmen Variables

Technical overview

1
2
3
4
5
6
7
8
9
10
echo "Hello World"
mkdir test
cp $(which echo) test/myecho
myecho "Hello World"
echo $PATH
export PATH=$PATH:/home/seed/test
echo $PATH
myecho "Hello World"

:::::{tab-set} ::::{tab-item} envp

1
2
3
4
gcc -w -o my_envp /local/repository/setup_scripts/software/env/my_envp.c
./my_envp

:::: ::::{tab-item} environ

1
2
3
4
gcc -w -o my_environ /local/repository/setup_scripts/software/env/my_environ.c
./my_environ

:::: :::::

1
2
3
4
5
6
7
gcc -w -o passenv /local/repository/setup_scripts/software/env/passenv.c
./passenv
./passenv 1
./passenv 2
./passenv 3

Memory placement of environment variables in process
1
2
3
4
5
6
7
8
9
10
gcc -g -o my_envp_gdb my_envp.c
gdb my_envp_gdb
gdb-peda$ b main
gdb-peda$ run
gdb-peda$ n
gdb-peda$ p argv
gdb-peda$ p envp
gdb-peda$ x/s *((char **)envp + 1)

1
2
3
4
5
6
7
strings /proc/$$/environ | grep LOGNAME
echo $LOGNAME
LOGNAME=ram
echo $LOGNAME
strings /proc/$$/environ | grep LOGNAME

Shell variables
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
## Attack surface caused by environment variables







<figure
  
>
  <picture>
    <!-- Auto scaling with imagemagick -->
    <!--
      See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
      https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
    -->
    
      
        <source
          class="responsive-img-srcset"
          
            srcset="/assets/img/courses/csc302/env/attack_surface-480.webp 480w,/assets/img/courses/csc302/env/attack_surface-800.webp 800w,/assets/img/courses/csc302/env/attack_surface-1400.webp 1400w,"
            type="image/webp"
          
          
            sizes="95vw"
          
        >
      
    
    <img
      src="/assets/img/courses/csc302/env/attack_surface.png"
      
      
        width="50%"
      
      
        height="auto"
      
      
      
        alt="Attack surface caused by environment variables"
      
      
      
        data-zoomable
      
      
        loading="lazy"
      
      onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
    >
  </picture>

  
</figure>




- Linking finds the external library code referenced in the 
program and links the code to the program (think import in Java).
- Linking can be done when a program is compiled
  - Static linking
  - Requires a lot of memory
  - Compiled external functions are static and cannot be updated/patched

~~~bash

gcc -o my_envp my_envp.c
gcc -static -o my_envp_static my_envp.c
ls -lh my_envp*

~~~

- Linking can be done on the fly
  - External functions are linked at run time, called `shared libraries`.
  - `.so` on Unix system, `.dll` on Windows system
   - `ldd` can be used to see what shared libraries a program depends on. 

~~~bash

ldd my_envp_static
ldd my_envp

~~~



- Part of what is being executed is undecided during compilation time, when 
the developer has full control.
- Instead, the binaries rely on linked libraries, which are determined during 
runtime, under users' control.
- Examples: **LD_PRELOAD** and **LD_LIBRARY_PATH**
  - During the linking stage, the Linux system search for library 
  functions from default locations.
  - These locations are determined via **LD_PRELOAD** and **LD_LIBRARY_PATH**

~~~bash

echo $LD_PRELOAD
echo $LD_LIBRARY_PATH
cp /local/repository/setup_scripts/software/env/linking.c .
gcc -o linking linking.c
./linking
cp /local/repository/setup_scripts/software/env/sleep.c .
gcc -c sleep.c
gcc -shared -o libmylib.so.1.0.1 sleep.o
export LD_PRELOAD=./libmylib.so.1.0.1
echo $LD_PRELOAD
./linking
export LD_PRELOAD=""
./linking

~~~



- Linux system libraries, `ld.so` or `ld-linux.so`, implement a countermeasure, which 
ignores **LD_PRELOAD** and **LD_LIBRARY_PATH** when there is a difference in a process' real 
and effective user IDs or group IDs.

~~~bash

cp $(which env) myenv
export LD_PRELOAD=./libmylib.so.1.0.1
export LD_LIBRARY_PATH=.
export LD_MYOWN="my own EV"
env
env | grep LD_
myenv | grep LD_
sudo chown root myenv
sudo chmod 4755 myenv
myenv | grep LD_

~~~




- `PATH` environment variable

~~~bash

echo $PATH

~~~

- Many system commands are placed in `/usr/bin` or `/bin`, which are 
mapped in `PATH`

~~~bash

cp /local/repository/setup_scripts/software/env/vul.c .
gcc -o vul vul.c
./vul
cp /local/repository/setup_scripts/software/env/cal.c .
gcc -o cal cal.c
sudo chown root vul
sudo chmod 4755 vul
export PATH=.:$PATH
vul
$ id
$ exit

~~~

- Open a new terminator shell to reset `PATH` to default. 
- What happens when you switch the order of the new `PATH` 
export for the previous exercise?
- Why?

~~~bash

cp /local/repository/setup_scripts/software/env/vul.c .
gcc -o vul vul.c
./vul
cp /local/repository/setup_scripts/software/env/cal.c .
gcc -o cal cal.c
sudo chown root vul
sudo chmod 4755 vul
export PATH=$PATH:.
vul
$ id
$ exit

~~~




- Happens when application uses environment variables, 
which can be manipulated. 


~~~bash

cp /local/repository/setup_scripts/software/env/pwd.c .
echo $PWD
gcc -o pwd pwd.c
./pwd
cd /tmp
~/pwd
PWD=randomdir
~/pwd

~~~








<figure
  
>
  <picture>
    <!-- Auto scaling with imagemagick -->
    <!--
      See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
      https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
    -->
    
      
        <source
          class="responsive-img-srcset"
          
            srcset="/assets/img/courses/csc302/env/service_based-480.webp 480w,/assets/img/courses/csc302/env/service_based-800.webp 800w,/assets/img/courses/csc302/env/service_based-1400.webp 1400w,"
            type="image/webp"
          
          
            sizes="95vw"
          
        >
      
    
    <img
      src="/assets/img/courses/csc302/env/service_based.png"
      
      
        width="50%"
      
      
        height="auto"
      
      
      
        alt="Service-based approach"
      
      
      
        data-zoomable
      
      
        loading="lazy"
      
      onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
    >
  </picture>

  
</figure>