Computer Security Assignment2
Computer Security Assignment2
Palvi Aggarwal
Lab environment: This lab has been tested on our pre-built Ubuntu 20.04 VM, which can be downloaded
from the SEED website.
-------------------------------------------------------------------------------------------------------------------------------
• Use printenv or env command to print out the environment variables. If you are interested in
some environment variables, such as PWD, you can use "printenv PWD" or "env |grep PWD".
• Use export and unset to set or unset environment variables. It should be noted that these two
commands are not separate programs; they are two of the Bash’s internal commands (you will not
be able to find them outside of Bash).
(20 Points) Task 2: Passing Environment Variables from Parent Process to Child Process
In this task, we study how a child process gets its environment variables from its parent. In Unix, fork ()
creates a new process by duplicating the calling process. The new process, referred to as the child, is an
exact duplicate of the calling process, referred to as the parent; however, several things are not inherited
by the child (please see the manual of fork () by typing the following command: man fork). In this task,
we would like to know whether the parent’s environment variables are inherited by the child process or
not.
a. Please compile and run “myprintenv.c” and describe your observation. Because the output
contains many strings, you should save the output into a file, such as using a.out > child (if a.out
is your executable file name).
b. Now comment out the printenv() statement in the child process case and uncomment the
printenv() statement in the parent process case. Compile and run the code again and describe your
observation. Save the output in another file.
c. Compare the difference of these two files using the diff command. Please draw your conclusion.
(20 Points) Task 3: Environment Variables, execve() and system()
In this task, we study how environment variables are affected when a new program is executed via
execve(). The function execve() calls a system call to load a new command and execute it; this function
never returns.
No new process is created; instead, the calling process’s text, data, bss, and stack are overwritten by that
of the program loaded. Essentially, execve() runs the new program inside the calling process. We are
interested in what happens to the environment variables; are they automatically inherited by the new
program?
a) Please compile and run “myenv.c” and describe your observation. This program simply executes
a program called /usr/bin/env, which prints out the environment variables of the current process.
b) Change the invocation of execve() in the code to the following line and describe your
observation. execve("/usr/bin/env", argv, environ)
c) Please draw your conclusion regarding how the new program gets its environment variables.
Dr. Palvi Aggarwal
d) How would you use system() function to do the above task? Please explain the difference
between system() and execve() system calls.