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

index b5a10edc12e183ac994b939f2310f9f7ce7a1900..3776389ebb645b9d9cba4bc3f589d2af933951f1 100644 (file)
@@ -3997,6 +3997,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:
+            pass
+
+        C.__name__ = Nasty("abc")
+        C.__name__ = "normal"
+
     def test_subclass_right_op(self):
         # Testing correct dispatch of subclass overloading __r<op>__...
 
index 4bee5b508e6199b8e94e870081ef4369f392dd6d..94a3ed32e9af4f05f3a3884c3bd76e774dd85bc2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.2?
 Core and Builtins
 -----------------
 
+- Issue #16447: Fixed potential segmentation fault when setting __name__ on a
+  class.
+
 - Issue #17669: Fix crash involving finalization of generators using yield from.
 
 - Issue #17619: Make input() check for Ctrl-C correctly on Windows.
index 9f89972594208338231ab365e029468444678ca3..6ece741833a03459e543c18d0453ed45eeb2ba5e 100644 (file)
@@ -298,10 +298,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 = tp_name;
+    Py_DECREF(tmp);
 
     return 0;
 }