Skip to content

gh-162657: Fix use-after-free in _decimal.localcontext() on reentrancy - #162667

Open
xuziqiang98 wants to merge 1 commit into
python:mainfrom
xuziqiang98:gh-162657-decimal-localcontext-uaf
Open

xuziqiang98 wants to merge 1 commit into
python:mainfrom
xuziqiang98:gh-162657-decimal-localcontext-uaf

Conversation

@xuziqiang98

@xuziqiang98 xuziqiang98 commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #162657

Problem

_decimal_localcontext_impl() kept only a borrowed reference to the current context across context_setattrs():

CURRENT_CONTEXT(state, global);       /* borrowed */
...
context_setattrs(local_copy, ...);    /* can run arbitrary Python code */
...
self->global = Py_NewRef(global);     /* use-after-free */

context_setattrs() calls PyObject_IsTrue() on the values of the traps / flags mappings. If the __bool__() of such a value replaces the current context (e.g. with decimal.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:

  • On the failure path only the manager is released. It already owns local_copy through self->local, so the extra Py_DECREF(local_copy) there would be a double decref: ctxmanager_dealloc() releases self->local again. Any call that raises, e.g. decimal.localcontext(prec="x"), would hit it (ASan: heap-use-after-free in ctxmanager_clear(); Py_DEBUG: _PyObject_AssertFailed).
  • local_copy is 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-contextvar builds. With --without-decimal-contextvar it returns a borrowed reference, so that patch over-decrefs there. Py_NewRef(global) works for both.

Test

CWhitebox.test_c_localcontext_reentrancy replaces the current context from the __bool__() of a traps / flags value and checks that the previous context is restored on exit. Without the C fix it fails in a plain build with AssertionError: 1 != 2 (the dangling global pointer aliases a freshly allocated context, which then gets restored), and with ASan as a heap-use-after-free at Py_NewRef(global).

Verification

_decimal built from this branch and loaded into existing builds of main (4b66b87):

build test_decimal new test without the C fix
ASan (-O1) OK heap-use-after-free
Py_DEBUG OK fails
plain -O2 OK fails (50/50 runs)
  • The diff from the issue comment, applied verbatim, makes test_decimal abort 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). The test_decimal module does not load in that configuration, before and after the change alike (TypeError: argument must be a context in load_tests()).
  • 3.13.18, 3.14.10 and 3.15.2 (ASan builds): the C fix and the test apply unchanged; the new test reports heap-use-after-free before the fix, and test_decimal passes after it.

Needs backport to 3.13, 3.14 and 3.15.

…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.
@python-cla-bot

python-cla-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@skirpichev skirpichev 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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use-after-free in decimal.localcontext(): the current context is only borrowed while traps/flags values are converted

2 participants