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.

ate on the name of the file, especially if multiple queries are required for the same file<br />

or if you intend to open it based on the information obtained from queries. Operating<br />

on filenames introduces the possibility of race conditions because filenames can<br />

change between calls.<br />

On Unix, use the fstat( ) function instead of the stat( ) function. Both functions<br />

return the same information, but fstat( ) uses an open file descriptor while stat( )<br />

uses a filename. Doing so removes the possibility of a race condition, because the file<br />

to which the file descriptor points can never change unless you reopen the file<br />

descriptor. When operating on just the filename, there is no guarantee that the<br />

underlying file pointed to by the filename remains the same after the call to stat( ).<br />

On Windows, use the function GetFileInformationByHandle( ) instead of functions<br />

like FindFirstFile( ) or FindFirstFileEx( ). As with fstat( ) versus stat( ) on Unix<br />

(which are also available on Windows if you’re using the C runtime API), the primary<br />

difference between these functions is that one uses a file handle while the others<br />

use filenames. If the only information you need is the size of the file, you can use<br />

GetFileSize( ) instead of GetFileInformationByHandle( ).<br />

Discussion<br />

Accessing file information using filenames can lead to race conditions, particularly if<br />

multiple queries are necessary or if you intend to open the file depending on information<br />

previously obtained. In particular, if symbolic links are involved, an attacker<br />

could potentially change the file to which the link points between queries or between<br />

the time information is queried and the time the file is actually opened. This type of<br />

race condition, known as a Time of Check, Time of Use (TOCTOU) race condition,<br />

was also discussed in Recipe 2.3.<br />

In most cases, when you need information about a file, such as its size, you also have<br />

some intention of opening the file and using it in some way. For example, if you’re<br />

checking to see whether a file exists before trying to create it, you might think to use<br />

stat( ) or FindFirstFile( ) first, and if the function fails with an error indicating the<br />

file does not exist, create the file with creat( ) or CreateFile( ). A better solution is<br />

to use open( ) with the O_CREAT and O_EXCL flags, or to use CreateFile( ) with CREATE_<br />

NEW specified as the creation disposition.<br />

See Also<br />

Recipe 2.3<br />

54 | Chapter 2: Access Control<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!