界: Input Validation and Representation

输入验证与表示问题是由元字符、交替编码和数字表示引起的。安全问题源于信任输入。这些问题包括:“Buffer Overflows”、“Cross-Site Scripting”攻击、“SQL Injection”等其他问题。

String Termination Error

Abstract
需要适当的字符串终止可能会导致缓冲区溢出。
Explanation
string termination error 在以下情况下出现:

1.数据通过某一函数进入程序,而该函数不会以“null”结束输出。

2.数据被传递到某个函数,而该函数的输入需要以“null”结尾。
示例 1:以下代码会从 cfgfile 进行读取,并使用 strcpy() 将输入复制到 inputbuf 中。但是,该代码会错误的假定 inputbuf 始终包含 null 终止符。


#define MAXLEN 1024
...
char *pathbuf[MAXLEN];
...
read(cfgfile,inputbuf,MAXLEN); //does not null-terminate
strcpy(pathbuf,inputbuf); //requires null-terminated input
...


如果从 cfgfile 中读取的数据在磁盘上按预期以“null”结尾,则Example 1 中的代码可正常运行。但是,如果攻击者能够篡改此输入,使其不包含所需的 null 字符,则对 strcpy() 的调用将连续从内存进行复制,直到遇到任意 null 字符为止。因此,可能会溢出目标缓冲区,更有甚者,如果攻击者能够控制紧随 inputbuf 之后的内存内容,那么就会使应用程序遭受 Buffer Overflow 攻击。

示例 2:在以下代码中,readlink() 对存储在缓冲区 path 上的某个符号链接名进行了扩展,以使缓冲区 buf 包含可通过该符号链接而引用的文件的绝对路径。而最终的长度值将通过 strlen() 来计算。


...
char buf[MAXPATH];
...
readlink(path, buf, MAXPATH);
int length = strlen(buf);
...


由于通过 readlink() 读入 buf 的值不会以“null”结尾,Example 2 中的代码将无法正确运行。在测试过程中,类似于这样的漏洞可能不会被捕捉到,因为 buf 中未使用的内容或紧随其后的内存可能都为 null,因此会显示 strlen(),而这看上去似乎运行正常一样。然而,在默认情况下,strlen() 将持续遍历内存,直到在堆栈中遇到任意 null 字符为止,这会导致 length 的值远远大于 buf 的大小,从而在随后使用该值的操作中可能会造成缓冲区溢出。

示例 3:以下代码使用 snprintf() 复制用户输入字符串并将其放置在多个输出字符串中。尽管与 sprintf() 相比,snprintf() 函数提供了额外的保护机制,特别是指定了最大输出大小,但当指定的输出大小大于预期输入时,该函数仍然容易发生字符串终止错误。字符串终止错误可能导致下游问题,例如内存泄漏或缓冲区溢出。


...
char no_null_term[5] = getUserInput();

char output_1[20];
snprintf(output_1, 20, "%s", no_null_term);

char output_2[20];
snprintf(output_2, 20, "%s", no_null_term);


printf("%s\n", output_1);
printf("%s\n", output_2);
...
Example 3 中的代码演示了内存泄漏。当 output_2 填充有 no_null_term 时,snprintf() 必须从 no_null_term 位置读取值,直到遇到 null 字符或达到指定的大小限制。因为 no_null_term 没有终止,snprintf 继续读入 output_1 数据,最终到达第一次调用 snprintf() 提供的 null 终止字符。output_2printf() 演示了内存泄漏,其中包含 no_null_term 字符序列两次。

一般来说,字符串表示为一个内存区域,其中的数据以 null 字符结尾。先前的字符串处理方法往往会通过此 null 字符来确定字符串的长度。如果某个不包含 null 终止符的缓冲区被传递给其中一个函数,那么该函数就会读取到该缓冲区末尾之后的内容。

恶意用户通常会通过对应用程序注入意外大小的数据或内容,从而达到利用这类漏洞的目的。为了达到这个目的,他们可能会直接向程序提供恶意输入,或者间接地修改应用程序的资源,如配置文件等。如果攻击者能够使应用程序读取超出缓冲区边界的信息,那么他就可以利用 buffer overflow 漏洞在系统中注入或是执行任意代码。
References
[1] M. Howard, D. LeBlanc Writing Secure Code, Second Edition Microsoft Press
[2] Standards Mapping - Common Weakness Enumeration CWE ID 170, CWE ID 665
[3] Standards Mapping - Common Weakness Enumeration Top 25 2024 [12] CWE ID 020
[4] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-002754, CCI-002824
[5] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[6] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 1.3
[7] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2023 Directive 4.14, Rule 1.3, Rule 21.17
[8] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 0-3-1
[9] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2023 Rule 4.1.3
[10] Standards Mapping - NIST Special Publication 800-53 Revision 4 SI-10 Information Input Validation (P1), SI-16 Memory Protection (P1)
[11] Standards Mapping - NIST Special Publication 800-53 Revision 5 SI-10 Information Input Validation, SI-16 Memory Protection
[12] Standards Mapping - OWASP Mobile 2024 M4 Insufficient Input/Output Validation
[13] Standards Mapping - OWASP Mobile Application Security Verification Standard 2.0 MASVS-CODE-4
[14] Standards Mapping - OWASP Top 10 2004 A5 Buffer Overflow
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.5
[16] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 Requirement 6.3.1.1
[17] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 Requirement 6.5.2
[18] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.2
[19] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.2
[20] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.2
[21] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.2
[22] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[23] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0.1 Requirement 6.2.4
[24] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[25] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.1 - Terminal Software Attack Mitigation, Control Objective B.3.1.1 - Terminal Software Attack Mitigation
[26] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.1 - Terminal Software Attack Mitigation, Control Objective B.3.1.1 - Terminal Software Attack Mitigation, Control Objective C.3.2 - Web Software Attack Mitigation
[27] Standards Mapping - SANS Top 25 2009 Risky Resource Management - CWE ID 665
[28] Standards Mapping - SANS Top 25 2010 Risky Resource Management - CWE ID 665
[29] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3510 CAT I, APP3590.1 CAT I
[30] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3510 CAT I, APP3590.1 CAT I
[31] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3510 CAT I, APP3590.1 CAT I
[32] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3510 CAT I, APP3590.1 CAT I
[33] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3510 CAT I, APP3590.1 CAT I
[34] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3510 CAT I, APP3590.1 CAT I
[35] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3510 CAT I, APP3590.1 CAT I
[36] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[37] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[38] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[39] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[40] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[41] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[42] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[43] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[44] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[45] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[46] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[47] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[48] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[49] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002530 CAT II, APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[50] Standards Mapping - Security Technical Implementation Guide Version 6.1 APSC-DV-002530 CAT II, APSC-DV-002560 CAT I, APSC-DV-002590 CAT I
[51] Standards Mapping - Web Application Security Consortium Version 2.00 Buffer Overflow (WASC-07)
[52] Standards Mapping - Web Application Security Consortium 24 + 2 Buffer Overflow
desc.dataflow.cpp.string_termination_error.master