From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 11 Jun 2024 07:28:45 +0000 (+0200) Subject: [3.13] gh-120298: Fix use-after-free in `list_richcompare_impl` (GH-120303) (#120340) X-Git-Tag: v3.13.0b3~141 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=52225c64f7cd55f2bfe8515d4daf1a5ed4be6d7b;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-120298: Fix use-after-free in `list_richcompare_impl` (GH-120303) (#120340) gh-120298: Fix use-after-free in `list_richcompare_impl` (GH-120303) (cherry picked from commit 141babad9b4eceb83371bf19ba3a36b50dd05250) Co-authored-by: Nikita Sobolev Co-authored-by: Serhiy Storchaka --- diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py index 0601b33e79eb..d21429fae09b 100644 --- a/Lib/test/test_list.py +++ b/Lib/test/test_list.py @@ -234,6 +234,17 @@ class ListTest(list_tests.CommonTest): list4 = [1] self.assertFalse(list3 == list4) + def test_lt_operator_modifying_operand(self): + # See gh-120298 + class evil: + def __lt__(self, other): + other.clear() + return NotImplemented + + a = [[evil()]] + with self.assertRaises(TypeError): + a[0] < a + @cpython_only def test_preallocation(self): iterable = [0] * 10 diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-10-10-42-48.gh-issue-120298.napREA.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-10-10-42-48.gh-issue-120298.napREA.rst new file mode 100644 index 000000000000..531d39517ac4 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-10-10-42-48.gh-issue-120298.napREA.rst @@ -0,0 +1,2 @@ +Fix use-after free in ``list_richcompare_impl`` which can be invoked via +some specificly tailored evil input. diff --git a/Objects/listobject.c b/Objects/listobject.c index d09bb6391034..6829d5d28656 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3382,7 +3382,14 @@ list_richcompare_impl(PyObject *v, PyObject *w, int op) } /* Compare the final item again using the proper operator */ - return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); + PyObject *vitem = vl->ob_item[i]; + PyObject *witem = wl->ob_item[i]; + Py_INCREF(vitem); + Py_INCREF(witem); + PyObject *result = PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); + Py_DECREF(vitem); + Py_DECREF(witem); + return result; } static PyObject *