BPF List
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Reject kernel threads in bpf_task_work_schedule()
@ 2026-11-26  5:30 Sechang Lim
  2026-11-26  5:40 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Sechang Lim @ 2026-11-26  5:30 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, yatsenko

bpf_task_work_schedule_signal() on a kernel thread sets
TIF_NOTIFY_SIGNAL, but the thread never returns to user space, so
nothing clears it and signal_pending() stays true. Interruptible sleeps
in the thread return immediately.

For kthreadd this means spinning at 100% CPU. io_ring_exit_work() waits
on ctx->ref_comp interruptibly, assuming a kworker gets no signals. If a
prog running in that kworker schedules task work on current, the wait
returns -ERESTARTSYS and the ctx is freed while ctx->refs is still
switching to atomic:

  WARNING: lib/percpu-refcount.c:114 at __percpu_ref_exit+0x77/0x110
  Workqueue: iou_exit io_ring_exit_work
  ...
  BUG: KASAN: slab-use-after-free in rcu_cblist_dequeue+0xd3/0x120

The callback can't run before the thread exits anyway. Reject kernel
threads with -EPERM, as bpf_send_signal() does.

Fixes: 38aa7003e369 ("bpf: task work scheduling kfuncs")
Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
---
 kernel/bpf/helpers.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index bc0dd36a2799..42f3f7599567 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4806,6 +4806,9 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
 
 	BTF_TYPE_EMIT(struct bpf_task_work);
 
+	if (task->flags & PF_KTHREAD)
+		return -EPERM;
+
 	prog = bpf_prog_inc_not_zero(aux->prog);
 	if (IS_ERR(prog))
 		return -EBADF;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf] bpf: Reject kernel threads in bpf_task_work_schedule()
  2026-11-26  5:30 [PATCH bpf] bpf: Reject kernel threads in bpf_task_work_schedule() Sechang Lim
@ 2026-11-26  5:40 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26  5:40 UTC (permalink / raw)
  To: Sechang Lim, 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: irq_work data race in bpf_task_work_cancel_and_free()
  [1]
--

commit 582f8b25ac8ecb8f5cc73a481c3b1dcd182a967f
Author: Sechang Lim <rhkrqnwk98@gmail.com>

bpf: Reject kernel threads in bpf_task_work_schedule()

This commit rejects kernel threads (PF_KTHREAD) with -EPERM in
bpf_task_work_schedule() so that TIF_NOTIFY_SIGNAL is not left set on
threads that never return to user space.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 1b95fe795b4d9..784674c5fddf9 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -4804,6 +4804,9 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
>  
>  	BTF_TYPE_EMIT(struct bpf_task_work);
>  
> +	if (task->flags & PF_KTHREAD)
> +		return -EPERM;
> +
>  	prog = bpf_prog_inc_not_zero(aux->prog);
>  	if (IS_ERR(prog))
>  		return -EBADF;

[Severity: Critical]
This problem wasn't introduced by this patch [1], but can
bpf_task_work_cancel_and_free() race with irq_work_single() and corrupt
ctx->irq_work?

When bpf_task_work_irq() finishes task_work_add(), it switches ctx->state
from BPF_TW_SCHEDULING to BPF_TW_SCHEDULED before returning to
irq_work_single().  If map deletion calls bpf_task_work_cancel_and_free()
right after that cmpxchg(), it sees BPF_TW_SCHEDULED and calls
init_irq_work() and irq_work_queue() on ctx->irq_work while
irq_work_single() is still clearing IRQ_WORK_BUSY in work->node.a_flags:

CPU0                              CPU1
irq_work_single()
  bpf_task_work_irq()
    cmpxchg(&ctx->state,
            BPF_TW_SCHEDULING,
            BPF_TW_SCHEDULED)
                                  bpf_task_work_cancel_and_free()
                                    state = xchg(&ctx->state, BPF_TW_FREED)
                                    if (state == BPF_TW_SCHEDULED)
                                      init_irq_work(&ctx->irq_work, ...)
                                      irq_work_queue(&ctx->irq_work)
  atomic_cmpxchg(&work->node.a_flags, ...)  <-- data race / flag corruption

kernel/bpf/helpers.c:bpf_task_work_cancel_and_free() {
	...
	state = xchg(&ctx->state, BPF_TW_FREED);
	if (state == BPF_TW_SCHEDULED) {
		/* run in irq_work to avoid locks in NMI */
		init_irq_work(&ctx->irq_work, bpf_task_work_cancel_scheduled);
		irq_work_queue(&ctx->irq_work);
		return;
	}
	...
}

Because init_irq_work() overwrites ctx->irq_work non-atomically and
bpf_task_work_cancel_and_free() can run in NMI context where
irq_work_sync() cannot be used, this corrupts the irq_work flags and can
cause lost cancellation or warnings and panics in the irq_work subsystem.

[1] https://sashiko.dev/#/bug/linux-a7b739e8-61a5-48f3-9ad1-75d3c31446fa

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26  5:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  5:30 [PATCH bpf] bpf: Reject kernel threads in bpf_task_work_schedule() Sechang Lim
2026-11-26  5:40 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox