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

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