Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_importlib/import_/test_relative_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ def test_relative_import_no_package_exists_absolute(self):
self.__import__('sys', {'__package__': '', '__spec__': None},
level=1)

def test_malicious_relative_import(self):
# testing for gh-134100
import sys
loooong = "".ljust(0x23000, "b")
sys.modules.update({f"a.{loooong}.c": {}})
with self.assertRaisesRegex(KeyError, r"'a\.b+' not in sys\.modules as expected"):
__import__(f"{loooong}.c", {"__package__": "a"}, level=1)


(Frozen_RelativeImports,
Source_RelativeImports
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a use-after-free bug that would occur when an imported module wasn't
in :data:`sys.modules` after the initial import. Patch by Nico-Posada.
4 changes: 3 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3854,15 +3854,17 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}

final_mod = import_get_module(tstate, to_return);
Py_DECREF(to_return);
if (final_mod == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_KeyError,
"%R not in sys.modules as expected",
to_return);
}
Py_DECREF(to_return);
goto error;
}

Py_DECREF(to_return);
}
}
else {
Expand Down
Loading