* [PATCH bpf] bpf: Set the run context for async callbacks
@ 2026-11-26 3:31 Alexei Starovoitov
2026-11-26 4:00 ` sashiko-bot
2026-11-26 4:11 ` bot+bpf-ci
0 siblings, 2 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 3:31 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
bpf_get_retval(), bpf_set_retval() and bpf_get_local_storage() use
current->bpf_ctx, which the hook sets. bpf_getsockopt() and
bpf_setsockopt() count on the hook to hold the socket lock.
bpf_sys_bpf(), bpf_sys_close(), bpf_btf_find_by_name_kind() and
bpf_kallsyms_lookup_name() use the credentials and the files of
current. An async callback runs in a timer, in a kworker, in an RCU
callback or in a task that the program has picked.
Give async callbacks a run context of their own, and make these helpers
fail in it. bpf_get_retval() returns 0. bpf_get_local_storage() cannot
fail. Refuse async callbacks in a program with cgroup storage.
Fixes: b00628b1c7d5 ("bpf: Introduce bpf timers.")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
include/linux/bpf.h | 13 +++++++++++++
kernel/bpf/btf.c | 3 +++
kernel/bpf/cgroup.c | 4 +++-
kernel/bpf/helpers.c | 35 ++++++++++++++++++-----------------
kernel/bpf/syscall.c | 7 +++++--
kernel/bpf/verifier.c | 6 ++++++
net/core/filter.c | 14 ++++++++++----
net/ipv4/bpf_tcp_ops.c | 2 +-
8 files changed, 59 insertions(+), 25 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 3a5a0380304d..a6976776e90b 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3440,6 +3440,14 @@ static inline bool has_current_bpf_ctx(void)
return !!current->bpf_ctx;
}
+/* An async callback runs in no hook, and current is not the task that has armed it */
+extern struct bpf_run_ctx bpf_async_run_ctx;
+
+static inline bool bpf_in_async_cb(void)
+{
+ return current->bpf_ctx == &bpf_async_run_ctx;
+}
+
void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -3765,6 +3773,11 @@ static inline bool has_current_bpf_ctx(void)
return false;
}
+static inline bool bpf_in_async_cb(void)
+{
+ return false;
+}
+
static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
{
}
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 7b3291556e6e..23e8977164e7 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -9703,6 +9703,9 @@ BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int
if (name_sz <= 1 || name[name_sz - 1])
return -EINVAL;
+ if (bpf_in_async_cb())
+ return -EPERM;
+
ret = bpf_find_btf_id(name, kind, &btf);
if (ret > 0 && btf_is_module(btf)) {
btf_obj_fd = __btf_new_fd(btf);
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 1ff030f4314f..3bf3081bfa27 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -2110,7 +2110,7 @@ BPF_CALL_0(bpf_get_retval)
struct bpf_cg_run_ctx *ctx =
container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
- return ctx->retval;
+ return bpf_in_async_cb() ? 0 : ctx->retval;
}
const struct bpf_func_proto bpf_get_retval_proto = {
@@ -2124,6 +2124,8 @@ BPF_CALL_1(bpf_set_retval, int, retval)
struct bpf_cg_run_ctx *ctx =
container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
+ if (bpf_in_async_cb())
+ return -EOPNOTSUPP;
ctx->retval = retval;
return 0;
}
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 1b95fe795b4d..490dd14b6a70 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);
this_cpu_write(hrtimer_running, NULL);
out:
@@ -1259,7 +1270,6 @@ static void bpf_wq_work(struct work_struct *work)
{
struct bpf_work *w = container_of(work, struct bpf_work, work);
struct bpf_async_cb *cb = &w->cb;
- struct bpf_timed_may_goto saved;
struct bpf_prog *prog;
struct bpf_map *map;
bpf_callback_t callback_fn;
@@ -1282,9 +1292,7 @@ static void bpf_wq_work(struct work_struct *work)
migrate_disable();
- 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_run_async_cb(prog, callback_fn, map, key, value);
migrate_enable();
out:
@@ -4624,7 +4632,6 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,
static void bpf_task_work_callback(struct callback_head *cb)
{
struct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);
- struct bpf_timed_may_goto saved;
enum bpf_task_work_state state;
u32 idx;
void *key;
@@ -4646,10 +4653,7 @@ static void bpf_task_work_callback(struct callback_head *cb)
key = (void *)map_key_from_value(ctx->map, ctx->map_val, &idx);
migrate_disable();
- bpf_timed_may_goto_save(ctx->prog, &saved);
- ctx->callback_fn((u64)(long)ctx->map, (u64)(long)key,
- (u64)(long)ctx->map_val, 0, 0);
- bpf_timed_may_goto_restore(ctx->prog, &saved);
+ bpf_run_async_cb(ctx->prog, ctx->callback_fn, ctx->map, key, ctx->map_val);
migrate_enable();
bpf_task_work_ctx_reset(ctx);
@@ -4889,7 +4893,6 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)
struct bpf_rcu_head_kern *rh = container_of(rcu, struct bpf_rcu_head_kern, rcu);
bpf_callback_t callback_fn = rh->callback_fn;
struct bpf_prog *prog = rh->prog;
- struct bpf_timed_may_goto saved;
struct bpf_map *map = rh->map;
void *value, *key;
u32 idx;
@@ -4901,9 +4904,7 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)
smp_store_release(&rh->armed, 0);
rcu_read_lock_dont_migrate();
- 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_run_async_cb(prog, callback_fn, map, key, value);
rcu_read_unlock_migrate();
bpf_prog_put(prog);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 7e1085d6a67e..91d078bb13d2 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -6618,6 +6618,9 @@ static bool syscall_prog_is_valid_access(int off, int size,
BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
{
+ if (bpf_in_async_cb())
+ return -EPERM;
+
switch (cmd) {
case BPF_MAP_CREATE:
case BPF_MAP_DELETE_ELEM:
@@ -6707,7 +6710,7 @@ BPF_CALL_1(bpf_sys_close, u32, fd)
* This helper is allowed in the following callchain only:
* sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
*/
- return close_fd(fd);
+ return bpf_in_async_cb() ? -EPERM : close_fd(fd);
}
static const struct bpf_func_proto bpf_sys_close_proto = {
@@ -6727,7 +6730,7 @@ BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flag
if (name_sz <= 1 || name[name_sz - 1])
return -EINVAL;
- if (!bpf_dump_raw_ok(current_cred()))
+ if (bpf_in_async_cb() || !bpf_dump_raw_ok(current_cred()))
return -EPERM;
*res = kallsyms_lookup_name(name);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d4b31990fcb9..57fed2517e8e 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11493,6 +11493,12 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
struct bpf_verifier_state *async_cb;
/* there is no real recursion here. timer and workqueue callbacks are async */
+ /* there is no cgroup to take the storage from */
+ if (env->prog->aux->cgroup_storage[BPF_CGROUP_STORAGE_SHARED] ||
+ env->prog->aux->cgroup_storage[BPF_CGROUP_STORAGE_PERCPU]) {
+ verbose(env, "async callback in a program with cgroup storage\n");
+ return -EINVAL;
+ }
env->subprog_info[subprog].is_async_cb = true;
async_cb = push_async_cb(env, env->subprog_info[subprog].start,
insn_idx, subprog,
diff --git a/net/core/filter.c b/net/core/filter.c
index fb403e18a5d7..ea20fd2be65c 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5866,10 +5866,16 @@ static int sol_ipv6_sockopt(struct sock *sk, int optname,
KERNEL_SOCKPTR(optval), *optlen);
}
+/* No hook has locked the socket for an async callback */
+static bool bpf_sockopt_allowed(const struct sock *sk)
+{
+ return sk && sk_fullsock(sk) && !bpf_in_async_cb();
+}
+
static int __bpf_setsockopt(struct sock *sk, int level, int optname,
char *optval, int optlen)
{
- if (!sk || !sk_fullsock(sk))
+ if (!bpf_sockopt_allowed(sk))
return -EINVAL;
if (level == SOL_SOCKET)
@@ -5892,7 +5898,7 @@ static bool is_locked_tcp_sock_ops(struct bpf_sock_ops_kern *bpf_sock)
static int _bpf_setsockopt(struct sock *sk, int level, int optname,
char *optval, int optlen)
{
- if (sk && sk_fullsock(sk))
+ if (bpf_sockopt_allowed(sk))
sock_owned_by_me(sk);
return __bpf_setsockopt(sk, level, optname, optval, optlen);
}
@@ -5902,7 +5908,7 @@ static int __bpf_getsockopt(struct sock *sk, int level, int optname,
{
int err, saved_optlen = optlen;
- if (!sk || !sk_fullsock(sk)) {
+ if (!bpf_sockopt_allowed(sk)) {
err = -EINVAL;
goto done;
}
@@ -5929,7 +5935,7 @@ static int __bpf_getsockopt(struct sock *sk, int level, int optname,
static int _bpf_getsockopt(struct sock *sk, int level, int optname,
char *optval, int optlen)
{
- if (sk && sk_fullsock(sk))
+ if (bpf_sockopt_allowed(sk))
sock_owned_by_me(sk);
return __bpf_getsockopt(sk, level, optname, optval, optlen);
}
diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
index 2ba73dd6c52c..432ca0783771 100644
--- a/net/ipv4/bpf_tcp_ops.c
+++ b/net/ipv4/bpf_tcp_ops.c
@@ -207,7 +207,7 @@ BPF_CALL_0(bpf_tcp_ops_get_retval)
* return value chained across the bpf_tcp_ops attached to the cgroup
* and is this program's saved_run_ctx.
*/
- if (WARN_ON_ONCE(!ctx->saved_run_ctx))
+ if (bpf_in_async_cb() || WARN_ON_ONCE(!ctx->saved_run_ctx))
return 0;
return container_of(ctx->saved_run_ctx, struct bpf_tramp_run_ctx,
base-commit: 3db9d99ba7b8d5a4a520f5abf785e17406f518a5
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] bpf: Set the run context for async callbacks
2026-11-26 3:31 [PATCH bpf] bpf: Set the run context for async callbacks Alexei Starovoitov
@ 2026-11-26 4:00 ` sashiko-bot
2026-11-26 4:11 ` bot+bpf-ci
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-26 4:00 UTC (permalink / raw)
To: Alexei Starovoitov, bpf
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] bpf: Set the run context for async callbacks
2026-11-26 3:31 [PATCH bpf] bpf: Set the run context for async callbacks Alexei Starovoitov
2026-11-26 4:00 ` sashiko-bot
@ 2026-11-26 4:11 ` bot+bpf-ci
1 sibling, 0 replies; 3+ messages in thread
From: bot+bpf-ci @ 2026-11-26 4:11 UTC (permalink / raw)
To: alexei.starovoitov, bpf
Cc: daniel, andrii, eddyz87, memxor, ast, andrii, daniel, martin.lau,
eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1592 bytes --]
> bpf: Set the run context for async callbacks
TL;DR: Nit only: the new comment on bpf_async_run_ctx says current is not the
arming task, but task_work or a short timer can run in the arming task.
Suggests rewording.
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
[ ... ]
> @@ -3440,6 +3440,14 @@ static inline bool has_current_bpf_ctx(void)
> return !!current->bpf_ctx;
> }
>
> +/* An async callback runs in no hook, and current is not the task that has armed it */
> +extern struct bpf_run_ctx bpf_async_run_ctx;
This isn't a bug, but is this comment accurate?
bpf_task_work_schedule_signal() and bpf_task_work_schedule_resume() accept
any trusted task, including the one returned by bpf_get_current_task_btf(),
and the task_work selftests do exactly that:
task = bpf_get_current_task_btf();
bpf_task_work_schedule_resume(task, ...);
In that case bpf_task_work_callback() runs the callback through
bpf_run_async_cb() in the same task that armed it. A bpf_timer with a
short expiry can also have its softirq run on irq exit while the arming
task is current.
The commit message says the callback runs "in a task that the program has
picked", which matches this. Would "current need not be the task that has
armed it" be a more accurate wording for the comment?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45750674148
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-26 4:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 3:31 [PATCH bpf] bpf: Set the run context for async callbacks Alexei Starovoitov
2026-11-26 4:00 ` sashiko-bot
2026-11-26 4:11 ` bot+bpf-ci
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox