<div dir="ltr">Test case?<br><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Apr 13, 2013 at 7:19 AM, mark.dickinson <span dir="ltr"><<a href="mailto:python-checkins@python.org" target="_blank">python-checkins@python.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><a href="http://hg.python.org/cpython/rev/d5e5017309b1" target="_blank">http://hg.python.org/cpython/rev/d5e5017309b1</a><br>


changeset:   83283:d5e5017309b1<br>
branch:      2.7<br>
user:        Mark Dickinson <<a href="mailto:dickinsm@gmail.com">dickinsm@gmail.com</a>><br>
date:        Sat Apr 13 15:19:05 2013 +0100<br>
summary:<br>
  Issue #16447: Fix potential segfault when setting __name__ on a class.<br>
<br>
files:<br>
  Lib/test/test_descr.py |  14 ++++++++++++++<br>
  Misc/NEWS              |   3 +++<br>
  Objects/typeobject.c   |   6 +++++-<br>
  3 files changed, 22 insertions(+), 1 deletions(-)<br>
<br>
<br>
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py<br>
--- a/Lib/test/test_descr.py<br>
+++ b/Lib/test/test_descr.py<br>
@@ -4136,6 +4136,20 @@<br>
         C.__name__ = 'D.E'<br>
         self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))<br>
<br>
+    def test_evil_type_name(self):<br>
+        # A badly placed Py_DECREF in type_set_name led to arbitrary code<br>
+        # execution while the type structure was not in a sane state, and a<br>
+        # possible segmentation fault as a result.  See bug #16447.<br>
+        class Nasty(str):<br>
+            def __del__(self):<br>
+                C.__name__ = "other"<br>
+<br>
+        class C(object):<br>
+            pass<br>
+<br>
+        C.__name__ = Nasty("abc")<br>
+        C.__name__ = "normal"<br>
+<br>
     def test_subclass_right_op(self):<br>
         # Testing correct dispatch of subclass overloading __r<op>__...<br>
<br>
diff --git a/Misc/NEWS b/Misc/NEWS<br>
--- a/Misc/NEWS<br>
+++ b/Misc/NEWS<br>
@@ -17,6 +17,9 @@<br>
 Core and Builtins<br>
 -----------------<br>
<br>
+- Issue #16447: Fixed potential segmentation fault when setting __name__ on a<br>
+  class.<br>
+<br>
 - Issue #17610: Don't rely on non-standard behavior of the C qsort() function.<br>
<br>
 Library<br>
diff --git a/Objects/typeobject.c b/Objects/typeobject.c<br>
--- a/Objects/typeobject.c<br>
+++ b/Objects/typeobject.c<br>
@@ -225,6 +225,7 @@<br>
 type_set_name(PyTypeObject *type, PyObject *value, void *context)<br>
 {<br>
     PyHeapTypeObject* et;<br>
+    PyObject *tmp;<br>
<br>
     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {<br>
         PyErr_Format(PyExc_TypeError,<br>
@@ -253,10 +254,13 @@<br>
<br>
     Py_INCREF(value);<br>
<br>
-    Py_DECREF(et->ht_name);<br>
+    /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name<br>
+       value.  (Bug #16447.)  */<br>
+    tmp = et->ht_name;<br>
     et->ht_name = value;<br>
<br>
     type->tp_name = PyString_AS_STRING(value);<br>
+    Py_DECREF(tmp);<br>
<br>
     return 0;<br>
 }<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Repository URL: <a href="http://hg.python.org/cpython" target="_blank">http://hg.python.org/cpython</a><br>
</font></span><br>_______________________________________________<br>
Python-checkins mailing list<br>
<a href="mailto:Python-checkins@python.org">Python-checkins@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-checkins" target="_blank">http://mail.python.org/mailman/listinfo/python-checkins</a><br>
<br></blockquote></div><br></div></div></div></div>