Skip to contentSkip to conversation

bpf: Do not allow to load sleepable BPF_TRACE_RAW_TP program

PATCH Superseded Jiri Olsa 3 participants 5 messages Jan 16, 2023 – Jan 17, 2023

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH v3 1/2 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.

---
v3 changes:
- use switch in can_be_sleepable [Alexei]
- added acks [Song]

 kernel/bpf/verifier.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +19 -3
index fa4c911603e9..966dbfc14288 100644
@@ -16743,6 +16743,23 @@ BTF_ID(func, rcu_read_unlock_strict)
 #endif
 BTF_SET_END(btf_id_deny)
 
+static bool can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		switch (prog->expected_attach_type) {
+		case BPF_TRACE_FENTRY:
+		case BPF_TRACE_FEXIT:
+		case BPF_MODIFY_RETURN:
+		case BPF_TRACE_ITER:
+			return true;
+		default:
+			return false;
+		}
+	}
+	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 +16778,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
author reviewer
#1 lore raw

PATCH v3 2/2 bpf/selftests: Add verifier tests for loading sleepable programs

Adding verifier tests for loading all types od allowed sleepable programs plus reject for tp_btf type.

---
 .../selftests/bpf/verifier/sleepable.c        | 91 +++++++++++++++++++
 1 file changed, 91 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/verifier/sleepable.c
tools/testing/selftests/bpf/verifier/sleepable.c new file +91 -0
new file mode 100644
index 000000000000..4248c3326b89
@@ -0,0 +1,91 @@
+{
+	"sleepable fentry accept",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_TRACING,
+	.expected_attach_type = BPF_TRACE_FENTRY,
+	.kfunc = "bpf_fentry_test1",
+	.result = ACCEPT,
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
+{
+	"sleepable fexit accept",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_TRACING,
+	.expected_attach_type = BPF_TRACE_FENTRY,
+	.kfunc = "bpf_fentry_test1",
+	.result = ACCEPT,
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
+{
+	"sleepable fmod_ret accept",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_TRACING,
+	.expected_attach_type = BPF_MODIFY_RETURN,
+	.kfunc = "bpf_fentry_test1",
+	.result = ACCEPT,
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
+{
+	"sleepable iter accept",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_TRACING,
+	.expected_attach_type = BPF_TRACE_ITER,
+	.kfunc = "task",
+	.result = ACCEPT,
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
+{
+	"sleepable lsm accept",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_LSM,
+	.kfunc = "bpf",
+	.expected_attach_type = BPF_LSM_MAC,
+	.result = ACCEPT,
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
+{
+	"sleepable kprobe accept",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_KPROBE,
+	.kfunc = "bpf_fentry_test1",
+	.result = ACCEPT,
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
+{
+	"sleepable raw tracepoint reject",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_TRACING,
+	.expected_attach_type = BPF_TRACE_RAW_TP,
+	.kfunc = "sched_switch",
+	.result = REJECT,
+	.errstr = "Only fentry/fexit/fmod_ret, lsm, iter and kprobe/uprobe programs can be sleepable",
+	.flags = BPF_F_SLEEPABLE,
+	.runs = -1,
+},
Signature · 1 line
2.39.0
Acked-by on v3 1/2
#2 lore raw
In reply to Jiri Olsa
Quoted 9 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.

Ack with a minor comment below.

Quoted 37 lines

---
v3 changes:
- use switch in can_be_sleepable [Alexei]
- added acks [Song]

  kernel/bpf/verifier.c | 22 +++++++++++++++++++---
  1 file changed, 19 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +17 -0
index fa4c911603e9..966dbfc14288 100644
@@ -16743,6 +16743,23 @@ BTF_ID(func, rcu_read_unlock_strict)
  #endif
  BTF_SET_END(btf_id_deny)
 
+static bool can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		switch (prog->expected_attach_type) {
+		case BPF_TRACE_FENTRY:
+		case BPF_TRACE_FEXIT:
+		case BPF_MODIFY_RETURN:
+		case BPF_TRACE_ITER:
+			return true;
+		default:
+			return false;
+		}
+	}
+	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 16782

Quoted 8 lines
kernel/bpf/verifier.c +2 -3
@@ -16761,9 +16778,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");

actually kprobe programs cannot be sleepable. See kernel/events/core.c. perf_event_set_bpf_prog(...)
...

        if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable 
&& !is_uprobe)
                /* only uprobe programs are allowed to be sleepable */
                return -EINVAL;

So I suggest to add a comment and remove the above 'kprobe' from error message.

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
  		return -EINVAL;
  	}
 
#3 lore raw
Quoted 65 lines · Yonghong Song
Quoted 9 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.

Ack with a minor comment below.

Quoted 36 lines

---
v3 changes:
- use switch in can_be_sleepable [Alexei]
- added acks [Song]

  kernel/bpf/verifier.c | 22 +++++++++++++++++++---
  1 file changed, 19 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +17 -0
index fa4c911603e9..966dbfc14288 100644
@@ -16743,6 +16743,23 @@ BTF_ID(func, rcu_read_unlock_strict)
  #endif
  BTF_SET_END(btf_id_deny)
+static bool can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		switch (prog->expected_attach_type) {
+		case BPF_TRACE_FENTRY:
+		case BPF_TRACE_FEXIT:
+		case BPF_MODIFY_RETURN:
+		case BPF_TRACE_ITER:
+			return true;
+		default:
+			return false;
+		}
+	}
+	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 16781

Quoted 7 lines
kernel/bpf/verifier.c +2 -3
@@ -16761,9 +16778,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");

actually kprobe programs cannot be sleepable. See kernel/events/core.c. perf_event_set_bpf_prog(...)
...

Quoted 7 lines
        if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable &&
!is_uprobe)
                /* only uprobe programs are allowed to be sleepable */
                return -EINVAL;

So I suggest to add a comment and remove the above 'kprobe' from error message.

ok, is comment below ok?

jirka

---

kernel/bpf/verifier.c +19 -3
index fa4c911603e9..ca7db2ce70b9 100644
@@ -16743,6 +16743,23 @@ BTF_ID(func, rcu_read_unlock_strict)
 #endif
 BTF_SET_END(btf_id_deny)
 
+static bool can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		switch (prog->expected_attach_type) {
+		case BPF_TRACE_FENTRY:
+		case BPF_TRACE_FEXIT:
+		case BPF_MODIFY_RETURN:
+		case BPF_TRACE_ITER:
+			return true;
+		default:
+			return false;
+		}
+	}
+	return prog->type == BPF_PROG_TYPE_LSM ||
+	       prog->type == BPF_PROG_TYPE_KPROBE; /* only for uprobes */
+}
+
 static int check_attach_btf_id(struct bpf_verifier_env *env)
 {
 	struct bpf_prog *prog = env->prog;
@@ -16761,9 +16778,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 uprobe programs can be sleepable\n");
 		return -EINVAL;
 	}
 
#4 lore raw
Quoted 71 lines · Jiri Olsa
Quoted 70 lines · Yonghong Song
Quoted 9 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.

Ack with a minor comment below.

Quoted 36 lines

---
v3 changes:
- use switch in can_be_sleepable [Alexei]
- added acks [Song]

   kernel/bpf/verifier.c | 22 +++++++++++++++++++---
   1 file changed, 19 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +17 -0
index fa4c911603e9..966dbfc14288 100644
@@ -16743,6 +16743,23 @@ BTF_ID(func, rcu_read_unlock_strict)
   #endif
   BTF_SET_END(btf_id_deny)
+static bool can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		switch (prog->expected_attach_type) {
+		case BPF_TRACE_FENTRY:
+		case BPF_TRACE_FEXIT:
+		case BPF_MODIFY_RETURN:
+		case BPF_TRACE_ITER:
+			return true;
+		default:
+			return false;
+		}
+	}
+	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 16781

Quoted 7 lines
kernel/bpf/verifier.c +2 -3
@@ -16761,9 +16778,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");

actually kprobe programs cannot be sleepable. See kernel/events/core.c. perf_event_set_bpf_prog(...)
...

         if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable &&
!is_uprobe)
                 /* only uprobe programs are allowed to be sleepable */
                 return -EINVAL;
Quoted 4 lines
Quoted 2 lines

So I suggest to add a comment and remove the above 'kprobe' from error message.

ok, is comment below ok?

Thanks! Sounds good to me.

Quoted 45 lines

jirka

---

kernel/bpf/verifier.c +19 -3
index fa4c911603e9..ca7db2ce70b9 100644
@@ -16743,6 +16743,23 @@ BTF_ID(func, rcu_read_unlock_strict)
  #endif
  BTF_SET_END(btf_id_deny)
 
+static bool can_be_sleepable(struct bpf_prog *prog)
+{
+	if (prog->type == BPF_PROG_TYPE_TRACING) {
+		switch (prog->expected_attach_type) {
+		case BPF_TRACE_FENTRY:
+		case BPF_TRACE_FEXIT:
+		case BPF_MODIFY_RETURN:
+		case BPF_TRACE_ITER:
+			return true;
+		default:
+			return false;
+		}
+	}
+	return prog->type == BPF_PROG_TYPE_LSM ||
+	       prog->type == BPF_PROG_TYPE_KPROBE; /* only for uprobes */
+}
+
  static int check_attach_btf_id(struct bpf_verifier_env *env)
  {
  	struct bpf_prog *prog = env->prog;
@@ -16761,9 +16778,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 uprobe programs can be sleepable\n");
  		return -EINVAL;
  	}