Skip to content

GH-115322: fix ctypes call_function audit hook on 32-bit platforms #132496

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ def test_ctypes_call_function():

with TestHook() as hook:
_ctypes.call_function(ctypes._memmove_addr, (0, 0, 0))
assert ("ctypes.call_function", (ctypes._memmove_addr, (0, 0, 0))) in hook.seen
assert ("ctypes.call_function", (ctypes._memmove_addr, (0, 0, 0))) in hook.seen, f"{ctypes._memmove_addr=} {hook.seen=}"

ctypes.CFUNCTYPE(ctypes.c_voidp)(ctypes._memset_addr)(1, 0, 0)
assert ("ctypes.call_function", (ctypes._memset_addr, (1, 0, 0))) in hook.seen
assert ("ctypes.call_function", (ctypes._memset_addr, (1, 0, 0))) in hook.seen, f"{ctypes._memset_addr=} {hook.seen=}"

with TestHook() as hook:
ctypes.cast(ctypes.c_voidp(0), ctypes.POINTER(ctypes.c_char))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
The underlying extension modules behind :mod:`readline`:, :mod:`subprocess`,
and :mod:`ctypes` now raise audit events on previously uncovered code paths
that could lead to file system access related to C function calling and
external binary execution.
external binary execution. The ``ctypes.call_function`` audit hook has also
been fixed to use an unsigned value for its ``function pointer``.
9 changes: 9 additions & 0 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,17 @@ PyObject *_ctypes_callproc(ctypes_state *st,
PyObject *retval = NULL;

// Both call_function and call_cdeclfunction call us:
#if SIZEOF_VOID_P == SIZEOF_LONG
if (PySys_Audit("ctypes.call_function", "kO",
(unsigned long)pProc, argtuple) < 0) {
#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
if (PySys_Audit("ctypes.call_function", "KO",
(unsigned long long)pProc, argtuple) < 0) {
#else
# warning "unexpected pointer size, you may see odd values in audit hooks"
if (PySys_Audit("ctypes.call_function", "nO",
(Py_ssize_t)pProc, argtuple) < 0) {
#endif
return NULL;
}

Expand Down
Loading