mirror of
https://github.com/python/cpython.git
synced 2025-07-23 19:25:40 +00:00
[3.14] gh-134100: Fix use-after-free in PyImport_ImportModuleLevelObject
(GH-134117) (#134171)
gh-134100: Fix use-after-free in `PyImport_ImportModuleLevelObject` (GH-134117)
(cherry picked from commit 4e9005d32f
)
Co-authored-by: Nico-Posada <102486290+Nico-Posada@users.noreply.github.com>
This commit is contained in:
parent
bf39decabd
commit
8d51ed6b05
3 changed files with 20 additions and 1 deletions
|
@ -223,6 +223,21 @@ class RelativeImports:
|
||||||
self.__import__('sys', {'__package__': '', '__spec__': None},
|
self.__import__('sys', {'__package__': '', '__spec__': None},
|
||||||
level=1)
|
level=1)
|
||||||
|
|
||||||
|
def test_malicious_relative_import(self):
|
||||||
|
# https://github.com/python/cpython/issues/134100
|
||||||
|
# Test to make sure UAF bug with error msg doesn't come back to life
|
||||||
|
import sys
|
||||||
|
loooong = "".ljust(0x23000, "b")
|
||||||
|
name = f"a.{loooong}.c"
|
||||||
|
|
||||||
|
with util.uncache(name):
|
||||||
|
sys.modules[name] = {}
|
||||||
|
with self.assertRaisesRegex(
|
||||||
|
KeyError,
|
||||||
|
r"'a\.b+' not in sys\.modules as expected"
|
||||||
|
):
|
||||||
|
__import__(f"{loooong}.c", {"__package__": "a"}, level=1)
|
||||||
|
|
||||||
|
|
||||||
(Frozen_RelativeImports,
|
(Frozen_RelativeImports,
|
||||||
Source_RelativeImports
|
Source_RelativeImports
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix a use-after-free bug that occurs when an imported module isn't
|
||||||
|
in :data:`sys.modules` after its initial import. Patch by Nico-Posada.
|
|
@ -3852,15 +3852,17 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
|
||||||
}
|
}
|
||||||
|
|
||||||
final_mod = import_get_module(tstate, to_return);
|
final_mod = import_get_module(tstate, to_return);
|
||||||
Py_DECREF(to_return);
|
|
||||||
if (final_mod == NULL) {
|
if (final_mod == NULL) {
|
||||||
if (!_PyErr_Occurred(tstate)) {
|
if (!_PyErr_Occurred(tstate)) {
|
||||||
_PyErr_Format(tstate, PyExc_KeyError,
|
_PyErr_Format(tstate, PyExc_KeyError,
|
||||||
"%R not in sys.modules as expected",
|
"%R not in sys.modules as expected",
|
||||||
to_return);
|
to_return);
|
||||||
}
|
}
|
||||||
|
Py_DECREF(to_return);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Py_DECREF(to_return);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue