Variant · Low-Medium

CWE-457: Use of Uninitialized Variable

The code uses a variable that has not been initialized, leading to unpredictable or unintended results.

CWE-457 · Variant Level ·6 CVEs ·5 Mitigations

Description

The code uses a variable that has not been initialized, leading to unpredictable or unintended results.

In some languages such as C and C++, stack variables are not initialized by default. They generally contain junk data with the contents of stack memory before the function was invoked. An attacker can sometimes control or read these contents. In other languages or conditions, a variable that is not explicitly initialized can be given a default value that has security implications, depending on the logic of the program. The presence of an uninitialized variable can sometimes indicate a typographic error in the code.

Potential Impact

Availability, Integrity, Other

Other

Authorization, Other

Other

Demonstrative Examples

This code prints a greeting using information stored in a POST request:
Bad
if (isset($_POST['names'])) {$nameArray = $_POST['names'];}echo "Hello " . $nameArray['first'];
This code checks if the POST array 'names' is set before assigning it to the $nameArray variable. However, if the array is not in the POST request, $nameArray will remain uninitialized. This will cause an error when the array is accessed to print the greeting message, which could lead to further exploit.
The following switch statement is intended to set the values of the variables aN and bN before they are used:
Bad
int aN, Bn;switch (ctl) {
                        case -1:aN = 0;bN = 0;break;
                           case 0:aN = i;bN = -i;break;
                           case 1:aN = i + NEXT_SZ;bN = i - NEXT_SZ;break;
                           default:aN = -1;aN = -1;break;
                        
                     }repaint(aN, bN);
In the default case of the switch statement, the programmer has accidentally set the value of aN twice. As a result, bN will have an undefined value. Most uninitialized variable issues result in general software reliability problems, but if attackers can intentionally trigger the use of an uninitialized variable, they might be able to launch a denial of service attack by crashing the program. Under the right circumstances, an attacker may be able to control the value of an uninitialized variable by affecting the values on the stack prior to the invocation of the function.
This example will leave test_string in an unknown condition when i is the same value as err_val, because test_string is not initialized (CWE-456). Depending on where this code segment appears (e.g. within a function body), test_string might be random if it is stored on the heap or stack. If the variable is declared in static memory, it might be zero or NULL. Compiler optimization might contribute to the unpredictability of this address.
Bad
char *test_string;
                if (i != err_val)
                {
                test_string = "Hello World!";
                }
                printf("%s", test_string);
Good
char *test_string = "Done at the beginning";
				if (i != err_val)
				{
				test_string = "Hello World!";
				}
				printf("%s", test_string);
Another solution is to ensure that each branch of the conditional - including the default/else branch - could ensure that test_string is set:
Good
char *test_string;
				if (i != err_val)
				{
				test_string = "Hello World!";
				}
				else {
				test_string = "Done on the other side!";
				}
				printf("%s", test_string);

Mitigations & Prevention

Implementation

Ensure that critical variables are initialized before first use [REF-1485].

Build and Compilation

Most compilers will complain about the use of uninitialized variables if warnings are turned on.

ImplementationOperation

When using a language that does not require explicit declaration of variables, run or compile the software in a mode that reports undeclared or unknown variables. This may indicate the presence of a typographic error in the variable's name.

Requirements

Choose a language that is not susceptible to these issues.

Architecture and Design

Mitigating technologies such as safe string libraries and container abstractions could be introduced.

Detection Methods

  • Fuzzing High — Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption,
  • 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-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-2008-3688Chain: A denial of service may be caused by an uninitialized variable (CWE-457) allowing an infinite loop (CWE-835) resulting from a connection to an unresponsive server.
CVE-2008-0081Uninitialized variable leads to code execution in popular desktop application.
CVE-2007-4682Crafted input triggers dereference of an uninitialized object pointer.
CVE-2007-3468Crafted audio file triggers crash when an uninitialized variable is used.
CVE-2007-2728Uninitialized random seed variable used.

Taxonomy Mappings

  • CLASP: — Uninitialized variable
  • 7 Pernicious Kingdoms: — Uninitialized Variable
  • Software Fault Patterns: SFP1 — Glitch in computation
  • SEI CERT Perl Coding Standard: DCL33-PL — Declare identifiers before using them

Frequently Asked Questions

What is CWE-457?

CWE-457 (Use of Uninitialized Variable) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The code uses a variable that has not been initialized, leading to unpredictable or unintended results.

How can CWE-457 be exploited?

Attackers can exploit CWE-457 (Use of Uninitialized Variable) to other. This weakness is typically introduced during the Implementation phase of software development.

How do I prevent CWE-457?

Key mitigations include: Ensure that critical variables are initialized before first use [REF-1485].

What is the severity of CWE-457?

CWE-457 is classified as a Variant-level weakness (Low-Medium abstraction). It has been observed in 6 real-world CVEs.