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 (!MakeTempFilename(lpszBuffer, dwBuffer)) {<br />

hFile = INVALID_HANDLE_VALUE;<br />

break;<br />

}<br />

hFile = CreateFile(lpszBuffer, GENERIC_READ | GENERIC_WRITE,<br />

FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,<br />

0, CREATE_NEW,<br />

FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, 0);<br />

if (hFile = = INVALID_HANDLE_VALUE && GetLastError( ) != ERROR_ALREADY_EXISTS)<br />

break;<br />

} while (hFile = = INVALID_HANDLE_VALUE);<br />

return hFile;<br />

}<br />

See Also<br />

Recipes 2.4, 11.11<br />

2.12 Restricting Filesystem Access on Unix<br />

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

You want to restrict your program’s ability to access important parts of the filesystem.<br />

Solution<br />

Unix systems provide a system call known as chroot( ) that will restrict the process’s<br />

access to the filesystem. Specifically, chroot( ) alters a process’s perception of the<br />

filesystem by changing its root directory, which effectively prevents the process from<br />

accessing any part of the filesystem above the new root directory.<br />

Discussion<br />

Normally, a process’s root directory is the actual system root directory, which allows<br />

the process to access any part of the filesystem. However, by using the chroot( ) system<br />

call, a process can alter its view of the filesystem by changing its root directory to<br />

another directory within the filesystem. Once the process’s root directory has been<br />

changed once, it can only be made more restrictive. It is not possible to change the<br />

process’s root directory to another directory outside of its current view of the filesystem.<br />

Using chroot( ) is a simple way to increase security for processes that do not require<br />

access to the filesystem outside of a directory or hierarchy of directories containing<br />

its data files. If an attacker is somehow able to compromise the program and gain<br />

68 | 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!