CWE-416 释放后使用

Use After Free

结构: Simple

Abstraction: Variant

状态: Stable

被利用可能性: High

基本描述

Referencing memory after it has been freed can cause a program to crash, use unexpected values, or execute code.

扩展描述

The use of previously-freed memory can have any number of adverse consequences, ranging from the corruption of valid data to the execution of arbitrary code, depending on the instantiation and timing of the flaw. The simplest way data corruption may occur involves the system's reuse of the freed memory. Use-after-free errors have two common and sometimes overlapping causes:

In this scenario, the memory in question is allocated to another pointer validly at some point after it has been freed. The original pointer to the freed memory is used again and points to somewhere within the new allocation. As the data is changed, it corrupts the validly used memory; this induces undefined behavior in the process.

If the newly allocated data chances to hold a class, in C++ for example, various function pointers may be scattered within the heap data. If one of these function pointers is overwritten with an address to valid shellcode, execution of arbitrary code can be achieved.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 825 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 672 cwe_View_ID: 1003 cwe_Ordinal: Primary

  • cwe_Nature: CanPrecede cwe_CWE_ID: 120 cwe_View_ID: 1000

  • cwe_Nature: CanPrecede cwe_CWE_ID: 123 cwe_View_ID: 1000

适用平台

Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
Integrity Modify Memory The use of previously freed memory may corrupt valid data, if the memory area in question has been allocated and used properly elsewhere.
Availability DoS: Crash, Exit, or Restart If chunk consolidation occurs after the use of previously freed data, the process may crash when invalid data is used as chunk information.
['Integrity', 'Confidentiality', 'Availability'] Execute Unauthorized Code or Commands If malicious data is entered before chunk consolidation can take place, it may be possible to take advantage of a write-what-where primitive to execute arbitrary code.

可能的缓解方案

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.

示例代码

The following example demonstrates the weakness.

bad 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);
}

The following code illustrates a use after free error:

bad C

char ptr = (char)malloc (SIZE);
if (err) {
abrt = 1;
free(ptr);
}
...
if (abrt) {
logError("operation aborted before commit", ptr);
}

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

分析过的案例

标识 说明 链接
CVE-2010-4168 Use-after-free triggered by closing a connection while data is still being transmitted. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-4168
CVE-2010-2941 Improper allocation for invalid data leads to use-after-free. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2941
CVE-2010-2547 certificate with a large number of Subject Alternate Names not properly handled in realloc, leading to use-after-free https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2547
CVE-2010-1772 Timers are not disabled when a related object is deleted https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-1772
CVE-2010-1437 Access to a "dead" object that is being cleaned up https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-1437
CVE-2010-1208 object is deleted even with a non-zero reference count, and later accessed https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-1208
CVE-2010-0629 use-after-free involving request containing an invalid version number https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-0629
CVE-2010-0378 unload of an object that is currently being accessed by other functionality https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-0378
CVE-2010-0302 incorrectly tracking a reference count leads to use-after-free https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-0302
CVE-2010-0249 use-after-free related to use of uninitialized memory https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-0249
CVE-2010-0050 HTML document with incorrectly-nested tags https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-0050
CVE-2009-3658 Use after free in ActiveX object by providing a malformed argument to a method https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3658
CVE-2009-3616 use-after-free by disconnecting during data transfer, or a message containing incorrect data types https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3616
CVE-2009-3553 disconnect during a large data transfer causes incorrect reference count, leading to use-after-free https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3553
CVE-2009-2416 use-after-free found by fuzzing https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-2416
CVE-2009-1837 Chain: race condition (CWE-362) from improper handling of a page transition in web client while an applet is loading (CWE-368) leads to use after free (CWE-416) https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-1837
CVE-2009-0749 realloc generates new buffer and pointer, but previous pointer is still retained, leading to use after free https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-0749
CVE-2010-3328 Use-after-free in web browser, probably resultant from not initializing memory. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3328
CVE-2008-5038 use-after-free when one thread accessed memory that was freed by another thread https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5038
CVE-2008-0077 assignment of malformed values to certain properties triggers use after free https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-0077
CVE-2006-4434 mail server does not properly handle a long header. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-4434
CVE-2010-2753 chain: integer overflow leads to use-after-free https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2753
CVE-2006-4997 freed pointer dereference https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-4997

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms Use After Free
CLASP Using freed memory
CERT C Secure Coding MEM00-C Allocate and free memory in the same module, at the same level of abstraction
CERT C Secure Coding MEM01-C Store a new value in pointers immediately after free()
CERT C Secure Coding MEM30-C Exact Do not access freed memory
Software Fault Patterns SFP15 Faulty Resource Use

引用