BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yun Lu" <luyun_611@163.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v7 3/4] bpf: Prevent task work context reuse while irq_work is busy
Date: Mon, 12 Oct 2026 06:34:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-167741@kernel.org> (raw)
In-Reply-To: <20261012061915.231254-4-luyun_611@163.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: UAF in task_work/timer callbacks due to async context re-init on deleted map elements
--

commit 580f53f590a0a7077f231292b13de90213f646c5
Author: Yun Lu <luyun@kylinos.cn>

bpf: Prevent task work context reuse while irq_work is busy

This commit fixes a race condition where a task work context could return to
standby before its scheduling irq_work handler completes. It initializes the
irq_work during context creation and checks for the BUSY state before reusing
it to prevent overlapping invocations.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 028fc6796b597..350269b8646e4 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -4553,6 +4553,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
>  	memset(ctx, 0, sizeof(*ctx));
>  	refcount_set(&ctx->refcnt, 1); /* map's own ref */
>  	ctx->state = BPF_TW_STANDBY;
> +	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
>  
>  	old_ctx = cmpxchg(&twk->ctx, NULL, ctx);

[Severity: High]
Can a newly allocated context be installed on a deleted map element here?

If a concurrent map deletion calls bpf_task_work_cancel_and_free(), it
synchronously clears the context:

bpf_task_work_cancel_and_free(struct bpf_task_work *tw) {
    ...
    ctx = xchg(&twk->ctx, NULL);
    ...
}

Since it doesn't leave a poison value, a concurrent BPF program running under
RCU read lock that calls bpf_task_work_schedule() would see twk->ctx as NULL
and successfully cmpxchg the newly allocated context into the deleted element.

[ ... ]

> @@ -4644,7 +4657,6 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
>  	ctx->map = map;
>  	ctx->map_val = (void *)tw - map->record->task_work_off;
>  	init_task_work(&ctx->work, bpf_task_work_callback);
> -	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
>  
>  	irq_work_queue(&ctx->irq_work);
>  	return 0;

[Severity: High]
Could this lead to a use-after-free if the element was concurrently deleted?

If bpf_task_work_fetch_ctx() re-initializes a context on a deleted element as
described above, ctx->map_val captures a pointer to the element's memory:

bpf_task_work_schedule(...) {
    ...
    ctx->map_val = (void *)tw - map->record->task_work_off;
    ...
    irq_work_queue(&ctx->irq_work);
}

Since the deleted map element will be freed after the RCU grace period, but
the newly initialized asynchronous irq_work remains armed, could the later
execution of this callback access the freed element memory through
ctx->map_val?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261012061915.231254-1-luyun_611@163.com?part=3

  reply	other threads:[~2026-10-12  6:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12  6:19 [PATCH bpf-next v7 0/4] bpf: Fix task work scheduling races Yun Lu
2026-10-12  6:19 ` [PATCH bpf-next v7 1/4] bpf: Fix NULL task dereference in task work cancellation Yun Lu
2026-10-12  6:19 ` [PATCH bpf-next v7 2/4] selftests/bpf: Add task work cancellation race test Yun Lu
2026-10-12  6:19 ` [PATCH bpf-next v7 3/4] bpf: Prevent task work context reuse while irq_work is busy Yun Lu
2026-10-12  6:34   ` sashiko-bot [this message]
2026-10-13  6:17     ` luyun
2026-10-12  6:19 ` [PATCH bpf-next v7 4/4] selftests/bpf: Add task work BUSY window test Yun Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-167741@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=luyun_611@163.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox