CWE-558

Use of getlogin() in Multithreaded Application. [Improper-Control-Of-A-Resource-Through-Its-Lifetime, Insufficient-Control-Flow-Management]

Required inputs: IR

The product uses the getlogin() function in a multithreaded context, potentially causing it to return incorrect values. 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.
Demonstrative Examples
Example 1

The following code relies on getlogin() to determine whether or not a user is trusted. It is easily subverted.

Example Language:C
    pwd = getpwnam(getlogin());
    if (isTrustedGroup(pwd->pw_gid)) {
        allow();
    } else {
        deny();
    }
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

forbidden_libfunc_call

Call to forbidden function.

None

False

Options

blacklist

blacklist

Type: dict[bauhaus.analysis.config.FileGlobPattern, list[bauhaus.analysis.config.GlobPattern]]

Default:

{
   'unistd.h': ['getlogin']
}
Dictionary of header globbing to (list of) function name globbing(s) of forbidden functions.