Skip to content

gh-115243: Fix crash in deque.index() when the deque is concurrently modified #115247

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
Feb 14, 2024

Conversation

kcatss
Copy link
Contributor

@kcatss kcatss commented Feb 10, 2024

Increase the reference count of item to prevent it from being freed

@Eclips4 Eclips4 added skip news needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Feb 10, 2024
Co-authored-by: Kirill Podoprigora <[email protected]>
Co-authored-by: Radislav Chugunov <[email protected]>
Copy link
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

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

CI/CD failure is unrelated, see #115258 for more details.
LGTM.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka
Copy link
Member

Please add a NEWS entry. It is a user visible change.

@@ -0,0 +1 @@
Fix possible crashes when operating with the functions in the :func:`deque.index`and custom comparison operators.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Fix possible crashes when operating with the functions in the :func:`deque.index`and custom comparison operators.
Fix possible crashes in :func:`deque.index` when calling
:c:func:`PyObject_RichCompareBool`.

Copy link
Member

Choose a reason for hiding this comment

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

I suggest to revert this change. The original wording was more correct.

It has nothing with PyObject_RichCompareBool (it is not the only C API for comparison, and you can trigger a crash from Python code).

Copy link
Contributor Author

@kcatss kcatss Feb 12, 2024

Choose a reason for hiding this comment

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

In Python, deque.index invokes PyObject_RichCompareBool in C code. Subsequently, PyObject_RichCompareBool calls the object's __eq__ method in Python. However, I am unsure which explanation is clearer. If the goal is to eliminate the mention of PyObject_RichCompareBool because deque.index always calls it, then the suggestion you provided would indeed be clearer

Copy link
Member

Choose a reason for hiding this comment

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

It is an implementation detail. The common Python user does not call PyObject_RichCompareBool(), and can be affected by the bug.

How this bug affects Python users?

If deque.index() is called and simultaneously (in other thread, in garbage collector) the same deque is modified, the result is an undefined behavior, possibly crash. You only need to use deque.index() and modify the deque simultaneously. Please describe the effect of this change in terms of the visible behavior change in Python code.

Copy link
Member

Choose a reason for hiding this comment

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

Serhiy is right. Also, I remember same wording for the same change: 2d5bf56

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this change refers to my patched code, I believe it will be safe. This is because the issue of use-after-free arises within the inner code of PyObject_RichCompareBool, specifically after the object's reference count has been incremented

@serhiy-storchaka serhiy-storchaka changed the title gh-115243 : Fix crash in deque with custom comparison operators gh-115243: Fix crash in deque.index() when the deque is concurrently modified Feb 14, 2024
@serhiy-storchaka
Copy link
Member

"Custom comparison operator" is just the simplest way to reproduce the issue for tests.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) February 14, 2024 15:40
@serhiy-storchaka serhiy-storchaka merged commit 6713601 into python:main Feb 14, 2024
@miss-islington-app
Copy link

Thanks @kcatss for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 14, 2024
…ently modified (pythonGH-115247)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 14, 2024
…ently modified (pythonGH-115247)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Feb 14, 2024

GH-115465 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Feb 14, 2024
@bedevere-app
Copy link

bedevere-app bot commented Feb 14, 2024

GH-115466 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Feb 14, 2024
serhiy-storchaka pushed a commit that referenced this pull request Feb 14, 2024
…rently modified (GH-115247) (GH-115465)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
serhiy-storchaka pushed a commit that referenced this pull request Feb 14, 2024
…rently modified (GH-115247) (GH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request May 21, 2024
…concurrently modified (pythonGH-115247) (pythonGH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request May 21, 2024
…concurrently modified (pythonGH-115247) (pythonGH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request Sep 19, 2024
…concurrently modified (pythonGH-115247) (pythonGH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request Sep 19, 2024
…concurrently modified (pythonGH-115247) (pythonGH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <[email protected]>
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.

5 participants