21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

and avoiding having an attacker place untrusted files on special hardcoded file<br />

descriptors.<br />

Solution<br />

On Unix, use the function getdtablesize( ) to obtain the size of the process’s file<br />

descriptor table. For each file descriptor in the process’s table, close the descriptors<br />

that are not stdin, stdout, orstderr, which are always 0, 1, and 2, respectively. Test<br />

stdin, stdout, and stderr to ensure that they’re open using fstat( ) for each descriptor.<br />

If any one is not open, open /dev/null and associate with the descriptor. If the<br />

program is running setuid, stdin, stdout, and stderr should also be closed if they’re<br />

not associated with a tty, and reopened using /dev/null.<br />

On Windows, there is no way to determine what file handles are open, but the same<br />

issue with open descriptors does not exist on Windows as it does on Unix.<br />

Discussion<br />

Normally, when a process is started, it inherits all open file descriptors from its parent.<br />

This can be a problem because the size of the file descriptor table on Unix is typically<br />

a fixed size. The parent process could therefore fill the file descriptor table with<br />

bogus files to deny your program any file handles for opening its own files. The result<br />

is essentially a denial of service for your program.<br />

When a new file is opened, a descriptor is assigned using the first available entry in<br />

the process’s file descriptor table. If stdin is not open, for example, the first file<br />

opened is assigned a file descriptor of 0, which is normally reserved for stdin. Similarly,<br />

if stdout is not open, file descriptor 1 is assigned next, followed by stderr’s file<br />

descriptor of 2 if it is not open.<br />

The only file descriptors that should remain open when your program starts are the<br />

stdin, stdout, and stderr descriptors. If the standard descriptors are not open, your<br />

program should open them using /dev/null and leave them open. Otherwise, calls to<br />

functions like printf( ) can have unexpected and potentially disastrous effects.<br />

Worse, the standard C library considers the standard descriptors to be special, and<br />

some functions expect stderr to be properly opened for writing error messages to. If<br />

your program opens a data file for writing and gets stderr’s file descriptor, an error<br />

message written to stderr will destroy your data file.<br />

24 | Chapter 1: Safe Initialization<br />

Particularly in a chroot( ) environment (see Recipe 2.12), the /dev/null<br />

device may not be available (it can be made available if the environment<br />

is set up properly). If it is not available, the proper thing for your<br />

program to do is to refuse to run.<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!