Weakness · High

CWE-761: Free of Pointer not at Start of Buffer

What it is

The product calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start of the buffer.

MITRE CWE 4.19.1

Severity (our rating)
High
Likelihood of exploit
Not rated by MITRE
Introduced during
Implementation
Exploited in the wild
No CVE in CISA's catalog

In more detail

This can cause the product to crash, or in some cases, modify critical program variables or execute code. This weakness often occurs when the memory is allocated explicitly on the heap with one of the malloc() family functions and free() is called, but pointer arithmetic has caused the pointer to be in the interior or end of the buffer.

What an attacker can do

  • Modify Memory (Integrity, Availability, Confidentiality)
  • DoS: Crash, Exit, or Restart (Integrity, Availability, Confidentiality)
  • Execute Unauthorized Code or Commands (Integrity, Availability, Confidentiality)

How to prevent it

Implementation

  • When utilizing pointer arithmetic to traverse a buffer, use a separate variable to track progress through memory and preserve the originally allocated address for later freeing.
  • When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.

Architecture and Design

  • Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. For example, glibc in Linux provides protection against free of invalid pointers.
  • Use a language that provides abstractions for memory allocation and deallocation.

How to find it

  • Dynamic Analysis with Automated Results Interpretation. Use a tool that dynamically detects memory management problems, such as valgrind.
  • Automated Dynamic Analysis. Use tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++.

CWE-761 on MITRE's CWE site

H2 Security

Found it in your code?

We review where it appears, fix it with your team, and check it was not already used against you.

Talk to our team

T Academy

Stop writing it in the first place

Learn to build software and systems without weaknesses like this one, with practice on real hosts in your browser, every objective checked on the machine.

Start free

Source: MITRE Common Weakness Enumeration, CWE version 4.19.1. CWE is a trademark of The MITRE Corporation.