Skip to content

Commit 0bae2d4

Browse files
Jiong WangAlexei Starovoitov
authored andcommitted
bpf: correct slot_type marking logic to allow more stack slot sharing
Verifier is supposed to support sharing stack slot allocated to ptr with SCALAR_VALUE for privileged program. However this doesn't happen for some cases. The reason is verifier is not clearing slot_type STACK_SPILL for all bytes, it only clears part of them, while verifier is using: slot_type[0] == STACK_SPILL as a convention to check one slot is ptr type. So, the consequence of partial clearing slot_type is verifier could treat a partially overridden ptr slot, which should now be a SCALAR_VALUE slot, still as ptr slot, and rejects some valid programs. Before this patch, test_xdp_noinline.o under bpf selftests, bpf_lxc.o and bpf_netdev.o under Cilium bpf repo, when built with -mattr=+alu32 are rejected due to this issue. After this patch, they all accepted. There is no processed insn number change before and after this patch on Cilium bpf programs. Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Reviewed-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent a38d110 commit 0bae2d4

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,10 @@ static int check_stack_write(struct bpf_verifier_env *env,
12861286

12871287
/* regular write of data into stack destroys any spilled ptr */
12881288
state->stack[spi].spilled_ptr.type = NOT_INIT;
1289+
/* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1290+
if (state->stack[spi].slot_type[0] == STACK_SPILL)
1291+
for (i = 0; i < BPF_REG_SIZE; i++)
1292+
state->stack[spi].slot_type[i] = STACK_MISC;
12891293

12901294
/* only mark the slot as written if all 8 bytes were written
12911295
* otherwise read propagation may incorrectly stop too soon
@@ -1303,6 +1307,7 @@ static int check_stack_write(struct bpf_verifier_env *env,
13031307
register_is_null(&cur->regs[value_regno]))
13041308
type = STACK_ZERO;
13051309

1310+
/* Mark slots affected by this stack write. */
13061311
for (i = 0; i < size; i++)
13071312
state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
13081313
type;

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,14 +1001,44 @@ static struct bpf_test tests[] = {
10011001
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
10021002
/* mess up with R1 pointer on stack */
10031003
BPF_ST_MEM(BPF_B, BPF_REG_10, -7, 0x23),
1004-
/* fill back into R0 should fail */
1004+
/* fill back into R0 is fine for priv.
1005+
* R0 now becomes SCALAR_VALUE.
1006+
*/
10051007
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
1008+
/* Load from R0 should fail. */
1009+
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 8),
10061010
BPF_EXIT_INSN(),
10071011
},
10081012
.errstr_unpriv = "attempt to corrupt spilled",
1009-
.errstr = "corrupted spill",
1013+
.errstr = "R0 invalid mem access 'inv",
10101014
.result = REJECT,
10111015
},
1016+
{
1017+
"check corrupted spill/fill, LSB",
1018+
.insns = {
1019+
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
1020+
BPF_ST_MEM(BPF_H, BPF_REG_10, -8, 0xcafe),
1021+
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
1022+
BPF_EXIT_INSN(),
1023+
},
1024+
.errstr_unpriv = "attempt to corrupt spilled",
1025+
.result_unpriv = REJECT,
1026+
.result = ACCEPT,
1027+
.retval = POINTER_VALUE,
1028+
},
1029+
{
1030+
"check corrupted spill/fill, MSB",
1031+
.insns = {
1032+
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
1033+
BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0x12345678),
1034+
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
1035+
BPF_EXIT_INSN(),
1036+
},
1037+
.errstr_unpriv = "attempt to corrupt spilled",
1038+
.result_unpriv = REJECT,
1039+
.result = ACCEPT,
1040+
.retval = POINTER_VALUE,
1041+
},
10121042
{
10131043
"invalid src register in STX",
10141044
.insns = {

0 commit comments

Comments
 (0)