^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * bpf-script-test-relocation.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Test BPF loader checking relocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef LINUX_VERSION_CODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # error Need LINUX_VERSION_CODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define BPF_ANY 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define BPF_MAP_TYPE_ARRAY 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define BPF_FUNC_map_lookup_elem 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define BPF_FUNC_map_update_elem 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static void *(*bpf_map_lookup_elem)(void *map, void *key) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) (void *) BPF_FUNC_map_lookup_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) (void *) BPF_FUNC_map_update_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct bpf_map_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned int max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define SEC(NAME) __attribute__((section(NAME), used))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct bpf_map_def SEC("maps") my_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .type = BPF_MAP_TYPE_ARRAY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .key_size = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .value_size = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .max_entries = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int this_is_a_global_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) SEC("func=sys_write")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int bpf_func__sys_write(void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Incorrect relocation. Should not allow this program be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * loaded into kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bpf_map_update_elem(&this_is_a_global_val, &key, &value, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) char _license[] SEC("license") = "GPL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int _version SEC("version") = LINUX_VERSION_CODE;