Use of getlogin() in Multithreaded Application

Draft Variant
Structure: Simple
Description

Using the getlogin() function in a multithreaded application can lead to unreliable or incorrect username results, creating security and logic flaws.

Extended Description

The getlogin() function retrieves the username associated with the current process, but it is not thread-safe (non-reentrant). In a multithreaded environment, if another thread or process calls getlogin() simultaneously or modifies the underlying data, the string it returns can be overwritten or changed before your code uses it. This race condition means you cannot trust the value it provides, potentially leading to incorrect access decisions, faulty logging, or corrupted user sessions. To avoid this vulnerability, developers should use secure, thread-safe alternatives. On POSIX systems, consider functions like getpwuid(geteuid()) or environment variables verified in a secure manner, ensuring the user identity remains consistent and reliable throughout the application's execution. Always validate that your chosen method is explicitly designed for concurrent execution contexts.

Common Consequences 1
Scope: IntegrityAccess ControlOther

Impact: Modify Application DataBypass Protection MechanismOther

Potential Mitigations 2
Phase: Architecture and Design
Using names for security purposes is not advised. Names are easy to forge and can have overlapping user IDs, potentially causing confusion or impersonation.
Phase: Implementation
Use getlogin_r() instead, which is reentrant, meaning that other processes are locked out from changing the username.
Demonstrative Examples 1

ID : DX-172

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

Code Example:

Bad
C
c
Applicable Platforms
Languages:
C : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Taxonomy Mapping
  • 7 Pernicious Kingdoms
  • Software Fault Patterns