COMMON WEAKNESS ENUMERATIONDraft

CWE-457

Use of Uninitialized Variable

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

Abstraction

Variant

Structure

Simple

Likelihood of Exploit

High

Understand the problem

What is the problem?

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.

Root cause

How and when does the problem arise?

Lifecycle phase

Implementation

In C, using an uninitialized char * in some string libraries will return incorrect results, as the libraries expect the null terminator to always be at the end of a string, even if the string is empty.

Risk

What does successful exploitation lead to?

Other

Affected security scopes: Availability, Integrity, 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.
Other

Affected security scopes: Authorization, 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.

Defence

How is it prevented and fixed?

Implementation

Attack Surface Reduction

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

Build and Compilation

Compilation or Build Hardening

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

Implementation · Operation

Defence to apply across several lifecycle phases

Detail
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

Language Selection

Detail
Choose a language that is not susceptible to these issues.

Architecture and Design

Architecture and design approach

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

Verification

How is it detected?

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, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.

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