mirror of
https://github.com/torvalds/linux.git
synced 2026-09-23 05:04:02 +02:00
Merge branch 'bpf-track-overlapping-rcu-protection'
Ning Ding says: ==================== bpf: Track overlapping RCU protection Preemption-disabled and IRQ-disabled regions provide RCU protection, but the verifier does not account for them. Current implementation can invalidate a task kptr while another RCU source remains active, or keep it valid after the final source ends. Track these regions and invalidate RCU-protected pointers only after the last protection ends. Add task kptr tests for overlapping protection and final-exit rejection. This follows review of the applied spin-unlock fix series [1]. Tested in QEMU/KVM: ./test_progs -t task_kfunc ./test_progs -t preempt_lock ./test_progs -t irq [1] https://lore.kernel.org/r/20260803112615.3362122-1-dingning04@gmail.com ==================== Link: https://patch.msgid.link/20260805233940.3966981-1-dingning04@gmail.com Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
commit
8c7f55d60a
|
|
@ -4452,7 +4452,9 @@ static bool in_sleepable(struct bpf_verifier_env *env)
|
|||
static bool in_rcu_cs(struct bpf_verifier_env *env)
|
||||
{
|
||||
return env->cur_state->active_rcu_locks ||
|
||||
env->cur_state->active_preempt_locks ||
|
||||
env->cur_state->active_locks ||
|
||||
env->cur_state->active_irq_id ||
|
||||
!in_sleepable(env);
|
||||
}
|
||||
|
||||
|
|
@ -7166,7 +7168,6 @@ 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;
|
||||
|
||||
|
|
@ -7194,12 +7195,11 @@ 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))
|
||||
if (!in_rcu_cs(env))
|
||||
invalidate_rcu_protected_refs(env);
|
||||
|
||||
invalidate_non_owning_refs(env);
|
||||
|
|
@ -11663,6 +11663,9 @@ static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *
|
|||
err = unmark_stack_slot_irq_flag(env, reg, kfunc_class);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if (!in_rcu_cs(env))
|
||||
invalidate_rcu_protected_refs(env);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -13159,7 +13162,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
|
|||
verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (--env->cur_state->active_rcu_locks == 0)
|
||||
env->cur_state->active_rcu_locks--;
|
||||
if (!in_rcu_cs(env))
|
||||
invalidate_rcu_protected_refs(env);
|
||||
} else if (preempt_disable) {
|
||||
env->cur_state->active_preempt_locks++;
|
||||
|
|
@ -13169,6 +13173,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
|
|||
return -EINVAL;
|
||||
}
|
||||
env->cur_state->active_preempt_locks--;
|
||||
if (!in_rcu_cs(env))
|
||||
invalidate_rcu_protected_refs(env);
|
||||
}
|
||||
|
||||
if (sleepable && !in_sleepable_context(env)) {
|
||||
|
|
|
|||
|
|
@ -178,6 +178,12 @@ static const char * const success_tests[] = {
|
|||
"task_kfunc_acquire_trusted_walked",
|
||||
"task_kfunc_acquire_after_spin_unlock_non_sleepable",
|
||||
"task_kfunc_acquire_after_spin_unlock_explicit_rcu",
|
||||
"task_kfunc_acquire_after_spin_unlock_preempt_disabled",
|
||||
"task_kfunc_acquire_after_spin_unlock_irq_disabled",
|
||||
"task_kfunc_acquire_after_rcu_unlock_preempt_disabled",
|
||||
"task_kfunc_acquire_after_rcu_unlock_irq_disabled",
|
||||
"task_kfunc_acquire_after_preempt_enable_explicit_rcu",
|
||||
"task_kfunc_acquire_after_irq_restore_explicit_rcu",
|
||||
"test_task_kfunc_flavor_relo",
|
||||
"test_task_kfunc_flavor_relo_not_found",
|
||||
};
|
||||
|
|
|
|||
|
|
@ -116,9 +116,9 @@ int BPF_PROG(test_cpumask_null, struct task_struct *task, u64 clone_flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SEC("tp_btf/task_newtask")
|
||||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
__failure __msg("R2 must be a rcu pointer")
|
||||
int BPF_PROG(test_global_mask_out_of_rcu, struct task_struct *task, u64 clone_flags)
|
||||
int BPF_PROG(test_global_mask_out_of_rcu)
|
||||
{
|
||||
struct bpf_cpumask *local, *prev;
|
||||
|
||||
|
|
@ -133,6 +133,10 @@ int BPF_PROG(test_global_mask_out_of_rcu, struct task_struct *task, u64 clone_fl
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Use a sleepable program so explicit RCU is the only source of RCU
|
||||
* protection.
|
||||
*/
|
||||
bpf_rcu_read_lock();
|
||||
local = global_mask;
|
||||
if (!local) {
|
||||
|
|
|
|||
|
|
@ -38,6 +38,8 @@ struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
|
|||
struct task_struct *bpf_task_from_vpid(s32 vpid) __ksym;
|
||||
void bpf_rcu_read_lock(void) __ksym;
|
||||
void bpf_rcu_read_unlock(void) __ksym;
|
||||
void bpf_local_irq_save(unsigned long *flags) __weak __ksym;
|
||||
void bpf_local_irq_restore(unsigned long *flags) __weak __ksym;
|
||||
|
||||
static inline struct __tasks_kfunc_map_value *tasks_kfunc_map_value_lookup(struct task_struct *p)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -402,3 +402,52 @@ int BPF_PROG(task_kfunc_acquire_after_final_spin_unlock)
|
|||
bpf_task_release(acquired);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
__failure __msg("R1 must be a rcu pointer")
|
||||
int BPF_PROG(task_kfunc_acquire_after_preempt_enable)
|
||||
{
|
||||
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_preempt_disable();
|
||||
task = v->task;
|
||||
bpf_preempt_enable();
|
||||
if (!task)
|
||||
return 0;
|
||||
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
__failure __msg("R1 must be a rcu pointer")
|
||||
int BPF_PROG(task_kfunc_acquire_after_irq_restore)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
unsigned long flags;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_local_irq_save(&flags);
|
||||
task = v->task;
|
||||
bpf_local_irq_restore(&flags);
|
||||
if (!task)
|
||||
return 0;
|
||||
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -414,6 +414,153 @@ int BPF_PROG(task_kfunc_acquire_after_spin_unlock_explicit_rcu)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_spin_unlock_preempt_disabled)
|
||||
{
|
||||
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_preempt_disable();
|
||||
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_preempt_enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_spin_unlock_irq_disabled)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
unsigned long flags;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_local_irq_save(&flags);
|
||||
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_local_irq_restore(&flags);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_rcu_unlock_preempt_disabled)
|
||||
{
|
||||
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_preempt_disable();
|
||||
bpf_rcu_read_lock();
|
||||
task = v->task;
|
||||
bpf_rcu_read_unlock();
|
||||
if (task) {
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
}
|
||||
bpf_preempt_enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_rcu_unlock_irq_disabled)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
unsigned long flags;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_local_irq_save(&flags);
|
||||
bpf_rcu_read_lock();
|
||||
task = v->task;
|
||||
bpf_rcu_read_unlock();
|
||||
if (task) {
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
}
|
||||
bpf_local_irq_restore(&flags);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_preempt_enable_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_preempt_disable();
|
||||
task = v->task;
|
||||
bpf_rcu_read_lock();
|
||||
bpf_preempt_enable();
|
||||
if (task) {
|
||||
acquired = bpf_task_acquire(task);
|
||||
if (acquired)
|
||||
bpf_task_release(acquired);
|
||||
}
|
||||
bpf_rcu_read_unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
|
||||
int BPF_PROG(task_kfunc_acquire_after_irq_restore_explicit_rcu)
|
||||
{
|
||||
struct task_kptr_lock_value *v;
|
||||
struct task_struct *task, *acquired;
|
||||
unsigned long flags;
|
||||
int key = 0;
|
||||
|
||||
v = bpf_map_lookup_elem(&task_kptr_lock_map, &key);
|
||||
if (!v)
|
||||
return 0;
|
||||
|
||||
bpf_local_irq_save(&flags);
|
||||
task = v->task;
|
||||
bpf_rcu_read_lock();
|
||||
bpf_local_irq_restore(&flags);
|
||||
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