Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * bpf-script-test-relocation.c
0004  * Test BPF loader checking relocation
0005  */
0006 #ifndef LINUX_VERSION_CODE
0007 # error Need LINUX_VERSION_CODE
0008 # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
0009 #endif
0010 #define BPF_ANY 0
0011 #define BPF_MAP_TYPE_ARRAY 2
0012 #define BPF_FUNC_map_lookup_elem 1
0013 #define BPF_FUNC_map_update_elem 2
0014 
0015 static void *(*bpf_map_lookup_elem)(void *map, void *key) =
0016     (void *) BPF_FUNC_map_lookup_elem;
0017 static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
0018     (void *) BPF_FUNC_map_update_elem;
0019 
0020 struct bpf_map_def {
0021     unsigned int type;
0022     unsigned int key_size;
0023     unsigned int value_size;
0024     unsigned int max_entries;
0025 };
0026 
0027 #define SEC(NAME) __attribute__((section(NAME), used))
0028 struct bpf_map_def SEC("maps") my_table = {
0029     .type = BPF_MAP_TYPE_ARRAY,
0030     .key_size = sizeof(int),
0031     .value_size = sizeof(int),
0032     .max_entries = 1,
0033 };
0034 
0035 int this_is_a_global_val;
0036 
0037 SEC("func=sys_write")
0038 int bpf_func__sys_write(void *ctx)
0039 {
0040     int key = 0;
0041     int value = 0;
0042 
0043     /*
0044      * Incorrect relocation. Should not allow this program be
0045      * loaded into kernel.
0046      */
0047     bpf_map_update_elem(&this_is_a_global_val, &key, &value, 0);
0048     return 0;
0049 }
0050 char _license[] SEC("license") = "GPL";
0051 int _version SEC("version") = LINUX_VERSION_CODE;