CWE-469 使用指针的减法来确定大小

admin 2021年12月16日16:30:53评论50 views字数 2130阅读7分6秒阅读模式

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 {

int data;
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 current = head;
struct node
tail;
while (current != NULL) {

tail = current;
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) {

struct node current = head;
int count = 0;
while (current != NULL) {

count++;
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中文网

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月16日16:30:53
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-469 使用指针的减法来确定大小http://cn-sec.com/archives/613016.html

发表评论

匿名网友 填写信息