Versions Compared

Key

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

...

If the program is run as a setuid root program, over time, the state of the UIDs might look like the following:

Description

Code

EUID

RUID

SSUID

program

Program startup

 


0

user

User

0

temporary

Temporary drop

seteuid(getuid())

user

User

user

User

0

restore

Restore

seteuid(0)

0

user

User

0

permanent

Permanent drop

setuid(getuid())

user

User

user

User

user

User

restore

Restore (attacker)

setuid(0) (fails)

user

User

user

User

user

User

If the program fails to restore privileges, it will be unable to permanently drop them later:

Description

Code

EUID

RUID

SSUID

program startup

 


0

user

User

0

temporary

Temporary drop

seteuid(getuid())

user

User

user

User

0

restore

Restore

seteuid(0)

user

User

user

User

0

permanent

Permanent drop

setuid(getuid())

user

User

user

User

0

restore

Restore (attacker)

setuid(0)

0

0

0

Compliant Solution

This compliant solution was implemented in sendmail, a popular mail transfer agent, to determine if superuser privileges were successfully dropped [Wheeler 2003]. If the setuid() call succeeds after (supposedly) dropping privileges permanently, then the privileges were not dropped as intended.

...

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
 */

...

If privilege relinquishment conditions are left unchecked, any flaw in the program may lead to unintended system compromise corresponding to the more privileged user or group account.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

POS37-C

high

probable

low

P18

L1

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

user_defined

Soundly supported
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-POS37
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

DF4876, DF4877, DF4878


Klocwork
Include Page
Klocwork_V
Klocwork_V

SV.

FIU.PERMISSIONS
SV.USAGERULES.PERMISSIONS 

USAGERULES.PERMISSIONS


Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-POS37-a
Ensure that privilege relinquishment is successful

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule POS37-CChecks for priviledge drop not verified (rule fully covered)

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

Taxonomy

Taxonomy item

Relationship

ISO/IEC

...

MITRE CWE: CWE-250, "Execution with unnecessary privileges"

...

TR 24772Privilege Sandbox Issues [XYO]Prior to 2018-01-12: CERT: Unspecified Relationship
CWE 2.11CWE-273,

...

Failure to check whether privileges were dropped successfully

...

2017-07-07: CERT: Exact

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

...


...

Image Modified Image Modified Image Modified