CWE-457: Use of Uninitialized Variable

Description

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

Submission Date :

July 19, 2006, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

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

Example Vulnerable Codes

Example - 1

This code prints a greeting using information stored in a POST request:

$nameArray = $_POST['names'];
if (isset($_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.

Example - 2

The following switch statement is intended to set the values of the variables aN and bN before they are used:



aN = 0;bN = 0;break;

aN = i;bN = -i;break;

aN = i + NEXT_SZ;bN = i - NEXT_SZ;break;

aN = -1;aN = -1;break;case -1:case 0:case 1:default:
int aN, Bn;switch (ctl) {}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.

Example - 3

This example will leave test_string in anunknown 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 berandom if it is stored on the heap or stack. If thevariable is declared in static memory, it might be zeroor NULL. Compiler optimization might contribute to theunpredictability of this address.


test_string = "Hello World!";
char *test_string;if (i != err_val){}printf("%s", test_string);

When the printf() is reached,test_string might be an unexpected address, so theprintf might print junk strings (CWE-457).To fix this code, there are a couple approaches tomaking sure that test_string has been properly set onceit reaches the printf().One solution would be to set test_string to anacceptable default before the conditional:


test_string = "Hello World!";
char *test_string = "Done at the beginning";if (i != err_val){}printf("%s", test_string);

Another solution is to ensure that eachbranch of the conditional - including the default/elsebranch - could ensure that test_string is set:


test_string = "Hello World!";
test_string = "Done on the other side!";
char *test_string;if (i != err_val){}else {}printf("%s", test_string);

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.

© cvefeed.io
Latest DB Update: Nov. 24, 2024 13:10