CWE-558: Use of getlogin() in Multithreaded Application
Use of getlogin() in Multithreaded Application
Weakness ID: 558 (Weakness Variant)
Status: Draft
Description
Description Summary
The application uses the getlogin() function in a multithreaded context, potentially causing it to return incorrect values.
Extended Description
The getlogin() function returns a pointer to a string that contains the name of the user associated with the calling process. The function is not reentrant, meaning that if it is called from another process, the contents are not locked out and the value of the string can be changed by another process. This makes it very risky to use because the username can be changed by other processes, so the results of the function cannot be trusted.
Time of Introduction
Implementation
Applicable Platforms
Languages
C
C++
Common Consequences
Scope
Effect
Integrity
Access Control
Other
Technical Impact: Modify application
data; Bypass protection
mechanism; Other
Demonstrative Examples
Example 1
The following code relies on getlogin() to determine whether or not
a user is trusted. It is easily subverted.
(Bad Code)
Example
Language: C
pwd = getpwnam(getlogin());
if (isTrustedGroup(pwd->pw_gid)) {
allow();
} else {
deny();
}
Potential Mitigations
Using names for security purposes is not advised. Names are easy to
forge and can have overlapping user IDs, potentially causing confusion
or impersonation.
Use getlogin_r() instead, which is reentrant, meaning that other
processes are locked out from changing the username.