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.

1.6 Creating a Child Process Securely<br />

<strong>Problem</strong><br />

Your program needs to create a child process either to perform work within the same<br />

program or, more frequently, to execute another program.<br />

Solution<br />

On Unix, creating a child process is done by calling fork( ). When fork( ) completes<br />

successfully, a nearly identical copy of the calling process is created as a new process.<br />

Most frequently, a new program is immediately executed using one of the<br />

exec*( ) family of functions (see Recipe 1.7). However, especially in the days before<br />

threading, it was common to use fork( ) to create separate “threads” of execution<br />

within a program. *<br />

If the newly created process is going to continue running the same program, any<br />

pseudo-random number generators (PRNGs) must be reseeded so that the two processes<br />

will each yield different random data as they continue to execute. In addition,<br />

any inherited file descriptors that are not needed should be closed; they remain open<br />

in the other process because the new process only has a copy of them.<br />

Finally, if the original process had extra privileges from being executed as setuid or<br />

setgid, those privileges will be inherited by the new process, and they should be<br />

dropped immediately if they are not needed. In particular, if the new process is going<br />

to be used to execute a new program, privileges should always be dropped so that<br />

the new program does not inherit privileges that it should not have.<br />

Discussion<br />

When fork( ) is used to create a new process, the new process is a nearly identical<br />

copy of the original process. The only differences in the processes are the process ID,<br />

the parent process ID, and the resource utilization counters, which are reset to zero<br />

in the new process. Execution in both processes continues immediately after the<br />

return from fork( ). Each process can determine whether it is the parent or the child<br />

by checking the return value from fork( ). In the parent or original process, fork( )<br />

returns the process ID of the new process, while 0 will be returned in the child process.<br />

* Note that we say “program” here rather than “process.” When fork( ) completes, the same program is running,<br />

but there are now two processes. The newly created process has a nearly identical copy of the original<br />

process, but it is a copy; any action performed in one process does not affect the other. In a threaded environment,<br />

each thread shares the same process, so all memory, file descriptors, signals, and so on are shared.<br />

26 | Chapter 1: Safe Initialization<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!