selftests/bpf: precision tracking test for BPF_NEG and BPF_END
As seen from previous commit that fix backtracking for BPF_ALU | BPF_TO_BE | BPF_END, both BPF_NEG and BPF_END require special handling. Add tests written with inline assembly to check that the verifier does not incorrecly use the src_reg field of BPF_NEG and BPF_END (including bswap added in v4). Suggested-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com> Link: https://lore.kernel.org/r/20231102053913.12004-4-shung-hsi.yu@suse.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
291d044fd5
commit
3c41971550
@ -46,6 +46,7 @@
|
||||
#include "verifier_movsx.skel.h"
|
||||
#include "verifier_netfilter_ctx.skel.h"
|
||||
#include "verifier_netfilter_retcode.skel.h"
|
||||
#include "verifier_precision.skel.h"
|
||||
#include "verifier_prevent_map_lookup.skel.h"
|
||||
#include "verifier_raw_stack.skel.h"
|
||||
#include "verifier_raw_tp_writable.skel.h"
|
||||
@ -153,6 +154,7 @@ void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
|
||||
void test_verifier_movsx(void) { RUN(verifier_movsx); }
|
||||
void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
|
||||
void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
|
||||
void test_verifier_precision(void) { RUN(verifier_precision); }
|
||||
void test_verifier_prevent_map_lookup(void) { RUN(verifier_prevent_map_lookup); }
|
||||
void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); }
|
||||
void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
|
||||
|
93
tools/testing/selftests/bpf/progs/verifier_precision.c
Normal file
93
tools/testing/selftests/bpf/progs/verifier_precision.c
Normal file
@ -0,0 +1,93 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (C) 2023 SUSE LLC */
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "bpf_misc.h"
|
||||
|
||||
SEC("?raw_tp")
|
||||
__success __log_level(2)
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 3: (bf) r1 = r10")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 2: (55) if r2 != 0xfffffff8 goto pc+2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 1: (87) r2 = -r2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 0: (b7) r2 = 8")
|
||||
__naked int bpf_neg(void)
|
||||
{
|
||||
asm volatile (
|
||||
"r2 = 8;"
|
||||
"r2 = -r2;"
|
||||
"if r2 != -8 goto 1f;"
|
||||
"r1 = r10;"
|
||||
"r1 += r2;"
|
||||
"1:"
|
||||
"r0 = 0;"
|
||||
"exit;"
|
||||
::: __clobber_all);
|
||||
}
|
||||
|
||||
SEC("?raw_tp")
|
||||
__success __log_level(2)
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 3: (bf) r1 = r10")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 2: (55) if r2 != 0x0 goto pc+2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 1: (d4) r2 = le16 r2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 0: (b7) r2 = 0")
|
||||
__naked int bpf_end_to_le(void)
|
||||
{
|
||||
asm volatile (
|
||||
"r2 = 0;"
|
||||
"r2 = le16 r2;"
|
||||
"if r2 != 0 goto 1f;"
|
||||
"r1 = r10;"
|
||||
"r1 += r2;"
|
||||
"1:"
|
||||
"r0 = 0;"
|
||||
"exit;"
|
||||
::: __clobber_all);
|
||||
}
|
||||
|
||||
|
||||
SEC("?raw_tp")
|
||||
__success __log_level(2)
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 3: (bf) r1 = r10")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 2: (55) if r2 != 0x0 goto pc+2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 1: (dc) r2 = be16 r2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 0: (b7) r2 = 0")
|
||||
__naked int bpf_end_to_be(void)
|
||||
{
|
||||
asm volatile (
|
||||
"r2 = 0;"
|
||||
"r2 = be16 r2;"
|
||||
"if r2 != 0 goto 1f;"
|
||||
"r1 = r10;"
|
||||
"r1 += r2;"
|
||||
"1:"
|
||||
"r0 = 0;"
|
||||
"exit;"
|
||||
::: __clobber_all);
|
||||
}
|
||||
|
||||
#if (defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) || \
|
||||
(defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64) || \
|
||||
defined(__TARGET_ARCH_arm) || defined(__TARGET_ARCH_s390)) && \
|
||||
__clang_major__ >= 18
|
||||
|
||||
SEC("?raw_tp")
|
||||
__success __log_level(2)
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 3: (bf) r1 = r10")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 2: (55) if r2 != 0x0 goto pc+2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 1: (d7) r2 = bswap16 r2")
|
||||
__msg("mark_precise: frame0: regs=r2 stack= before 0: (b7) r2 = 0")
|
||||
__naked int bpf_end_bswap(void)
|
||||
{
|
||||
asm volatile (
|
||||
"r2 = 0;"
|
||||
"r2 = bswap16 r2;"
|
||||
"if r2 != 0 goto 1f;"
|
||||
"r1 = r10;"
|
||||
"r1 += r2;"
|
||||
"1:"
|
||||
"r0 = 0;"
|
||||
"exit;"
|
||||
::: __clobber_all);
|
||||
}
|
||||
|
||||
#endif /* v4 instruction */
|
Loading…
x
Reference in New Issue
Block a user