Skip to content

ucontext-cp: don't free coroutine stack and context in use#1591

Open
rootvector2 wants to merge 1 commit into
axboe:masterfrom
rootvector2:ucontext-cp-coro-uaf
Open

ucontext-cp: don't free coroutine stack and context in use#1591
rootvector2 wants to merge 1 commit into
axboe:masterfrom
rootvector2:ucontext-cp-coro-uaf

Conversation

@rootvector2
Copy link
Copy Markdown
Contributor

copy_file_wrapper runs as a makecontext coroutine on pctx->stack_buf, but on completion it frees that stack and pctx and then swapcontexts through them, so the final switch back to main runs on freed memory. found it reading the cleanup path. move the frees of stack_buf and pctx into main, which reclaims them once the coroutine has finished.

Copy link
Copy Markdown
Contributor

@ammarfaizi2 ammarfaizi2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, read the pull request guidelines.

  • The commit message must contain a Signed-off-by tag.
  • The explanation about the commit should be included in the commit message itself, word-wrapped at 72 chars.

copy_file_wrapper() runs as a makecontext() coroutine on the stack
pointed to by pctx->stack_buf. On completion it freed stack_buf and
pctx, then called swapcontext(&pctx->ctx_fnew, &pctx->ctx_main). That
swap saves into and loads from the just-freed pctx while still running
on the freed stack, so the final switch back to main is a
use-after-free.

Drop the two frees from the coroutine and reclaim stack_buf and pctx
in main()'s event loop once the coroutine has finished, detected via
the completion counter. A coroutine only completes in the event loop,
since the setup loop always yields at the first await_readv.

Signed-off-by: rootvector2 <dxbnaveed.k@gmail.com>
@rootvector2 rootvector2 force-pushed the ucontext-cp-coro-uaf branch from 0749150 to d9fe09c Compare June 4, 2026 13:13
@rootvector2
Copy link
Copy Markdown
Contributor Author

done. added the sign-off and moved the explanation into the commit message body, wrapped at 72.

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.

2 participants