CWE-416 Variante Stable High likelihood

Use After Free

Use After Free happens when a program continues to use a pointer to a memory location after that memory has been freed. This can lead to unpredictable behavior, crashes, or security vulnerabilities…

Définition

What is CWE-416?

Use After Free happens when a program continues to use a pointer to a memory location after that memory has been freed. This can lead to unpredictable behavior, crashes, or security vulnerabilities because the freed memory may have been reallocated for a different purpose.
This vulnerability occurs because the pointer isn't invalidated after `free()` is called. The memory manager can then reassign that same memory block to a different part of the program, perhaps for storing unrelated data. When the original, now 'dangling' pointer is used again, it reads or writes to this new data, corrupting it and leading to instability or exploitable conditions like arbitrary code execution. Detecting use-after-free bugs manually is challenging as they often depend on complex, state-specific execution paths. While SAST tools can flag the dangerous pattern, Plexicus uses AI to analyze context and suggest precise code fixes—such as nullifying pointers or reordering logic—saving developers hours of manual debugging and helping secure the entire application lifecycle.
Vulnerability Diagram CWE-416
Use After Free malloc → p free(p) p still held attacker malloc reclaims slot use *p (vtable call) jumps to attacker data A freed pointer is reused after the slot is owned by attacker data.
Impact réel

Real-world CVEs caused by CWE-416

  • Chain: an operating system kernel has insufficent resource locking (CWE-413) leading to a use after free (CWE-416).

  • Chain: two threads in a web browser use the same resource (CWE-366), but one of those threads can destroy the resource before the other has completed (CWE-416).

  • Chain: mobile platform race condition (CWE-362) leading to use-after-free (CWE-416), as exploited in the wild per CISA KEV.

  • Chain: race condition (CWE-362) leads to use-after-free (CWE-416), as exploited in the wild per CISA KEV.

  • Use-after-free triggered by closing a connection while data is still being transmitted.

  • Improper allocation for invalid data leads to use-after-free.

  • certificate with a large number of Subject Alternate Names not properly handled in realloc, leading to use-after-free

  • Timers are not disabled when a related object is deleted

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    The following example demonstrates the weakness.

  2. 2

    The following code illustrates a use after free error:

  3. 3

    When an error occurs, the pointer is immediately freed. However, this pointer is later incorrectly used in the logError function.

Exemple de code vulnérable

Vulnerable C

The following example demonstrates the weakness.

Vulnérable C
#include <stdio.h>
  #include <unistd.h>
  #define BUFSIZER1 512
  #define BUFSIZER2 ((BUFSIZER1/2) - 8)
  int main(int argc, char **argv) {
  	char *buf1R1;
  	char *buf2R1;
  	char *buf2R2;
  	char *buf3R2;
  	buf1R1 = (char *) malloc(BUFSIZER1);
  	buf2R1 = (char *) malloc(BUFSIZER1);
  	free(buf2R1);
  	buf2R2 = (char *) malloc(BUFSIZER2);
  	buf3R2 = (char *) malloc(BUFSIZER2);
  	strncpy(buf2R1, argv[1], BUFSIZER1-1);
  	free(buf1R1);
  	free(buf2R2);
  	free(buf3R2);
  }
Exemple de code sécurisé

Secure pseudo

Sécurisé pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
  const safe = validateAndEscape(input);
  return executeWithGuards(safe);
}
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-416

  • Architecture and Design Choose a language that provides automatic memory management.
  • Implementation When freeing pointers, be sure to set them to NULL once they are freed. However, the utilization of multiple or complex data structures may lower the usefulness of this strategy.
Signaux de détection

How to detect CWE-416

Fuzzing High

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 Analysis High

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

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-416 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-416 ?

Use After Free happens when a program continues to use a pointer to a memory location after that memory has been freed. This can lead to unpredictable behavior, crashes, or security vulnerabilities because the freed memory may have been reallocated for a different purpose.

Quelle est la gravité de CWE-416 ?

MITRE évalue la probabilité d'exploitation comme Élevée — cette faiblesse est activement exploitée et doit être priorisée pour la remédiation.

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

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

Comment puis-je prévenir CWE-416 ?

Choose a language that provides automatic memory management. When freeing pointers, be sure to set them to NULL once they are freed. However, the utilization of multiple or complex data structures may lower the usefulness of this strategy.

Comment Plexicus détecte et corrige CWE-416 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-416 à 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-416 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/416.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.