Skip to contentSkip to conversation

bpf: remove incorrect 'verifier bug' warning

PATCH In review Paul Chaignon 3 participants 6 messages Mar 20, 2019 – Mar 26, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover bpf: remove incorrect 'verifier bug' warning

The BPF verifier checks the maximum number of call stack frames twice, first in the main CFG traversal (do_check) and then in a subsequent traversal (check_max_stack_depth). If the second check fails, it logs a 'verifier bug' warning and errors out, as the number of call stack frames should have been verified already.

However, the second check may fail without indicating a verifier bug: if the excessive function calls reside in dead code, the main CFG traversal may not visit them; the subsequent traversal visits all instructions, including dead code.

This case raises the question of how invalid dead code should be treated. The first patch implements the conservative option and rejects such code; the second adds a test case.

Paul Chaignon (2):
bpf: remove incorrect 'verifier bug' warning selftests/bpf: test case for invalid call stack in dead code

 kernel/bpf/verifier.c                        |  5 +--
 tools/testing/selftests/bpf/verifier/calls.c | 38 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 bpf: remove incorrect 'verifier bug' warning

The BPF verifier checks the maximum number of call stack frames twice, first in the main CFG traversal (do_check) and then in a subsequent traversal (check_max_stack_depth). If the second check fails, it logs a 'verifier bug' warning and errors out, as the number of call stack frames should have been verified already.

However, the second check may fail without indicating a verifier bug: if the excessive function calls reside in dead code, the main CFG traversal may not visit them; the subsequent traversal visits all instructions, including dead code.

This case raises the question of how invalid dead code should be treated. This patch implements the conservative option and rejects such code.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +3 -2
index 86f9cd5d1c4e..6719bb9b332e 100644
@@ -1888,8 +1888,9 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
 		}
 		frame++;
 		if (frame >= MAX_CALL_FRAMES) {
-			WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
-			return -EFAULT;
+			verbose(env, "the call stack of %d frames is too deep !\n",
+				frame);
+			return -E2BIG;
 		}
 		goto process_func;
 	}
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/2 selftests/bpf: test case for invalid call stack in dead code

In reply to Paul Chaignon

This patch adds a test case with an excessive number of call stack frames in dead code.

---
 tools/testing/selftests/bpf/verifier/calls.c | 38 ++++++++++++++++++++
 1 file changed, 38 insertions(+)
tools/testing/selftests/bpf/verifier/calls.c +38 -0
index 4004891afa9c..ff147fcda82b 100644
@@ -907,6 +907,44 @@
 	.errstr = "call stack",
 	.result = REJECT,
 },
+{
+	"calls: stack depth check in dead code",
+	.insns = {
+	/* main */
+	BPF_MOV64_IMM(BPF_REG_1, 0),
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call A */
+	BPF_EXIT_INSN(),
+	/* A */
+	BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call B */
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	/* B */
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call C */
+	BPF_EXIT_INSN(),
+	/* C */
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call D */
+	BPF_EXIT_INSN(),
+	/* D */
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call E */
+	BPF_EXIT_INSN(),
+	/* E */
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call F */
+	BPF_EXIT_INSN(),
+	/* F */
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call G */
+	BPF_EXIT_INSN(),
+	/* G */
+	BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call H */
+	BPF_EXIT_INSN(),
+	/* H */
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_XDP,
+	.errstr = "call stack",
+	.result = REJECT,
+},
 {
 	"calls: spill into caller stack frame",
 	.insns = {
Signature · 1 line
2.17.1
#3 lore raw
In reply to Paul Chaignon
Quoted 6 lines · Paul Chaignon

The BPF verifier checks the maximum number of call stack frames twice, first in the main CFG traversal (do_check) and then in a subsequent traversal (check_max_stack_depth). If the second check fails, it logs a 'verifier bug' warning and errors out, as the number of call stack frames should have been verified already.

Quoted 6 lines

However, the second check may fail without indicating a verifier bug: if the excessive function calls reside in dead code, the main CFG traversal may not visit them; the subsequent traversal visits all instructions, including dead code.

This case raises the question of how invalid dead code should be treated.

Maybe we should do this check after dead code elimination to be consistent with do_check? There could some other kinds of illegal stuff in the dead code, e.g., illegal/unsupported helpers, etc. I suppose we did not warn or reject the program, right?

Quoted 11 lines

The first patch implements the conservative option and rejects such code; the second adds a test case.

Paul Chaignon (2):
bpf: remove incorrect 'verifier bug' warning selftests/bpf: test case for invalid call stack in dead code

  kernel/bpf/verifier.c                        |  5 +--
  tools/testing/selftests/bpf/verifier/calls.c | 38 ++++++++++++++++++++
  2 files changed, 41 insertions(+), 2 deletions(-)
#4 lore raw
Quoted 12 lines · Yonghong Song
Quoted 11 lines · Paul Chaignon

The BPF verifier checks the maximum number of call stack frames twice, first in the main CFG traversal (do_check) and then in a subsequent traversal (check_max_stack_depth). If the second check fails, it logs a 'verifier bug' warning and errors out, as the number of call stack frames should have been verified already.

However, the second check may fail without indicating a verifier bug: if the excessive function calls reside in dead code, the main CFG traversal may not visit them; the subsequent traversal visits all instructions, including dead code.

Quoted 4 lines
Quoted 1 line

This case raises the question of how invalid dead code should be treated.

Maybe we should do this check after dead code elimination to be consistent with do_check? There could some other kinds of illegal stuff

To be clear, are you suggesting we run check_max_stack_depth after the dead code elimination? That would indeed solve this issue, but Jakub made the exact reverse change not so long ago, in 9b38c40 ("bpf: verifier:
reorder stack size check with dead code sanitization"). I think the idea was to avoid having code modifications in between code checks.

Quoted 2 lines

in the dead code, e.g., illegal/unsupported helpers, etc. I suppose we did not warn or reject the program, right?

As far as I know, we do not warn or reject the programs for other illegal stuff found in dead code, no.

Quoted 12 lines
Quoted 12 lines

The first patch implements the conservative option and rejects such code; the second adds a test case.

Paul Chaignon (2):
bpf: remove incorrect 'verifier bug' warning selftests/bpf: test case for invalid call stack in dead code

  kernel/bpf/verifier.c                        |  5 +--
  tools/testing/selftests/bpf/verifier/calls.c | 38 ++++++++++++++++++++
  2 files changed, 41 insertions(+), 2 deletions(-)
#5 lore raw
Quoted 15 lines · Paul Chaignon
Quoted 14 lines · Yonghong Song
Quoted 12 lines · Paul Chaignon

The BPF verifier checks the maximum number of call stack frames twice, first in the main CFG traversal (do_check) and then in a subsequent traversal (check_max_stack_depth). If the second check fails, it logs a 'verifier bug' warning and errors out, as the number of call stack frames should have been verified already.

However, the second check may fail without indicating a verifier bug: if the excessive function calls reside in dead code, the main CFG traversal may not visit them; the subsequent traversal visits all instructions, including dead code.

This case raises the question of how invalid dead code should be treated.

Quoted 8 lines
Quoted 2 lines

Maybe we should do this check after dead code elimination to be consistent with do_check? There could some other kinds of illegal stuff

To be clear, are you suggesting we run check_max_stack_depth after the dead code elimination? That would indeed solve this issue, but Jakub made the exact reverse change not so long ago, in 9b38c40 ("bpf: verifier:
reorder stack size check with dead code sanitization"). I think the idea was to avoid having code modifications in between code checks.

I think it's fine fix as it is.
I've applied it to bpf tree, since the verifier shouldn't be warning like this.

As far as changing the order back I think it's good to keep 'too many frames' check before dead code elimination.
'too many frames' is similar to 'too many instructions'.
The verifier rejects large programs before removing dead code.
So in that sense it's similar.