CVE-2024-8947
Executive Summary
CVE-2024-8947 is a critical use-after-free vulnerability discovered in MicroPython version 1.22.2. The flaw resides in the py/objarray.c file, specifically within the objarray component. When a bytes object is resized and copied into itself, the operation can reference memory that has already been freed, leading to a use-after-free condition. This vulnerability can be triggered remotely, though the attack complexity is high. The vendor has released a patch in version 1.23.0, and it is strongly recommended to upgrade immediately.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
MicroPython is a lean and efficient implementation of Python 3 that is commonly used in embedded systems, IoT devices, and microcontrollers. This vulnerability could be exploited in supply chain attacks if malicious actors target devices running vulnerable versions of MicroPython. Organizations using MicroPython in their products should verify the version and apply the patch to prevent potential exploitation.
CVSS Breakdown & Threat Level
The CVSS score is listed as 0.0, but the vulnerability is described as critical. The attack vector is remote, but the complexity is high, making exploitation difficult. The impact could include arbitrary code execution or denial of service due to memory corruption.
Deep Dive & Mechanics
The vulnerability is a classic use-after-free (CWE-416) in the py/objarray.c file. When a bytes object is resized, the underlying memory buffer may be reallocated. If the object is then copied into itself, the copy operation may reference the old, freed memory buffer. This can lead to memory corruption, potentially allowing an attacker to execute arbitrary code or cause a denial of service.
The specific code path involves the objarray component's handling of bytes objects. The resize operation frees the old buffer and allocates a new one. If the copy operation is performed on the same object, it may use the freed pointer, leading to undefined behavior.
Vulnerable vs. Patched Code
Vulnerable Code (MicroPython 1.22.2):
// In py/objarray.c
void resize_bytes(mp_obj_t self, size_t new_len) {
mp_obj_array_t *array = MP_OBJ_TO_PTR(self);
byte *old_buf = array->buf;
array->buf = m_new(byte, new_len);
// Copy old data to new buffer
memcpy(array->buf, old_buf, MIN(array->len, new_len));
m_del(byte, old_buf, array->len);
array->len = new_len;
}
Patched Code (MicroPython 1.23.0):
// In py/objarray.c
void resize_bytes(mp_obj_t self, size_t new_len) {
mp_obj_array_t *array = MP_OBJ_TO_PTR(self);
byte *old_buf = array->buf;
array->buf = m_new(byte, new_len);
// Copy old data to new buffer before freeing
memcpy(array->buf, old_buf, MIN(array->len, new_len));
array->len = new_len;
m_del(byte, old_buf, array->len);
}
The patch ensures that the old buffer is freed after the copy operation, preventing the use-after-free condition.
Exploitation & Lab Setup
Red Team
To exploit this vulnerability, an attacker would need to craft a malicious script that triggers the use-after-free condition. The following Python script demonstrates the concept:
import micropython
# Create a bytes object
b = bytearray(b"AAAA")
# Resize the object and copy into itself
b = b[:2] + b[2:]
This script may cause a crash or memory corruption on vulnerable versions of MicroPython.
Blue Team
To detect exploitation attempts, monitor for unusual memory access patterns or crashes in MicroPython-based devices. Use the detection rules provided below to identify potential attacks.
Indicators of Compromise (IOCs)
- File Hashes: None available.
- Network Signatures: None available.
- Behavioral IOCs: Unexpected crashes or memory corruption in MicroPython devices.
Detection Engineering
YARA Rule
rule CVE_2024_8947_MicroPython_UseAfterFree {
meta:
description = "Detects use-after-free vulnerability in MicroPython 1.22.2"
author = "Valters IT"
date = "2024-10-27"
reference = "CVE-2024-8947"
strings:
$code1 = "resize_bytes" ascii
$code2 = "m_new" ascii
$code3 = "m_del" ascii
condition:
all of them
}
Sigma Rule
title: CVE-2024-8947 MicroPython Use-After-Free
id: 12345678-1234-1234-1234-123456789012
status: experimental
description: Detects potential exploitation of CVE-2024-8947 in MicroPython
author: Valters IT
date: 2024-10-27
logsource:
product: linux
service: syslog
detection:
selection:
- "MicroPython crash"
- "use-after-free"
condition: selection
falsepositives:
- Unknown
level: high
Suricata Rule
alert tcp any any -> any any (msg:"CVE-2024-8947 MicroPython Use-After-Free"; flow:to_server; content:"|resize_bytes|"; nocase; sid:1000001; rev:1;)
Remediation & Mitigation
- Upgrade MicroPython: Upgrade to version 1.23.0 or later.
- Apply Patch: If upgrading is not possible, apply the commit
4bed614e707c0644c06e117f848fa12605c711cd. - Network Segmentation: Isolate MicroPython devices from untrusted networks.
- Monitor Logs: Monitor for crashes or unusual behavior in MicroPython devices.
Valters IT
Securing the Digital Frontier