Skip to content

Commit 6b80ad2

Browse files
thejhborkmann
authored andcommitted
bpf: selftest for late caller stack size increase
This checks that it is not possible to bypass the total stack size check in update_stack_depth() by calling a function that uses a large amount of stack memory *before* using a large amount of stack memory in the caller. Currently, the first added testcase causes a rejection as expected, but the second testcase is (AFAICS incorrectly) accepted: [...] torvalds#483/p calls: stack overflow using two frames (post-call access) FAIL Unexpected success to load! 0: (85) call pc+2 caller: R10=fp0,call_-1 callee: frame1: R1=ctx(id=0,off=0,imm=0) R10=fp0,call_0 3: (72) *(u8 *)(r10 -300) = 0 4: (b7) r0 = 0 5: (95) exit returning from callee: frame1: R0_w=inv0 R1=ctx(id=0,off=0,imm=0) R10=fp0,call_0 to caller at 1: R0_w=inv0 R10=fp0,call_-1 from 5 to 1: R0=inv0 R10=fp0,call_-1 1: (72) *(u8 *)(r10 -300) = 0 2: (95) exit processed 6 insns, stack depth 300+300 [...] Summary: 704 PASSED, 1 FAILED AFAICS the JIT-generated code for the second testcase shows that this really causes the stack pointer to be decremented by 300+300: first function: 00000000 55 push rbp 00000001 4889E5 mov rbp,rsp 00000004 4881EC58010000 sub rsp,0x158 0000000B 4883ED28 sub rbp,byte +0x28 [...] 00000025 E89AB3AFE5 call 0xffffffffe5afb3c4 0000002A C685D4FEFFFF00 mov byte [rbp-0x12c],0x0 [...] 00000041 4883C528 add rbp,byte +0x28 00000045 C9 leave 00000046 C3 ret second function: 00000000 55 push rbp 00000001 4889E5 mov rbp,rsp 00000004 4881EC58010000 sub rsp,0x158 0000000B 4883ED28 sub rbp,byte +0x28 [...] 00000025 C685D4FEFFFF00 mov byte [rbp-0x12c],0x0 [...] 0000003E 4883C528 add rbp,byte +0x28 00000042 C9 leave 00000043 C3 ret Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent fd05e57 commit 6b80ad2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8729,6 +8729,40 @@ static struct bpf_test tests[] = {
87298729
.prog_type = BPF_PROG_TYPE_XDP,
87308730
.result = ACCEPT,
87318731
},
8732+
{
8733+
"calls: stack overflow using two frames (pre-call access)",
8734+
.insns = {
8735+
/* prog 1 */
8736+
BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
8737+
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
8738+
BPF_EXIT_INSN(),
8739+
8740+
/* prog 2 */
8741+
BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
8742+
BPF_MOV64_IMM(BPF_REG_0, 0),
8743+
BPF_EXIT_INSN(),
8744+
},
8745+
.prog_type = BPF_PROG_TYPE_XDP,
8746+
.errstr = "combined stack size",
8747+
.result = REJECT,
8748+
},
8749+
{
8750+
"calls: stack overflow using two frames (post-call access)",
8751+
.insns = {
8752+
/* prog 1 */
8753+
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
8754+
BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
8755+
BPF_EXIT_INSN(),
8756+
8757+
/* prog 2 */
8758+
BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
8759+
BPF_MOV64_IMM(BPF_REG_0, 0),
8760+
BPF_EXIT_INSN(),
8761+
},
8762+
.prog_type = BPF_PROG_TYPE_XDP,
8763+
.errstr = "combined stack size",
8764+
.result = REJECT,
8765+
},
87328766
{
87338767
"calls: spill into caller stack frame",
87348768
.insns = {

0 commit comments

Comments
 (0)