Repository navigation
gh-162657: Fix use-after-free in _decimal.localcontext() on reentrancy - #162667
Open
xuziqiang98 wants to merge 1 commit into
Open
xuziqiang98 wants to merge 1 commit into
xuziqiang98 wants to merge 1 commit into
Conversation
…ntrancy _decimal_localcontext_impl() kept only a borrowed reference to the current context while context_setattrs() converted the traps and flags values. Determining the truth value of such a value can run arbitrary Python code, which may replace the current context and drop the last reference to the old one. The Py_NewRef(global) afterwards then operated on a freed object (a regression since fb0d9b9). Allocate the context manager before context_setattrs() and let it own the references to both contexts from the start, as suggested by skirpichev. On failure only the manager is released, since it already owns the local context copy.
skirpichev
approved these changes
Nov 26, 2026
skirpichev
left a comment
Member
There was a problem hiding this comment.
LGTM
It's good that regression fixed by simple patch, that restores code flow before fb0d9b9. But the bug seems too artificial for me to backport.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #162657
Problem
_decimal_localcontext_impl()kept only a borrowed reference to the current context acrosscontext_setattrs():context_setattrs()callsPyObject_IsTrue()on the values of thetraps/flagsmappings. If the__bool__()of such a value replaces the current context (e.g. withdecimal.setcontext()), the previous context loses its last reference and is freed. This is a regression since fb0d9b9 (gh-106078), i.e. it affects 3.13+.Fix
As suggested by @skirpichev in the issue, the context manager is now allocated before
context_setattrs(), so it owns the references to both contexts while arbitrary code runs.Two details differ from the diff posted in the issue:
local_copythroughself->local, so the extraPy_DECREF(local_copy)there would be a double decref:ctxmanager_dealloc()releasesself->localagain. Any call that raises, e.g.decimal.localcontext(prec="x"), would hit it (ASan: heap-use-after-free inctxmanager_clear();Py_DEBUG:_PyObject_AssertFailed).local_copyis released if allocating the manager fails.I did not use the patch from the issue (owning the result of
current_context()): that function returns a new reference only in--with-decimal-contextvarbuilds. With--without-decimal-contextvarit returns a borrowed reference, so that patch over-decrefs there.Py_NewRef(global)works for both.Test
CWhitebox.test_c_localcontext_reentrancyreplaces the current context from the__bool__()of atraps/flagsvalue and checks that the previous context is restored on exit. Without the C fix it fails in a plain build withAssertionError: 1 != 2(the danglingglobalpointer aliases a freshly allocated context, which then gets restored), and with ASan as a heap-use-after-free atPy_NewRef(global).Verification
_decimalbuilt from this branch and loaded into existing builds of main (4b66b87):test_decimal-O1)Py_DEBUG-O2test_decimalabort under ASan on its failure path (see above).--without-decimal-contextvar: the reproducer, the failing calls and the new test's scenario behave the same as with the default configuration (bug present before, gone after). Thetest_decimalmodule does not load in that configuration, before and after the change alike (TypeError: argument must be a contextinload_tests()).test_decimalpasses after it.Needs backport to 3.13, 3.14 and 3.15.
decimal.localcontext(): the current context is only borrowed whiletraps/flagsvalues are converted #162657