Skip to content
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

_sre.template crashes in case of negative or non-integer group index #106524

Closed
chgnrdv opened this issue Jul 7, 2023 · 2 comments · Fixed by #106525
Closed

_sre.template crashes in case of negative or non-integer group index #106524

chgnrdv opened this issue Jul 7, 2023 · 2 comments · Fixed by #106525
Labels
topic-regex type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@chgnrdv
Copy link
Contributor

chgnrdv commented Jul 7, 2023

_sre.template crashes if template argument contains group index that is negative or not an int instance.
Examples:

>>> import _sre
>>> _sre.template("", ["", -1, ""])
Segmentation fault (core dumped)
>>> _sre.template("", ["", (), ""])
Segmentation fault (core dumped)

In _sre_template_impl part of self->items remains uninitialized if call to PyLong_AsSsize_t returns negative value or fails with exception. Then attempt to clear self->items[i].literal in template_clear leads to dereferencing of uninitialized pointer.

Not sure if this worth fixing, since _sre.template is an internal implementation detail that is used only in _compile_template function, where it accepts only (I guess) correct templates created in _parser.parse_template function, and additional checks/initialization can affect its performance. But I'll submit a PR anyway.

Linked PRs

@chgnrdv chgnrdv added the type-crash A hard crash of the interpreter, possibly with a core dump label Jul 7, 2023
chgnrdv added a commit to chgnrdv/cpython that referenced this issue Jul 7, 2023
….template

* made `_sre_template_impl` set items of 'self->items' array to zero before initializing them with group indices and literals
* added test
@terryjreedy
Copy link
Member

In Windows with 3.12.0b3 installed, I just get 'TypeError: invalid template'

@chgnrdv
Copy link
Contributor Author

chgnrdv commented Jul 7, 2023

@terryjreedy, sorry, I should have mentioned that Python needs to be build in debug mode, or with ASAN enabled.

serhiy-storchaka pushed a commit that referenced this issue Jul 8, 2023
Some items remained uninitialized if _sre.template() was called with invalid
indices. Then attempt to clear them in the destructor led to dereferencing
of uninitialized pointer.
serhiy-storchaka pushed a commit that referenced this issue Jul 8, 2023
Some items remained uninitialized if _sre.template() was called with invalid
indices. Then attempt to clear them in the destructor led to dereferencing
of uninitialized pointer.
(cherry picked from commit 2ef1dc3)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-regex type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants