Skip to content

Commit

Permalink
selftests/bpf: verifier/prevent_map_lookup converted to inline assembly
Browse files Browse the repository at this point in the history
Test verifier/prevent_map_lookup automatically converted to use inline assembly.

This was a part of a series [1] but could not be applied becuase
another patch from a series had to be witheld.

[1] https://lore.kernel.org/bpf/20230421174234.2391278-1-eddyz87@gmail.com/

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20230421204514.2450907-1-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Eduard Zingerman authored and Alexei Starovoitov committed Apr 22, 2023
1 parent 6d26d98 commit 3515020
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "verifier_meta_access.skel.h"
#include "verifier_netfilter_ctx.skel.h"
#include "verifier_netfilter_retcode.skel.h"
#include "verifier_prevent_map_lookup.skel.h"
#include "verifier_raw_stack.skel.h"
#include "verifier_raw_tp_writable.skel.h"
#include "verifier_reg_equal.skel.h"
Expand Down Expand Up @@ -140,6 +141,7 @@ void test_verifier_masking(void) { RUN(verifier_masking); }
void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
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); }
void test_verifier_reg_equal(void) { RUN(verifier_reg_equal); }
Expand Down
61 changes: 61 additions & 0 deletions tools/testing/selftests/bpf/progs/verifier_prevent_map_lookup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-2.0
/* Converted from tools/testing/selftests/bpf/verifier/prevent_map_lookup.c */

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, __u64);
} map_stacktrace SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
__uint(max_entries, 8);
__uint(key_size, sizeof(int));
__array(values, void (void));
} map_prog2_socket SEC(".maps");

SEC("perf_event")
__description("prevent map lookup in stack trace")
__failure __msg("cannot pass map_type 7 into func bpf_map_lookup_elem")
__naked void map_lookup_in_stack_trace(void)
{
asm volatile (" \
r1 = 0; \
*(u64*)(r10 - 8) = r1; \
r2 = r10; \
r2 += -8; \
r1 = %[map_stacktrace] ll; \
call %[bpf_map_lookup_elem]; \
exit; \
" :
: __imm(bpf_map_lookup_elem),
__imm_addr(map_stacktrace)
: __clobber_all);
}

SEC("socket")
__description("prevent map lookup in prog array")
__failure __msg("cannot pass map_type 3 into func bpf_map_lookup_elem")
__failure_unpriv
__naked void map_lookup_in_prog_array(void)
{
asm volatile (" \
r1 = 0; \
*(u64*)(r10 - 8) = r1; \
r2 = r10; \
r2 += -8; \
r1 = %[map_prog2_socket] ll; \
call %[bpf_map_lookup_elem]; \
exit; \
" :
: __imm(bpf_map_lookup_elem),
__imm_addr(map_prog2_socket)
: __clobber_all);
}

char _license[] SEC("license") = "GPL";
29 changes: 0 additions & 29 deletions tools/testing/selftests/bpf/verifier/prevent_map_lookup.c

This file was deleted.

0 comments on commit 3515020

Please sign in to comment.