Skip to content

Commit 19e2dbb

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: improve stacksafe state comparison
"if (old->allocated_stack > cur->allocated_stack)" check is too conservative. In some cases explored stack could have allocated more space, but that stack space was not live. The test case improves from 19 to 15 processed insns and improvement on real programs is significant as well: before after bpf_lb-DLB_L3.o 1940 1831 bpf_lb-DLB_L4.o 3089 3029 bpf_lb-DUNKNOWN.o 1065 1064 bpf_lxc-DDROP_ALL.o 28052 26309 bpf_lxc-DUNKNOWN.o 35487 33517 bpf_netdev.o 10864 9713 bpf_overlay.o 6643 6184 bpf_lcx_jit.o 38437 37335 Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Edward Cree <ecree@solarflare.com> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 730ff40 commit 19e2dbb

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

kernel/bpf/verifier.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5191,12 +5191,6 @@ static bool stacksafe(struct bpf_func_state *old,
51915191
{
51925192
int i, spi;
51935193

5194-
/* if explored stack has more populated slots than current stack
5195-
* such stacks are not equivalent
5196-
*/
5197-
if (old->allocated_stack > cur->allocated_stack)
5198-
return false;
5199-
52005194
/* walk slots of the explored stack and ignore any additional
52015195
* slots in the current stack, since explored(safe) state
52025196
* didn't use them
@@ -5212,6 +5206,13 @@ static bool stacksafe(struct bpf_func_state *old,
52125206

52135207
if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
52145208
continue;
5209+
5210+
/* explored stack has more populated slots than current stack
5211+
* and these slots were used
5212+
*/
5213+
if (i >= cur->allocated_stack)
5214+
return false;
5215+
52155216
/* if old state was safe with misc data in the stack
52165217
* it will be safe with zero-initialized stack.
52175218
* The opposite is not true

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13647,6 +13647,28 @@ static struct bpf_test tests[] = {
1364713647
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
1364813648
.result = ACCEPT,
1364913649
},
13650+
{
13651+
"allocated_stack",
13652+
.insns = {
13653+
BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
13654+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
13655+
BPF_ALU64_REG(BPF_MOV, BPF_REG_7, BPF_REG_0),
13656+
BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
13657+
BPF_MOV64_IMM(BPF_REG_0, 0),
13658+
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, -8),
13659+
BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, -8),
13660+
BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_7, -9),
13661+
BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_10, -9),
13662+
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0),
13663+
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0),
13664+
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0),
13665+
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0),
13666+
BPF_EXIT_INSN(),
13667+
},
13668+
.result = ACCEPT,
13669+
.result_unpriv = ACCEPT,
13670+
.insn_processed = 15,
13671+
},
1365013672
{
1365113673
"reference tracking in call: free reference in subprog and outside",
1365213674
.insns = {

0 commit comments

Comments
 (0)