Description
The product does not properly acquire or release a lock on a resource, leading to unexpected resource state changes and behaviors.
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.
Potential Impact
Availability
DoS: Resource Consumption (CPU)
Demonstrative Examples
private long someLongValue;public long getLongValue() {return someLongValue;}
public void setLongValue(long l) {someLongValue = l;}function writeToLog($message){$logfile = fopen("logFile.log", "a");
//attempt to get logfile lock
if (flock($logfile, LOCK_EX)) {fwrite($logfile,$message);
// unlock logfile
flock($logfile, LOCK_UN);}else {print "Could not obtain lock on logFile.log, message not recorded\n";}}fclose($logFile);void f(pthread_mutex_t *mutex) {
pthread_mutex_lock(mutex);
/* access shared resource */
pthread_mutex_unlock(mutex);
}int f(pthread_mutex_t *mutex) {
int result;
result = pthread_mutex_lock(mutex);if (0 != result)return result;
/* access shared resource */
return pthread_mutex_unlock(mutex);
}if (helper == null) {
synchronized (this) {if (helper == null) {helper = new Helper();}}
}return helper;helper = new Helper();Mitigations & Prevention
Use industry standard APIs to implement locking mechanism.
Detection Methods
- Automated Static Analysis High — 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 sea
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2021-1782 | Chain: improper locking (CWE-667) leads to race condition (CWE-362), as exploited in the wild per CISA KEV. |
| CVE-2009-0935 | Attacker provides invalid address to a memory-reading function, causing a mutex to be unlocked twice |
| CVE-2010-4210 | function in OS kernel unlocks a mutex that was not previously locked, causing a panic or overwrite of arbitrary memory. |
| CVE-2008-4302 | Chain: OS kernel does not properly handle a failure of a function call (CWE-755), leading to an unlock of a resource that was not locked (CWE-832), with resultant crash. |
| CVE-2009-1243 | OS kernel performs an unlock in some incorrect circumstances, leading to panic. |
| CVE-2009-2857 | OS deadlock |
| CVE-2009-1961 | OS deadlock involving 3 separate functions |
| CVE-2009-2699 | deadlock in library |
| CVE-2009-4272 | deadlock triggered by packets that force collisions in a routing table |
| CVE-2002-1850 | read/write deadlock between web server and script |
| CVE-2004-0174 | web server deadlock involving multiple listening connections |
| CVE-2009-1388 | multiple simultaneous calls to the same function trigger deadlock. |
| CVE-2006-5158 | chain: other weakness leads to NULL pointer dereference (CWE-476) or deadlock (CWE-833). |
| CVE-2006-4342 | deadlock when an operation is performed on a resource while it is being removed. |
| CVE-2006-2374 | Deadlock in device driver triggered by using file handle of a related device. |
Showing 15 of 26 observed examples.
Related Weaknesses
Taxonomy Mappings
- CERT C Secure Coding: CON31-C — Do not destroy a mutex while it is locked
- CERT C Secure Coding: POS48-C — Do not unlock or destroy another POSIX thread's mutex
- The CERT Oracle Secure Coding Standard for Java (2011): VNA00-J — Ensure visibility when accessing shared primitive variables
- The CERT Oracle Secure Coding Standard for Java (2011): VNA02-J — Ensure that compound operations on shared variables are atomic
- The CERT Oracle Secure Coding Standard for Java (2011): VNA05-J — Ensure atomicity when reading and writing 64-bit values
- The CERT Oracle Secure Coding Standard for Java (2011): LCK06-J — Do not use an instance lock to protect shared static data
- Software Fault Patterns: SFP19 — Missing Lock
- OMG ASCSM: ASCSM-CWE-667 —
Frequently Asked Questions
What is CWE-667?
CWE-667 (Improper Locking) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Class-level weakness. The product does not properly acquire or release a lock on a resource, leading to unexpected resource state changes and behaviors.
How can CWE-667 be exploited?
Attackers can exploit CWE-667 (Improper Locking) to dos: resource consumption (cpu). This weakness is typically introduced during the Architecture and Design, Implementation phase of software development.
How do I prevent CWE-667?
Key mitigations include: Use industry standard APIs to implement locking mechanism.
What is the severity of CWE-667?
CWE-667 is classified as a Class-level weakness (High abstraction). It has been observed in 26 real-world CVEs.