Skip to content

gh-127681: Fix overflow vulnerabilities in PyMem_Malloc calls - #153489

Closed
pranavchoudhary-tech wants to merge 3 commits into
python:mainfrom
pranavchoudhary-tech:fix/overflows-127681
Closed

pranavchoudhary-tech wants to merge 3 commits into
python:mainfrom
pranavchoudhary-tech:fix/overflows-127681

Conversation

@pranavchoudhary-tech

@pranavchoudhary-tech pranavchoudhary-tech commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #127681 by systematically replacing PyMem_Malloc calls involving arithmetic with PyMem_New or adding explicit PY_SSIZE_T_MAX checks, as well as adding safety notes to appropriately constrained variables.

@pranavchoudhary-tech pranavchoudhary-tech changed the title Fix overflow vulnerabilities in PyMem_Malloc calls gh-127681: Fix overflow vulnerabilities in PyMem_Malloc calls Jul 10, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 12, 2026
@pranavchoudhary-tech

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest main branch and resolved the merge conflict in Objects/capsule.c. All tests are passing.

Comment thread Modules/_ctypes/stgdict.c

oom:
if (src_info->format) {
PyMem_Free(dst_info->format);

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.

There is a risk of a double free in the destructor. If you free it here, please set the variable to NULL.

Comment thread Modules/posixmodule.c
Comment on lines +7300 to +7304
if (size == PY_SSIZE_T_MAX) {
PyErr_NoMemory();
Py_DECREF(ub);
return 0;
}

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.

Since size is obtained from PyUnicode_AsWideCharString(), adding 1 cannot cause an overflow.

Comment thread Python/instrumentation.c
else {
bytes_per_entry = 5;
}
if (code_len > (PY_SSIZE_T_MAX - 1) / bytes_per_entry) {

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.

code_len and bytes_per_entry are int, not Py_ssize_t.

@gpshead gpshead left a comment

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.

overall this PR feels like an attempt to redo #127686 which was also incomplete and rejected (including suspiciously similar placements of TODO comments...). There are enough flaws in here I don't think this is worth working on. Instead of one large PR trying to cover issues of this form, smaller targeted ones aiming to make clear defensible improvements in individual files would be better.

@@ -0,0 +1 @@
Fix overflow vulnerabilities in PyMem_Malloc calls by systematically replacing them with PyMem_New or adding explicit PY_SSIZE_T_MAX checks.

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.

these are not vulnerabilities. just bugs. "Fix potentially overflowing size computations on internal uses of PyMem_Malloc."

Comment thread Modules/_ctypes/stgdict.c
PyMem_Free(dst_info->format);
}
if (src_info->shape) {
PyMem_Free(dst_info->shape);

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.

this is also a double free as this can be reached when the format allocation failed.

Comment thread Objects/memoryobject.c
return -1;

if (!last_dim_is_contiguous(dest, src)) {
if (dest->shape[0] > PY_SSIZE_T_MAX / dest->itemsize) {

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.

be defensive, itemsize could be 0.

Comment thread Modules/_ctypes/stgdict.c
Comment on lines +45 to +46
size_t s = strlen(src_info->format);
if (s >= (size_t)PY_SSIZE_T_MAX) {

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.

you're checking strlen > PY_SSIZE_T_MAX in multiple places - this is impossible.

Comment thread Modules/posixmodule.c
Comment on lines 7299 to +7300
size = PyBytes_GET_SIZE(ub);
if (size == PY_SSIZE_T_MAX) {

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.

another impossible condition.

@bedevere-app

bedevere-app Bot commented Oct 12, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@pranavchoudhary-tech

Copy link
Copy Markdown
Contributor Author

Thank you both @gpshead and @methane for the thorough review and candid feedback.

You are right. This PR tried to take on too much across disparate modules based on #127686, and in doing so introduced new issues (like the double-free risks in stgdict) while adding checks for conditions that cannot happen in practice.

I agree with your assessment that an omnibus PR of this nature is the wrong approach. I will close this PR. If there are any genuine, isolated issues worth addressing in specific files down the line, I will handle them as small, targeted PRs with proper tests.

Thanks again for your time and guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add overflow checks before calling memory allocators

3 participants