...
A better solution is to ensure that proper privileges exist before attempting to perform a permanent drop.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* Store the privileged ID for later verification */ uid_t privid = geteuid(); /* Code intended to run with elevated privileges */ /* Temporarily drop privileges */ if (seteuid(getuid()) != 0) { /* Handle error */ } /* Code intended to run with lower privileges */ if (need_more_privileges) { /* Restore Privileges */ if (seteuid(privid) != 0) { /* Handle error */ } /* Code intended to run with elevated privileges */ } /* ... */ /* Restore privileges if needed */ if (geteuid() != privid) { if (seteuid(privid) != 0) { /* Handle error */ } } /* Permanently drop privileges */ if (setuid(getuid()) != 0) { /* Handle error */ } if (setuid(0) != -1) { /* Privileges can be restored, handle error */ } /* * Code intended to run with lower privileges; * attacker cannot regain elevated privileges */ |
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
...
...
Execution with unnecessary privileges |
...
...
...
-273, |
...
Failure to check whether privileges were dropped successfully |
...
...
Bibliography
[Chen 2002] | "Setuid Demystified" |
[Dowd 2006] | Chapter 9, "Unix I: Privileges and Files" |
[Open Group 2004] | setuid() |
...
...
seteuid() | |
[Tsafrir 2008] | "The Murky Issue of Changing Process Identity: Revising 'Setuid Demystified'" |
[Wheeler 2003] | Section 7.4, "Minimize |
...
...