| 1 |
From 9381d9fbd0a1bf07c265ca0badc261bf82411bf4 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Yuri Konotopov <[email protected]> |
| 3 |
Date: Fri, 28 Aug 2026 07:50:37 +0000 |
| 4 |
Subject: [PATCH 2/5] Fixed buffer sizes handling on 32 bit platforms |
| 5 |
|
| 6 |
Signed-off-by: Yuri Konotopov <[email protected]> |
| 7 |
|
| 8 |
Forwarded: https://github.com/mosquito/caio/pull/76 |
| 9 |
--- |
| 10 |
caio/linux_aio.c | 15 +++++++++++++-- |
| 11 |
caio/linux_uring.c | 9 +++++++++ |
| 12 |
caio/thread_aio.c | 15 ++++++++++++--- |
| 13 |
3 files changed, 34 insertions(+), 5 deletions(-) |
| 14 |
|
| 15 |
diff --git a/caio/linux_aio.c b/caio/linux_aio.c |
| 16 |
index f610d83..a519b88 100644 |
| 17 |
--- a/caio/linux_aio.c |
| 18 |
+++ b/caio/linux_aio.c |
| 19 |
@@ -804,12 +804,21 @@ static PyObject* AIOOperation_read( |
| 20 |
return NULL; |
| 21 |
} |
| 22 |
|
| 23 |
+ if (nbytes > (uint64_t) PY_SSIZE_T_MAX) { |
| 24 |
+ Py_DECREF(self); |
| 25 |
+ PyErr_SetString( |
| 26 |
+ PyExc_OverflowError, |
| 27 |
+ "nbytes does not fit in Py_ssize_t" |
| 28 |
+ ); |
| 29 |
+ return NULL; |
| 30 |
+ } |
| 31 |
+ |
| 32 |
/* PyMem_Calloc can return NULL for a large enough (or just OOM-at-the- |
| 33 |
* time) nbytes - proceeding with a NULL buf would hand the kernel (via |
| 34 |
* aio_buf) and PyMemoryView_FromMemory a NULL pointer with a nonzero |
| 35 |
* declared size, corrupting memory instead of raising a catchable |
| 36 |
* error. */ |
| 37 |
- self->buffer = PyMem_Calloc(nbytes, sizeof(char)); |
| 38 |
+ self->buffer = PyMem_Calloc((size_t) nbytes, sizeof(char)); |
| 39 |
if (self->buffer == NULL && nbytes > 0) { |
| 40 |
Py_DECREF(self); |
| 41 |
PyErr_NoMemory(); |
| 42 |
@@ -817,7 +826,9 @@ static PyObject* AIOOperation_read( |
| 43 |
} |
| 44 |
self->iocb.aio_buf = (uint64_t)(uintptr_t) self->buffer; |
| 45 |
self->iocb.aio_nbytes = nbytes; |
| 46 |
- self->py_buffer = PyMemoryView_FromMemory(self->buffer, nbytes, PyBUF_READ); |
| 47 |
+ self->py_buffer = PyMemoryView_FromMemory( |
| 48 |
+ self->buffer, (Py_ssize_t) nbytes, PyBUF_READ |
| 49 |
+ ); |
| 50 |
if (self->py_buffer == NULL) { |
| 51 |
Py_DECREF(self); |
| 52 |
return NULL; |
| 53 |
diff --git a/caio/linux_uring.c b/caio/linux_uring.c |
| 54 |
index 876a951..abd5200 100644 |
| 55 |
--- a/caio/linux_uring.c |
| 56 |
+++ b/caio/linux_uring.c |
| 57 |
@@ -219,6 +219,15 @@ static PyObject *AIOOperation_read( |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
+ if (nbytes > UINT32_MAX || nbytes > (uint64_t) PY_SSIZE_T_MAX) { |
| 62 |
+ Py_DECREF(self); |
| 63 |
+ PyErr_SetString( |
| 64 |
+ PyExc_OverflowError, |
| 65 |
+ "nbytes exceeds the io_uring read limit" |
| 66 |
+ ); |
| 67 |
+ return NULL; |
| 68 |
+ } |
| 69 |
+ |
| 70 |
/* Allocate the result bytes object directly — the kernel writes into |
| 71 |
* its internal buffer, so get_value() can return it with no copy. |
| 72 |
* PyBytes_FromStringAndSize(NULL, n) leaves the memory uninitialized |
| 73 |
diff --git a/caio/thread_aio.c b/caio/thread_aio.c |
| 74 |
index 3666c96..b8d96d8 100644 |
| 75 |
--- a/caio/thread_aio.c |
| 76 |
+++ b/caio/thread_aio.c |
| 77 |
@@ -637,18 +637,27 @@ static PyObject* AIOOperation_read( |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
+ if (nbytes > (uint64_t) PY_SSIZE_T_MAX) { |
| 82 |
+ Py_DECREF(self); |
| 83 |
+ PyErr_SetString( |
| 84 |
+ PyExc_OverflowError, |
| 85 |
+ "nbytes does not fit in Py_ssize_t" |
| 86 |
+ ); |
| 87 |
+ return NULL; |
| 88 |
+ } |
| 89 |
+ |
| 90 |
// PyMem_Calloc can return NULL for a large enough (or just |
| 91 |
// OOM-at-the-time) nbytes - proceeding with a NULL buf would hand the |
| 92 |
// kernel (via pread() in worker()) and PyMemoryView_FromMemory a NULL |
| 93 |
// pointer with a nonzero declared size, corrupting memory instead of |
| 94 |
// raising a catchable error. |
| 95 |
- self->buf = PyMem_Calloc(nbytes, sizeof(char)); |
| 96 |
+ self->buf = PyMem_Calloc((size_t) nbytes, sizeof(char)); |
| 97 |
if (self->buf == NULL && nbytes > 0) { |
| 98 |
Py_DECREF(self); |
| 99 |
PyErr_NoMemory(); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
- self->buf_size = nbytes; |
| 103 |
+ self->buf_size = (Py_ssize_t) nbytes; |
| 104 |
|
| 105 |
self->py_buffer = PyMemoryView_FromMemory( |
| 106 |
self->buf, |
| 107 |
@@ -993,7 +1002,7 @@ static PyMemberDef AIOOperation_members[] = { |
| 108 |
READONLY, "offset" |
| 109 |
}, |
| 110 |
{ |
| 111 |
- "nbytes", T_ULONGLONG, |
| 112 |
+ "nbytes", T_PYSSIZET, |
| 113 |
offsetof(AIOOperation, buf_size), |
| 114 |
READONLY, "nbytes" |
| 115 |
}, |
| 116 |
-- |
| 117 |
2.55.0 |
| 118 |
|