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.

Temporary files on Unix<br />

The best solution for creating a temporary file on Unix is to use the mkstemp( ) function<br />

in the standard C runtime library. This function generates a random filename, *<br />

attempts to create it, and repeats the whole process until it is successful, thus guaranteeing<br />

that a unique file is created. The file created by mkstemp( ) will be readable and<br />

writable by the owner, but not by anyone else.<br />

To help further ensure that the file cannot be accessed by any other process, and to<br />

be sure that the file will not be left behind by your program if it should terminate<br />

unexpectedly before being able to delete it, the file can be deleted by name while it is<br />

open immediately after mkstemp( ) returns. Even though the file has been deleted, you<br />

will still be able to read from and write to it because there is a valid descriptor for the<br />

file. No other process will be able to open the file because a name will no longer be<br />

associated with it. Once the last open descriptor to the file is closed, the file will no<br />

longer be accessible.<br />

66 | Chapter 2: Access Control<br />

Between the time that a file is created with mkstemp( ) and the time<br />

that unlink( ) is called to delete the file, a window of opportunity<br />

exists where an attacker could open the file before it can be deleted.<br />

The mkstemp( ) function works by specifying a template from which a random filename<br />

can be generated. From the end of the template, “X” characters are replaced<br />

with random characters. The template is modified in place, so the specified buffer<br />

must be writable. The return value from mkstemp( ) is –1 if an error occurs; otherwise,<br />

it is the file descriptor to the file that was created.<br />

Temporary files on Windows<br />

The Win32 API does not contain a functional equivalent of the standard C mkstemp( )<br />

function. The Microsoft C Runtime implementation does not even provide support<br />

for the function, although it does provide an implementation of mktemp( ). However,<br />

we strongly advise against using that function on either Unix or Windows.<br />

The Win32 API does provide a function, GetTempFileName( ), that will generate a<br />

temporary filename, but that is all that it does; it does not open the file for you. Further,<br />

if asked to generate a unique name itself, it will use the system time, which is<br />

highly predictable.<br />

Instead, we recommend using GetTempPath( ) to obtain the current user’s setting for<br />

the location to place temporary files, and generating your own random filename<br />

using CryptoAPI or some other cryptographically strong pseudo-random number<br />

* The filename may not be strongly random. An attacker might be able to predict the filename, but that is generally<br />

okay.<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!