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.

of the object, regardless of what the object’s DACLsays. Unfortunately, if an object<br />

is not sufficiently protected, an attacker can nefariously take ownership of the object,<br />

rendering the rightful owner powerless to counter the attacker.<br />

2.3 Determining Whether a User Has Access to<br />

a File on Unix<br />

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

Your program is running with extra permissions because its executable has the setuid<br />

or setgid bit set. You need to determine whether the user running the program<br />

will be able to access a file without the extra privileges granted by setuid or setgid.<br />

Solution<br />

Temporarily drop privileges to the user and group for which access is to be checked.<br />

With the process’s privileges lowered, perform the access check, then restore privileges<br />

to what they were before the check. See Recipe 1.3 for additional discussion of<br />

elevated privileges and how to drop and restore them.<br />

Discussion<br />

It is always best to allow the operating system to do the bulk of the work of performing<br />

access checks. The only way to do so is to manipulate the privileges under which<br />

the process is running. Recipe 1.3 provides implementations for functions that temporarily<br />

drop privileges and then restore them again.<br />

When performing access checks on files, you need to be careful to avoid the types of<br />

race conditions known as Time of Check, Time of Use (TOCTOU), which are illustrated<br />

in Figures 2-1 and 2-2. These race conditions occur when access is checked<br />

before opening a file. The most common way for this to occur is to use the access( )<br />

system call to verify access to a file, and then to use open( ) or fopen( ) to open the<br />

file if the return from access( ) indicates that access will be granted.<br />

The problem is that between the time the access check via access( ) completes and<br />

the time open( ) begins (both system calls are atomic within the operating system kernel),<br />

there is a window of vulnerability where an attacker can replace the file that is<br />

being operated upon. Let’s say that a program uses access( ) to check to see whether<br />

an attacker has write permissions to a particular file, as shown in Figure 2-1. If that<br />

file is a symbolic link, access( ) will follow it, and report that the attacker does<br />

indeed have write permissions for the underlying file. If the attacker can change the<br />

symbolic link after the check occurs, but before the program starts using the file,<br />

pointing it to a file he couldn’t otherwise access, the privileged program will end up<br />

Determining Whether a User Has Access to a File on Unix | 43<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!