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.

} else {<br />

if (newgid != oldgid && getegid( ) != newgid) abort( );<br />

if (newuid != olduid && geteuid( ) != newuid) abort( );<br />

}<br />

}<br />

void spc_restore_privileges(void) {<br />

if (geteuid( ) != orig_uid)<br />

if (seteuid(orig_uid) = = -1 || geteuid( ) != orig_uid) abort( );<br />

if (getegid( ) != orig_gid)<br />

if (setegid(orig_gid) = = -1 || getegid( ) != orig_gid) abort( );<br />

if (!orig_uid)<br />

setgroups(orig_ngroups, orig_groups);<br />

}<br />

See Also<br />

• “Setuid Demystified” by Hao Chen, David Wagner, and Drew Dean: http://www.<br />

cs.berkeley.edu/~daw/papers/setuid-usenix02.pdf<br />

• Recipe 2.1<br />

1.4 Limiting Risk with Privilege Separation<br />

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

Your process runs with extra privileges granted by the setuid or setgid bits on the<br />

executable. Because it requires those privileges at various times throughout its lifetime,<br />

it can’t permanently drop the extra privileges. You would like to limit the risk<br />

of those extra privileges being compromised in the event of an attack.<br />

Solution<br />

When your program first initializes, create a Unix domain socket pair using<br />

socketpair( ), which will create two endpoints of a connected unnamed socket. Fork<br />

the process using fork( ), drop the extra privileges in the child process, and keep<br />

them in the parent process. Establish communication between the parent and child<br />

processes. Whenever the child process needs to perform an operation that requires<br />

the extra privileges held by the parent process, defer the operation to the parent.<br />

The result is that the child performs the bulk of the program’s work. The parent<br />

retains the extra privileges and does nothing except communicate with the child and<br />

perform privileged operations on its behalf.<br />

If the privileged process opens files on behalf of the unprivileged process, you will<br />

need to use a Unix domain socket, as opposed to an anonymous pipe or some other<br />

other interprocess communication mechanism. The reason is that only Unix domain<br />

20 | Chapter 1: Safe Initialization<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!