Skip to content

gh-144128: Fix crash in array.fromlist with reentrant __index__#144138

Merged
vstinner merged 10 commits intopython:mainfrom
priyanshu2282-cyber:fix-array-fromlist-uaf
Jan 26, 2026
Merged

gh-144128: Fix crash in array.fromlist with reentrant __index__#144138
vstinner merged 10 commits intopython:mainfrom
priyanshu2282-cyber:fix-array-fromlist-uaf

Conversation

@priyanshu2282-cyber
Copy link
Copy Markdown
Contributor

@priyanshu2282-cyber priyanshu2282-cyber commented Jan 22, 2026

This change fixes a crash in array.fromlist() that can happen if an element’s __index__ method mutates the input list while it is being processed.
Previously, array.fromlist() assumed the list would remain unchanged during conversion. If __index__ cleared the list, the element being converted could be freed while still in use, leading to a crash. The implementation now keeps the element alive for the duration of the conversion, I have added a regression test to cover this case.

@priyanshu2282-cyber
Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka I have proposed a fix to make array.fromlist() safe against reentrant __index__ calls that mutate the input list, and I have added a regresion test. Please let me know if this approach is appropriate when you have time. Thankyou!

Comment thread Misc/NEWS.d/next/Security/2026-01-22-10-18-17.gh-issue-144128.akwY06.rst Outdated
Comment thread Modules/arraymodule.c Outdated
if (!PyLong_Check(v)) {
PyObject *orig_v = v;
Py_INCREF(orig_v);
v = _PyNumber_Index(v);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to also pass orig_v here. It's technically unnecessary, but it makes the code easier to follow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, thanks for pointing it out, from next time I will try to take care of all these stuffs.

Comment thread Lib/test/test_array.py Outdated
Comment thread Lib/test/test_array.py Outdated
Comment thread Lib/test/test_array.py
Comment thread Modules/arraymodule.c Outdated
Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I just left minor coding style suggestions.

Comment thread Modules/arraymodule.c Outdated
Comment thread Modules/arraymodule.c Outdated
Comment thread Modules/arraymodule.c Outdated
priyanshu2282-cyber and others added 3 commits January 23, 2026 22:46
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
@priyanshu2282-cyber
Copy link
Copy Markdown
Contributor Author

Thanks @vstinner for constant suggestions and changes, your feedback is much appriciated and very helpful for me, I will stick to the format and try not to repeat same mistakes in future.

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vstinner vstinner merged commit decb25e into python:main Jan 26, 2026
51 checks passed
@serhiy-storchaka
Copy link
Copy Markdown
Member

I am not sure this is the right way to fix this issue. Instead of changing some setitems (what about others?), would not it be better to handle this in array.fromarray() and other methods that pass borrowed references to setters?

There was also other similar array issue.

@vstinner
Copy link
Copy Markdown
Member

Instead of changing some setitems (what about others?)

I checked the other setters and they don't seem to be vulnerable to the borrowed reference issue. They cannot call arbitrary Python code while using the borrowed reference.

would not it be better to handle this in array.fromarray() and other methods that pass borrowed references to setters?

I'm not sure that it would better, it would be basically the same, but requires to modify more code, no?

There was also other similar array issue.

Would you mind to elaborate?

thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
…python#144138)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants