selftests/bpf: verifier/map_ptr.c converted to inline assembly
Test verifier/map_ptr.c automatically converted to use inline assembly. Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20230325025524.144043-28-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
583c7ce5be
commit
caf345cf12
@ -24,6 +24,7 @@
|
||||
#include "verifier_int_ptr.skel.h"
|
||||
#include "verifier_ld_ind.skel.h"
|
||||
#include "verifier_leak_ptr.skel.h"
|
||||
#include "verifier_map_ptr.skel.h"
|
||||
|
||||
__maybe_unused
|
||||
static void run_tests_aux(const char *skel_name, skel_elf_bytes_fn elf_bytes_factory)
|
||||
@ -70,3 +71,4 @@ void test_verifier_helper_value_access(void) { RUN(verifier_helper_value_access
|
||||
void test_verifier_int_ptr(void) { RUN(verifier_int_ptr); }
|
||||
void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }
|
||||
void test_verifier_leak_ptr(void) { RUN(verifier_leak_ptr); }
|
||||
void test_verifier_map_ptr(void) { RUN(verifier_map_ptr); }
|
||||
|
159
tools/testing/selftests/bpf/progs/verifier_map_ptr.c
Normal file
159
tools/testing/selftests/bpf/progs/verifier_map_ptr.c
Normal file
@ -0,0 +1,159 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Converted from tools/testing/selftests/bpf/verifier/map_ptr.c */
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "bpf_misc.h"
|
||||
|
||||
#define MAX_ENTRIES 11
|
||||
|
||||
struct test_val {
|
||||
unsigned int index;
|
||||
int foo[MAX_ENTRIES];
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_ARRAY);
|
||||
__uint(max_entries, 1);
|
||||
__type(key, int);
|
||||
__type(value, struct test_val);
|
||||
} map_array_48b SEC(".maps");
|
||||
|
||||
struct other_val {
|
||||
long long foo;
|
||||
long long bar;
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 1);
|
||||
__type(key, long long);
|
||||
__type(value, struct other_val);
|
||||
} map_hash_16b SEC(".maps");
|
||||
|
||||
SEC("socket")
|
||||
__description("bpf_map_ptr: read with negative offset rejected")
|
||||
__failure __msg("R1 is bpf_array invalid negative access: off=-8")
|
||||
__failure_unpriv
|
||||
__msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
|
||||
__naked void read_with_negative_offset_rejected(void)
|
||||
{
|
||||
asm volatile (" \
|
||||
r1 = r10; \
|
||||
r1 = %[map_array_48b] ll; \
|
||||
r6 = *(u64*)(r1 - 8); \
|
||||
r0 = 1; \
|
||||
exit; \
|
||||
" :
|
||||
: __imm_addr(map_array_48b)
|
||||
: __clobber_all);
|
||||
}
|
||||
|
||||
SEC("socket")
|
||||
__description("bpf_map_ptr: write rejected")
|
||||
__failure __msg("only read from bpf_array is supported")
|
||||
__failure_unpriv
|
||||
__msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
|
||||
__naked void bpf_map_ptr_write_rejected(void)
|
||||
{
|
||||
asm volatile (" \
|
||||
r0 = 0; \
|
||||
*(u64*)(r10 - 8) = r0; \
|
||||
r2 = r10; \
|
||||
r2 += -8; \
|
||||
r1 = %[map_array_48b] ll; \
|
||||
*(u64*)(r1 + 0) = r2; \
|
||||
r0 = 1; \
|
||||
exit; \
|
||||
" :
|
||||
: __imm_addr(map_array_48b)
|
||||
: __clobber_all);
|
||||
}
|
||||
|
||||
SEC("socket")
|
||||
__description("bpf_map_ptr: read non-existent field rejected")
|
||||
__failure
|
||||
__msg("cannot access ptr member ops with moff 0 in struct bpf_map with off 1 size 4")
|
||||
__failure_unpriv
|
||||
__msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
|
||||
__flag(BPF_F_ANY_ALIGNMENT)
|
||||
__naked void read_non_existent_field_rejected(void)
|
||||
{
|
||||
asm volatile (" \
|
||||
r6 = 0; \
|
||||
r1 = %[map_array_48b] ll; \
|
||||
r6 = *(u32*)(r1 + 1); \
|
||||
r0 = 1; \
|
||||
exit; \
|
||||
" :
|
||||
: __imm_addr(map_array_48b)
|
||||
: __clobber_all);
|
||||
}
|
||||
|
||||
SEC("socket")
|
||||
__description("bpf_map_ptr: read ops field accepted")
|
||||
__success __failure_unpriv
|
||||
__msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
|
||||
__retval(1)
|
||||
__naked void ptr_read_ops_field_accepted(void)
|
||||
{
|
||||
asm volatile (" \
|
||||
r6 = 0; \
|
||||
r1 = %[map_array_48b] ll; \
|
||||
r6 = *(u64*)(r1 + 0); \
|
||||
r0 = 1; \
|
||||
exit; \
|
||||
" :
|
||||
: __imm_addr(map_array_48b)
|
||||
: __clobber_all);
|
||||
}
|
||||
|
||||
SEC("socket")
|
||||
__description("bpf_map_ptr: r = 0, map_ptr = map_ptr + r")
|
||||
__success __failure_unpriv
|
||||
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
||||
__retval(0)
|
||||
__naked void map_ptr_map_ptr_r(void)
|
||||
{
|
||||
asm volatile (" \
|
||||
r0 = 0; \
|
||||
*(u64*)(r10 - 8) = r0; \
|
||||
r2 = r10; \
|
||||
r2 += -8; \
|
||||
r0 = 0; \
|
||||
r1 = %[map_hash_16b] ll; \
|
||||
r1 += r0; \
|
||||
call %[bpf_map_lookup_elem]; \
|
||||
r0 = 0; \
|
||||
exit; \
|
||||
" :
|
||||
: __imm(bpf_map_lookup_elem),
|
||||
__imm_addr(map_hash_16b)
|
||||
: __clobber_all);
|
||||
}
|
||||
|
||||
SEC("socket")
|
||||
__description("bpf_map_ptr: r = 0, r = r + map_ptr")
|
||||
__success __failure_unpriv
|
||||
__msg_unpriv("R0 has pointer with unsupported alu operation")
|
||||
__retval(0)
|
||||
__naked void _0_r_r_map_ptr(void)
|
||||
{
|
||||
asm volatile (" \
|
||||
r0 = 0; \
|
||||
*(u64*)(r10 - 8) = r0; \
|
||||
r2 = r10; \
|
||||
r2 += -8; \
|
||||
r1 = 0; \
|
||||
r0 = %[map_hash_16b] ll; \
|
||||
r1 += r0; \
|
||||
call %[bpf_map_lookup_elem]; \
|
||||
r0 = 0; \
|
||||
exit; \
|
||||
" :
|
||||
: __imm(bpf_map_lookup_elem),
|
||||
__imm_addr(map_hash_16b)
|
||||
: __clobber_all);
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
@ -1,99 +0,0 @@
|
||||
{
|
||||
"bpf_map_ptr: read with negative offset rejected",
|
||||
.insns = {
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
|
||||
BPF_LD_MAP_FD(BPF_REG_1, 0),
|
||||
BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, -8),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 1),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_array_48b = { 1 },
|
||||
.result_unpriv = REJECT,
|
||||
.errstr_unpriv = "access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
|
||||
.result = REJECT,
|
||||
.errstr = "R1 is bpf_array invalid negative access: off=-8",
|
||||
},
|
||||
{
|
||||
"bpf_map_ptr: write rejected",
|
||||
.insns = {
|
||||
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
|
||||
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
|
||||
BPF_LD_MAP_FD(BPF_REG_1, 0),
|
||||
BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, 0),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 1),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_array_48b = { 3 },
|
||||
.result_unpriv = REJECT,
|
||||
.errstr_unpriv = "access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
|
||||
.result = REJECT,
|
||||
.errstr = "only read from bpf_array is supported",
|
||||
},
|
||||
{
|
||||
"bpf_map_ptr: read non-existent field rejected",
|
||||
.insns = {
|
||||
BPF_MOV64_IMM(BPF_REG_6, 0),
|
||||
BPF_LD_MAP_FD(BPF_REG_1, 0),
|
||||
BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1, 1),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 1),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_array_48b = { 1 },
|
||||
.result_unpriv = REJECT,
|
||||
.errstr_unpriv = "access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
|
||||
.result = REJECT,
|
||||
.errstr = "cannot access ptr member ops with moff 0 in struct bpf_map with off 1 size 4",
|
||||
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
|
||||
},
|
||||
{
|
||||
"bpf_map_ptr: read ops field accepted",
|
||||
.insns = {
|
||||
BPF_MOV64_IMM(BPF_REG_6, 0),
|
||||
BPF_LD_MAP_FD(BPF_REG_1, 0),
|
||||
BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 1),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_array_48b = { 1 },
|
||||
.result_unpriv = REJECT,
|
||||
.errstr_unpriv = "access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
|
||||
.result = ACCEPT,
|
||||
.retval = 1,
|
||||
},
|
||||
{
|
||||
"bpf_map_ptr: r = 0, map_ptr = map_ptr + r",
|
||||
.insns = {
|
||||
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
|
||||
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 0),
|
||||
BPF_LD_MAP_FD(BPF_REG_1, 0),
|
||||
BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_0),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_hash_16b = { 4 },
|
||||
.result_unpriv = REJECT,
|
||||
.errstr_unpriv = "R1 has pointer with unsupported alu operation",
|
||||
.result = ACCEPT,
|
||||
},
|
||||
{
|
||||
"bpf_map_ptr: r = 0, r = r + map_ptr",
|
||||
.insns = {
|
||||
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
|
||||
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
|
||||
BPF_MOV64_IMM(BPF_REG_1, 0),
|
||||
BPF_LD_MAP_FD(BPF_REG_0, 0),
|
||||
BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_0),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
|
||||
BPF_MOV64_IMM(BPF_REG_0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_hash_16b = { 4 },
|
||||
.result_unpriv = REJECT,
|
||||
.errstr_unpriv = "R0 has pointer with unsupported alu operation",
|
||||
.result = ACCEPT,
|
||||
},
|
Loading…
x
Reference in New Issue
Block a user