Skip to content

Commit 6d40191

Browse files
anakryikogregkh
authored andcommitted
selftests/bpf: validate STACK_ZERO is preserved on subreg spill
[ Upstream commit b33ceb6 ] Add tests validating that STACK_ZERO slots are preserved when slot is partially overwritten with subregister spill. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20231205184248.1502704-6-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com> Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 57f41f1 commit 6d40191

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tools/testing/selftests/bpf/progs/verifier_spill_fill.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
66
#include "bpf_misc.h"
7+
#include <../../../tools/include/linux/filter.h>
78

89
struct {
910
__uint(type, BPF_MAP_TYPE_RINGBUF);
@@ -450,4 +451,43 @@ l0_%=: r1 >>= 16; \
450451
: __clobber_all);
451452
}
452453

454+
SEC("raw_tp")
455+
__log_level(2)
456+
__success
457+
__msg("fp-8=0m??mmmm")
458+
__msg("fp-16=00mm??mm")
459+
__msg("fp-24=00mm???m")
460+
__naked void spill_subregs_preserve_stack_zero(void)
461+
{
462+
asm volatile (
463+
"call %[bpf_get_prandom_u32];"
464+
465+
/* 32-bit subreg spill with ZERO, MISC, and INVALID */
466+
".8byte %[fp1_u8_st_zero];" /* ZERO, LLVM-18+: *(u8 *)(r10 -1) = 0; */
467+
"*(u8 *)(r10 -2) = r0;" /* MISC */
468+
/* fp-3 and fp-4 stay INVALID */
469+
"*(u32 *)(r10 -8) = r0;"
470+
471+
/* 16-bit subreg spill with ZERO, MISC, and INVALID */
472+
".8byte %[fp10_u16_st_zero];" /* ZERO, LLVM-18+: *(u16 *)(r10 -10) = 0; */
473+
"*(u16 *)(r10 -12) = r0;" /* MISC */
474+
/* fp-13 and fp-14 stay INVALID */
475+
"*(u16 *)(r10 -16) = r0;"
476+
477+
/* 8-bit subreg spill with ZERO, MISC, and INVALID */
478+
".8byte %[fp18_u16_st_zero];" /* ZERO, LLVM-18+: *(u16 *)(r18 -10) = 0; */
479+
"*(u16 *)(r10 -20) = r0;" /* MISC */
480+
/* fp-21, fp-22, and fp-23 stay INVALID */
481+
"*(u8 *)(r10 -24) = r0;"
482+
483+
"r0 = 0;"
484+
"exit;"
485+
:
486+
: __imm(bpf_get_prandom_u32),
487+
__imm_insn(fp1_u8_st_zero, BPF_ST_MEM(BPF_B, BPF_REG_FP, -1, 0)),
488+
__imm_insn(fp10_u16_st_zero, BPF_ST_MEM(BPF_H, BPF_REG_FP, -10, 0)),
489+
__imm_insn(fp18_u16_st_zero, BPF_ST_MEM(BPF_H, BPF_REG_FP, -18, 0))
490+
: __clobber_all);
491+
}
492+
453493
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)