mirror of
https://github.com/torvalds/linux.git
synced 2026-09-22 04:34:03 +02:00
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 bpf_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
task_struct use-after-free in __bpf_get_task_stack().
Record whether the program is in an RCU-protected context before releasing
the lock. Invalidate RCU-protected pointers only when the unlock leaves the
final such context. This preserves valid pointers in non-sleepable programs
and inside an explicit RCU read-side section.
Fixes: 5861d1e8db ("bpf: Allow bpf_spin_{lock,unlock} in sleepable progs")
Assisted-by: Codex:gpt-5.6-sol
Assisted-by: ChatGPT:GPT-5.6-Pro
Signed-off-by: Ning Ding <dingning04@gmail.com>
Link: https://lore.kernel.org/bpf/20260803112615.3362122-2-dingning04@gmail.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
parent
457d4ecb47
commit
180c700071
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user