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

b3527f
From e92381a0a6a3e1f000956e1f1e70e543b9c2bcd5 Mon Sep 17 00:00:00 2001
b3527f
From: Benjamin Peterson <benjamin@python.org>
b3527f
Date: Mon, 18 Jan 2021 14:47:05 -0600
b3527f
Subject: [PATCH] [3.6] closes bpo-42938: Replace snprintf with Python unicode
b3527f
 formatting in ctypes param reprs. (24239). (cherry picked from commit
b3527f
 916610ef90a0d0761f08747f7b0905541f0977c7)
b3527f
b3527f
Co-authored-by: Benjamin Peterson <benjamin@python.org>
b3527f
---
b3527f
 Lib/ctypes/test/test_parameters.py            | 43 +++++++++++++++
b3527f
 .../2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst  |  2 +
b3527f
 Modules/_ctypes/callproc.c                    | 55 +++++++------------
b3527f
 3 files changed, 66 insertions(+), 34 deletions(-)
b3527f
 create mode 100644 Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
b3527f
b3527f
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
b3527f
index e4c25fd880cef..531894fdec838 100644
b3527f
--- a/Lib/ctypes/test/test_parameters.py
b3527f
+++ b/Lib/ctypes/test/test_parameters.py
b3527f
@@ -201,6 +201,49 @@ def __dict__(self):
b3527f
         with self.assertRaises(ZeroDivisionError):
b3527f
             WorseStruct().__setstate__({}, b'foo')
b3527f
 
b3527f
+    def test_parameter_repr(self):
b3527f
+        from ctypes import (
b3527f
+            c_bool,
b3527f
+            c_char,
b3527f
+            c_wchar,
b3527f
+            c_byte,
b3527f
+            c_ubyte,
b3527f
+            c_short,
b3527f
+            c_ushort,
b3527f
+            c_int,
b3527f
+            c_uint,
b3527f
+            c_long,
b3527f
+            c_ulong,
b3527f
+            c_longlong,
b3527f
+            c_ulonglong,
b3527f
+            c_float,
b3527f
+            c_double,
b3527f
+            c_longdouble,
b3527f
+            c_char_p,
b3527f
+            c_wchar_p,
b3527f
+            c_void_p,
b3527f
+        )
b3527f
+        self.assertRegex(repr(c_bool.from_param(True)), r"^<cparam '\?' at 0x[A-Fa-f0-9]+>$")
b3527f
+        self.assertEqual(repr(c_char.from_param(97)), "<cparam 'c' ('a')>")
b3527f
+        self.assertRegex(repr(c_wchar.from_param('a')), r"^<cparam 'u' at 0x[A-Fa-f0-9]+>$")
b3527f
+        self.assertEqual(repr(c_byte.from_param(98)), "<cparam 'b' (98)>")
b3527f
+        self.assertEqual(repr(c_ubyte.from_param(98)), "<cparam 'B' (98)>")
b3527f
+        self.assertEqual(repr(c_short.from_param(511)), "<cparam 'h' (511)>")
b3527f
+        self.assertEqual(repr(c_ushort.from_param(511)), "<cparam 'H' (511)>")
b3527f
+        self.assertRegex(repr(c_int.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
b3527f
+        self.assertRegex(repr(c_uint.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
b3527f
+        self.assertRegex(repr(c_long.from_param(20000)), r"^<cparam '[li]' \(20000\)>$")
b3527f
+        self.assertRegex(repr(c_ulong.from_param(20000)), r"^<cparam '[LI]' \(20000\)>$")
b3527f
+        self.assertRegex(repr(c_longlong.from_param(20000)), r"^<cparam '[liq]' \(20000\)>$")
b3527f
+        self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$")
b3527f
+        self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
b3527f
+        self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
b3527f
+        self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
b3527f
+        self.assertRegex(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' \(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
b3527f
+        self.assertRegex(repr(c_char_p.from_param(b'hihi')), "^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
b3527f
+        self.assertRegex(repr(c_wchar_p.from_param('hihi')), "^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
b3527f
+        self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
b3527f
+
b3527f
 ################################################################
b3527f
 
b3527f
 if __name__ == '__main__':
b3527f
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
b3527f
new file mode 100644
b3527f
index 0000000000000..7df65a156feab
b3527f
--- /dev/null
b3527f
+++ b/Misc/NEWS.d/next/Security/2021-01-18-09-27-31.bpo-42938.4Zn4Mp.rst
b3527f
@@ -0,0 +1,2 @@
b3527f
+Avoid static buffers when computing the repr of :class:`ctypes.c_double` and
b3527f
+:class:`ctypes.c_longdouble` values.
b3527f
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
b3527f
index d1c190f359108..2bb289bce043f 100644
b3527f
--- a/Modules/_ctypes/callproc.c
b3527f
+++ b/Modules/_ctypes/callproc.c
b3527f
@@ -461,58 +461,47 @@ is_literal_char(unsigned char c)
b3527f
 static PyObject *
b3527f
 PyCArg_repr(PyCArgObject *self)
b3527f
 {
b3527f
-    char buffer[256];
b3527f
     switch(self->tag) {
b3527f
     case 'b':
b3527f
     case 'B':
b3527f
-        sprintf(buffer, "<cparam '%c' (%d)>",
b3527f
+        return PyUnicode_FromFormat("<cparam '%c' (%d)>",
b3527f
             self->tag, self->value.b);
b3527f
-        break;
b3527f
     case 'h':
b3527f
     case 'H':
b3527f
-        sprintf(buffer, "<cparam '%c' (%d)>",
b3527f
+        return PyUnicode_FromFormat("<cparam '%c' (%d)>",
b3527f
             self->tag, self->value.h);
b3527f
-        break;
b3527f
     case 'i':
b3527f
     case 'I':
b3527f
-        sprintf(buffer, "<cparam '%c' (%d)>",
b3527f
+        return PyUnicode_FromFormat("<cparam '%c' (%d)>",
b3527f
             self->tag, self->value.i);
b3527f
-        break;
b3527f
     case 'l':
b3527f
     case 'L':
b3527f
-        sprintf(buffer, "<cparam '%c' (%ld)>",
b3527f
+        return PyUnicode_FromFormat("<cparam '%c' (%ld)>",
b3527f
             self->tag, self->value.l);
b3527f
-        break;
b3527f
 
b3527f
     case 'q':
b3527f
     case 'Q':
b3527f
-        sprintf(buffer,
b3527f
-#ifdef MS_WIN32
b3527f
-            "<cparam '%c' (%I64d)>",
b3527f
-#else
b3527f
-            "<cparam '%c' (%lld)>",
b3527f
-#endif
b3527f
+        return PyUnicode_FromFormat("<cparam '%c' (%lld)>",
b3527f
             self->tag, self->value.q);
b3527f
-        break;
b3527f
     case 'd':
b3527f
-        sprintf(buffer, "<cparam '%c' (%f)>",
b3527f
-            self->tag, self->value.d);
b3527f
-        break;
b3527f
-    case 'f':
b3527f
-        sprintf(buffer, "<cparam '%c' (%f)>",
b3527f
-            self->tag, self->value.f);
b3527f
-        break;
b3527f
-
b3527f
+    case 'f': {
b3527f
+        PyObject *f = PyFloat_FromDouble((self->tag == 'f') ? self->value.f : self->value.d);
b3527f
+        if (f == NULL) {
b3527f
+            return NULL;
b3527f
+        }
b3527f
+        PyObject *result = PyUnicode_FromFormat("<cparam '%c' (%R)>", self->tag, f);
b3527f
+        Py_DECREF(f);
b3527f
+        return result;
b3527f
+    }
b3527f
     case 'c':
b3527f
         if (is_literal_char((unsigned char)self->value.c)) {
b3527f
-            sprintf(buffer, "<cparam '%c' ('%c')>",
b3527f
+            return PyUnicode_FromFormat("<cparam '%c' ('%c')>",
b3527f
                 self->tag, self->value.c);
b3527f
         }
b3527f
         else {
b3527f
-            sprintf(buffer, "<cparam '%c' ('\\x%02x')>",
b3527f
+            return PyUnicode_FromFormat("<cparam '%c' ('\\x%02x')>",
b3527f
                 self->tag, (unsigned char)self->value.c);
b3527f
         }
b3527f
-        break;
b3527f
 
b3527f
 /* Hm, are these 'z' and 'Z' codes useful at all?
b3527f
    Shouldn't they be replaced by the functionality of c_string
b3527f
@@ -521,22 +510,20 @@ PyCArg_repr(PyCArgObject *self)
b3527f
     case 'z':
b3527f
     case 'Z':
b3527f
     case 'P':
b3527f
-        sprintf(buffer, "<cparam '%c' (%p)>",
b3527f
+        return PyUnicode_FromFormat("<cparam '%c' (%p)>",
b3527f
             self->tag, self->value.p);
b3527f
         break;
b3527f
 
b3527f
     default:
b3527f
         if (is_literal_char((unsigned char)self->tag)) {
b3527f
-            sprintf(buffer, "<cparam '%c' at %p>",
b3527f
-                (unsigned char)self->tag, self);
b3527f
+            return PyUnicode_FromFormat("<cparam '%c' at %p>",
b3527f
+                (unsigned char)self->tag, (void *)self);
b3527f
         }
b3527f
         else {
b3527f
-            sprintf(buffer, "<cparam 0x%02x at %p>",
b3527f
-                (unsigned char)self->tag, self);
b3527f
+            return PyUnicode_FromFormat("<cparam 0x%02x at %p>",
b3527f
+                (unsigned char)self->tag, (void *)self);
b3527f
         }
b3527f
-        break;
b3527f
     }
b3527f
-    return PyUnicode_FromString(buffer);
b3527f
 }
b3527f
 
b3527f
 static PyMemberDef PyCArgType_members[] = {