Skip to content

Commit

Permalink
pythongh-123083: Fix STORE_ATTR_WITH_HINT has potential use-after-free
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Aug 17, 2024
1 parent 35d8ac7 commit 6cdacd8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
18 changes: 18 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,24 @@ def test_dict_items_result_gc_reversed(self):
gc.collect()
self.assertTrue(gc.is_tracked(next(it)))

def test_store_evilattr(self):
class EvilAttr:
def __init__(self, d):
self.d = d

def __del__(self):
if 'attr' in self.d:
del self.d['attr']
gc.collect()

class Obj:
pass

obj = Obj()
obj.__dict__ = {}
for _ in range(10):
obj.attr = EvilAttr(obj.__dict__)

def test_str_nonstr(self):
# cpython uses a different lookup function if the dict only contains
# `str` keys. Make sure the unoptimized path is used when a non-`str`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix STORE_ATTR_WITH_HINT has potential use-after-free.
2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2234,12 +2234,12 @@ dummy_func(
PyDict_WatchEvent event = old_value == NULL ? PyDict_EVENT_ADDED : PyDict_EVENT_MODIFIED;
new_version = _PyDict_NotifyEvent(tstate->interp, event, dict, name, PyStackRef_AsPyObjectBorrow(value));
ep->me_value = PyStackRef_AsPyObjectSteal(value);
Py_XDECREF(old_value);
STAT_INC(STORE_ATTR, hit);
/* Ensure dict is GC tracked if it needs to be */
if (!_PyObject_GC_IS_TRACKED(dict) && _PyObject_GC_MAY_BE_TRACKED(PyStackRef_AsPyObjectBorrow(value))) {
_PyObject_GC_TRACK(dict);
}
Py_XDECREF(old_value);
/* PEP 509 */
dict->ma_version_tag = new_version;
PyStackRef_CLOSE(owner);
Expand Down
2 changes: 1 addition & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6cdacd8

Please sign in to comment.