Use of Uninitialized Variable

Draft Variant
Structure: Simple
Description

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

Extended Description

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.

Common Consequences 2
Scope: AvailabilityIntegrityOther

Impact: Other

Initial variables usually contain junk, which can not be trusted for consistency. This can lead to denial of service conditions, or modify control flow in unexpected ways. In some cases, an attacker can "pre-initialize" the variable using previous actions, which might enable code execution. This can cause a race condition if a lock variable check passes when it should not.

Scope: AuthorizationOther

Impact: Other

Strings that are not initialized are especially dangerous, since many functions expect a null at the end -- and only at the end -- of a string.

Detection Methods 2
FuzzingHigh
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, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.
Automated Static AnalysisHigh
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.)
Potential Mitigations 5
Phase: Implementation

Strategy: Attack Surface Reduction

Ensure that critical variables are initialized before first use [REF-1485].
Phase: Build and Compilation

Strategy: Compilation or Build Hardening

Most compilers will complain about the use of uninitialized variables if warnings are turned on.
Phase: 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.
Phase: Requirements

Strategy: Language Selection

Choose a language that is not susceptible to these issues.
Phase: Architecture and Design
Mitigating technologies such as safe string libraries and container abstractions could be introduced.
Demonstrative Examples 3
This code prints a greeting using information stored in a POST request:

Code Example:

Bad
PHP
php
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:

Code Example:

Bad
C
c
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.

ID : DX-144

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 (Missing Initialization of a Variable). 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.

Code Example:

Bad
C

char *test_string; if (i != err_val) {

c
When the printf() is reached, test_string might be an unexpected address, so the printf might print junk strings (Use of Uninitialized Variable). To fix this code, there are a couple approaches to making sure that test_string has been properly set once it reaches the printf(). One solution would be to set test_string to an acceptable default before the conditional:

Code Example:

Good
C

char *test_string = "Done at the beginning"; if (i != err_val) {

c
Another solution is to ensure that each branch of the conditional - including the default/else branch - could ensure that test_string is set:

Code Example:

Good
C

char *test_string; if (i != err_val) {

c
Observed Examples 6
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 (Unchecked Return Value), causing an uninitialized variable to be checked (Use of Uninitialized Variable), returning success to allow authorization bypass for executing a privileged (Incorrect Authorization).
CVE-2008-3688Chain: A denial of service may be caused by an uninitialized variable (Use of Uninitialized Variable) allowing an infinite loop (Loop with Unreachable Exit Condition ('Infinite Loop')) 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.
References 6
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
MS08-014 : The Case of the Uninitialized Stack Variable Vulnerability
Microsoft Security Vulnerability Research & Defense
11-03-2008
ID: REF-437
24 Deadly Sins of Software Security
Michael Howard, David LeBlanc, and John Viega
McGraw-Hill
2010
ID: REF-44
The Art of Software Security Assessment
Mark Dowd, John McDonald, and Justin Schuh
Addison Wesley
2006
ID: REF-62
D3FEND: D3-VI Variable Initialization
D3FEND
ID: REF-1485
Likelihood of Exploit

High

Applicable Platforms
Languages:
C : SometimesC++ : SometimesPerl : OftenPHP : OftenNot Language-Specific : Undetermined
Modes of Introduction
Implementation
Taxonomy Mapping
  • CLASP
  • 7 Pernicious Kingdoms
  • Software Fault Patterns
  • SEI CERT Perl Coding Standard