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.

if (!(hResourceLock = CreateMutex(0, FALSE, lpName))) return 0;<br />

if (WaitForSingleObject(hResourceLock, INFINITE) = = WAIT_FAILED) {<br />

CloseHandle(hResourceLock);<br />

return 0;<br />

}<br />

return hResourceLock;<br />

}<br />

BOOL SpcUnlockResource(HANDLE hResourceLock) {<br />

if (!ReleaseMutex(hResourceLock)) return FALSE;<br />

CloseHandle(hResourceLock);<br />

return TRUE;<br />

}<br />

See Also<br />

Recipe 2.9<br />

2.11 Creating Files for Temporary Use<br />

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

You need to create a file to use as scratch space that may contain sensitive data.<br />

Solution<br />

Generate a random filename and attempt to create the file, failing if the file already<br />

exists. If the file cannot be created because it already exists, repeat the process until it<br />

succeeds. If creating the file fails for any other reason, abort the process.<br />

Discussion<br />

When creating temporary files, you should consider using a knownsafe<br />

directory to store them, as described in Recipe 2.4.<br />

The need for temporary files is common. More often than not, other processes have<br />

no need to access the temporary files you create, and especially if the files contain<br />

sensitive data, it is best to do everything possible to ensure that other processes cannot<br />

access them. It is also important that temporary files do not remain on the filesystem<br />

any longer than necessary. If the program creating temporary files terminates<br />

unexpectedly before it cleans up the files, temporary directories often become littered<br />

with files of no interest or value to anyone or anything. Worse, if the temporary<br />

files contain sensitive data, they are suddenly both interesting and valuable to an<br />

attacker.<br />

Creating Files for Temporary Use | 65<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!