Skip to content

Commit 752fccc

Browse files
White-Reaper0claude
andcommitted
gh-162539: Pin ctypes memory while a value is being stored
Storing into a structure field, an array item or the value of a simple type converts the Python value after the destination address has been computed. The conversion can run Python code (__index__, __float__, __bool__, or calling a structure type for a tuple), and if that code called ctypes.resize() on the object, the store wrote into the freed memory block. Pin the object that owns the memory for the duration of the store in PyCData_set() and in the value setter of simple types, so resize() raises BufferError instead. Writes through a sub-object pin the outer object that owns the memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 12fca6c commit 752fccc

3 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎Lib/test/test_ctypes/test_varsize_struct.py‎

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
2-
from ctypes import Structure, sizeof, resize, c_int
2+
from ctypes import (Structure, sizeof, resize, c_int, c_longlong, c_double,
3+
c_bool, c_uint32, c_char)
34

45

56
class VarSizeTest(unittest.TestCase):
@@ -48,5 +49,109 @@ def test_zerosized_array(self):
4849
self.assertRaises(IndexError, array.__getitem__, -1)
4950

5051

52+
class ResizeDuringStoreTest(unittest.TestCase):
53+
# gh-162539: converting the value being stored can run Python code.
54+
# If that code resized the object, the store wrote into the freed
55+
# memory block. resize() must refuse while a store is running.
56+
57+
BIG = 1 << 20
58+
59+
def resizer(self, target, value):
60+
test = self
61+
class Resizer:
62+
def _resize(self):
63+
with test.assertRaises(BufferError):
64+
resize(target, test.BIG)
65+
def __index__(self):
66+
self._resize()
67+
return value
68+
def __float__(self):
69+
self._resize()
70+
return float(value)
71+
def __bool__(self):
72+
self._resize()
73+
return bool(value)
74+
return Resizer()
75+
76+
def check_released(self, owner):
77+
# The store is over, so the memory can be resized again.
78+
resize(owner, self.BIG)
79+
self.assertEqual(sizeof(owner), self.BIG)
80+
81+
def test_structure_field(self):
82+
class S(Structure):
83+
_fields_ = [("pad", c_char * 4080), ("val", c_longlong)]
84+
s = S()
85+
s.val = self.resizer(s, 42)
86+
self.assertEqual(s.val, 42)
87+
self.check_released(s)
88+
89+
def test_structure_field_float_and_bool(self):
90+
class S(Structure):
91+
_fields_ = [("d", c_double), ("b", c_bool)]
92+
s = S()
93+
s.d = self.resizer(s, 1.5)
94+
s.b = self.resizer(s, 1)
95+
self.assertEqual((s.d, s.b), (1.5, True))
96+
self.check_released(s)
97+
98+
def test_bit_field(self):
99+
class S(Structure):
100+
_fields_ = [("pad", c_uint32), ("val", c_uint32, 5)]
101+
s = S()
102+
s.val = self.resizer(s, 3)
103+
self.assertEqual(s.val, 3)
104+
self.check_released(s)
105+
106+
def test_array_item(self):
107+
a = (c_longlong * 600)()
108+
a[599] = self.resizer(a, 7)
109+
self.assertEqual(a[599], 7)
110+
self.check_released(a)
111+
112+
def test_simple_value(self):
113+
v = c_longlong()
114+
resize(v, 4096)
115+
v.value = self.resizer(v, 9)
116+
self.assertEqual(v.value, 9)
117+
self.check_released(v)
118+
119+
def test_nested_structure_field(self):
120+
# The store goes through a sub-object; the outer object owns the
121+
# memory and is the one that must not be resized.
122+
class P(Structure):
123+
_fields_ = [("x", c_longlong)]
124+
class S(Structure):
125+
_fields_ = [("pad", c_char * 4080), ("p", P)]
126+
s = S()
127+
s.p.x = self.resizer(s, 5)
128+
self.assertEqual(s.p.x, 5)
129+
self.check_released(s)
130+
131+
def test_tuple_to_structure(self):
132+
# A tuple is converted by calling the field's type with it.
133+
class P(Structure):
134+
_fields_ = [("x", c_longlong)]
135+
class S(Structure):
136+
_fields_ = [("pad", c_char * 4080), ("p", P)]
137+
s = S()
138+
s.p = (self.resizer(s, 6),)
139+
self.assertEqual(s.p.x, 6)
140+
self.check_released(s)
141+
142+
def test_failed_conversion_releases(self):
143+
class Bad:
144+
def __index__(self):
145+
raise ZeroDivisionError
146+
v = c_int()
147+
with self.assertRaises(ZeroDivisionError):
148+
v.value = Bad()
149+
a = (c_int * 4)()
150+
with self.assertRaises(ZeroDivisionError):
151+
a[0] = Bad()
152+
self.check_released(v)
153+
self.check_released(a)
154+
155+
51156
if __name__ == "__main__":
52157
unittest.main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a use-after-free in :mod:`ctypes` when converting a value stored into a
2+
structure field, an array item or the ``value`` of a simple type calls
3+
:func:`ctypes.resize` on the object being written to. :func:`!resize` now
4+
raises :exc:`BufferError` while a store into the object is in progress.

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,8 +3641,12 @@ PyCData_set(ctypes_state *st,
36413641
return -1;
36423642
}
36433643

3644+
// gh-162539: converting value can run Python code; keep the memory
3645+
// block (and so ptr) from being moved by ctypes.resize() meanwhile.
3646+
_PyCData_Pin(mem);
36443647
result = _PyCData_set(st, mem, type, setfunc, value,
36453648
size, ptr);
3649+
_PyCData_Unpin(mem);
36463650
if (result == NULL)
36473651
return -1;
36483652

@@ -5540,7 +5544,10 @@ _ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value)
55405544
assert(info); /* Cannot be NULL for CDataObject instances */
55415545
assert(info->setfunc);
55425546

5547+
// gh-162539: see PyCData_set().
5548+
_PyCData_Pin(self);
55435549
result = info->setfunc(self->b_ptr, value, info->size);
5550+
_PyCData_Unpin(self);
55445551
if (!result)
55455552
return -1;
55465553

0 commit comments

Comments
 (0)