Skip to content

[3.12] gh-123083: Fix a potential use-after-free in ``STORE_ATTR_WITH… #123237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 a potential use-after-free in ``STORE_ATTR_WITH_HINT``.
9 changes: 5 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1999,14 +1999,15 @@ dummy_func(
new_version = _PyDict_NotifyEvent(tstate->interp, PyDict_EVENT_MODIFIED, dict, name, value);
ep->me_value = value;
}
Py_DECREF(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(value)) {
_PyObject_GC_TRACK(dict);
}
/* PEP 509 */
dict->ma_version_tag = new_version;
dict->ma_version_tag = new_version; // PEP 509
// old_value should be DECREFed after GC track checking is done, if not, it could raise a segmentation fault,
// when dict only holds the strong reference to value in ep->me_value.
Py_DECREF(old_value);
STAT_INC(STORE_ATTR, hit);
Py_DECREF(owner);
}

Expand Down
Loading
Loading