CWE-242 Base Rascunho High likelihood

Use of Inherently Dangerous Function

This vulnerability occurs when code uses functions that are inherently unsafe and cannot be reliably secured, posing a direct risk to application stability and security.

Definição

What is CWE-242?

This vulnerability occurs when code uses functions that are inherently unsafe and cannot be reliably secured, posing a direct risk to application stability and security.
Some functions are dangerous by design because they were created without security in mind, such as failing to check boundaries on user input. A classic example is the `gets()` function, which reads input without any limit on its size, allowing an attacker to overflow the destination buffer and potentially execute arbitrary code. Similarly, using the `>>` operator to read into a fixed-size character array is risky for the same reason—it doesn't validate input length, leading to buffer overflows. To prevent these issues, developers should replace these inherently dangerous functions with secure alternatives. For instance, use `fgets()` instead of `gets()` and employ methods with explicit bounds checking, like `std::string` in C++ or `scanf()` with width specifiers. Always validate and limit all external input to ensure it fits within the allocated buffer size, eliminating the root cause of these exploitable overflows.
Impacto no mundo real

Real-world CVEs caused by CWE-242

  • FTP client uses inherently insecure gets() function and is setuid root on some systems, allowing buffer overflow

Como os atacantes a exploram

Trajeto do atacante passo a passo

  1. 1

    The code below calls gets() to read information into a buffer.

  2. 2

    The gets() function in C is inherently unsafe.

  3. 3

    The code below calls the gets() function to read in data from the command line.

  4. 4

    However, gets() is inherently unsafe, because it copies all input from STDIN to the buffer without checking size. This allows the user to provide a string that is larger than the buffer size, resulting in an overflow condition.

Exemplo de código vulnerável

Vulnerable C

The code below calls gets() to read information into a buffer.

Vulnerável C
char buf[BUFSIZE];
  gets(buf);
Exemplo de código seguro

Secure pseudo

Seguro 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.
Lista de verificação de prevenção

How to prevent CWE-242

  • Implementation / Requirements Ban the use of dangerous functions. Use their safe equivalent.
  • Testing Use grep or static analysis tools to spot usage of dangerous functions.
Sinais de deteção

How to detect CWE-242

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

Correção automática do Plexicus

O Plexicus deteta automaticamente o CWE-242 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.

Perguntas frequentes

Frequently asked questions

O que é o CWE-242?

This vulnerability occurs when code uses functions that are inherently unsafe and cannot be reliably secured, posing a direct risk to application stability and security.

Qual a gravidade do CWE-242?

A MITRE classifica a probabilidade de exploração como Alta — esta fraqueza é ativamente explorada em campo e deve ser priorizada para remediação.

Que linguagens ou plataformas são afetadas pelo CWE-242?

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

Como posso prevenir o CWE-242?

Ban the use of dangerous functions. Use their safe equivalent. Use grep or static analysis tools to spot usage of dangerous functions.

Como é que o Plexicus deteta e corrige o CWE-242?

O motor SAST do Plexicus correlaciona a assinatura de fluxo de dados do CWE-242 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-242?

A MITRE publica a definição canónica em https://cwe.mitre.org/data/definitions/242.html. Pode também consultar a documentação da OWASP e do NIST para orientações adjacentes.

Pronto quando você estiver

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.