1.7. Executing External Programs Securely
Problem
Your Unix program needs to execute another program.
Solution
On Unix, one of the exec*( )
family of functions is used to
replace the current program within a process with another program.
Typically, when you’re executing another program,
the original program continues to run while the new program is
executed, thus requiring two processes to achieve the desired effect.
The exec*( ) functions do not create a new
process. Instead, you must first use fork( ) to
create a new process, and then use one of the exec*(
) functions in the new process to run the new program. See
Recipe 1.6 for a discussion of using fork( )
securely.
Discussion
execve( )
is the system call used to load and begin
execution of a new program. The other functions in the
exec*( ) family are wrappers around the
execve( ) system call, and they are implemented in
user space in the standard C runtime library. When a new program is
loaded and executed with execve( ), the new
program replaces the old program within the same process. As part of
the process of loading the new program, the old
program’s address space is replaced with a new
address space. File descriptors that are marked to close on execute
are closed; the new program inherits all others. All other
system-level properties are tied to the process, so the new program
inherits them from the old program. Such properties include the
process ID, user IDs, group IDs, working and root directories, and
signal mask.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access