Skip to contentSkip to conversation

bpf: move security_bpf_prog_free() out of RCU callback

PATCH In review Sechang Lim 3 participants 4 messages Jun 26 – Jul 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: move security_bpf_prog_free() out of RCU callback

__bpf_prog_put_rcu() is the call_rcu() callback for non-sleepable programs. security_bpf_prog_free() called from there fires bpf_prog_free in softirq; if a sleepable LSM prog is attached to that hook, might_fault() BUGs:

  BUG: sleeping function called from invalid context
  in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 5038
  preempt_count: 101, expected: 0
  Call Trace:
   <IRQ>
   __bpf_prog_enter_sleepable+0x1cd/0x320 kernel/bpf/trampoline.c:1255
   bpf_trampoline_6442549705+0x53/0xd7
   security_bpf_prog_free+0xde/0x130 security/security.c:5465
   __bpf_prog_put_rcu+0xab/0xd0 kernel/bpf/syscall.c:2365
   rcu_do_batch kernel/rcu/tree.c:2617 [inline]
   handle_softirqs+0x236/0x800 kernel/softirq.c:622
   </IRQ>

The call_rcu/call_rcu_tasks_trace split reflects the freed program's sleepability, not that of any attached observer.

Move security_bpf_prog_free() to __bpf_prog_put_noref() before the RCU deferral.

Fixes 1b67772e4e3f ("bpf,lsm: Refactor bpf_prog_alloc/bpf_prog_free LSM hooks")
---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
index 630d530782fe..f14c3f0f8827 100644
@@ -2362,7 +2362,6 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
 	kvfree(aux->func_info);
 	kfree(aux->func_info_aux);
 	free_uid(aux->user);
-	security_bpf_prog_free(aux->prog);
 	bpf_prog_free(aux->prog);
 }
 
@@ -2378,6 +2377,7 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 	if (prog->aux->attach_btf)
 		btf_put(prog->aux->attach_btf);
 
+	security_bpf_prog_free(prog);
 	if (deferred) {
 		if (prog->sleepable)
 			call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch fails to fix the BUG: sleeping function called from invalid context because security_bpf_prog_free() can still execute synchronously in softirq contexts.
- [High] Use-After-Free on prog->aux fields when traversing the bpf_prog structure in the bpf_prog_free LSM hook.
--

commit ce00646a70b27dcf100e6bb9652d3943185fcac5
Author: Sechang Lim <[email protected]>

bpf: move security_bpf_prog_free() out of RCU callback

This commit moves security_bpf_prog_free() from __bpf_prog_put_rcu() to __bpf_prog_put_noref() before the RCU deferral. This change is intended to prevent a BUG caused by sleeping LSM BPF programs executing in atomic context when attached to the bpf_prog_free hook.

Quoted 4 lines
kernel/bpf/syscall.c +0 -0
index 6db306d23b479..6d9a662241159 100644

[ ... ]

kernel/bpf/syscall.c static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) · line 2459

Quoted 5 lines
kernel/bpf/syscall.c +1 -0
@@ -2457,6 +2456,7 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 	if (prog->aux->attach_btf)
 		btf_put(prog->aux->attach_btf);
 
+	security_bpf_prog_free(prog);

[Severity: High]
This is a pre-existing issue, but does moving this hook here expose a use-after-free read if an attached LSM BPF program traverses the bpf_prog structure?

The hook is invoked after several dynamically allocated fields in prog->aux (such as linfo, jited_linfo, and kfunc_tab) have been freed earlier in __bpf_prog_put_noref() via kvfree() and kfree(). An LSM BPF program attached to bpf_prog_free could read these dangling pointers.

[Severity: High]
This isn't a bug introduced by this patch, but will this placement actually resolve the "sleeping function called from invalid context" BUG?

If a program refcount is dropped from an RCU softirq path, such as in __dev_map_entry_free() in kernel/bpf/devmap.c, it appears we still reach this hook synchronously in atomic context:

kernel/bpf/syscall.c:__bpf_prog_put() {
    ...
	if (in_hardirq() || irqs_disabled()) {
		INIT_WORK(&aux->work, bpf_prog_put_deferred);
		schedule_work(&aux->work);
	} else {
		bpf_prog_put_deferred(&aux->work);
	}
}

Because in_hardirq() and irqs_disabled() are both false in softirq context, bpf_prog_put_deferred() and consequently __bpf_prog_put_noref() will execute synchronously, firing the LSM hook and hitting the same BUG.

Quoted 3 lines
kernel/bpf/syscall.c +0 -0
 	if (deferred) {
 		if (prog->sleepable)
 			call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Sechang Lim
Quoted 42 lines · Sechang Lim

__bpf_prog_put_rcu() is the call_rcu() callback for non-sleepable programs. security_bpf_prog_free() called from there fires bpf_prog_free in softirq; if a sleepable LSM prog is attached to that hook, might_fault() BUGs:

  BUG: sleeping function called from invalid context
  in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 5038
  preempt_count: 101, expected: 0
  Call Trace:
   <IRQ>
   __bpf_prog_enter_sleepable+0x1cd/0x320 kernel/bpf/trampoline.c:1255
   bpf_trampoline_6442549705+0x53/0xd7
   security_bpf_prog_free+0xde/0x130 security/security.c:5465
   __bpf_prog_put_rcu+0xab/0xd0 kernel/bpf/syscall.c:2365
   rcu_do_batch kernel/rcu/tree.c:2617 [inline]
   handle_softirqs+0x236/0x800 kernel/softirq.c:622
   </IRQ>

The call_rcu/call_rcu_tasks_trace split reflects the freed program's sleepability, not that of any attached observer.

Move security_bpf_prog_free() to __bpf_prog_put_noref() before the RCU deferral.

Fixes 1b67772e4e3f ("bpf,lsm: Refactor bpf_prog_alloc/bpf_prog_free LSM hooks")
---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +0 -1
index 630d530782fe..f14c3f0f8827 100644
@@ -2362,7 +2362,6 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
 	kvfree(aux->func_info);
 	kfree(aux->func_info_aux);
 	free_uid(aux->user);
-	security_bpf_prog_free(aux->prog);
 	bpf_prog_free(aux->prog);
 }
 

kernel/bpf/syscall.c static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) · line 2380

Quoted 4 lines
kernel/bpf/syscall.c +1 -0
@@ -2378,6 +2377,7 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 	if (prog->aux->attach_btf)
 		btf_put(prog->aux->attach_btf);
 
+	security_bpf_prog_free(prog);

I don't think you can just move it like that, since LSM side may rely on RCU GP.
I think removing security_bpf_prog_free from sleepable is cleaner.

pw-bot: cr

#3 lore raw
Quoted 46 lines · Alexei Starovoitov
Quoted 45 lines · Sechang Lim

__bpf_prog_put_rcu() is the call_rcu() callback for non-sleepable programs. security_bpf_prog_free() called from there fires bpf_prog_free in softirq; if a sleepable LSM prog is attached to that hook, might_fault() BUGs:

  BUG: sleeping function called from invalid context
  in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 5038
  preempt_count: 101, expected: 0
  Call Trace:
   <IRQ>
   __bpf_prog_enter_sleepable+0x1cd/0x320 kernel/bpf/trampoline.c:1255
   bpf_trampoline_6442549705+0x53/0xd7
   security_bpf_prog_free+0xde/0x130 security/security.c:5465
   __bpf_prog_put_rcu+0xab/0xd0 kernel/bpf/syscall.c:2365
   rcu_do_batch kernel/rcu/tree.c:2617 [inline]
   handle_softirqs+0x236/0x800 kernel/softirq.c:622
   </IRQ>

The call_rcu/call_rcu_tasks_trace split reflects the freed program's sleepability, not that of any attached observer.

Move security_bpf_prog_free() to __bpf_prog_put_noref() before the RCU deferral.

Fixes 1b67772e4e3f ("bpf,lsm: Refactor bpf_prog_alloc/bpf_prog_free LSM hooks")
---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +0 -1
index 630d530782fe..f14c3f0f8827 100644
@@ -2362,7 +2362,6 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
 	kvfree(aux->func_info);
 	kfree(aux->func_info_aux);
 	free_uid(aux->user);
-	security_bpf_prog_free(aux->prog);
 	bpf_prog_free(aux->prog);
 }
 
@@ -2378,6 +2377,7 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 	if (prog->aux->attach_btf)
 		btf_put(prog->aux->attach_btf);
 
Quoted 6 lines

kernel/bpf/syscall.c static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) · line 2380

Quoted 1 line
kernel/bpf/syscall.c +1 -0
+	security_bpf_prog_free(prog);

I don't think you can just move it like that, since LSM side may rely on RCU GP.
I think removing security_bpf_prog_free from sleepable is cleaner.

Ah, that sounds right. Will fix in v2.
Thanks!

Bests,
Sechang