1.4. Limiting Risk with Privilege Separation
Problem
Your process runs with extra privileges granted by the setuid or setgid bits on the executable. Because it requires those privileges at various times throughout its lifetime, it can’t permanently drop the extra privileges. You would like to limit the risk of those extra privileges being compromised in the event of an attack.
Solution
When your program first initializes, create a Unix domain socket pair
using socketpair( ), which will create two
endpoints of a connected unnamed socket. Fork the process using
fork( )
, drop the extra privileges in the child
process, and keep them in the parent process. Establish communication
between the parent and child processes.
Whenever the child process needs to perform an operation that
requires the extra privileges held by the parent process, defer the
operation to the parent.
The result is that the child performs the bulk of the program’s work. The parent retains the extra privileges and does nothing except communicate with the child and perform privileged operations on its behalf.
If the privileged process opens files on behalf of the unprivileged
process, you will need to use a Unix domain socket, as opposed to an
anonymous pipe or some other other interprocess communication
mechanism. The reason is that only Unix domain sockets provide a
means by which file descriptors can be exchanged between the
processes after the initial fork( ).
Discussion
In Recipe 1.3, we discussed setuid, setgid, and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access