All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf] bpf: Set the run context for async callbacks
Date: Thu, 26 Nov 2026 04:00:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-234714@kernel.org> (raw)
In-Reply-To: <20261126033114.4111944-1-alexei.starovoitov@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] bpf: use-after-free in bpf_timer_cb() [1]
--

commit dec841fde8b353a2dff54ed68ef5d92e5156f040
Author: Alexei Starovoitov <ast@kernel.org>

bpf: Set the run context for async callbacks

This commit sets a dedicated run context (bpf_async_run_ctx) for BPF
asynchronous callbacks and rejects helpers that rely on the arming hook's
task or socket context.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 1b95fe795b4d9..490dd14b6a70c 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -1219,12 +1219,25 @@ static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
>  
>  static void bpf_async_refcount_put(struct bpf_async_cb *cb);
>  
> +struct bpf_run_ctx bpf_async_run_ctx;
> +
> +static void bpf_run_async_cb(struct bpf_prog *prog, bpf_callback_t callback_fn,
> +			     struct bpf_map *map, void *key, void *value)
> +{
> +	struct bpf_run_ctx *old_ctx = bpf_set_run_ctx(&bpf_async_run_ctx);
> +	struct bpf_timed_may_goto saved;
> +
> +	bpf_timed_may_goto_save(prog, &saved);
> +	callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
> +	bpf_timed_may_goto_restore(prog, &saved);
> +	bpf_reset_run_ctx(old_ctx);
> +}
> +
>  static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
>  {
>  	struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
>  	struct bpf_map *map = READ_ONCE(t->cb.map);
>  	struct bpf_prog *prog = READ_ONCE(t->cb.prog);
> -	struct bpf_timed_may_goto saved;
>  	void *value = t->cb.value;
>  	bpf_callback_t callback_fn;
>  	void *key;
> @@ -1245,10 +1258,8 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
>  
>  	key = map_key_from_value(map, value, &idx);
>  
> -	bpf_timed_may_goto_save(prog, &saved);
> -	callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
>  	/* The verifier checked that return value is zero. */
> -	bpf_timed_may_goto_restore(prog, &saved);
> +	bpf_run_async_cb(prog, callback_fn, map, key, value);

[Severity: Critical]
This problem wasn't introduced by this patch, but can bpf_timer_cb() pass a
freed map element value to callback_fn() [1]?

htab_map_delete_elem() unlinks the element and cancels any existing async
callbacks in free_htab_elem(), but another CPU that looked up the element
under RCU before it was unlinked can still call __bpf_async_init() and arm
a new timer on it:

CPU0                                CPU1
htab_map_delete_elem()
  hlist_nulls_del_rcu(&l->hash_node)
  free_htab_elem()
    ...                             __bpf_async_init()
                                      hrtimer_setup(&t->timer, ...)
                                      cmpxchg(&async->cb, NULL, cb)
                                    ...
  /* RCU grace period expires, l is freed */
                                    bpf_timer_cb()
                                      bpf_run_async_cb(..., value)
                                        <-- use-after-free

Because the element has already passed free_htab_elem(), nothing cancels
the newly initialized timer before the element is freed after the RCU grace
period, leaving bpf_timer_cb() to pass reclaimed memory to the BPF program.

[1] https://sashiko.dev/#/bug/linux-25136054-3592-42c2-bca7-ba4763a56761

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126033114.4111944-1-alexei.starovoitov@gmail.com?part=1

  reply	other threads:[~2026-11-26  4:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  3:31 [PATCH bpf] bpf: Set the run context for async callbacks Alexei Starovoitov
2026-11-26  4:00 ` sashiko-bot [this message]
2026-11-26  4:11 ` bot+bpf-ci
2026-11-27  6:20 ` patchwork-bot+netdevbpf

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-234714@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=bpf@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.