Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-41175: Guard against a possible NULL pointer dereference within bytearrayobject #21240

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
merged 1 commit into from
Jul 8, 2020

Conversation

stratakis
Copy link
Contributor

@stratakis stratakis commented Jun 30, 2020

Detected by GCC 10 static analysis tool

https://bugs.python.org/issue41175

@@ -267,6 +267,7 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
result = (PyByteArrayObject *) \
PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
if (result != NULL) {
assert(result->ob_bytes != NULL);
Copy link
Member

Choose a reason for hiding this comment

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

It seems like it can be NULL if PyByteArray_FromStringAndSize() is called with size=0: if va.len+vb.len=0 (if both strings are empty).

At least, I don't see any code path handling handling this case.

Copy link
Member

Choose a reason for hiding this comment

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

Python code bytearray() + bytearray() triggers exactly this case. It seems like with the glibc, memcpy(NULL, _PyByteArray_empty_string, 0) doesn't crash. But as I far as I recall, it's an undefined behavior.

We should avoid calling memcpy() with n=0.

See:

Try also UBSan of clang.

cc @benjaminp @tiran

Copy link
Member

Choose a reason for hiding this comment

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

Right, this assert is not correct.

What about only doing the first memcpy if va.len is non-zero, and the second one iff vb.len is non-zero? ISTM that if memcpy is inlined, the compiler would have a chance to skip the check.

…bject

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool
Copy link
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. Thanks for the update!

@vstinner vstinner merged commit 61fc23c into python:master Jul 8, 2020
@vstinner
Copy link
Member

vstinner commented Jul 8, 2020

I merged your PR, thanks.

@stratakis: Is a backport needed?

@stratakis
Copy link
Contributor Author

I merged your PR, thanks.

@stratakis: Is a backport needed?

I would say yes, the issue affects previous releases as well.

@vstinner vstinner added the needs backport to 3.9 only security fixes label Jul 10, 2020
@miss-islington
Copy link
Contributor

Thanks @stratakis for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Jul 10, 2020
@bedevere-bot
Copy link

GH-21431 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 10, 2020
…bject (pythonGH-21240)

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool.
(cherry picked from commit 61fc23c)

Co-authored-by: stratakis <[email protected]>
@miss-islington
Copy link
Contributor

Thanks @stratakis for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-21432 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 10, 2020
…bject (pythonGH-21240)

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool.
(cherry picked from commit 61fc23c)

Co-authored-by: stratakis <[email protected]>
miss-islington added a commit that referenced this pull request Jul 10, 2020
…bject (GH-21240)

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool.
(cherry picked from commit 61fc23c)

Co-authored-by: stratakis <[email protected]>
miss-islington added a commit that referenced this pull request Jul 10, 2020
…bject (GH-21240)

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool.
(cherry picked from commit 61fc23c)

Co-authored-by: stratakis <[email protected]>
@stratakis stratakis deleted the bytearraynull branch July 10, 2020 10:52
arun-mani-j pushed a commit to arun-mani-j/cpython that referenced this pull request Jul 21, 2020
…bject (pythonGH-21240)

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool.
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Aug 20, 2020
…bject (pythonGH-21240)

The issue is triggered by the bytearray() + bytearray() operation.

Detected by GCC 10 static analysis tool.
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.

6 participants