From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Yun Lu" <luyun_611@163.com>, <daniel@iogearbox.net>,
<andrii@kernel.org>, <eddyz87@gmail.com>, <memxor@gmail.com>,
<martin.lau@linux.dev>, <song@kernel.org>,
<yonghong.song@linux.dev>, <jolsa@kernel.org>,
<emil@etsalapatis.com>, <ihor.solodrai@linux.dev>
Cc: <yatsenko@meta.com>, <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v4 0/6] bpf: Fix async callback use-after-free on map deletion
Date: Mon, 23 Nov 2026 00:27:17 +0000 [thread overview]
Message-ID: <DN25XEMY8JO1.249FAY7XQUC31@gmail.com> (raw)
In-Reply-To: <20261119033051.196177-1-luyun_611@163.com>
On Wed, Nov 18, 2026 at 07:30 PM Yun Lu <luyun_611@163.com> wrote:
> - Reject zero usercnt in the shared setter (Sashiko), order the check
> after lookup with a full barrier, and add a wq regression for reuse.
bpf_task_work_acquire_ctx() has the same two reads without a barrier.
On arm64 the 2nd load can pass the 1st:
bpf_map_value_is_linked() finds the element inserted again
atomic64_read(&map->usercnt) returns the old nonzero count
The uref walk missed the element, so nothing cancels this task work.
Task work is allowed in inner maps and ctx->prog doesn't hold them.
The callback can run on a freed map, like the timer in Mani's fix:
https://lore.kernel.org/bpf/20261027-bpf-timer-freed-map-v1-1-30402b0ee99b@columbia.edu/
> - Use the existing map lookup and move the wq/timer check to the shared
> setter (Alexei).
That lookup was my suggestion, sorry. bpf-ci was right on v2.
It changes the eviction order in LRU maps:
bpf_timer_set_callback(&val->timer, cb)
bpf_map_value_is_linked(map, val)
htab_lru_map_lookup_elem(map, key)
__htab_lru_map_lookup_elem(map, key, true)
bpf_lru_node_set_ref()
A timer callback gets val without a lookup.
It can call the setter in each period, like timer_cb2() in timer_mim.c.
Then an idle element looks hot and elements in use get evicted.
See commit 50b045a8c0cc ("bpf, lru: avoid messing with eviction
heuristics upon syscall lookup").
> - Give slot detachment to the first invalidator and reuse the existing
> task-work, wq and timer selftests.
Now the 2nd canceller returns with a dead cb in the slot:
CPU 0: update from syscall CPU 1: prog, same array element
xchg(&cb->map, NULL) wins
preempted
bpf_map_update_elem()
xchg(&cb->map, NULL) loses, return
bpf_timer_init() -EBUSY
bpf_timer_set_callback() -ENOENT
WRITE_ONCE(async->cb, NULL)
Today xchg(&async->cb, NULL) empties the slot first and init returns 0.
Task work gets -EBUSY in the same window.
You replied that cmpxchg() races with the pending WRITE_ONCE():
https://lore.kernel.org/bpf/84286263-5111-4358-8b90-9e9d86d846cb@163.com/
It doesn't when both cancellers use cmpxchg(), like in v2.
guard(rcu) keeps cb alive, so its address can't come back.
All three on top of the series, helpers.c compiles but I didn't run it:
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 029f60b..6696539 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1155,8 +1155,12 @@ static void *map_key_from_value(struct bpf_map *map, void *value, u32 *arr_idx)
static bool bpf_map_value_is_linked(struct bpf_map *map, void *value)
{
u32 idx;
+ void *key = map_key_from_value(map, value, &idx);
- return map->ops->map_lookup_elem(map, map_key_from_value(map, value, &idx)) == value;
+ /* It's not a use of the element. Don't set the ref bit of LRU node. */
+ if (map->ops->map_lookup_elem_sys_only)
+ return map->ops->map_lookup_elem_sys_only(map, key) == value;
+ return map->ops->map_lookup_elem(map, key) == value;
}
enum bpf_async_type {
@@ -1774,13 +1778,15 @@ static void bpf_async_cancel_and_free(struct bpf_async_kern *async)
guard(rcu)();
/* Order unlink before reading async->cb, including an empty slot. */
smp_mb();
- /* Acquire any invalidation published before the slot was cleared. */
- cb = smp_load_acquire(&async->cb);
- /* Only the first invalidator detaches cb and drops the map's reference. */
- if (!cb || !xchg(&cb->map, NULL))
+ cb = READ_ONCE(async->cb);
+ if (!cb)
return;
- WRITE_ONCE(async->cb, NULL);
+ /* Callbacks that run after this see NULL and return. */
+ WRITE_ONCE(cb->map, NULL);
+ /* The one who detaches cb drops the map's reference. */
+ if (cmpxchg(&async->cb, cb, NULL) != cb)
+ return;
bpf_async_update_prog_callback(cb, NULL, NULL);
/*
@@ -4918,6 +4924,9 @@ static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work
return ERR_PTR(-ENOENT);
}
+ /* Order the lookup before the read of usercnt. See __bpf_async_set_callback(). */
+ smp_mb();
+
/*
* If no process or bpffs is holding a reference to the map, no new callbacks should be
* scheduled. This does not address any race or correctness issue, but rather is a policy
@@ -5245,10 +5254,10 @@ void bpf_task_work_cancel_and_free(void *val)
/* Prevent entry to RUNNING before clearing the slot. */
state = xchg(&ctx->state, BPF_TW_FREED);
- /* Only the first FREED transition detaches ctx and owns the map's ref. */
+ (void)cmpxchg(&twk->ctx, ctx, NULL);
+ /* Only the first FREED transition owns the map's ref. */
if (state == BPF_TW_FREED)
return;
- WRITE_ONCE(twk->ctx, NULL);
if (state == BPF_TW_SCHEDULED) {
/* run in irq_work to avoid locks in NMI */
pw-bot: cr
prev parent reply other threads:[~2026-11-23 0:27 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 3:30 [PATCH bpf-next v4 0/6] bpf: Fix async callback use-after-free on map deletion Yun Lu
2026-11-19 3:30 ` [PATCH bpf-next v4 1/6] bpf: Mark task work FREED before detaching it Yun Lu
2026-11-19 3:30 ` [PATCH bpf-next v4 2/6] bpf: Check task work value membership before scheduling Yun Lu
2026-11-19 3:30 ` [PATCH bpf-next v4 3/6] selftests/bpf: Test task work scheduling on a deleted value Yun Lu
2026-11-20 11:12 ` Mykyta Yatsenko
2026-11-22 12:55 ` Yun Lu
2026-11-19 3:30 ` [PATCH bpf-next v4 4/6] bpf: Invalidate async callbacks before detaching them Yun Lu
2026-11-19 3:30 ` [PATCH bpf-next v4 5/6] bpf: Check async callback membership and user references Yun Lu
2026-11-19 4:20 ` bot+bpf-ci
2026-11-19 3:30 ` [PATCH bpf-next v4 6/6] selftests/bpf: Test wq callback setup after deletion and user release Yun Lu
2026-11-19 3:43 ` sashiko-bot
2026-11-19 4:05 ` bot+bpf-ci
2026-11-20 11:55 ` Mykyta Yatsenko
2026-11-22 13:30 ` Yun Lu
2026-11-23 0:27 ` Alexei Starovoitov [this message]
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=DN25XEMY8JO1.249FAY7XQUC31@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=luyun_611@163.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=yatsenko@meta.com \
--cc=yonghong.song@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