CWE-457 使用未经初始化的变量

Use of Uninitialized Variable

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: High

基本描述

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.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 1187 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 1187 cwe_View_ID: 699 cwe_Ordinal: Primary

适用平台

Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Sometimes'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Sometimes'}, {'cwe_Name': 'Perl', 'cwe_Prevalence': 'Often'}, {'cwe_Name': 'PHP', 'cwe_Prevalence': 'Often'}, {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
['Availability', 'Integrity', 'Other'] 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.
['Authorization', 'Other'] 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.

可能的缓解方案

Implementation

策略: Attack Surface Reduction

Assign all variables to an initial value.

Build and Compilation

策略: Compilation or Build Hardening

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

['Implementation', 'Operation']

策略:

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

策略:

The choice could be made to use 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.

示例代码

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

bad PHP

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 C

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.

分析过的案例

标识 说明 链接
CVE-2008-0081 Uninitialized variable leads to code execution in popular desktop application. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-0081
CVE-2007-4682 Crafted input triggers dereference of an uninitialized object pointer. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-4682
CVE-2007-3468 Crafted audio file triggers crash when an uninitialized variable is used. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-3468
CVE-2007-2728 Uninitialized random seed variable used. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-2728

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CLASP Uninitialized variable
7 Pernicious Kingdoms Uninitialized Variable
Software Fault Patterns SFP1 Glitch in computation
SEI CERT Perl Coding Standard DCL33-PL Imprecise Declare identifiers before using them

引用