CWE-469 使用指针的减法来确定大小
Use of Pointer Subtraction to Determine Size
结构: Simple
Abstraction: Base
状态: Draft
被利用可能性: Medium
基本描述
The application subtracts one pointer from another in order to determine size, but this calculation can be incorrect if the pointers do not exist in the same memory chunk.
相关缺陷
- cwe_Nature: ChildOf cwe_CWE_ID: 682 cwe_View_ID: 1000 cwe_Ordinal: Primary
适用平台
Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}]
常见的影响
范围 | 影响 | 注释 |
---|---|---|
['Access Control', 'Integrity', 'Confidentiality', 'Availability'] | ['Execute Unauthorized Code or Commands', 'Gain Privileges or Assume Identity'] | There is the potential for arbitrary code execution with privileges of the vulnerable program. |
可能的缓解方案
Implementation
策略:
Save an index variable. This is the recommended solution. Rather than subtract pointers from one another, use an index variable of the same size as the pointers in question. Use this variable to "walk" from one pointer to the other and calculate the difference. Always sanity check this number.
示例代码
例
The following example contains the method size that is used to determine the number of nodes in a linked list. The method is passed a pointer to the head of the linked list.
bad C
struct node next;
};
// Returns the number of nodes in a linked list from
// the given pointer to the head of the list.
int size(struct node head) {
struct node tail;
while (current != NULL) {
current = current->next;
}
return tail - head;
}
// other methods for manipulating the list
...
However, the method creates a pointer that points to the end of the list and uses pointer subtraction to determine the number of nodes in the list by subtracting the tail pointer from the head pointer. There no guarantee that the pointers exist in the same memory area, therefore using pointer subtraction in this way could return incorrect results and allow other unintended behavior. In this example a counter should be used to determine the number of nodes in the list, as shown in the following code.
good C
...
int size(struct node head) {
int count = 0;
while (current != NULL) {
current = current->next;
}
return count;
}
分类映射
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
CLASP | Improper pointer subtraction | ||
CERT C Secure Coding | ARR36-C | Exact | Do not subtract or compare two pointers that do not refer to the same array |
Software Fault Patterns | SFP7 | Faulty Pointer Use |
文章来源于互联网:scap中文网
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论