Use of Pointer Subtraction to Determine Size

Draft Base
Structure: Simple
Description

This vulnerability occurs when a program calculates a size or offset by subtracting two memory pointers, but the pointers point to locations in different memory blocks, leading to an incorrect and potentially dangerous result.

Extended Description

Pointer subtraction is a valid C/C++ operation, but it only yields a meaningful size or element count when both pointers point within the same contiguous memory allocation (like a single array or buffer). When developers use this trick on pointers from different allocations, the calculation produces a nonsensical value based on the arbitrary memory distance between the two chunks. This flawed size is then often used in buffer operations, leading to out-of-bounds reads or writes, memory corruption, and crashes. Detecting these flaws manually is tricky because the code looks mathematically simple. While SAST tools can flag the pattern, Plexicus uses AI to analyze the pointer origins and suggest the correct fix—such as storing the allocation size separately—saving hours of debugging. Managing this at scale across a large codebase is difficult; an ASPM like Plexicus can help you track and remediate these subtle memory flaws across your entire application stack.

Common Consequences 1
Scope: Access ControlIntegrityConfidentialityAvailability

Impact: Modify MemoryRead MemoryExecute Unauthorized Code or CommandsGain Privileges or Assume Identity

There is the potential for arbitrary code execution with privileges of the vulnerable program.

Detection Methods 2
FuzzingHigh
Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Potential Mitigations 1
Phase: 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 validate this number.
Demonstrative Examples 1
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.

Code Example:

Bad
C
c

// Returns the number of nodes in a linked list from*

c
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.

Code Example:

Good
C
c
References 1
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
Likelihood of Exploit

Medium

Applicable Platforms
Languages:
C : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Related Weaknesses
Taxonomy Mapping
  • CLASP
  • CERT C Secure Coding
  • Software Fault Patterns