Title: ctypes: out-of-bounds read/write in bool() and argument passing after resize() · Issue #160708 · python/cpython · GitHub
Open Graph Title: ctypes: out-of-bounds read/write in bool() and argument passing after resize() · Issue #160708 · python/cpython
X Title: ctypes: out-of-bounds read/write in bool() and argument passing after resize() · Issue #160708 · python/cpython
Description: Crash report After ctypes.resize(), the buffer of a simple ctypes object is larger than the type itself. Two places in _ctypes.c use the buffer size (b_size) instead of the type size and go out of bounds. Found with a fuzzer. Tested on m...
Open Graph Description: Crash report After ctypes.resize(), the buffer of a simple ctypes object is larger than the type itself. Two places in _ctypes.c use the buffer size (b_size) instead of the type size and go out of ...
X Description: Crash report After ctypes.resize(), the buffer of a simple ctypes object is larger than the type itself. Two places in _ctypes.c use the buffer size (b_size) instead of the type size and go out of ...
Opengraph URL: https://github.com/python/cpython/issues/160708
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ctypes: out-of-bounds read/write in bool() and argument passing after resize()","articleBody":"### Crash report\n\nAfter `ctypes.resize()`, the buffer of a simple ctypes object is larger than the type itself. Two places in `_ctypes.c` use the buffer size (`b_size`) instead of the type size and go out of bounds.\n\nFound with a fuzzer. Tested on main (c436e8d8359).\n\n**1. Reading out of bounds in `bool()`**\n\n```python\nimport ctypes\nx = ctypes.c_bool()\nctypes.resize(x, 190)\nbool(x)\n```\n\n```\n==94599==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f54a2fc1b71 at pc 0x563480cae666 bp 0x7ffc5c95adf0 sp 0x7ffc5c95a598\nREAD of size 190 at 0x7f54a2fc1b71 thread T0\n #0 in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long) /usr/src/RPM/BUILD/llvm-project-18/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:814:7\n #1 in memcmp /usr/src/RPM/BUILD/llvm-project-18/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:845:10\n #2 in PyObject_IsTrue Objects/object.c:2163:15\n #3 in bool_vectorcall Objects/boolobject.c:60:14\n #4 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:143:11\n ...\n\n0x7f54a2fc1b71 is located 0 bytes after global variable '.str.245' defined in '/host/triage/cpython/Modules/_ctypes/_ctypes.c:5525' (0x7f54a2fc1b60) of size 17\nSUMMARY: AddressSanitizer: global-buffer-overflow Objects/object.c:2163:15 in PyObject_IsTrue\n```\n\n`Simple_bool_impl` compares the object with a 16-byte string literal (`_ctypes.c:5525`) using `b_size` as the length.\n\nWithout ASAN, a debug build aborts with:\n```\nFatal Python error: _Py_CheckFunctionResult: a function returned NULL without setting an exception\nPython runtime state: initialized\nSystemError: \u003cclass 'bool'> returned NULL without setting an exception\n```\n\n**2. Writing out of bounds when calling a function**\n\n```python\nimport ctypes\nx = ctypes.c_bool()\nctypes.resize(x, 128)\nabs_ = ctypes.CDLL(None).abs\nabs_.argtypes = (ctypes.c_bool,)\nabs_(x)\n```\n\n```\n==94604==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x508000021000 at pc 0x55a5906863d4 bp 0x7fff29dbc9b0 sp 0x7fff29dbc170\nWRITE of size 128 at 0x508000021000 thread T0\n #0 in __asan_memcpy /usr/src/RPM/BUILD/llvm-project-18/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:63:3\n #1 in PyCSimpleType_paramfunc Modules/_ctypes/_ctypes.c:2320:5\n #2 in ConvParam Modules/_ctypes/callproc.c:728:16\n #3 in _ctypes_callproc Modules/_ctypes/callproc.c:1308:19\n #4 in PyCFuncPtr_call Modules/_ctypes/_ctypes.c:4698:14\n ...\n\n0x508000021000 is located 0 bytes after 96-byte region [0x508000020fa0,0x508000021000)\nallocated by thread T0 here:\n #0 in malloc /usr/src/RPM/BUILD/llvm-project-18/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3\n #1 in _PyObject_MallocWithType Include/internal/pycore_object_alloc.h:46:17\n #2 in gc_alloc Python/gc.c:2034:17\n #3 in _PyObject_GC_New Python/gc.c:2054:20\n #4 in PyCArgObject_new Modules/_ctypes/callproc.c:520:9\n\nSUMMARY: AddressSanitizer: heap-buffer-overflow Modules/_ctypes/_ctypes.c:2320:5 in PyCSimpleType_paramfunc\n```\n\n`PyCSimpleType_paramfunc` copies `b_size` bytes into the `value` union of `PyCArgObject`. The union is 32 bytes on x86-64 (its largest member is `long double G[2]`), so the copy goes past the end of the object.\n\nWithout ASAN, a debug build aborts with:\n```\nFatal Python error: _PyMem_DebugRawFree: bad trailing pad byte\nPython runtime state: initialized\n```\n\nI think both places should use `info->size`, as gh-159767 did in `_ctypes_Simple_value_get_impl`. I'm going to open a PR.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a1+dev (heads/main:c436e8d8359, Oct 26 2026, 12:26:29) [Clang 18.1.8 ]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160720\n* gh-161806\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/DmitryTD","@type":"Person","name":"DmitryTD"},"datePublished":"2026-10-26T13:59:51Z","url":"https://github.com/python/cpython/issues/160708"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:78d91dbd-9f45-8445-be25-6b68dec359c3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D212:16AB79:436EBD1:55958C4:6B022F41 |
| html-safe-nonce | 160fb6b909f0422e33aa5e40bd9a79144c6c75631ee01b61c73a22f422fa76f3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjEyOjE2QUI3OTo0MzZFQkQxOjU1OTU4QzQ6NkIwMjJGNDEiLCJ2aXNpdG9yX2lkIjoiMzU1OTg0MDMwNDU2OTkyMTM0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b1c2a74d1455aa4b5bb18ec4b6027bd879ec4bb6422dc0f8e8cf624836f269ec |
| hovercard-subject-tag | issue:6041166287 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/160708/issue_layout |
| twitter:image | https://opengraph.githubassets.com/92e9b971da384798ddf19e6ff542564ad0bb2efb380ba69c8984c914ba130390/python/cpython/issues/160708 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/92e9b971da384798ddf19e6ff542564ad0bb2efb380ba69c8984c914ba130390/python/cpython/issues/160708 |
| og:image:alt | Crash report After ctypes.resize(), the buffer of a simple ctypes object is larger than the type itself. Two places in _ctypes.c use the buffer size (b_size) instead of the type size and go out of ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DmitryTD |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width