Skip to content

Commit 297f2e0

Browse files
authored
gh-123083: Fix a potential use-after-free in STORE_ATTR_WITH_HINT (gh-123092)
1 parent 4abc1c1 commit 297f2e0

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

Lib/test/test_dict.py

+18
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,24 @@ def test_dict_items_result_gc_reversed(self):
14761476
gc.collect()
14771477
self.assertTrue(gc.is_tracked(next(it)))
14781478

1479+
def test_store_evilattr(self):
1480+
class EvilAttr:
1481+
def __init__(self, d):
1482+
self.d = d
1483+
1484+
def __del__(self):
1485+
if 'attr' in self.d:
1486+
del self.d['attr']
1487+
gc.collect()
1488+
1489+
class Obj:
1490+
pass
1491+
1492+
obj = Obj()
1493+
obj.__dict__ = {}
1494+
for _ in range(10):
1495+
obj.attr = EvilAttr(obj.__dict__)
1496+
14791497
def test_str_nonstr(self):
14801498
# cpython uses a different lookup function if the dict only contains
14811499
# `str` keys. Make sure the unoptimized path is used when a non-`str`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a potential use-after-free in ``STORE_ATTR_WITH_HINT``.

Objects/dictobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,8 @@ insert_split_value(PyInterpreterState *interp, PyDictObject *mp, PyObject *key,
17031703
uint64_t new_version = _PyDict_NotifyEvent(interp, PyDict_EVENT_MODIFIED, mp, key, value);
17041704
STORE_SPLIT_VALUE(mp, ix, Py_NewRef(value));
17051705
mp->ma_version_tag = new_version;
1706+
// old_value should be DECREFed after GC track checking is done, if not, it could raise a segmentation fault,
1707+
// when dict only holds the strong reference to value in ep->me_value.
17061708
Py_DECREF(old_value);
17071709
}
17081710
ASSERT_CONSISTENT(mp);

Python/bytecodes.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -2235,18 +2235,19 @@ dummy_func(
22352235
DEOPT_IF(!DK_IS_UNICODE(dict->ma_keys));
22362236
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
22372237
DEOPT_IF(ep->me_key != name);
2238+
/* Ensure dict is GC tracked if it needs to be */
2239+
if (!_PyObject_GC_IS_TRACKED(dict) && _PyObject_GC_MAY_BE_TRACKED(PyStackRef_AsPyObjectBorrow(value))) {
2240+
_PyObject_GC_TRACK(dict);
2241+
}
22382242
old_value = ep->me_value;
22392243
PyDict_WatchEvent event = old_value == NULL ? PyDict_EVENT_ADDED : PyDict_EVENT_MODIFIED;
22402244
new_version = _PyDict_NotifyEvent(tstate->interp, event, dict, name, PyStackRef_AsPyObjectBorrow(value));
22412245
ep->me_value = PyStackRef_AsPyObjectSteal(value);
2246+
dict->ma_version_tag = new_version; // PEP 509
2247+
// old_value should be DECREFed after GC track checking is done, if not, it could raise a segmentation fault,
2248+
// when dict only holds the strong reference to value in ep->me_value.
22422249
Py_XDECREF(old_value);
22432250
STAT_INC(STORE_ATTR, hit);
2244-
/* Ensure dict is GC tracked if it needs to be */
2245-
if (!_PyObject_GC_IS_TRACKED(dict) && _PyObject_GC_MAY_BE_TRACKED(PyStackRef_AsPyObjectBorrow(value))) {
2246-
_PyObject_GC_TRACK(dict);
2247-
}
2248-
/* PEP 509 */
2249-
dict->ma_version_tag = new_version;
22502251
PyStackRef_CLOSE(owner);
22512252
}
22522253

Python/executor_cases.c.h

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)