`_decimal::CURRENT_CONTEXT`: UAF via borrowed reference across Python callbacks
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Crash report
Originally reported by @PidSS as a security advisory
What happened?
The CURRENT_CONTEXT() macro in Modules/_decimal/_decimal.c:1951 obtains a new reference from PyContextVar_Get(), then immediately calls Py_DECREF, leaving a borrowed reference. If Python code executes before the context pointer is next used — via warning handlers, GC finalizers, instancecheck, etc. — and that code calls decimal.setcontext(), the old context is freed while C code still holds a dangling pointer. The macro is used at 20+ call sites covering virtually every decimal operation.
Original reproducer (I will try to make a shorter one):
import decimal, warnings, gc, sys, ctypes
gc.disable()
ctx = decimal.getcontext()
CTX_ID, CTX_SIZE = id(ctx), sys.getsizeof(ctx)
del ctx # refcount = 1, held only by ContextVar HAMT
def evil_warning_handler(message, category, filename, lineno, file=None, line=None):
if 'Format specifier' not in str(message):
return
ctx = decimal.getcontext() # refcount -> 2
decimal.setcontext(decimal.Context(prec=999)) # HAMT ref dropped -> 1
del ctx # refcount -> 0, FREED
spray = [bytearray(b'\xCC' * (CTX_SIZE - 32)) for _ in range(100000)]
warnings.showwarning = evil_warning_handler
warnings.filterwarnings('always', category=DeprecationWarning)
format(decimal.Decimal('123.456'), 'N')
FTR, I saw that we have this macro twice, but it depends on whether we have WITH_DECIMAL_CONTEXTVAR. If I build Python with --without-decimal-contextvar, I also have a crash so it's a bit weird.
Related but not with the same root cause: https://github.com/python/cpython/issues/146011.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Output from running 'python -VV' on the command line:
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Modules/_decimal/_decimal.c at the CURRENT_CONTEXT macro around line 1951, then inspect its 20+ call sites and the second macro used without WITH_DECIMAL_CONTEXTVAR. Run the supplied warning-handler reproducer on a CPython main build. Done means the reproduced callback path no longer uses a freed context in either configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- security
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100