FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-145244: Fix use-after-free on borrowed dict key in json encoder by raminfp · Pull Request #145245 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions Lib/test/test_json/test_dump.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ def __lt__(self, o):
d[1337] = "true.dat"
self.assertEqual(self.dumps(d, sort_keys=True), '{"1337": "true.dat"}')

# gh-145244: UAF on borrowed key when default callback mutates dict
def test_default_clears_dict_key_uaf(self):
class Evil:
pass

class AlsoEvil:
pass

# Use a non-interned string key so it can actually be freed
key = "A" * 100
target = {key: Evil()}
del key

def evil_default(obj):
if isinstance(obj, Evil):
target.clear()
return AlsoEvil()
raise TypeError("not serializable")

with self.assertRaises(TypeError):
self.json.dumps(target, default=evil_default,
check_circular=False)

def test_dumps_str_subclass(self):
# Don't call obj.__str__() on str subclasses

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a use-after-free in :mod:`json` encoder when a ``default`` callback
mutates the dictionary being serialized.
11 changes: 4 additions & 7 deletions Modules/_json.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1784,24 +1784,21 @@ _encoder_iterate_dict_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(dct, &pos, &key, &value)) {
#ifdef Py_GIL_DISABLED
// gh-119438: in the free-threading build the critical section on dct can get suspended
// gh-119438, gh-145244: key and value are borrowed refs from
// PyDict_Next(). encoder_encode_key_value() may invoke user
// Python code (the 'default' callback) that can mutate or
// clear the dict, so we must hold strong references.
Py_INCREF(key);
Py_INCREF(value);
#endif
if (encoder_encode_key_value(s, writer, first, dct, key, value,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

reviewer note for the future: I explored if we could deterministically understand if encoder_encode_key_value was going to call user code which could lead to the problem... it's too complicated. while a couple heuristics exist for a subset of cases, they'd be fragile.

indent_level, indent_cache,
separator) < 0) {
#ifdef Py_GIL_DISABLED
Py_DECREF(key);
Py_DECREF(value);
#endif
return -1;
}
#ifdef Py_GIL_DISABLED
Py_DECREF(key);
Py_DECREF(value);
#endif
}
return 0;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL