Skip to content

Commit 6116d70

Browse files
pythongh-143403: Fix a UAF in _BINARY_OP_INPLACE_ADD_UNICODE (pythonGH-143404)
Fix a UAF in `_BINARY_OP_INPLACE_ADD_UNICODE`
1 parent 9609574 commit 6116d70

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

Python/bytecodes.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,15 +802,18 @@ dummy_func(
802802
*/
803803
assert(Py_REFCNT(left_o) >= 2 || !PyStackRef_IsHeapSafe(left));
804804
PyObject *temp = PyStackRef_AsPyObjectSteal(*target_local);
805-
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
806-
PyUnicode_Append(&temp, right_o);
807-
PyStackRef_CLOSE_SPECIALIZED(right, _PyUnicode_ExactDealloc);
808-
DEAD(right);
805+
PyObject *right_o = PyStackRef_AsPyObjectSteal(right);
806+
/* gh-143403: It's critical to close this reference *before*
807+
* we append. Otherwise, append can move the underlying
808+
* unicode object, which will cause a use after free!
809+
*/
809810
PyStackRef_CLOSE_SPECIALIZED(left, _PyUnicode_ExactDealloc);
810811
DEAD(left);
812+
PyUnicode_Append(&temp, right_o);
813+
_Py_DECREF_SPECIALIZED(right_o, _PyUnicode_ExactDealloc);
814+
*target_local = PyStackRef_NULL;
811815
ERROR_IF(temp == NULL);
812816
res = PyStackRef_FromPyObjectSteal(temp);
813-
*target_local = PyStackRef_NULL;
814817
}
815818

816819
op(_GUARD_BINARY_OP_EXTEND, (descr/4, left, right -- left, right)) {

Python/executor_cases.c.h

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)