Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A better solution is to ensure that proper privileges exist before attempting to perform a permanent drop.:

Code Block
bgColor#ccccff
langc
/* 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

...

TR 24772Privilege Sandbox Issues [XYO]
MITRE CWE

...

...

Execution with unnecessary privileges

...


...

...

...

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

...

 

...