Base · Medium

CWE-252: Unchecked Return Value

The product does not check the return value from a method or function, which can prevent it from detecting unexpected states and conditions.

CWE-252 · Base Level ·10 CVEs ·4 Mitigations

Description

The product does not check the return value from a method or function, which can prevent it from detecting unexpected states and conditions.

Two common programmer assumptions are "this function call can never fail" and "it doesn't matter if this function call fails". If an attacker can force the function to fail or otherwise return a value that is not expected, then the subsequent program logic could lead to a vulnerability, because the product is not in a state that the programmer assumes. For example, if the program calls a function to drop privileges but does not check the return code to ensure that privileges were successfully dropped, then the program will continue to operate with the higher privileges.

Potential Impact

Availability, Integrity

Unexpected State, DoS: Crash, Exit, or Restart

Demonstrative Examples

Consider the following code segment:
Bad
char buf[10], cp_buf[10];fgets(buf, 10, stdin);strcpy(cp_buf, buf);
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().
In the following example, it is possible to request that memcpy move a much larger segment of memory than assumed:
Bad
int returnChunkSize(void *) {
                        
                           
                           /* if chunk info is valid, return the size of usable memory,
                           
                           
                           * else, return -1 to indicate an error
                           
                           
                           */
                           ...
                     }int main() {...memcpy(destBuf, srcBuf, (returnChunkSize(destBuf)-1));...}
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).
The following code does not check to see if memory allocation succeeded before attempting to use the pointer returned by malloc().
Bad
buf = (char*) malloc(req_size);strncpy(buf, xfer, req_size);
The traditional defense of this coding error is: "If my program runs out of memory, it will fail. It doesn't matter whether I handle the error or allow the program to die with a segmentation fault when it tries to dereference the null pointer." This argument ignores three important considerations:
The following examples read a file into a byte array.
Bad
char[] byteArray = new char[1024];for (IEnumerator i=users.GetEnumerator(); i.MoveNext() ;i.Current()) {String userName = (String) i.Current();String pFileName = PFILE_ROOT + "/" + userName;StreamReader sr = new StreamReader(pFileName);sr.Read(byteArray,0,1024);//the file is always 1k bytessr.Close();processPFile(userName, byteArray);}
Bad
FileInputStream fis;byte[] byteArray = new byte[1024];for (Iterator i=users.iterator(); i.hasNext();) {
                        String userName = (String) i.next();String pFileName = PFILE_ROOT + "/" + userName;FileInputStream fis = new FileInputStream(pFileName);fis.read(byteArray); // the file is always 1k bytesfis.close();processPFile(userName, byteArray);
The code loops through a set of users, reading a private data file for each user. The programmer assumes that the files are always 1 kilobyte in size and therefore ignores the return value from Read(). If an attacker can create a smaller file, the program will recycle the remainder of the data from the previous user and treat it as though it belongs to the attacker.

Mitigations & Prevention

Implementation High

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.

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 IDDescription
CVE-2020-17533Chain: unchecked return value (CWE-252) of some functions for policy enforcement leads to authorization bypass (CWE-862)
CVE-2020-6078Chain: 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
CVE-2019-15900Chain: 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), returni
CVE-2007-3798Unchecked return value leads to resultant integer overflow and code execution.
CVE-2006-4447Program 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.
CVE-2006-2916Program 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.
CVE-2008-5183chain: unchecked return value can lead to NULL dereference
CVE-2010-0211chain: unchecked return value (CWE-252) leads to free of invalid, uninitialized pointer (CWE-824).
CVE-2017-6964Linux-based device mapper encryption program does not check the return value of setuid and setgid allowing attackers to execute code with unintended privileges.
CVE-2002-1372Chain: Return values of file/socket operations are not checked (CWE-252), allowing resultant consumption of file descriptors (CWE-772).

Taxonomy Mappings

  • 7 Pernicious Kingdoms: — Unchecked Return Value
  • CLASP: — Ignored function return value
  • OWASP Top Ten 2004: A7 — Improper Error Handling
  • CERT C Secure Coding: ERR33-C — Detect and handle standard library errors
  • CERT C Secure Coding: POS54-C — Detect and handle POSIX library errors
  • The CERT Oracle Secure Coding Standard for Java (2011): EXP00-J — Do not ignore values returned by methods
  • SEI CERT Perl Coding Standard: EXP32-PL — Do not ignore function return values
  • Software Fault Patterns: SFP4 — Unchecked Status Condition
  • OMG ASCSM: ASCSM-CWE-252-resource —
  • OMG ASCRM: ASCRM-CWE-252-data —
  • OMG ASCRM: ASCRM-CWE-252-resource —

Frequently Asked Questions

What is CWE-252?

CWE-252 (Unchecked Return Value) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. The product does not check the return value from a method or function, which can prevent it from detecting unexpected states and conditions.

How can CWE-252 be exploited?

Attackers can exploit CWE-252 (Unchecked Return Value) to unexpected state, dos: crash, exit, or restart. This weakness is typically introduced during the Implementation phase of software development.

How do I prevent CWE-252?

Key mitigations include: Check the results of all functions that return a value and verify that the value is expected.

What is the severity of CWE-252?

CWE-252 is classified as a Base-level weakness (Medium abstraction). It has been observed in 10 real-world CVEs.