CWE-762 Variante Incomplet Low likelihood

Mismatched Memory Management Routines

This vulnerability occurs when a program uses incompatible functions to allocate and free memory. For example, freeing memory with a function that doesn't match the one used to create it, like…

Définition

What is CWE-762?

This vulnerability occurs when a program uses incompatible functions to allocate and free memory. For example, freeing memory with a function that doesn't match the one used to create it, like mixing different memory management systems.
Mismatched memory management happens when allocation and deallocation routines come from incompatible sources. Common examples include trying to free stack-allocated memory with `free()` (which only works for heap memory), or allocating with C's `malloc()` but deallocating with C++'s `delete` operator. These functions manage memory in fundamentally different ways, and swapping them breaks the underlying memory manager's expectations. The consequences range from immediate crashes and data corruption to more severe security issues like memory corruption exploits that could lead to arbitrary code execution. The severity depends on the specific routines involved, the program's memory layout, and whether an attacker can control or influence the mismatched operation.
Impact réel

Real-world CVEs caused by CWE-762

Aucune référence CVE publique n'est liée à ce CWE dans le catalogue MITRE pour le moment.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    This example allocates a BarObj object using the new operator in C++, however, the programmer then deallocates the object using free(), which may lead to unexpected behavior.

  2. 2

    Instead, the programmer should have either created the object with one of the malloc family functions, or else deleted the object with the delete operator.

  3. 3

    In this example, the program does not use matching functions such as malloc/free, new/delete, and new[]/delete[] to allocate/deallocate the resource.

  4. 4

    In this example, the program calls the delete[] function on non-heap memory.

Exemple de code vulnérable

Vulnerable C++

This example allocates a BarObj object using the new operator in C++, however, the programmer then deallocates the object using free(), which may lead to unexpected behavior.

Vulnérable C++
void foo(){
  		BarObj *ptr = new BarObj()
```
/* do some work with ptr here */* 
  		
  		...
  		
  		free(ptr);}
Exemple de code sécurisé

Secure C++

Instead, the programmer should have either created the object with one of the malloc family functions, or else deleted the object with the delete operator.

Sécurisé C++
void foo(){
  		BarObj *ptr = new BarObj()
```
/* do some work with ptr here */* 
  		
  		...
  		
  		delete ptr;}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Liste de contrôle de prévention

How to prevent CWE-762

  • Implementation Only call matching memory management functions. Do not mix and match routines. For example, when you allocate a buffer with malloc(), dispose of the original pointer with free().
  • Implementation Choose a language or tool that provides automatic memory management, or makes manual memory management less error-prone. For example, glibc in Linux provides protection against free of invalid pointers. When using Xcode to target OS X or iOS, enable automatic reference counting (ARC) [REF-391]. To help correctly and consistently manage memory when programming in C++, consider using a smart pointer class such as std::auto_ptr (defined by ISO/IEC ISO/IEC 14882:2003), std::shared_ptr and std::unique_ptr (specified by an upcoming revision of the C++ standard, informally referred to as C++ 1x), or equivalent solutions such as Boost.
  • 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.
  • Architecture and Design Use a language that provides abstractions for memory allocation and deallocation.
  • Testing Use a tool that dynamically detects memory management problems, such as valgrind.
Signaux de détection

How to detect CWE-762

SAST High

Exécuter une analyse statique (SAST) sur le code source à la recherche du motif non sécurisé dans le flux de données.

DAST Moderate

Exécuter des tests de sécurité applicative dynamique (DAST) contre le point de terminaison en ligne.

Runtime Moderate

Surveiller les journaux runtime pour détecter des traces d'exception inhabituelles, des entrées malformées ou des tentatives de contournement d'autorisation.

Code review Moderate

Revue de code : signaler tout nouveau code qui traite les entrées de cette surface sans utiliser les helpers du framework validés.

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-762 et ouvre une PR de correction en moins de 60 secondes.

Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.

Questions fréquentes

Frequently asked questions

Qu'est-ce que CWE-762 ?

This vulnerability occurs when a program uses incompatible functions to allocate and free memory. For example, freeing memory with a function that doesn't match the one used to create it, like mixing different memory management systems.

Quelle est la gravité de CWE-762 ?

MITRE évalue la probabilité d'exploitation comme Faible — l'exploitation est rare, mais la faiblesse doit tout de même être corrigée lorsqu'elle est découverte.

Quels langages ou plateformes sont affectés par CWE-762 ?

MITRE lists the following affected platforms: C, C++.

Comment puis-je prévenir CWE-762 ?

Only call matching memory management functions. Do not mix and match routines. For example, when you allocate a buffer with malloc(), dispose of the original pointer with free(). Choose a language or tool that provides automatic memory management, or makes manual memory management less error-prone. For example, glibc in Linux provides protection against free of invalid pointers. When using Xcode to target OS X or iOS, enable automatic reference counting (ARC) [REF-391]. To help correctly and…

Comment Plexicus détecte et corrige CWE-762 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-762 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.

Où puis-je en savoir plus sur CWE-762 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/762.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.

Prêt quand vous l'êtes

Arrêtez de payer par développeur.
Commencez à fermer la boucle.

Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.