1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Richard Palethorpe <rpalethorpe@suse.com> 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Check if eBPF can do arithmetic with 64bits. This targets a specific 6f08c3bdfSopenharmony_ci * regression which only effects unprivileged users who are subject to extra 7f08c3bdfSopenharmony_ci * pointer arithmetic checks during verification. 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * Fixed by commit 3612af783cf52c74a031a2f11b82247b2599d3cd. 10f08c3bdfSopenharmony_ci * https://new.blog.cloudflare.com/ebpf-cant-count/ 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * This test is very similar in structure to bpf_prog01 which is better 13f08c3bdfSopenharmony_ci * annotated. 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include <limits.h> 17f08c3bdfSopenharmony_ci#include <string.h> 18f08c3bdfSopenharmony_ci#include <stdio.h> 19f08c3bdfSopenharmony_ci#include <inttypes.h> 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#include "config.h" 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci#include "tst_capability.h" 24f08c3bdfSopenharmony_ci#include "bpf_common.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define A64INT (((uint64_t)1) << 60) 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ciconst char MSG[] = "Ahoj!"; 29f08c3bdfSopenharmony_cistatic char *msg; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_cistatic char *log; 32f08c3bdfSopenharmony_cistatic uint32_t *key; 33f08c3bdfSopenharmony_cistatic uint64_t *val; 34f08c3bdfSopenharmony_cistatic union bpf_attr *attr; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_cistatic int load_prog(int fd) 37f08c3bdfSopenharmony_ci{ 38f08c3bdfSopenharmony_ci struct bpf_insn insn[] = { 39f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_6, 1), /* 0: r6 = 1 */ 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci BPF_LD_MAP_FD(BPF_REG_1, fd), /* 1: r1 = &fd */ 42f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* 3: r2 = fp */ 43f08c3bdfSopenharmony_ci BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* 4: r2 = r2 - 4 */ 44f08c3bdfSopenharmony_ci BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0), /* 5: *r2 = 0 */ 45f08c3bdfSopenharmony_ci BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),/* 6: map_lookup_elem */ 46f08c3bdfSopenharmony_ci BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 17), /* 7: if(!r0) goto 25 */ 47f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_3, BPF_REG_0), /* 8: r3 = r0 */ 48f08c3bdfSopenharmony_ci BPF_LD_IMM64(BPF_REG_4, A64INT), /* 9: r4 = 2^61 */ 49f08c3bdfSopenharmony_ci BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_6), /* 11: r4 += r6 */ 50f08c3bdfSopenharmony_ci BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* 12: *r3 = r4 */ 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci BPF_LD_MAP_FD(BPF_REG_1, fd), /* 13: r1 = &fd */ 53f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* 15: r2 = fp */ 54f08c3bdfSopenharmony_ci BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* 16: r2 = r2 - 4 */ 55f08c3bdfSopenharmony_ci BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 1), /* 17: *r2 = 1 */ 56f08c3bdfSopenharmony_ci BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),/* 18: map_lookup_elem */ 57f08c3bdfSopenharmony_ci BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5), /* 19: if(!r0) goto 25 */ 58f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_3, BPF_REG_0), /* 20: r3 = r0 */ 59f08c3bdfSopenharmony_ci BPF_LD_IMM64(BPF_REG_4, A64INT), /* 21: r4 = 2^60 */ 60f08c3bdfSopenharmony_ci BPF_ALU64_REG(BPF_SUB, BPF_REG_4, BPF_REG_6), /* 23: r4 -= r6 */ 61f08c3bdfSopenharmony_ci BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* 24: *r3 = r4 */ 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_0, 0), /* 25: r0 = 0 */ 64f08c3bdfSopenharmony_ci BPF_EXIT_INSN(), /* 26: return r0 */ 65f08c3bdfSopenharmony_ci }; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci bpf_init_prog_attr(attr, insn, sizeof(insn), log, BUFSIZE); 68f08c3bdfSopenharmony_ci return bpf_load_prog(attr, log); 69f08c3bdfSopenharmony_ci} 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_cistatic void setup(void) 72f08c3bdfSopenharmony_ci{ 73f08c3bdfSopenharmony_ci rlimit_bump_memlock(); 74f08c3bdfSopenharmony_ci memcpy(msg, MSG, sizeof(MSG)); 75f08c3bdfSopenharmony_ci} 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_cistatic void run(void) 78f08c3bdfSopenharmony_ci{ 79f08c3bdfSopenharmony_ci int map_fd, prog_fd; 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci map_fd = bpf_map_array_create(2); 82f08c3bdfSopenharmony_ci prog_fd = load_prog(map_fd); 83f08c3bdfSopenharmony_ci bpf_run_prog(prog_fd, msg, sizeof(MSG)); 84f08c3bdfSopenharmony_ci SAFE_CLOSE(prog_fd); 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci *key = 0; 87f08c3bdfSopenharmony_ci bpf_map_array_get(map_fd, key, val); 88f08c3bdfSopenharmony_ci if (*val != A64INT + 1) { 89f08c3bdfSopenharmony_ci tst_res(TFAIL, 90f08c3bdfSopenharmony_ci "val = %"PRIu64", but should be val = %"PRIu64" + 1", 91f08c3bdfSopenharmony_ci *val, A64INT); 92f08c3bdfSopenharmony_ci } else { 93f08c3bdfSopenharmony_ci tst_res(TPASS, "val = %"PRIu64" + 1", A64INT); 94f08c3bdfSopenharmony_ci } 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci *key = 1; 97f08c3bdfSopenharmony_ci bpf_map_array_get(map_fd, key, val); 98f08c3bdfSopenharmony_ci if (*val != A64INT - 1) { 99f08c3bdfSopenharmony_ci tst_res(TFAIL, 100f08c3bdfSopenharmony_ci "val = %"PRIu64", but should be val = %"PRIu64" - 1", 101f08c3bdfSopenharmony_ci *val, A64INT); 102f08c3bdfSopenharmony_ci } else { 103f08c3bdfSopenharmony_ci tst_res(TPASS, "val = %"PRIu64" - 1", A64INT); 104f08c3bdfSopenharmony_ci } 105f08c3bdfSopenharmony_ci 106f08c3bdfSopenharmony_ci SAFE_CLOSE(map_fd); 107f08c3bdfSopenharmony_ci} 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_cistatic struct tst_test test = { 110f08c3bdfSopenharmony_ci .setup = setup, 111f08c3bdfSopenharmony_ci .test_all = run, 112f08c3bdfSopenharmony_ci .min_kver = "3.18", 113f08c3bdfSopenharmony_ci .caps = (struct tst_cap []) { 114f08c3bdfSopenharmony_ci TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN), 115f08c3bdfSopenharmony_ci {} 116f08c3bdfSopenharmony_ci }, 117f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 118f08c3bdfSopenharmony_ci {&key, .size = sizeof(*key)}, 119f08c3bdfSopenharmony_ci {&val, .size = sizeof(*val)}, 120f08c3bdfSopenharmony_ci {&log, .size = BUFSIZE}, 121f08c3bdfSopenharmony_ci {&attr, .size = sizeof(*attr)}, 122f08c3bdfSopenharmony_ci {&msg, .size = sizeof(MSG)}, 123f08c3bdfSopenharmony_ci {}, 124f08c3bdfSopenharmony_ci }, 125f08c3bdfSopenharmony_ci .tags = (const struct tst_tag[]) { 126f08c3bdfSopenharmony_ci {"linux-git", "3612af783cf5"}, 127f08c3bdfSopenharmony_ci {} 128f08c3bdfSopenharmony_ci } 129f08c3bdfSopenharmony_ci}; 130