CWE-252 Base Draft Low likelihood

Unchecked Return Value

This vulnerability occurs when a program fails to verify the result of a function or method call, allowing it to continue execution without detecting errors or unexpected conditions.

Definition

What is CWE-252?

This vulnerability occurs when a program fails to verify the result of a function or method call, allowing it to continue execution without detecting errors or unexpected conditions.
Developers often assume critical operations will always succeed or that their failure is inconsequential. However, attackers can exploit these unchecked return values by forcing functions to fail or return unexpected data, tricking the application into operating in an insecure, unintended state. For instance, if a privilege-dropping function fails silently, the program continues running with elevated access, creating a severe security gap. To prevent this, treat every function call that can impact security or stability as having a potential failure state. Always validate return codes, especially for security-critical operations like memory allocation, file operations, authentication checks, and system configuration changes. Implementing consistent error handling ensures the application responds safely to unexpected conditions rather than proceeding with flawed assumptions.
Auswirkungen in der Praxis

Real-world CVEs caused by CWE-252

  • Chain: unchecked return value (CWE-252) of some functions for policy enforcement leads to authorization bypass (CWE-862)

  • Chain: The return value of a function returning a pointer is not checked for success (CWE-252) resulting in the later use of an uninitialized variable (CWE-456) and a null pointer dereference (CWE-476)

  • Chain: sscanf() call is used to check if a username and group exists, but the return value of sscanf() call is not checked (CWE-252), causing an uninitialized variable to be checked (CWE-457), returning success to allow authorization bypass for executing a privileged (CWE-863).

  • Unchecked return value leads to resultant integer overflow and code execution.

  • Program does not check return value when invoking functions to drop privileges, which could leave users with higher privileges than expected by forcing those functions to fail.

  • Program does not check return value when invoking functions to drop privileges, which could leave users with higher privileges than expected by forcing those functions to fail.

  • chain: unchecked return value can lead to NULL dereference

  • chain: unchecked return value (CWE-252) leads to free of invalid, uninitialized pointer (CWE-824).

Wie Angreifer es ausnutzen

Angreiferpfad Schritt für Schritt

  1. 1

    Consider the following code segment:

  2. 2

    The programmer expects that when fgets() returns, buf will contain a null-terminated string of length 9 or less. But if an I/O error occurs, fgets() will not null-terminate buf. Furthermore, if the end of the file is reached before any characters are read, fgets() returns without writing anything to buf. In both of these situations, fgets() signals that something unusual has happened by returning NULL, but in this code, the warning will not be noticed. The lack of a null terminator in buf can result in a buffer overflow in the subsequent call to strcpy().

  3. 3

    In the following example, it is possible to request that memcpy move a much larger segment of memory than assumed:

  4. 4

    If returnChunkSize() happens to encounter an error it will return -1. Notice that the return value is not checked before the memcpy operation (CWE-252), so -1 can be passed as the size argument to memcpy() (CWE-805). Because memcpy() assumes that the value is unsigned, it will be interpreted as MAXINT-1 (CWE-195), and therefore will copy far more memory than is likely available to the destination buffer (CWE-787, CWE-788).

  5. 5

    The following code does not check to see if memory allocation succeeded before attempting to use the pointer returned by malloc().

Verwundbares Codebeispiel

Vulnerable C

Consider the following code segment:

Verwundbar C
char buf[10], cp_buf[10];
  fgets(buf, 10, stdin);
  strcpy(cp_buf, buf);
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-252

  • Implementation Check the results of all functions that return a value and verify that the value is expected.
  • Implementation For any pointers that could have been modified or provided from a function that can return NULL, check the pointer for NULL before use. When working with a multithreaded or otherwise asynchronous environment, ensure that proper locking APIs are used to lock before the check, and unlock when it has finished [REF-1484].
  • Implementation Ensure that you account for all possible return values from the function.
  • Implementation When designing a function, make sure you return a value or throw an exception in case of an error.
Erkennungssignale

How to detect CWE-252

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-252 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-252?

This vulnerability occurs when a program fails to verify the result of a function or method call, allowing it to continue execution without detecting errors or unexpected conditions.

Wie gravierend ist CWE-252?

MITRE stuft die Exploit-Wahrscheinlichkeit als niedrig ein — eine Ausnutzung ist selten, die Schwachstelle sollte aber dennoch behoben werden, sobald sie entdeckt wird.

Welche Sprachen oder Plattformen sind von CWE-252 betroffen?

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

Wie kann ich CWE-252 verhindern?

Check the results of all functions that return a value and verify that the value is expected. For any pointers that could have been modified or provided from a function that can return NULL, check the pointer for NULL before use. When working with a multithreaded or otherwise asynchronous environment, ensure that proper locking APIs are used to lock before the check, and unlock when it has finished [REF-1484].

Wie erkennt und behebt Plexicus CWE-252?

Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-252 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-252?

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

Verwandte Schwachstellen

Weaknesses related to CWE-252

CWE-754 Parent

Improper Check for Unusual or Exceptional Conditions

This weakness occurs when software fails to properly anticipate and handle rare or unexpected runtime situations that fall outside normal…

CWE-253 Sibling

Incorrect Check of Function Return Value

This vulnerability occurs when a program misinterprets or improperly validates the return value from a function, causing it to miss…

CWE-273 Sibling

Improper Check for Dropped Privileges

This vulnerability occurs when an application tries to lower its system privileges but fails to verify that the operation was successful.

CWE-354 Sibling

Improper Validation of Integrity Check Value

This vulnerability occurs when software fails to properly check the integrity of data by validating its checksum or hash value. Without…

CWE-391 Sibling

Unchecked Error Condition

This vulnerability occurs when a program fails to properly check or handle error conditions, such as exceptions or return codes. By…

CWE-394 Sibling

Unexpected Status Code or Return Value

This vulnerability occurs when software fails to properly validate the full range of possible return values from a function or system…

CWE-476 Sibling

NULL Pointer Dereference

This vulnerability occurs when a program attempts to access or manipulate memory using a pointer that is set to NULL, causing a crash or…

CWE-690 Child

Unchecked Return Value to NULL Pointer Dereference

This vulnerability occurs when a program calls a function that can return a NULL pointer to signal failure, but the code does not check…

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.