]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #16447: Fix potential segfault when setting __name__ on a class.
authorMark Dickinson <dickinsm@gmail.com>
Sat, 13 Apr 2013 14:19:05 +0000 (15:19 +0100)
committerMark Dickinson <dickinsm@gmail.com>
Sat, 13 Apr 2013 14:19:05 +0000 (15:19 +0100)
Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index bfb3552f55ae42fc53f32b175aa099b8e02fc120..07664ac208de61bf064e08925768b50279022963 100644 (file)
@@ -4136,6 +4136,20 @@ order (MRO) for bases """
         C.__name__ = 'D.E'
         self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
 
+    def test_evil_type_name(self):
+        # A badly placed Py_DECREF in type_set_name led to arbitrary code
+        # execution while the type structure was not in a sane state, and a
+        # possible segmentation fault as a result.  See bug #16447.
+        class Nasty(str):
+            def __del__(self):
+                C.__name__ = "other"
+
+        class C(object):
+            pass
+
+        C.__name__ = Nasty("abc")
+        C.__name__ = "normal"
+
     def test_subclass_right_op(self):
         # Testing correct dispatch of subclass overloading __r<op>__...
 
index ee5393c286779c82f25bee4fb4d350590041343f..cb0dfc81e735973ee940b733b1bd46a483317d0f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Build
 Core and Builtins
 -----------------
 
+- Issue #16447: Fixed potential segmentation fault when setting __name__ on a
+  class.
+
 - Issue #17610: Don't rely on non-standard behavior of the C qsort() function.
 
 Library
index 6c88f53af42172dc975f5db804aae8563f509052..be04c9e9140f9b33ea72db3ad20ccfa3d00e880f 100644 (file)
@@ -225,6 +225,7 @@ static int
 type_set_name(PyTypeObject *type, PyObject *value, void *context)
 {
     PyHeapTypeObject* et;
+    PyObject *tmp;
 
     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
         PyErr_Format(PyExc_TypeError,
@@ -253,10 +254,13 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
 
     Py_INCREF(value);
 
-    Py_DECREF(et->ht_name);
+    /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name
+       value.  (Bug #16447.)  */
+    tmp = et->ht_name;
     et->ht_name = value;
 
     type->tp_name = PyString_AS_STRING(value);
+    Py_DECREF(tmp);
 
     return 0;
 }