Skip to content

Commit 29d3d2c

Browse files
authored
gh-161694: Check for integer overflow in _PyObject_VAR_SIZE() (#161911)
Check also for integer overflow in _PyType_AllocNoTrack(). Remove now redundant (and incomplete) integer overflow from tuple_alloc() and _PyTuple_Resize(). In debug mode, check also that nitems >= 0 in _PyType_AllocNoTrack() and _PyObject_GC_Resize().
1 parent b4e8f2a commit 29d3d2c

7 files changed

Lines changed: 73 additions & 28 deletions

File tree

‎Include/cpython/objimpl.h‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,40 @@ static inline size_t _PyObject_SIZE(PyTypeObject *type) {
1515
1616
Note that there's no memory wastage in doing this, as malloc has to
1717
return (at worst) pointer-aligned memory anyway.
18+
19+
Return SIZE_MAX on integer overflow (if the result would be greater than
20+
PY_SSIZE_T_MAX).
1821
*/
1922
#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
2023
# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
2124
#endif
2225

23-
static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) {
24-
size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize);
25-
size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize);
26-
return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);
26+
static inline size_t
27+
_PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed)
28+
{
29+
size_t basicsize;
30+
size_t size;
31+
32+
assert(type->tp_itemsize >= 0);
33+
basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize);
34+
35+
if (type->tp_itemsize != 0) {
36+
size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed);
37+
size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize);
38+
if (nitems > ((size_t)PY_SSIZE_T_MAX - basicsize) / itemsize) {
39+
return SIZE_MAX;
40+
}
41+
size = basicsize + nitems * itemsize;
42+
}
43+
else {
44+
size = basicsize;
45+
}
46+
47+
size = _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);
48+
if (size > (size_t)PY_SSIZE_T_MAX) {
49+
return SIZE_MAX;
50+
}
51+
return size;
2752
}
2853

2954

‎Include/objimpl.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
160160
#define PyObject_GC_Resize(type, op, n) \
161161
( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
162162

163-
164-
165163
PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
166164
PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
167165

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:c:func:`!_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if
2+
the result would be greater than ``PY_SSIZE_T_MAX``). Patch by Victor Stinner.

‎Objects/tupleobject.c‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ tuple_alloc(Py_ssize_t size)
5050
return op;
5151
}
5252
}
53-
/* Check for overflow */
54-
if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
55-
sizeof(PyObject *))) / sizeof(PyObject *)) {
56-
return (PyTupleObject *)PyErr_NoMemory();
57-
}
5853
PyTupleObject *result = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
5954
if (result != NULL) {
6055
_PyTuple_RESET_HASH_CACHE(result);
@@ -1079,14 +1074,6 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
10791074
*pv = PyTuple_New(newsize);
10801075
return *pv == NULL ? -1 : 0;
10811076
}
1082-
/* Check for overflow */
1083-
if ((size_t)newsize > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
1084-
sizeof(PyObject *))) / sizeof(PyObject *)) {
1085-
*pv = NULL;
1086-
Py_DECREF(v);
1087-
PyErr_NoMemory();
1088-
return -1;
1089-
}
10901077

10911078
if (_PyObject_GC_IS_TRACKED(v)) {
10921079
_PyObject_GC_UNTRACK(v);
@@ -1315,12 +1302,13 @@ maybe_freelist_push(PyTupleObject *op)
13151302
void
13161303
_PyTuple_DebugMallocStats(FILE *out)
13171304
{
1305+
PyTypeObject *type = &PyTuple_Type;
13181306
for (int i = 0; i < PyTuple_MAXSAVESIZE; i++) {
13191307
int len = i + 1;
13201308
char buf[128];
13211309
PyOS_snprintf(buf, sizeof(buf),
13221310
"free %d-sized PyTupleObject", len);
1323-
_PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]),
1324-
_PyType_PreHeaderSize(&PyTuple_Type) + _PyObject_VAR_SIZE(&PyTuple_Type, len));
1311+
size_t size = _PyType_PreHeaderSize(type) + _PyObject_VAR_SIZE(type, len);
1312+
_PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]), size);
13251313
}
13261314
}

‎Objects/typeobject.c‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,25 +2537,43 @@ _PyType_NewManagedObject(PyTypeObject *type)
25372537
PyObject *
25382538
_PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
25392539
{
2540-
PyObject *obj;
2540+
#ifdef Py_DEBUG
2541+
if (nitems < 0) {
2542+
PyErr_BadInternalCall();
2543+
return NULL;
2544+
}
2545+
#endif
2546+
25412547
/* The +1 on nitems is needed for most types but not all. We could save a
25422548
* bit of space by allocating one less item in certain cases, depending on
25432549
* the type. However, given the extra complexity (e.g. an additional type
25442550
* flag to indicate when that is safe) it does not seem worth the memory
25452551
* savings. An example type that doesn't need the +1 is a subclass of
25462552
* tuple. See GH-100659 and GH-81381. */
2553+
if (nitems > PY_SSIZE_T_MAX - 1) {
2554+
return PyErr_NoMemory();
2555+
}
25472556
size_t size = _PyObject_VAR_SIZE(type, nitems+1);
25482557

2549-
const size_t presize = _PyType_PreHeaderSize(type);
25502558
if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
25512559
assert(type->tp_itemsize == 0);
2552-
size += _PyInlineValuesSize(type);
2560+
size_t inline_values_size = _PyInlineValuesSize(type);
2561+
if (size > (size_t)PY_SSIZE_T_MAX - inline_values_size) {
2562+
return PyErr_NoMemory();
2563+
}
2564+
size += inline_values_size;
2565+
}
2566+
2567+
const size_t presize = _PyType_PreHeaderSize(type);
2568+
if (size > (size_t)PY_SSIZE_T_MAX - presize) {
2569+
return PyErr_NoMemory();
25532570
}
2571+
25542572
char *alloc = _PyObject_MallocWithType(type, size + presize);
2555-
if (alloc == NULL) {
2573+
if (alloc == NULL) {
25562574
return PyErr_NoMemory();
25572575
}
2558-
obj = (PyObject *)(alloc + presize);
2576+
PyObject *obj = (PyObject *)(alloc + presize);
25592577
if (presize) {
25602578
((PyObject **)alloc)[0] = NULL;
25612579
((PyObject **)alloc)[1] = NULL;

‎Python/gc.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,7 @@ static PyObject *
20672067
gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize)
20682068
{
20692069
PyThreadState *tstate = _PyThreadState_GET();
2070-
if (basicsize > PY_SSIZE_T_MAX - presize) {
2070+
if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
20712071
return _PyErr_NoMemory(tstate);
20722072
}
20732073
size_t size = presize + basicsize;
@@ -2138,6 +2138,13 @@ PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
21382138
PyVarObject *
21392139
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
21402140
{
2141+
#ifdef Py_DEBUG
2142+
if (nitems < 0) {
2143+
PyErr_BadInternalCall();
2144+
return NULL;
2145+
}
2146+
#endif
2147+
21412148
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
21422149
const size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
21432150
_PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));

‎Python/gc_free_threading.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,7 @@ static PyObject *
27752775
gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize)
27762776
{
27772777
PyThreadState *tstate = _PyThreadState_GET();
2778-
if (basicsize > PY_SSIZE_T_MAX - presize) {
2778+
if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
27792779
return _PyErr_NoMemory(tstate);
27802780
}
27812781
size_t size = presize + basicsize;
@@ -2847,6 +2847,13 @@ PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
28472847
PyVarObject *
28482848
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
28492849
{
2850+
#ifdef Py_DEBUG
2851+
if (nitems < 0) {
2852+
PyErr_BadInternalCall();
2853+
return NULL;
2854+
}
2855+
#endif
2856+
28502857
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
28512858
const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
28522859
_PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));

0 commit comments

Comments
 (0)