~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/verifier_map_ret_val.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /* Converted from tools/testing/selftests/bpf/verifier/map_ret_val.c */
  3 
  4 #include <linux/bpf.h>
  5 #include <bpf/bpf_helpers.h>
  6 #include "../../../include/linux/filter.h"
  7 #include "bpf_misc.h"
  8 
  9 struct {
 10         __uint(type, BPF_MAP_TYPE_HASH);
 11         __uint(max_entries, 1);
 12         __type(key, long long);
 13         __type(value, long long);
 14 } map_hash_8b SEC(".maps");
 15 
 16 SEC("socket")
 17 __description("invalid map_fd for function call")
 18 __failure __msg("fd 0 is not pointing to valid bpf_map")
 19 __failure_unpriv
 20 __naked void map_fd_for_function_call(void)
 21 {
 22         asm volatile ("                                 \
 23         r2 = 0;                                         \
 24         *(u64*)(r10 - 8) = r2;                          \
 25         r2 = r10;                                       \
 26         r2 += -8;                                       \
 27         .8byte %[ld_map_fd];                            \
 28         .8byte 0;                                       \
 29         call %[bpf_map_delete_elem];                    \
 30         exit;                                           \
 31 "       :
 32         : __imm(bpf_map_delete_elem),
 33           __imm_insn(ld_map_fd, BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, BPF_REG_1, BPF_PSEUDO_MAP_FD, 0, 0))
 34         : __clobber_all);
 35 }
 36 
 37 SEC("socket")
 38 __description("don't check return value before access")
 39 __failure __msg("R0 invalid mem access 'map_value_or_null'")
 40 __failure_unpriv
 41 __naked void check_return_value_before_access(void)
 42 {
 43         asm volatile ("                                 \
 44         r1 = 0;                                         \
 45         *(u64*)(r10 - 8) = r1;                          \
 46         r2 = r10;                                       \
 47         r2 += -8;                                       \
 48         r1 = %[map_hash_8b] ll;                         \
 49         call %[bpf_map_lookup_elem];                    \
 50         r1 = 0;                                         \
 51         *(u64*)(r0 + 0) = r1;                           \
 52         exit;                                           \
 53 "       :
 54         : __imm(bpf_map_lookup_elem),
 55           __imm_addr(map_hash_8b)
 56         : __clobber_all);
 57 }
 58 
 59 SEC("socket")
 60 __description("access memory with incorrect alignment")
 61 __failure __msg("misaligned value access")
 62 __failure_unpriv
 63 __flag(BPF_F_STRICT_ALIGNMENT)
 64 __naked void access_memory_with_incorrect_alignment_1(void)
 65 {
 66         asm volatile ("                                 \
 67         r1 = 0;                                         \
 68         *(u64*)(r10 - 8) = r1;                          \
 69         r2 = r10;                                       \
 70         r2 += -8;                                       \
 71         r1 = %[map_hash_8b] ll;                         \
 72         call %[bpf_map_lookup_elem];                    \
 73         if r0 == 0 goto l0_%=;                          \
 74         r1 = 0;                                         \
 75         *(u64*)(r0 + 4) = r1;                           \
 76 l0_%=:  exit;                                           \
 77 "       :
 78         : __imm(bpf_map_lookup_elem),
 79           __imm_addr(map_hash_8b)
 80         : __clobber_all);
 81 }
 82 
 83 SEC("socket")
 84 __description("sometimes access memory with incorrect alignment")
 85 __failure __msg("R0 invalid mem access")
 86 __msg_unpriv("R0 leaks addr")
 87 __flag(BPF_F_STRICT_ALIGNMENT)
 88 __naked void access_memory_with_incorrect_alignment_2(void)
 89 {
 90         asm volatile ("                                 \
 91         r1 = 0;                                         \
 92         *(u64*)(r10 - 8) = r1;                          \
 93         r2 = r10;                                       \
 94         r2 += -8;                                       \
 95         r1 = %[map_hash_8b] ll;                         \
 96         call %[bpf_map_lookup_elem];                    \
 97         if r0 == 0 goto l0_%=;                          \
 98         r1 = 0;                                         \
 99         *(u64*)(r0 + 0) = r1;                           \
100         exit;                                           \
101 l0_%=:  r1 = 1;                                         \
102         *(u64*)(r0 + 0) = r1;                           \
103         exit;                                           \
104 "       :
105         : __imm(bpf_map_lookup_elem),
106           __imm_addr(map_hash_8b)
107         : __clobber_all);
108 }
109 
110 char _license[] SEC("license") = "GPL";
111 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php