CWE-667 Class Draft

Improper Locking

This vulnerability occurs when a program fails to correctly acquire or release a lock on a shared resource, such as a file, database record, or memory location. This improper synchronization allows…

Definition

What is CWE-667?

This vulnerability occurs when a program fails to correctly acquire or release a lock on a shared resource, such as a file, database record, or memory location. This improper synchronization allows other processes or threads to interfere, leading to corrupted data, crashes, or unpredictable behavior.
Locking is a core synchronization technique that prevents multiple threads or processes from modifying a shared resource at the same time. When locking steps are skipped, inconsistent, or incorrectly implemented, one thread's changes can be overwritten or made invisible to another. This race condition often results in data corruption, memory errors, or application instability that is difficult to reproduce and debug. Detecting improper locking manually across a large codebase is challenging, as these flaws are highly dependent on timing and execution paths. While SAST tools can identify common locking patterns, Plexicus uses AI-powered analysis to not only flag these issues but also generate context-aware fixes, helping developers implement correct synchronization efficiently and avoid costly runtime failures.
Auswirkungen in der Praxis

Real-world CVEs caused by CWE-667

  • Chain: improper locking (CWE-667) leads to race condition (CWE-362), as exploited in the wild per CISA KEV.

  • Attacker provides invalid address to a memory-reading function, causing a mutex to be unlocked twice

  • function in OS kernel unlocks a mutex that was not previously locked, causing a panic or overwrite of arbitrary memory.

  • 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.

  • OS kernel performs an unlock in some incorrect circumstances, leading to panic.

  • OS deadlock

  • OS deadlock involving 3 separate functions

  • deadlock in library

Wie Angreifer es ausnutzen

Angreiferpfad Schritt für Schritt

  1. 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.

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    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.

Verwundbares Codebeispiel

Vulnerable Java

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.

Verwundbar Java
private long someLongValue;
  public long getLongValue() {
  	return someLongValue;
  }
  public void setLongValue(long l) {
  	someLongValue = l;
  }
Sicheres Codebeispiel

Secure C

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.

Sicher C
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);}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Präventions-Checkliste

How to prevent CWE-667

  • Implementation Use industry standard APIs to implement locking mechanism.
Erkennungssignale

How to detect CWE-667

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

Plexicus Auto-Fix

Plexicus erkennt CWE-667 automatisch und öffnet in unter 60 Sekunden einen Fix-PR.

Codex Remedium scannt jeden Commit, identifiziert genau diese Schwachstelle und liefert einen reviewer-ready Pull Request mit dem Patch. Keine Tickets. Keine Hand-offs.

Häufig gestellte Fragen

Frequently asked questions

Was ist CWE-667?

This vulnerability occurs when a program fails to correctly acquire or release a lock on a shared resource, such as a file, database record, or memory location. This improper synchronization allows other processes or threads to interfere, leading to corrupted data, crashes, or unpredictable behavior.

Wie gravierend ist CWE-667?

MITRE hat für diese Schwachstelle keine Exploit-Wahrscheinlichkeit veröffentlicht. Behandle sie als mittlere Auswirkung, bis dein Threat Model anderes belegt.

Welche Sprachen oder Plattformen sind von CWE-667 betroffen?

MITRE hat für diese CWE keine betroffenen Plattformen spezifiziert — sie kann in den meisten Anwendungs-Stacks auftreten.

Wie kann ich CWE-667 verhindern?

Use industry standard APIs to implement locking mechanism.

Wie erkennt und behebt Plexicus CWE-667?

Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-667 bei jedem Commit. Bei einem Treffer öffnet unser Codex-Remedium-Agent einen Fix-PR mit korrigiertem Code, Tests und einer einzeiligen Zusammenfassung für den Reviewer.

Wo erfahre ich mehr über CWE-667?

MITRE veröffentlicht die kanonische Definition unter https://cwe.mitre.org/data/definitions/667.html. Für ergänzende Hinweise kannst du auch die OWASP- und NIST-Dokumentation heranziehen.

Verwandte Schwachstellen

Weaknesses related to CWE-667

CWE-662 Parent

Improper Synchronization

This vulnerability occurs when a multi-threaded or multi-process application allows shared resources to be accessed by multiple threads or…

CWE-1058 Sibling

Invokable Control Element in Multi-Thread Context with non-Final Static Storable or Member Element

This happens when a method or function, designed to run in a multi-threaded environment, accesses or modifies a non-final static variable…

CWE-1096 Sibling

Singleton Class Instance Creation without Proper Locking or Synchronization

This flaw occurs when a Singleton class is implemented without proper thread-safe controls, allowing multiple instances to be created in…

CWE-366 Sibling

Race Condition within a Thread

This vulnerability occurs when two or more threads within the same application access and manipulate a shared resource (like a variable,…

CWE-543 Sibling

Use of Singleton Pattern Without Synchronization in a Multithreaded Context

This vulnerability occurs when a singleton pattern is implemented in a multithreaded application without proper synchronization,…

CWE-567 Sibling

Unsynchronized Access to Shared Data in a Multithreaded Context

This vulnerability occurs when multiple threads in an application can read and modify shared data, like static variables, without proper…

CWE-663 Sibling

Use of a Non-reentrant Function in a Concurrent Context

This vulnerability occurs when a program uses a function that is not safe for reentrancy within a concurrent environment, such as…

CWE-764 Sibling

Multiple Locks of a Critical Resource

This vulnerability occurs when a critical resource, such as a file, data structure, or connection, is locked more times than the software…

CWE-820 Sibling

Missing Synchronization

This vulnerability occurs when multiple parts of your application (like threads or processes) use the same resource—such as a variable,…

Bereit, wenn du es bist

Schluss mit dem Bezahlen pro Entwickler.
Schließ den Kreislauf.

Plexicus ist die KI-native ASPM, die scannt, filtert, fixt, pentestet und erklärt — autonom. Unbegrenzte Entwickler, unbegrenzte Repos, Fair-Use-KI-Aktionen. Echter kostenloser Tarif, €269/mo jährlich, wenn du bereit bist.