21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Discussion<br />

execve( ) is the system call used to load and begin execution of a new program. The<br />

other functions in the exec*( ) family are wrappers around the execve( ) system call,<br />

and they are implemented in user space in the standard C runtime library. When a<br />

new program is loaded and executed with execve( ), the new program replaces the<br />

old program within the same process. As part of the process of loading the new program,<br />

the old program’s address space is replaced with a new address space. File<br />

descriptors that are marked to close on execute are closed; the new program inherits<br />

all others. All other system-level properties are tied to the process, so the new program<br />

inherits them from the old program. Such properties include the process ID,<br />

user IDs, group IDs, working and root directories, and signal mask.<br />

Table 1-2 lists the various exec*( ) wrappers around the execve( ) system call. Note<br />

that many of these wrappers should not be used in secure code. In particular, never<br />

use the wrappers that are named with a “p” suffix because they will search the environment<br />

to locate the file to be executed. When executing external programs, you<br />

should always specify the full path to the file that you want to execute. If the PATH<br />

environment variable is used to locate the file, the file that is found to execute may<br />

not be the expected one.<br />

Table 1-2. The exec*( ) family of functions<br />

Function signature Comments<br />

int execl(const char *path, char *arg, ...); The argument list is terminated by a NULL. The<br />

calling program’s environment is passed on to<br />

the new program.<br />

int execle(const char *path, char *arg, ...); The argument list is terminated by a NULL, and<br />

the environment pointer to use follows immediately.<br />

int execlp(const char *file, char *arg, ...); The argument list is terminated by a NULL. The<br />

PATH environment variable is searched to<br />

locate the program to execute. The calling program’s<br />

environment is passed on to the new<br />

program.<br />

int exect(const char *path, const char *argv[ ],<br />

const char *envp[ ]);<br />

The same as execve( ), except that process<br />

tracing is enabled.<br />

int execv(const char *path, const char *argv[ ]); The PATH environment variable is searched to<br />

locate the program to execute.<br />

int execve(const char *path, const char *argv[ ],<br />

const char *envp[ ]);<br />

This is the main system call to load and execute<br />

a new program.<br />

int execvp(const char *file, const char *argv[ ]); The PATH environment variable is searched to<br />

locate the program to execute. The calling program’s<br />

environment is passed on to the new<br />

program.<br />

Executing External Programs Securely | 29<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!