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.

It’s important to remember that the new process is a copy of the original. The contents<br />

of the original process’s memory (including stack), file descriptor table, and any<br />

other process attributes are the same in both processes, but they’re not shared. Any<br />

changes to memory contents, file descriptors, and so on are private to the process<br />

that is making them. In other words, if the new process changes its file position<br />

pointer in an open file, the file position pointer for the same file in the original process<br />

remains unchanged.<br />

The fact that the new process is a copy of the original has important security considerations<br />

that are often overlooked. For example, if a PRNG is seeded in the original<br />

process, it will be seeded identically in the child process. This means that if both the<br />

original and new processes were to obtain random data from the PRNG, they would<br />

both get the same random data (see Figure 1-2)! The solution to this problem is to<br />

reseed the PRNG in one of the processes, or, preferably, both processes. By reseeding<br />

the PRNG in both processes, neither process will have any knowledge of the<br />

other’s PRNG state. Be sure to do this in a thread-safe manner if your program can<br />

fork multiple processes.<br />

Process #1<br />

PRNG output for parent process<br />

Process #2<br />

PRNG output for child process<br />

0x65 0xB7 0x6D 0xDE 0xDF 0xA1 0x88<br />

fork() happens here<br />

0x6D 0xDE 0xDF 0xA1 0x88<br />

Figure 1-2. Consequences of not reseeding PRNGs after calling fork( )<br />

At the time of the call to fork( ), any open file descriptors in the original process will<br />

also be open in the new process. If any of these descriptors are unnecessary, they<br />

should be closed; they will remain open in the other process. Closing unnecessary<br />

file descriptors is especially important if one of the processes is going to execute<br />

another program (see Recipe 1.5).<br />

Finally, the new process also inherits its access rights from the original process. Normally<br />

this is not an issue, but if the parent process had extra privileges because it was<br />

executed setuid or setgid, the new process will also have the extra privileges. If the<br />

new process does not need these privileges, they should be dropped immediately (see<br />

Recipe 1.3). Any extra privileges should be dropped especially if one of the two processes<br />

is going to execute a new program.<br />

The following function, spc_fork( ), is a wrapper around fork( ). As presented here,<br />

the code is incomplete when using an application-level random number generator; it<br />

will require the appropriate code to reseed whatever PRNG you’re using. It assumes<br />

that the new child process is the process that will be used to perform any work that<br />

does not require any extra privileges that the process may have. It is rare that when a<br />

Creating a Child Process Securely | 27<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!