selftests/bpf: Test overlapping RCU protection

Add task kptr tests that keep RCU protection active after a spin or RCU
unlock when preemption or IRQs remain disabled.

Also test the reverse order with explicit RCU. Verify that task kptrs are
rejected after leaving the final preemption-disabled or IRQ-disabled
region.

Signed-off-by: Ning Ding <dingning04@gmail.com>
Link: https://lore.kernel.org/bpf/20260805233940.3966981-3-dingning04@gmail.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
Ning Ding 2026-08-05 16:39:34 -07:00 committed by Kumar Kartikeya Dwivedi
parent d65739bf93
commit a7f62506df
4 changed files with 204 additions and 0 deletions

View File

@ -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",
};

View File

@ -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)
{

View File

@ -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;
}

View File

@ -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)
{