Improper Locking

Draft Class
Structure: Simple
Description

The product does not properly acquire or release a lock on a resource, leading to unexpected resource state changes and behaviors.

Extended Description

Locking is a type of synchronization behavior that ensures that multiple independently-operating processes or threads do not interfere with each other when accessing the same resource. All processes/threads are expected to follow the same steps for locking. If these steps are not followed precisely - or if no locking is done at all - then another process/thread could modify the shared resource in a way that is not visible or predictable to the original process. This can lead to data or memory corruption, denial of service, etc.

Common Consequences 1
Scope: Availability

Impact: DoS: Resource Consumption (CPU)

Inconsistent locking discipline can lead to deadlock.

Detection Methods 1
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Potential Mitigations 1
Phase: Implementation

Strategy: Libraries or Frameworks

Use industry standard APIs to implement locking mechanism.
Demonstrative Examples 4
In the following Java snippet, methods are defined to get and set a long field in an instance of a class that is shared across multiple threads. Because operations on double and long are nonatomic in Java, concurrent access may cause unexpected behavior. Thus, all operations on long and double fields should be synchronized.

Code Example:

Bad
Java
java

ID : DX-69

This code tries to obtain a lock for a file, then writes to it.

Code Example:

Bad
PHP
php

//attempt to get logfile lock* if (flock($logfile, LOCK_EX)) { ``` fwrite($logfile,$message);

php
PHP by default will wait indefinitely until a file lock is released. If an attacker is able to obtain the file lock, this code will pause execution, possibly leading to denial of service for other users. Note that in this case, if an attacker can perform an flock() on the file, they may already have privileges to destroy the log file. However, this still impacts the execution of other programs that depend on flock().

ID : DX-24

The following function attempts to acquire a lock in order to perform operations on a shared resource.

Code Example:

Bad
C
c

/* access shared resource /

c
However, the code does not check the value returned by pthread_mutex_lock() for errors. If pthread_mutex_lock() cannot acquire the mutex for any reason, the function may introduce a race condition into the program and result in undefined behavior.
In order to avoid data races, correctly written programs must check the result of thread synchronization functions and appropriately handle all errors, either by attempting to recover from them or reporting them to higher levels.

Code Example:

Good
C
c

/* access shared resource /

c

ID : DX-70

It may seem that the following bit of code achieves thread safety while avoiding unnecessary synchronization...

Code Example:

Bad
Java
java
The programmer wants to guarantee that only one Helper() object is ever allocated, but does not want to pay the cost of synchronization every time this code is called.
Suppose that helper is not initialized. Then, thread A sees that helper==null and enters the synchronized block and begins to execute:

Code Example:

Bad
Java
java
If a second thread, thread B, takes over in the middle of this call and helper has not finished running the constructor, then thread B may make calls on helper while its fields hold incorrect values.
Observed Examples 26
CVE-2021-1782Chain: improper locking (Improper Locking) leads to race condition (Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')), as exploited in the wild per CISA KEV.
CVE-2009-0935Attacker provides invalid address to a memory-reading function, causing a mutex to be unlocked twice
CVE-2010-4210function in OS kernel unlocks a mutex that was not previously locked, causing a panic or overwrite of arbitrary memory.
CVE-2008-4302Chain: OS kernel does not properly handle a failure of a function call (Improper Handling of Exceptional Conditions), leading to an unlock of a resource that was not locked (Unlock of a Resource that is not Locked), with resultant crash.
CVE-2009-1243OS kernel performs an unlock in some incorrect circumstances, leading to panic.
CVE-2009-2857OS deadlock
CVE-2009-1961OS deadlock involving 3 separate functions
CVE-2009-2699deadlock in library
CVE-2009-4272deadlock triggered by packets that force collisions in a routing table
CVE-2002-1850read/write deadlock between web server and script
CVE-2004-0174web server deadlock involving multiple listening connections
CVE-2009-1388multiple simultaneous calls to the same function trigger deadlock.
CVE-2006-5158chain: other weakness leads to NULL pointer dereference (NULL Pointer Dereference) or deadlock (Deadlock).
CVE-2006-4342deadlock when an operation is performed on a resource while it is being removed.
CVE-2006-2374Deadlock in device driver triggered by using file handle of a related device.
CVE-2006-2275Deadlock when large number of small messages cannot be processed quickly enough.
CVE-2005-3847OS kernel has deadlock triggered by a signal during a core dump.
CVE-2005-3106Race condition leads to deadlock.
CVE-2005-2456Chain: array index error (Improper Validation of Array Index) leads to deadlock (Deadlock)
CVE-2001-0682Program can not execute when attacker obtains a mutex.
CVE-2002-1914Program can not execute when attacker obtains a lock on a critical output file.
CVE-2002-1915Program can not execute when attacker obtains a lock on a critical output file.
CVE-2002-0051Critical file can be opened with exclusive read access by user, preventing application of security policy. Possibly related to improper permissions, large-window race condition.
CVE-2000-0338Chain: predictable file names used for locking, allowing attacker to create the lock beforehand. Resultant from permissions and randomness.
CVE-2000-1198Chain: Lock files with predictable names. Resultant from randomness.
CVE-2002-1869Product does not check if it can write to a log file, allowing attackers to avoid logging by accessing the file using an exclusive lock. Overlaps unchecked error condition. This is not quite Unrestricted Externally Accessible Lock, but close.
References 1
Automated Source Code Security Measure (ASCSM)
Object Management Group (OMG)
01-2016
ID: REF-962
Modes of Introduction
Architecture and Design
Implementation
Taxonomy Mapping
  • CERT C Secure Coding
  • CERT C Secure Coding
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • Software Fault Patterns
  • OMG ASCSM
Notes
MaintenanceDeeper research is necessary for synchronization and related mechanisms, including locks, mutexes, semaphores, and other mechanisms. Multiple entries are dependent on this research, which includes relationships to concurrency, race conditions, reentrant functions, etc. Improper Synchronization and its children - including Improper Locking, Missing Synchronization, Incorrect Synchronization, and others - may need to be modified significantly, along with their relationships.