Skip to contentSkip to conversation

bpf: Do not allow to load sleepable BPF_TRACE_RAW_TP program

PATCH Superseded Jiri Olsa 3 participants 3 messages Jan 9, 2023

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 bpf: Do not allow to load sleepable BPF_TRACE_RAW_TP program

Currently we allow to load any tracing program as sleepable, but BPF_TRACE_RAW_TP can't sleep. Making the check explicit for tracing programs attach types, so sleepable BPF_TRACE_RAW_TP will fail to load.

Updating the verifier error to mention iter programs as well.

---
 kernel/bpf/verifier.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +14 -3
index fa4c911603e9..121a64ee841a 100644
@@ -16743,6 +16743,18 @@ BTF_ID(func, rcu_read_unlock_strict)
 #endif
 BTF_SET_END(btf_id_deny)
 
+static int can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		return prog->expected_attach_type == BPF_TRACE_FENTRY ||
+		       prog->expected_attach_type == BPF_TRACE_FEXIT ||
+		       prog->expected_attach_type == BPF_MODIFY_RETURN ||
+		       prog->expected_attach_type == BPF_TRACE_ITER;
+	}
+	return prog->type == BPF_PROG_TYPE_LSM ||
+	       prog->type == BPF_PROG_TYPE_KPROBE;
+}
+
 static int check_attach_btf_id(struct bpf_verifier_env *env)
 {
 	struct bpf_prog *prog = env->prog;
@@ -16761,9 +16773,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 		return -EINVAL;
 	}
 
-	if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING &&
-	    prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n");
+	if (prog->aux->sleepable && !can_be_sleepable(prog)) {
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter and kprobe/uprobe programs can be sleepable\n");
 		return -EINVAL;
 	}
 
Signature · 1 line
2.39.0
reviewer
#1 lore raw
Quoted 18 lines · Jiri Olsa

Currently we allow to load any tracing program as sleepable, but BPF_TRACE_RAW_TP can't sleep. Making the check explicit for tracing programs attach types, so sleepable BPF_TRACE_RAW_TP will fail to load.

Updating the verifier error to mention iter programs as well.

---
 kernel/bpf/verifier.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +0 -0
index fa4c911603e9..121a64ee841a 100644

kernel/bpf/verifier.c BTF_ID(func, rcu_read_unlock_strict) · line 16746

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -16743,6 +16743,18 @@ BTF_ID(func, rcu_read_unlock_strict)
 #endif
 BTF_SET_END(btf_id_deny)
 
+static int can_be_sleepable(struct bpf_prog *prog)

Shall we return bool?

Quoted 15 lines
kernel/bpf/verifier.c +11 -0
+{
+       if (prog->type == BPF_PROG_TYPE_TRACING) {
+               return prog->expected_attach_type == BPF_TRACE_FENTRY ||
+                      prog->expected_attach_type == BPF_TRACE_FEXIT ||
+                      prog->expected_attach_type == BPF_MODIFY_RETURN ||
+                      prog->expected_attach_type == BPF_TRACE_ITER;
+       }
+       return prog->type == BPF_PROG_TYPE_LSM ||
+              prog->type == BPF_PROG_TYPE_KPROBE;
+}
+
 static int check_attach_btf_id(struct bpf_verifier_env *env)
 {
        struct bpf_prog *prog = env->prog;

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 16779

Quoted 10 lines
kernel/bpf/verifier.c +2 -3
@@ -16761,9 +16773,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
                return -EINVAL;
        }
 
-       if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING &&
-           prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) {
-               verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n");
+       if (prog->aux->sleepable && !can_be_sleepable(prog)) {
+               verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter and kprobe/uprobe programs can be sleepable\n");
                return -EINVAL;
        }

Maybe add a verifier test for this?

Thanks,
Song

Quoted 4 lines
kernel/bpf/verifier.c +0 -1
--

2.39.0

#2 lore raw
Quoted 22 lines · Song Liu
Quoted 21 lines · Jiri Olsa

Currently we allow to load any tracing program as sleepable, but BPF_TRACE_RAW_TP can't sleep. Making the check explicit for tracing programs attach types, so sleepable BPF_TRACE_RAW_TP will fail to load.

Updating the verifier error to mention iter programs as well.

---
 kernel/bpf/verifier.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +0 -0
index fa4c911603e9..121a64ee841a 100644
@@ -16743,6 +16743,18 @@ BTF_ID(func, rcu_read_unlock_strict)
 #endif
 BTF_SET_END(btf_id_deny)
 
Quoted 3 lines

kernel/bpf/verifier.c BTF_ID(func, rcu_read_unlock_strict) · line 16746

Quoted 1 line
kernel/bpf/verifier.c +1 -0
+static int can_be_sleepable(struct bpf_prog *prog)

Shall we return bool?

ok

Quoted 19 lines
Quoted 19 lines
kernel/bpf/verifier.c +11 -0
+{
+       if (prog->type == BPF_PROG_TYPE_TRACING) {
+               return prog->expected_attach_type == BPF_TRACE_FENTRY ||
+                      prog->expected_attach_type == BPF_TRACE_FEXIT ||
+                      prog->expected_attach_type == BPF_MODIFY_RETURN ||
+                      prog->expected_attach_type == BPF_TRACE_ITER;
+       }
+       return prog->type == BPF_PROG_TYPE_LSM ||
+              prog->type == BPF_PROG_TYPE_KPROBE;
+}
+
 static int check_attach_btf_id(struct bpf_verifier_env *env)
 {
        struct bpf_prog *prog = env->prog;
@@ -16761,9 +16773,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
                return -EINVAL;
        }
 
Quoted 9 lines

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 16779

Quoted 7 lines
kernel/bpf/verifier.c +2 -3
-       if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING &&
-           prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) {
-               verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n");
+       if (prog->aux->sleepable && !can_be_sleepable(prog)) {
+               verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter and kprobe/uprobe programs can be sleepable\n");
                return -EINVAL;
        }

Maybe add a verifier test for this?

ok, will add

thanks,
jirka