Repository navigation
Fix blockInPlace cancellation UAF by waiting through the event loop - #543
Merged
Merged
Conversation
blockInPlace kept ctx/work/waiter on its own stack and used a custom completion_fn that signaled the waiter directly from the worker thread. The cancel path returned as soon as it observed a `done` flag that completion_fn set *before* its trailing waiter.signal(), so the task could return and reclaim the frame while the worker was still inside signal() — a use-after-free that read the freed Waiter (its union tag decoded as .select, taking cmpxchgStrong on a garbage pointer and segfaulting at 0x0). It reproduced regularly on powerpc64le under QEMU in CI, where the resend-backoff timer reliably fired in the tiny window between the store and the signal. Fix: submit the work through the event loop and wait via waitForIo, so the loop owns completion delivery — it finalizes work.c and signals the waiter on the loop thread as the last step, and waitForIo waits for that before returning. Nothing touches the stack frame after we may return, so the UAF window is gone by construction. This deletes the custom completion_fn, the `done` flag, and the whole manual token/resend loop. To make this correct and cover the first-signal-lost case generically: - Generalize the loop's SIGURG cancel-resend list to key on any Work.cancel_token, not just DelegatedWork. The intrusive link moves from DelegatedWork to Work (resend_next/resend_key); resend_key is the public completion whose finalization drops the entry, so both a plain .work op (key == &work.c) and a delegated file op (key == the owning op completion) are handled uniformly. The generic .work cancel path now registers for resend too. - ThreadPool.cancel never drops token-bearing work: its func always runs so the result is always valid (a still-queued syscall aborts at begin(), a running one is interrupted via SIGURG). This preserves blockInPlace's "workFn always runs" invariant and matches the delegated ops (which report Canceled from the op func either way). A canceled blockInPlace now also re-arms the task's pending cancellation (via waitForIo's recancel), so the task stays canceled afterward.
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
waitForIo only returns error.Canceled when the completion carries a Canceled result, which only happens on the drop path where workFn never runs and ctx.result is undefined. Token-bearing work is never dropped, so that is unreachable here; assert it with `catch unreachable` rather than swallowing it, so a broken invariant crashes loudly instead of returning uninitialized ctx.result.
This was referenced Jul 12, 2026
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.
Stacked on #539 (
cancel-blocking-fileops). Fixes the use-after-free behind thecommon.test.blockInPlace: cancellation interrupts a blocking syscall on the workersegfault seen on powerpc64le/epoll under QEMU in CI (cmpxchgStrongat0x0).Root cause
blockInPlacekeepsctx/work/waiteron its own stack and used a customcompletion_fnthat signaled the waiter directly from the worker thread. The cancel path returned as soon as it observed adoneflag thatcompletion_fnset before its trailingwaiter.signal():The freed
Waiter's union tag then decoded as.select, taking thecmpxchgStrongpath on a garbagewinnerpointer → segfault at0x0. The window is ~2 instructions on the worker, so it needs the resend-backoff timer to fire in exactly that gap — which happens regularly under QEMU/CI load. It reproduces in CI fairly often.Fix: wait through the event loop
Submit the work through the loop (
loop.add) and wait viawaitForIo(&work.c). The loop owns completion delivery — it finalizeswork.cand signals the waiter on the loop thread as the last step, andwaitForIowaits for that before returning. Nothing touches the stack frame after we may return, so the UAF is gone by construction. This deletes the customcompletion_fn, thedoneflag, and the entire manual token/resend loop:I reproduced the original crash's structural conditions and then ran the pipe-based
blockInPlacecancellation test ~10k times under 14-way parallel QEMU load onpowerpc64le/epollwith zero failures.Supporting changes
Generalize the loop's SIGURG cancel-resend list (added in Cancelable delegated file ops on kqueue/poll backends #539 for
DelegatedWork) to key on anyWork.cancel_token. The intrusive link moves fromDelegatedWorktoWork(resend_next/resend_key).resend_keyis the public completion whose finalization drops the entry, so both a plain.workop (key == &work.c) and a delegated file op (key ==the owning op completion) are handled uniformly. The generic.workcancel path now registers for resend too. The entry is still removed as the completion finalizes, before its waiter is signaled, sosweepResendnever dereferences a token whose frame is about to be freed.ThreadPool.cancelnever drops token-bearing work. Itsfuncalways runs so the caller-owned result is always valid — a still-queued syscall aborts atbegin(), a running one is interrupted via SIGURG. This preservesblockInPlace's "workFn always runs" invariant (soctx.resultis never undefined) and matches the delegated ops, which reportCanceledfrom the op func whether the syscall was dropped or run-then-aborted (handleFileOpensets the completion error directly).Behavior note
A canceled
blockInPlacenow re-arms the task's pending cancellation (viawaitForIo'srecancel), so the task stays canceled afterward — consistent with the rest of thestd.Iocancellation semantics. The previous code consumed the cancel.Testing
zig build test -Dtarget=powerpc64le-linux -Dbackend=epoll -Dtest-filter=cancel— 51/51 pass under QEMU (incl. the original failing test and the delegatedDir: canceling a blocking opentest that exercises theThreadPool.cancelchange).-Dtest-filter=blockInPlacepasses.Supersedes #542 (a self-contained fix on
mainthat kept the custom completion path).