Executar análise estática (SAST) na base de código à procura do padrão inseguro no fluxo de dados.
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…
What is CWE-762?
Real-world CVEs caused by CWE-762
Ainda não há referências CVE públicas associadas a este CWE no catálogo da MITRE.
Trajeto do atacante passo a passo
- 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
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
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
In this example, the program calls the delete[] function on non-heap memory.
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.
void foo(){
BarObj *ptr = new BarObj()
```
/* do some work with ptr here */*
...
free(ptr);} 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.
void foo(){
BarObj *ptr = new BarObj()
```
/* do some work with ptr here */*
...
delete ptr;} 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.
How to detect CWE-762
Executar testes dinâmicos de segurança de aplicações (DAST) contra o endpoint em execução.
Monitorizar os registos em tempo de execução para traços de exceção invulgares, input malformado ou tentativas de contornar a autorização.
Revisão de código: sinalizar qualquer novo código que trate input desta superfície sem usar os ajudantes validados do framework.
O Plexicus deteta automaticamente o CWE-762 e abre um PR de correção em menos de 60 segundos.
O Codex Remedium analisa cada commit, identifica esta fraqueza exata e entrega um pull request pronto para revisão com o patch. Sem tickets. Sem transferências.
Frequently asked questions
O que é o 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.
Qual a gravidade do CWE-762?
A MITRE classifica a probabilidade de exploração como Baixa — a exploração é pouco comum, mas a fraqueza deve mesmo assim ser corrigida quando descoberta.
Que linguagens ou plataformas são afetadas pelo CWE-762?
MITRE lists the following affected platforms: C, C++.
Como posso prevenir o 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…
Como é que o Plexicus deteta e corrige o CWE-762?
O motor SAST do Plexicus correlaciona a assinatura de fluxo de dados do CWE-762 em cada commit. Quando é encontrada uma correspondência, o nosso agente Codex Remedium abre um PR de correção com o código corrigido, testes e um resumo de uma linha para o revisor.
Onde posso saber mais sobre o CWE-762?
A MITRE publica a definição canónica em https://cwe.mitre.org/data/definitions/762.html. Pode também consultar a documentação da OWASP e do NIST para orientações adjacentes.
Weaknesses related to CWE-762
Release of Invalid Pointer or Reference
This vulnerability occurs when a program tries to free a memory resource back to the system but uses an incorrect deallocation method or…
Free of Pointer not at Start of Buffer
This vulnerability occurs when a program incorrectly frees a memory pointer that no longer points to the beginning of the allocated heap…
Free of Memory not on the Heap
This vulnerability occurs when a program calls free() on a memory pointer that wasn't originally allocated using standard heap functions…
Further reading
- MITRE — CWE-762 oficial https://cwe.mitre.org/data/definitions/762.html
- boost C++ Library Smart Pointers https://www.boost.org/doc/libs/1_38_0/libs/smart_ptr/smart_ptr.htm
- Valgrind https://valgrind.org/
- Transitioning to ARC Release Notes https://developer.apple.com/library/archive/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html
Pare de pagar por desenvolvedor.
Comece a fechar o ciclo.
O Plexicus é o ASPM nativo de IA que verifica, filtra, corrige, pentesta e explica — de forma autónoma. Programadores ilimitados, repos ilimitados, ações de IA de utilização justa. Nível gratuito real, €269/mo anual quando estiver pronto.