CWE-192 整数强制转换错误
Integer Coercion Error
结构: Simple
Abstraction: Variant
状态: Incomplete
被利用可能性: Medium
基本描述
Integer coercion refers to a set of flaws pertaining to the type casting, extension, or truncation of primitive data types.
扩展描述
Several flaws fall under the category of integer coercion errors. For the most part, these errors in and of themselves result only in availability and data integrity issues. However, in some circumstances, they may result in other, more complicated security related flaws, such as buffer overflow conditions.
相关缺陷
-
cwe_Nature: ChildOf cwe_CWE_ID: 681 cwe_View_ID: 1000 cwe_Ordinal: Primary
-
cwe_Nature: ChildOf cwe_CWE_ID: 682 cwe_View_ID: 699 cwe_Ordinal: Primary
适用平台
Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C#', 'cwe_Prevalence': 'Undetermined'}]
常见的影响
范围 | 影响 | 注释 |
---|---|---|
Availability | ['DoS: Resource Consumption (CPU)', 'DoS: Resource Consumption (Memory)', 'DoS: Crash, Exit, or Restart'] | Integer coercion often leads to undefined states of execution resulting in infinite loops or crashes. |
['Integrity', 'Confidentiality', 'Availability'] | Execute Unauthorized Code or Commands | In some cases, integer coercion errors can lead to exploitable buffer overflow conditions, resulting in the execution of arbitrary code. |
['Integrity', 'Other'] | Other | Integer coercion errors result in an incorrect value being stored for the variable in question. |
可能的缓解方案
Requirements
策略:
A language which throws exceptions on ambiguous data casts might be chosen.
Architecture and Design
策略:
Design objects and program flow such that multiple or complex casts are unnecessary
Implementation
策略:
Ensure that any data type casting that you must used is entirely understood in order to reduce the plausibility of error in use.
示例代码
例
The following code is intended to read an incoming packet from a socket and extract one or more headers.
bad C
int numHeaders;
PacketHeader headers;
sock=AcceptSocketConnection();
ReadPacket(packet, sock);
numHeaders =packet->headers;
if (numHeaders > 100) {
}
headers = malloc(numHeaders * sizeof(PacketHeader);
ParsePacketHeaders(packet, headers);
The code performs a check to make sure that the packet does not contain too many headers. However, numHeaders is defined as a signed int, so it could be negative. If the incoming packet specifies a value such as -3, then the malloc calculation will generate a negative number (say, -300 if each header can be a maximum of 100 bytes). When this result is provided to malloc(), it is first converted to a size_t type. This conversion then produces a large value such as 4294966996, which may cause malloc() to fail or to allocate an extremely large amount of memory (CWE-195). With the appropriate negative numbers, an attacker could trick malloc() into using a very small positive number, which then allocates a buffer that is much smaller than expected, potentially leading to a buffer overflow.
例
The following code reads a maximum size and performs a sanity check on that size. It then performs a strncpy, assuming it will not exceed the boundaries of the array. While the use of "short s" is forced in this particular example, short int's are frequently used within real-world code, such as code that processes structured data.
bad C
}
void main (int argc, char argv) {
char input;
int i;
short s;
unsigned int sz;
i = GetUntrustedInt();
s = i;
/ s is -1 so it passes the safety check - CWE-697 /
if (s > 256) {
}
/ s is sign-extended and saved in sz /
sz = s;
/ output: i=65535, s=-1, sz=4294967295 - your mileage may vary /
printf("i=%d, s=%d, sz=%un", i, s, sz);
input = GetUserInput("Enter pathname:");
/ strncpy interprets s as unsigned int, so it's treated as MAX_INT
(CWE-195), enabling buffer overflow (CWE-119) /
strncpy(path, input, s);
path[255] = ' '; / don't want CWE-170 */
printf("Path is: %sn", path);
}
This code first exhibits an example of CWE-839, allowing "s" to be a negative number. When the negative short "s" is converted to an unsigned integer, it becomes an extremely large positive integer. When this converted integer is used by strncpy() it will lead to a buffer overflow (CWE-119).
Notes
分类映射
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
CLASP | Integer coercion error | ||
CERT C Secure Coding | INT02-C | Understand integer conversion rules | |
CERT C Secure Coding | INT05-C | Do not use input functions to convert character data if they cannot handle all possible inputs | |
CERT C Secure Coding | INT31-C | Exact | Ensure that integer conversions do not result in lost or misinterpreted data |
引用
文章来源于互联网:scap中文网
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论