Skip to content

sys.audit(0) aborts due to an assertion in debug build #126018

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

Closed
devdanzin opened this issue Oct 26, 2024 · 3 comments
Closed

sys.audit(0) aborts due to an assertion in debug build #126018

devdanzin opened this issue Oct 26, 2024 · 3 comments
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@devdanzin
Copy link
Contributor

devdanzin commented Oct 26, 2024

Crash report

What happened?

There is an assert in sysmodule.c that makes the interpreter abort when calling sys.audit with a non-string value in debug builds:

assert(PyUnicode_Check(args[0]));

Python 3.14.0a1+ (heads/main:c5b99f5c2c, Oct 26 2024, 12:35:53) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.audit(9)
python: ./Python/sysmodule.c:522: sys_audit: Assertion `PyUnicode_Check(args[0])' failed.
Aborted (core dumped)

This assert seems completely unnecessary, as the code that runs the hooks (which only triggers if there is some hook registered) will raise an exception if the first argument to sys.audit isn't a string:

Python 3.14.0a1+ (heads/remove_sysaudit_assert:ba8ac4d398, Oct 26 2024, 19:45:11) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.addaudithook(lambda *args: None)
>>> sys.audit(9)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    sys.audit(9)
    ~~~~~~~~~^^^
TypeError: expected str for argument 'event', not int

I'll submit a trivial PR removing this assert.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux, Windows

Output from running 'python -VV' on the command line:

Python 3.14.0a1+ (heads/main:c5b99f5c2c, Oct 26 2024, 12:35:53) [GCC 13.2.0]

Linked PRs

@devdanzin devdanzin added the type-crash A hard crash of the interpreter, possibly with a core dump label Oct 26, 2024
@Eclips4 Eclips4 added interpreter-core (Objects, Python, Grammar, and Parser dirs) extension-modules C modules in the Modules dir labels Oct 26, 2024
@picnixz picnixz added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes labels Oct 26, 2024
@picnixz
Copy link
Member

picnixz commented Oct 26, 2024

Ok, 3.12 is actually not affected:

cpython/Python/sysmodule.c

Lines 499 to 514 in 67b2701

if (!should_audit(tstate->interp)) {
Py_RETURN_NONE;
}
PyObject *auditEvent = args[0];
if (!auditEvent) {
_PyErr_SetString(tstate, PyExc_TypeError,
"expected str for argument 'event'");
return NULL;
}
if (!PyUnicode_Check(auditEvent)) {
_PyErr_Format(tstate, PyExc_TypeError,
"expected str for argument 'event', not %.200s",
Py_TYPE(auditEvent)->tp_name);
return NULL;
}

@picnixz picnixz removed the 3.12 only security fixes label Oct 26, 2024
@devdanzin
Copy link
Contributor Author

Ok, 3.12 is actually not affected:

Hmm. The assert was deliberately added in #108965. @vstinner, what do you think about the proposal to remove the eager assert for non-string argument to sys.audit?

JelleZijlstra added a commit that referenced this issue Oct 27, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 27, 2024
…it` (pythonGH-126020)

(cherry picked from commit 80eec52)

Co-authored-by: devdanzin <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
JelleZijlstra added a commit that referenced this issue Oct 27, 2024
…dit` (GH-126020) (#126042)



(cherry picked from commit 80eec52)

Co-authored-by: devdanzin <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
@picnixz
Copy link
Member

picnixz commented Oct 27, 2024

Closing since completed and backported. Thanks!

@picnixz picnixz closed this as completed Oct 27, 2024
picnixz added a commit to picnixz/cpython that referenced this issue Dec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this issue Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

3 participants