结构: 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.
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. |
策略:
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
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
映射的分类名 | 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 |