Skip to content

Commit 3091b66

Browse files
kchuyizhouAlexei Starovoitov
authored andcommitted
bpf: Relax allowlist for css_task iter
The newly added open-coded css_task iter would try to hold the global css_set_lock in bpf_iter_css_task_new, so the bpf side has to be careful in where it allows to use this iter. The mainly concern is dead locking on css_set_lock. check_css_task_iter_allowlist() in verifier enforced css_task can only be used in bpf_lsm hooks and sleepable bpf_iter. This patch relax the allowlist for css_task iter. Any lsm and any iter (even non-sleepable) and any sleepable are safe since they would not hold the css_set_lock before entering BPF progs context. This patch also fixes the misused BPF_TRACE_ITER in check_css_task_iter_allowlist which compared bpf_prog_type with bpf_attach_type. Fixes: 9c66dc9 ("bpf: Introduce css_task open-coded iterator kfuncs") Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com> Acked-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20231031050438.93297-2-zhouchuyi@bytedance.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 9af3775 commit 3091b66

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

kernel/bpf/verifier.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11402,17 +11402,25 @@ static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env,
1140211402
&meta->arg_rbtree_root.field);
1140311403
}
1140411404

11405+
/*
11406+
* css_task iter allowlist is needed to avoid dead locking on css_set_lock.
11407+
* LSM hooks and iters (both sleepable and non-sleepable) are safe.
11408+
* Any sleepable progs are also safe since bpf_check_attach_target() enforce
11409+
* them can only be attached to some specific hook points.
11410+
*/
1140511411
static bool check_css_task_iter_allowlist(struct bpf_verifier_env *env)
1140611412
{
1140711413
enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
1140811414

1140911415
switch (prog_type) {
1141011416
case BPF_PROG_TYPE_LSM:
1141111417
return true;
11412-
case BPF_TRACE_ITER:
11413-
return env->prog->aux->sleepable;
11418+
case BPF_PROG_TYPE_TRACING:
11419+
if (env->prog->expected_attach_type == BPF_TRACE_ITER)
11420+
return true;
11421+
fallthrough;
1141411422
default:
11415-
return false;
11423+
return env->prog->aux->sleepable;
1141611424
}
1141711425
}
1141811426

@@ -11671,7 +11679,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1167111679
case KF_ARG_PTR_TO_ITER:
1167211680
if (meta->func_id == special_kfunc_list[KF_bpf_iter_css_task_new]) {
1167311681
if (!check_css_task_iter_allowlist(env)) {
11674-
verbose(env, "css_task_iter is only allowed in bpf_lsm and bpf iter-s\n");
11682+
verbose(env, "css_task_iter is only allowed in bpf_lsm, bpf_iter and sleepable progs\n");
1167511683
return -EINVAL;
1167611684
}
1167711685
}

tools/testing/selftests/bpf/progs/iters_task_failure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ int BPF_PROG(iter_css_lock_and_unlock)
8484
return 0;
8585
}
8686

87-
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
88-
__failure __msg("css_task_iter is only allowed in bpf_lsm and bpf iter-s")
87+
SEC("?fentry/" SYS_PREFIX "sys_getpgid")
88+
__failure __msg("css_task_iter is only allowed in bpf_lsm, bpf_iter and sleepable progs")
8989
int BPF_PROG(iter_css_task_for_each)
9090
{
9191
u64 cg_id = bpf_get_current_cgroup_id();

0 commit comments

Comments
 (0)