CWE-667: Improper Locking
Description
The product does not properly acquire or release a lock on a resource, leading to unexpected resource state changes and behaviors.
Submission Date :
April 11, 2008, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
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.
Example - 1
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.
return someLongValue;
someLongValue = l;private long someLongValue;public long getLongValue() {}public void setLongValue(long l) {}
Example - 2
This code tries to obtain a lock for a file, then writes to it. 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().
// //attempt to get logfile lock//
// // unlock logfile//
fwrite($logfile,$message);flock($logfile, LOCK_UN);
print "Could not obtain lock on logFile.log, message not recorded\n";$logfile = fopen("logFile.log", "a");if (flock($logfile, LOCK_EX)) {}else {}
function writeToLog($message){}fclose($logFile);
Example - 3
The following function attempts to acquire a lock in order to perform operations on a shared resource. 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.
// /* access shared resource *///
pthread_mutex_lock(mutex);pthread_mutex_unlock(mutex);void f(pthread_mutex_t *mutex) {}
return result;
// /* access shared resource *///
int result;result = pthread_mutex_lock(mutex);if (0 != result)return pthread_mutex_unlock(mutex);int f(pthread_mutex_t *mutex) {}
Example - 4
It may seem that the following bit of code achieves thread safety while avoiding unnecessary synchronization... 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: 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.helper = new Helper();if (helper == null) {}synchronized (this) {}
if (helper == null) {}return helper;
helper = new Helper();
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-412: Unrestricted Externally Accessible Lock
CWE-413: Improper Resource Locking
CWE-414: Missing Lock Check
CWE-609: Double-Checked Locking
CWE-662: Improper Synchronization
CWE-764: Multiple Locks of a Critical Resource
CWE-765: Multiple Unlocks of a Critical Resource
CWE-832: Unlock of a Resource that is not Locked
CWE-833: Deadlock
CWE-1232: Improper Lock Behavior After Power State Transition
CWE-1233: Security-Sensitive Hardware Controls with Missing Lock Bit Protection
CWE-1234: Hardware Internal or Debug Modes Allow Override of Locks
Visit http://cwe.mitre.org/ for more details.