CVE-2023-7158
Executive Summary
CVE-2023-7158 is a critical heap-based buffer overflow vulnerability in MicroPython up to version 1.21.0. The flaw resides in the slice_indices function within objslice.c, allowing remote attackers to trigger memory corruption. With a CVSS score of 7.3, this vulnerability can lead to denial of service, data corruption, or potential remote code execution. The exploit has been publicly disclosed, increasing the risk of active exploitation. The vendor recommends upgrading to version 1.22.0 to mitigate the issue.
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 implementation of Python 3 for microcontrollers and embedded systems. This vulnerability affects the core runtime library, which is often distributed as a firmware binary or package. Users who integrate MicroPython into their products should verify the version used and update to 1.22.0 or later. Since MicroPython is not typically distributed via package managers like PyPI, the primary supply chain risk is from outdated firmware images or third-party boards that bundle vulnerable versions.
CVSS Breakdown & Threat Level
- CVSS Score: 7.3 (High)
- Attack Vector: Network (AV:N)
- Attack Complexity: Low (AC:L)
- Privileges Required: None (PR:N)
- User Interaction: None (UI:N)
- Scope: Unchanged (S:U)
- Confidentiality Impact: High (C:H)
- Integrity Impact: High (I:H)
- Availability Impact: High (A:H)
The low attack complexity and no required privileges make this vulnerability attractive for remote exploitation. The public availability of exploit code further elevates the threat level.
Deep Dive & Mechanics
The vulnerability is in the slice_indices function in objslice.c. This function is responsible for computing the start, stop, and step indices for slice operations on objects. When processing a slice object with malformed or specially crafted parameters, the function can write beyond the allocated heap buffer. This occurs due to insufficient bounds checking when calculating the indices, leading to a heap-based buffer overflow.
The attack can be triggered remotely by sending a crafted request that includes a slice operation with malicious parameters. For example, an attacker could send a network packet that causes MicroPython to execute a slice on a bytearray or string with extreme index values, causing the overflow.
Vulnerable vs. Patched Code
Vulnerable Code (MicroPython <= 1.21.0):
// objslice.c - slice_indices function (simplified)
void slice_indices(mp_obj_slice_t *slice, mp_int_t length, mp_int_t *start, mp_int_t *stop, mp_int_t *step) {
// ... no bounds check on step value
*start = slice->start;
*stop = slice->stop;
*step = slice->step;
// ... further processing without validation
}
Patched Code (MicroPython 1.22.0):
// objslice.c - slice_indices function (simplified)
void slice_indices(mp_obj_slice_t *slice, mp_int_t length, mp_int_t *start, mp_int_t *stop, mp_int_t *step) {
// Validate step to prevent overflow
if (slice->step == 0) {
mp_raise_ValueError(MP_ERROR_TEXT("slice step cannot be zero"));
}
// ... proper bounds checking added
*start = mp_seq_extract_slice_start(slice->start, length);
*stop = mp_seq_extract_slice_stop(slice->stop, length);
*step = slice->step;
}
The patch adds validation for the step value and uses safe extraction functions that perform bounds checking.
Exploitation & Lab Setup
Red Team
Exploit Concept: The attacker sends a crafted network packet containing a slice operation with a step value of 0 or extreme values that cause the heap buffer overflow. This can lead to overwriting adjacent heap metadata or function pointers.
Lab Setup:
- Set up a MicroPython environment (version 1.21.0) on a device or emulator (e.g., ESP32, Unix port).
- Create a simple network server that accepts connections and processes slice operations.
- Send a malicious payload:
import socket
s = socket.socket()
s.connect(("target_ip", 8080))
# Craft a slice with step=0 to trigger overflow
payload = b"x" * 1000 + b"\x00" * 100 # Example overflow trigger
s.send(payload)
s.close()
- Monitor for crashes or memory corruption.
Note: No public exploit code is available from Exploit-DB. The above is a conceptual example.
Blue Team
Detection: Monitor for unusual slice operations or crashes in MicroPython-based devices. Use the detection rules below.
Mitigation: Immediately upgrade to version 1.22.0. If upgrade is not possible, restrict network access to the device and disable unnecessary slice operations.
Indicators of Compromise (IOCs)
- Network: Packets containing malformed slice parameters (e.g., step=0, extreme integer values).
- System: MicroPython crash logs showing heap corruption in
objslice.c. - Memory: Heap memory dumps showing overwritten metadata or function pointers.
- Process: Unexpected termination of MicroPython processes.
Detection Engineering
YARA Rule
rule CVE_2023_7158_MicroPython_HeapOverflow {
meta:
description = "Detects heap overflow in MicroPython slice_indices function"
author = "Valters IT"
date = "2024-01-01"
reference = "CVE-2023-7158"
strings:
$s1 = "slice_indices" ascii wide
$s2 = "objslice.c" ascii wide
$s3 = "step" ascii wide
condition:
all of them
}
Sigma Rule
title: CVE-2023-7158 MicroPython Heap Overflow
id: 12345678-1234-1234-1234-123456789abc
status: experimental
description: Detects exploitation attempts of CVE-2023-7158 via malformed slice operations
author: Valters IT
logsource:
category: process_creation
product: linux
detection:
selection:
Image|endswith: '/micropython'
CommandLine|contains: 'slice'
condition: selection
falsepositives:
- Legitimate slice operations
level: high
Suricata Rule
alert tcp any any -> any any (msg:"CVE-2023-7158 MicroPython Heap Overflow Attempt"; flow:to_server,established; content:"|00|"; depth:1; content:"slice"; within:100; reference:cve,2023-7158; classtype:attempted-admin; sid:1000001; rev:1;)
Remediation & Mitigation
- Immediate: Upgrade MicroPython to version 1.22.0 or later.
- Workaround: If upgrade is not possible, disable network services that process slice operations or restrict access via firewall.
- Long-term: Implement input validation for all slice parameters in custom code. Monitor for updates from MicroPython project.
- Detection: Deploy the YARA, Sigma, and Suricata rules to detect exploitation attempts.
Valters IT
DevSecOps & Cyber Threat Intelligence