Blame SOURCES/00357-CVE-2021-3177.patch

05b462
commit 30e41798f40c684be57d7ccfebf5c6ad94c0ff97
05b462
Author: Petr Viktorin <pviktori@redhat.com>
05b462
Date:   Wed Jan 20 15:21:43 2021 +0100
05b462
05b462
    CVE-2021-3177: Replace snprintf with Python unicode formatting in ctypes param reprs
05b462
    
05b462
    Backport of Python3 commit 916610ef90a0d0761f08747f7b0905541f0977c7:
05b462
    https://bugs.python.org/issue42938
05b462
    https://github.com/python/cpython/pull/24239
05b462
05b462
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
05b462
index 23c1b6e2259..77300d71ae1 100644
05b462
--- a/Lib/ctypes/test/test_parameters.py
05b462
+++ b/Lib/ctypes/test/test_parameters.py
05b462
@@ -206,6 +206,49 @@ class SimpleTypesTestCase(unittest.TestCase):
05b462
         with self.assertRaises(ZeroDivisionError):
05b462
             WorseStruct().__setstate__({}, b'foo')
05b462
 
05b462
+    def test_parameter_repr(self):
05b462
+        from ctypes import (
05b462
+            c_bool,
05b462
+            c_char,
05b462
+            c_wchar,
05b462
+            c_byte,
05b462
+            c_ubyte,
05b462
+            c_short,
05b462
+            c_ushort,
05b462
+            c_int,
05b462
+            c_uint,
05b462
+            c_long,
05b462
+            c_ulong,
05b462
+            c_longlong,
05b462
+            c_ulonglong,
05b462
+            c_float,
05b462
+            c_double,
05b462
+            c_longdouble,
05b462
+            c_char_p,
05b462
+            c_wchar_p,
05b462
+            c_void_p,
05b462
+        )
05b462
+        self.assertRegexpMatches(repr(c_bool.from_param(True)), r"^<cparam '\?' at 0x[A-Fa-f0-9]+>$")
05b462
+        self.assertEqual(repr(c_char.from_param('a')), "<cparam 'c' ('a')>")
05b462
+        self.assertRegexpMatches(repr(c_wchar.from_param('a')), r"^<cparam 'u' at 0x[A-Fa-f0-9]+>$")
05b462
+        self.assertEqual(repr(c_byte.from_param(98)), "<cparam 'b' (98)>")
05b462
+        self.assertEqual(repr(c_ubyte.from_param(98)), "<cparam 'B' (98)>")
05b462
+        self.assertEqual(repr(c_short.from_param(511)), "<cparam 'h' (511)>")
05b462
+        self.assertEqual(repr(c_ushort.from_param(511)), "<cparam 'H' (511)>")
05b462
+        self.assertRegexpMatches(repr(c_int.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
05b462
+        self.assertRegexpMatches(repr(c_uint.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
05b462
+        self.assertRegexpMatches(repr(c_long.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
05b462
+        self.assertRegexpMatches(repr(c_ulong.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
05b462
+        self.assertRegexpMatches(repr(c_longlong.from_param(20000)), r"^<cparam '[liq]' \(20000\)>$")
05b462
+        self.assertRegexpMatches(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$")
05b462
+        self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
05b462
+        self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
05b462
+        self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
05b462
+        self.assertRegexpMatches(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' \(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
05b462
+        self.assertRegexpMatches(repr(c_char_p.from_param(b'hihi')), "^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
05b462
+        self.assertRegexpMatches(repr(c_wchar_p.from_param('hihi')), "^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
05b462
+        self.assertRegexpMatches(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
05b462
+
05b462
 ################################################################
05b462
 
05b462
 if __name__ == '__main__':
05b462
diff --git a/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst b/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
05b462
new file mode 100644
05b462
index 00000000000..7df65a156fe
05b462
--- /dev/null
05b462
+++ b/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
05b462
@@ -0,0 +1,2 @@
05b462
+Avoid static buffers when computing the repr of :class:`ctypes.c_double` and
05b462
+:class:`ctypes.c_longdouble` values.
05b462
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
05b462
index 066fefc0cca..5cc3c4cf685 100644
05b462
--- a/Modules/_ctypes/callproc.c
05b462
+++ b/Modules/_ctypes/callproc.c
05b462
@@ -460,50 +460,62 @@ PyCArg_dealloc(PyCArgObject *self)
05b462
 static PyObject *
05b462
 PyCArg_repr(PyCArgObject *self)
05b462
 {
05b462
-    char buffer[256];
05b462
     switch(self->tag) {
05b462
     case 'b':
05b462
     case 'B':
05b462
-        sprintf(buffer, "<cparam '%c' (%d)>",
05b462
+        return PyString_FromFormat("<cparam '%c' (%d)>",
05b462
             self->tag, self->value.b);
05b462
-        break;
05b462
     case 'h':
05b462
     case 'H':
05b462
-        sprintf(buffer, "<cparam '%c' (%d)>",
05b462
+        return PyString_FromFormat("<cparam '%c' (%d)>",
05b462
             self->tag, self->value.h);
05b462
-        break;
05b462
     case 'i':
05b462
     case 'I':
05b462
-        sprintf(buffer, "<cparam '%c' (%d)>",
05b462
+        return PyString_FromFormat("<cparam '%c' (%d)>",
05b462
             self->tag, self->value.i);
05b462
-        break;
05b462
     case 'l':
05b462
     case 'L':
05b462
-        sprintf(buffer, "<cparam '%c' (%ld)>",
05b462
+        return PyString_FromFormat("<cparam '%c' (%ld)>",
05b462
             self->tag, self->value.l);
05b462
-        break;
05b462
 
05b462
 #ifdef HAVE_LONG_LONG
05b462
     case 'q':
05b462
     case 'Q':
05b462
-        sprintf(buffer,
05b462
-            "<cparam '%c' (%" PY_FORMAT_LONG_LONG "d)>",
05b462
+        return PyString_FromFormat("<cparam '%c' (%lld)>",
05b462
             self->tag, self->value.q);
05b462
-        break;
05b462
 #endif
05b462
     case 'd':
05b462
-        sprintf(buffer, "<cparam '%c' (%f)>",
05b462
-            self->tag, self->value.d);
05b462
-        break;
05b462
-    case 'f':
05b462
-        sprintf(buffer, "<cparam '%c' (%f)>",
05b462
-            self->tag, self->value.f);
05b462
-        break;
05b462
-
05b462
+    case 'f': {
05b462
+        PyObject *s = PyString_FromFormat("<cparam '%c' (", self->tag);
05b462
+        if (s == NULL) {
05b462
+            return NULL;
05b462
+        }
05b462
+        PyObject *f = PyFloat_FromDouble((self->tag == 'f') ? self->value.f : self->value.d);
05b462
+        if (f == NULL) {
05b462
+            Py_DECREF(s);
05b462
+            return NULL;
05b462
+        }
05b462
+        PyObject *r = PyObject_Repr(f);
05b462
+        Py_DECREF(f);
05b462
+        if (r == NULL) {
05b462
+            Py_DECREF(s);
05b462
+            return NULL;
05b462
+        }
05b462
+        PyString_ConcatAndDel(&s, r);
05b462
+        if (s == NULL) {
05b462
+            return NULL;
05b462
+        }
05b462
+        r = PyString_FromString(")>");
05b462
+        if (r == NULL) {
05b462
+            Py_DECREF(s);
05b462
+            return NULL;
05b462
+        }
05b462
+        PyString_ConcatAndDel(&s, r);
05b462
+        return s;
05b462
+    }
05b462
     case 'c':
05b462
-        sprintf(buffer, "<cparam '%c' (%c)>",
05b462
+        return PyString_FromFormat("<cparam '%c' ('%c')>",
05b462
             self->tag, self->value.c);
05b462
-        break;
05b462
 
05b462
 /* Hm, are these 'z' and 'Z' codes useful at all?
05b462
    Shouldn't they be replaced by the functionality of c_string
05b462
@@ -512,16 +524,13 @@ PyCArg_repr(PyCArgObject *self)
05b462
     case 'z':
05b462
     case 'Z':
05b462
     case 'P':
05b462
-        sprintf(buffer, "<cparam '%c' (%p)>",
05b462
+        return PyUnicode_FromFormat("<cparam '%c' (%p)>",
05b462
             self->tag, self->value.p);
05b462
-        break;
05b462
 
05b462
     default:
05b462
-        sprintf(buffer, "<cparam '%c' at %p>",
05b462
-            self->tag, self);
05b462
-        break;
05b462
+        return PyString_FromFormat("<cparam '%c' at %p>",
05b462
+            (unsigned char)self->tag, (void *)self);
05b462
     }
05b462
-    return PyString_FromString(buffer);
05b462
 }
05b462
 
05b462
 static PyMemberDef PyCArgType_members[] = {