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.

opening a file that it shouldn’t, as shown in Figure 2-2. The problem is that the program<br />

can manipulate either file, and it gets tricked into opening one on behalf of the<br />

user that it shouldn’t have.<br />

While such an attack might sound impossible to perform, attackers have many tricks<br />

to slow down a program to make exploiting race conditions easier. Plus, even if an<br />

attacker can only exploit the race condition every 1,000 times, generally the attack<br />

can be automated.<br />

The best approach is to actually have the program take on the identity of the unprivileged<br />

user before opening the file. That way, the correct access permission checks<br />

will happen automatically when the file is opened. You need not even call access( ).<br />

After the file is opened, the program can revert to its privileged state. For example,<br />

here’s some pseudo-code that opens a file properly, using the spc_drop_privileges( )<br />

and spc_restore_privileges( ) functions from Recipe 1.3:<br />

int fd;<br />

Filesystem looks like this when<br />

someone calls access() on the<br />

symbolic link.<br />

Figure 2-1. Stage 1 of a TOCTOU race condition: Time of Check<br />

The attacker has changed the<br />

symbolic link to point to a file<br />

that he would otherwise not<br />

have access to.<br />

Figure 2-2. Stage 2 of a TOCTOU race condition: Time of Use<br />

/* Temporarily drop drivileges */<br />

spc_drop_privileges(0);<br />

/* Open the file with the limited privileges */<br />

fd = open("/some/file/that/needs/opening", O_RDWR);<br />

/* Restore privileges */<br />

spc_restore_privileges( );<br />

/* Check the return value from open to see if the file was opened successfully. */<br />

if (fd = = -1) {<br />

perror("open(\"/some/file/that/needs/opening\")");<br />

abort( );<br />

}<br />

44 | Chapter 2: Access Control<br />

Symbolic<br />

Link<br />

Symbolic<br />

Link<br />

/etc/passwd<br />

(attacker does not have<br />

privileges to write)<br />

/home/foo/bar<br />

(attacker has<br />

privileges to write)<br />

/etc/passwd<br />

(attacker does not have<br />

privileges to write)<br />

/home/foo/bar<br />

(attacker has<br />

privileges to write)<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!