Skip to content

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

Merged
serhiy-storchaka merged 6 commits into
python:mainfrom
kcatss:deque_index_uaf
Feb 14, 2024
Merged

gh-115243: Fix crash in deque.index() when the deque is concurrently modified#115247
serhiy-storchaka merged 6 commits into
python:mainfrom
kcatss:deque_index_uaf

Conversation

@kcatss

@kcatss kcatss commented Feb 10, 2024

Copy link
Copy Markdown
Contributor

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

Comment thread Modules/_collectionsmodule.c Outdated
@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 <kirill.bast9@mail.ru>
Comment thread Modules/_collectionsmodule.c Outdated
Comment thread Lib/test/test_deque.py Outdated
Comment thread Lib/test/test_deque.py Outdated
Comment thread Modules/_collectionsmodule.c Outdated
Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>

@Eclips4 Eclips4 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.

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

@serhiy-storchaka serhiy-storchaka 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.

LGTM.

@serhiy-storchaka

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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).

@kcatss kcatss Feb 12, 2024

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.

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Comment thread Misc/NEWS.d/next/Security/2024-02-12-00-33-01.gh-issue-115243.e1oGX8.rst Outdated
@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
Copy Markdown
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
Copy Markdown

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 <kcats9731@gmail.com>
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 <kcats9731@gmail.com>
@bedevere-app

bedevere-app Bot commented Feb 14, 2024

Copy link
Copy Markdown

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

bedevere-app Bot commented Feb 14, 2024

Copy link
Copy Markdown

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 <kcats9731@gmail.com>
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 <kcats9731@gmail.com>
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 <kcats9731@gmail.com>
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 <kcats9731@gmail.com>
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 <kcats9731@gmail.com>
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 <kcats9731@gmail.com>
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request Jul 30, 2025
…concurrently modified (pythonGH-115247) (pythonGH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <kcats9731@gmail.com>
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