mirror of
https://github.com/torvalds/linux.git
synced 2026-09-22 12:44:03 +02:00
Merge branch 'bpf-invalidate-rcu-pointers-after-final-spin-unlock'
Ning Ding says:
====================
bpf: Invalidate RCU pointers after final spin unlock
In a sleepable BPF program, a spin lock can provide the only RCU protection
for a kptr. The final spin unlock ends that protection, but the verifier
leaves the pointer valid. Another CPU can then free the object before the
pointer is used. A capability-limited runtime PoC triggered a
KASAN-confirmed task_struct use-after-free.
Patch 1 invalidates RCU-protected pointers only when an unlock leaves the
final RCU-protected context. Patch 2 adds a negative sleepable test and
positive controls for non-sleepable and explicit-RCU contexts.
Testing used fresh QEMU/KVM guests with KASAN enabled. The patched focused
test passed all three expected outcomes. The full task_kfunc test passed
all 39 subtests, and the selected RCU, refcount, and spin-lock group had no
failures.
---
v2:
- Rebase onto bpf-next commit 60781269e2.
- Target bpf-next and split the fix from its selftests, as requested.
- Add positive controls for RCU contexts that remain valid after unlock.
v1: https://lore.kernel.org/r/20260802231248.2781334-1-dingning04@gmail.com
====================
Link: https://patch.msgid.link/20260803112615.3362122-1-dingning04@gmail.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
commit
7f333f85f8
|
|
@ -206,6 +206,7 @@ static int acquire_reference(struct bpf_verifier_env *env, int insn_idx, int par
|
|||
static int release_reference_nomark(struct bpf_verifier_state *state, int id);
|
||||
static int release_reference(struct bpf_verifier_env *env, int id);
|
||||
static void invalidate_non_owning_refs(struct bpf_verifier_env *env);
|
||||
static void invalidate_rcu_protected_refs(struct bpf_verifier_env *env);
|
||||
static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env);
|
||||
static bool is_tracing_prog_type(enum bpf_prog_type type);
|
||||
static int ref_set_non_owning(struct bpf_verifier_env *env,
|
||||
|
|
@ -7165,6 +7166,7 @@ static int process_spin_lock(struct bpf_verifier_env *env, struct bpf_reg_state
|
|||
return err;
|
||||
}
|
||||
} else {
|
||||
bool was_in_rcu_cs;
|
||||
void *ptr;
|
||||
int type;
|
||||
|
||||
|
|
@ -7192,10 +7194,13 @@ static int process_spin_lock(struct bpf_verifier_env *env, struct bpf_reg_state
|
|||
verbose(env, "%s_unlock cannot be out of order\n", lock_str);
|
||||
return -EINVAL;
|
||||
}
|
||||
was_in_rcu_cs = in_rcu_cs(env);
|
||||
if (release_lock_state(cur, type, reg->id, ptr)) {
|
||||
verbose(env, "%s_unlock of different lock\n", lock_str);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (was_in_rcu_cs && !in_rcu_cs(env))
|
||||
invalidate_rcu_protected_refs(env);
|
||||
|
||||
invalidate_non_owning_refs(env);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,6 +176,8 @@ static const char * const success_tests[] = {
|
|||
"test_task_from_pid_current",
|
||||
"test_task_from_pid_invalid",
|
||||
"task_kfunc_acquire_trusted_walked",
|
||||
"task_kfunc_acquire_after_spin_unlock_non_sleepable",
|
||||
"task_kfunc_acquire_after_spin_unlock_explicit_rcu",
|
||||
"test_task_kfunc_flavor_relo",
|
||||
"test_task_kfunc_flavor_relo_not_found",
|
||||
};
|
||||
|
|
|
|||
|
|
@ -20,6 +20,18 @@ struct {
|
|||
__uint(max_entries, 1);
|
||||
} __tasks_kfunc_map SEC(".maps");
|
||||
|
||||
struct task_kptr_lock_value {
|
||||
struct bpf_spin_lock lock;
|
||||
struct task_struct __kptr * task;
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_ARRAY);
|
||||
__type(key, int);
|
||||
__type(value, struct task_kptr_lock_value);
|
||||
__uint(max_entries, 1);
|
||||
} task_kptr_lock_map SEC(".maps");
|
||||
|
||||
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
|
||||
void bpf_task_release(struct task_struct *p) __ksym;
|
||||
struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
|
||||
|
|
|
|||
|
|
@ -378,3 +378,27 @@ int BPF_PROG(task_kfunc_release_in_map, struct task_struct *task, u64 clone_flag
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
__failure __msg("R1 must be a rcu pointer")
|
||||
int BPF_PROG(task_kfunc_acquire_after_final_spin_unlock)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_spin_lock(&v->lock);
|
||||
task = v->task;
|
||||
bpf_spin_unlock(&v->lock);
|
||||
if (!task)
|
||||
return 0;
|
||||
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
#include "../bpf_experimental.h"
|
||||
#include "bpf_misc.h"
|
||||
#include "task_kfunc_common.h"
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
|
|
@ -366,6 +367,53 @@ int BPF_PROG(task_kfunc_acquire_trusted_walked, struct task_struct *task, u64 cl
|
|||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_spin_unlock_non_sleepable)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_spin_lock(&v->lock);
|
||||
task = v->task;
|
||||
bpf_spin_unlock(&v->lock);
|
||||
if (!task)
|
||||
return 0;
|
||||
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_spin_unlock_explicit_rcu)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_rcu_read_lock();
|
||||
bpf_spin_lock(&v->lock);
|
||||
task = v->task;
|
||||
bpf_spin_unlock(&v->lock);
|
||||
if (task) {
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
}
|
||||
bpf_rcu_read_unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int test_task_from_vpid_current(const void *ctx)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user