Skip to content

bpo-35193: Fix an off by one error in the RETURN_VALUE case. #10418

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 6 commits into from
Nov 9, 2018

Conversation

gpshead
Copy link
Member

@gpshead gpshead commented Nov 8, 2018

By doing a bounds check within find_op so it can
return before going past the end as a safety measure.

7db3c48#diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.

https://bugs.python.org/issue35193

By doing a bounds check within find_op so it can
return before going past the end as a safety measure.

python@7db3c48#diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.
@gpshead gpshead added type-bug An unexpected behavior, bug, or error needs backport to 3.6 labels Nov 8, 2018
@gpshead gpshead requested a review from Yhg1s November 8, 2018 21:16
@@ -417,7 +419,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
}
if (h > i + 1) {
fill_nops(codestr, i + 1, h);
nexti = find_op(codestr, h);
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't be simpler to just add a condition in this line?

nexti = (h < codelen) ? find_op(codestr, h) : codelen;

Copy link
Member

Choose a reason for hiding this comment

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

That would leave room for the same bug to be introduced in other calls to find_op.

Copy link
Member

@serhiy-storchaka serhiy-storchaka Nov 8, 2018

Choose a reason for hiding this comment

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

In other calls find_op() can't be called with codelen.

Copy link
Member

Choose a reason for hiding this comment

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

Yes. That's why I said "introduced". this prevents the bug from reappearing when the rest of the code changes.

I don't see how to test for the presence of this out of bounds memory
access issue itself from the level of the CPython interpreter, but this
test will reliably fail (crash) when the interpreter is built using the
clang memory sanitizer.
Fix an off by one error in the bytecode peephole optimizer where it could read
bytes beyond the end of bounds of a bytecode array when removing unreachable
code after a return. This did not always crash an interpreter, but could.
An interpreter compiled using the clang memory sanitizer always failed.
Copy link
Member

Choose a reason for hiding this comment

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

This seems a little longer and more detailed than most news entries... not sure if that's good or bad.

@gpshead gpshead self-assigned this Nov 9, 2018
@gpshead gpshead merged commit 49fa4a9 into python:master Nov 9, 2018
@miss-islington
Copy link
Contributor

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-bot
Copy link

GH-10421 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Sorry, @gpshead, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 49fa4a9f1ef387e16596f271414c855339eadf09 3.6

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 9, 2018
…H-10418)

Fix an off by one error in the peephole optimizer when checking for unreachable code beyond a return.

Do a bounds check within find_op so it can return before going past the end as a safety measure.

python@7db3c488335168993689ddae5914a28e16188447GH-diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.

This bug was shipped in all Python 3.6 and 3.7 releases.

The included unittest won't fail unless you do a clang msan build.
(cherry picked from commit 49fa4a9)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
@gpshead gpshead deleted the issue35193_off_by_one_fix branch November 9, 2018 01:56
miss-islington added a commit that referenced this pull request Nov 9, 2018
Fix an off by one error in the peephole optimizer when checking for unreachable code beyond a return.

Do a bounds check within find_op so it can return before going past the end as a safety measure.

7db3c488335168993689ddae5914a28e16188447GH-diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.

This bug was shipped in all Python 3.6 and 3.7 releases.

The included unittest won't fail unless you do a clang msan build.
(cherry picked from commit 49fa4a9)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
gpshead added a commit to gpshead/cpython that referenced this pull request Nov 9, 2018
…H-10418)

Fix an off by one error in the peephole optimizer when checking for unreachable code beyond a return.

Do a bounds check within find_op so it can return before going past the end as a safety measure.

python@7db3c48#diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.

This bug was shipped in all Python 3.6 and 3.7 releases.

The included unittest won't fail unless you do a clang msan build.

(cherry picked from commit 49fa4a9)
@bedevere-bot
Copy link

GH-10422 is a backport of this pull request to the 3.6 branch.

gpshead added a commit that referenced this pull request Nov 9, 2018
… (GH-10422)

Fix an off by one error in the peephole optimizer when checking for unreachable code beyond a return.

Do a bounds check within find_op so it can return before going past the end as a safety measure.

7db3c48#diff-a33329ae6ae0bb295d742f0caf93c137
introduced this off by one error while fixing another one nearby.

This bug was shipped in all Python 3.6 and 3.7 releases.

The included unittest won't fail unless you do a clang msan build.

(cherry picked from commit 49fa4a9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants