Skip to content

ctypes.Structure: bitfield of underaligned type can cause read of unrelated memory #130410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
encukou opened this issue Feb 21, 2025 · 0 comments
Labels
extension-modules C modules in the Modules dir topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@encukou
Copy link
Member

encukou commented Feb 21, 2025

Bug report

Bug description:

(This is an exotic edge case found by tests that are perhaps too stringent, but, we prefer issues over XXX comments in the code, so, here goes.)

A bitfield of an “underaligned” type (one whose alignment is smaller than its size) can cause the “storage unit” that ctypes uses for handling the bitfield to extend past the containing Structure.

For example, on 32-bit x86 architecture, where int64_t is 8 bytes long but only aligned to 4 bytes, we have:

>>> import ctypes
>>> class S(ctypes.Structure):
...     _fields_ = [('f', ctypes.c_int64, 1)]
>>> ctypes.sizeof(S)
4
>>> ctypes.sizeof(ctypes.c_int64)
8

This matches GCC struct layout:

#include <stdio.h>
#include <stdint.h>

struct S {
    int64_t f: 1;
};

int main() {
    printf("%zd\n", sizeof(struct S));  // -> 4
}

ctypes handles bitfield reads/writes by reading the entire storage unit, masking/shifting, and (for writes) writing the entire unit back. So, in this case it can read/write memory that doesn't belong to the struct.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

@encukou encukou added topic-ctypes type-bug An unexpected behavior, bug, or error labels Feb 21, 2025
@picnixz picnixz added the extension-modules C modules in the Modules dir label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir topic-ctypes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants