CWE-667 加锁机制不恰当

Improper Locking

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown


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


  • cwe_Nature: ChildOf cwe_CWE_ID: 662 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 662 cwe_View_ID: 699 cwe_Ordinal: Primary


范围 影响 注释
Availability DoS: Resource Consumption (CPU) Inconsistent locking discipline can lead to deadlock.



策略: Libraries or Frameworks

Use industry standard APIs to implement locking mechanism.


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.

bad Java

private long someLongValue;
public long getLongValue() {
return someLongValue;

public void setLongValue(long l) {
someLongValue = l;

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

bad PHP

function writeToLog($message){
$logfile = fopen("logFile.log", "a");
//attempt to get logfile lock
if (flock($logfile, LOCK_EX)) {
// unlock logfile
flock($logfile, LOCK_UN);
else {
print "Could not obtain lock on logFile.log, message not recorded\n";

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().

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

bad C

void f(pthread_mutex_t mutex) {

access 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 it to higher levels.


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);

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

bad Java

if (helper == null) {
synchronized (this) {
if (helper == null) {
helper = new Helper();
return helper;

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:


helper = new Helper();

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.


标识 说明 链接
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.
CVE-2006-2275 Deadlock when large number of small messages cannot be processed quickly enough.
CVE-2005-3847 OS kernel has deadlock triggered by a signal during a core dump.
CVE-2005-3106 Race condition leads to deadlock.
CVE-2005-2456 Chain: array index error (CWE-129) leads to deadlock (CWE-833)
CVE-2001-0682 Program can not execute when attacker obtains a mutex.
CVE-2002-1914 Program can not execute when attacker obtains a lock on a critical output file.
CVE-2002-1915 Program can not execute when attacker obtains a lock on a critical output file.
CVE-2002-0051 Critical 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-0338 Chain: predictable file names used for locking, allowing attacker to create the lock beforehand. Resultant from permissions and randomness.
CVE-2000-1198 Chain: Lock files with predictable names. Resultant from randomness.
CVE-2002-1869 Product 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 CWE-412, but close.


映射的分类名 ImNode ID Fit Mapped Node Name
CERT C Secure Coding CON31-C CWE More Abstract Do not destroy a mutex while it is locked
CERT C Secure Coding POS48-C CWE More Abstract 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


  • CAPEC-25
  • CAPEC-26
  • CAPEC-27