1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2022 SUSE LLC <rpalethorpe@suse.com> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/*\ 7f08c3bdfSopenharmony_ci * [Description] 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * The verifier did not properly restrict some *_OR_NULL pointer 10f08c3bdfSopenharmony_ci * types. Including RET_PTR_TO_ALLOC_MEM_OR_NULL which is returned by 11f08c3bdfSopenharmony_ci * ringbuf_reserve. Somehow this means they can be used to perform 12f08c3bdfSopenharmony_ci * arbitrary pointer arithmetic. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * The test tries to do some pointer arithmetic on the return value of 15f08c3bdfSopenharmony_ci * ringbuf_reserve. Possibly with a trick to make the verifier believe 16f08c3bdfSopenharmony_ci * the pointer (in r1) is NULL. The test will pass if the eBPF is 17f08c3bdfSopenharmony_ci * rejected and will fail otherwise. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * This test does not try to cause a crash. Howver it does run the 20f08c3bdfSopenharmony_ci * eBPF if it can. This will result in an instant crash or memory 21f08c3bdfSopenharmony_ci * corruption which may later cause a crash. 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * This test is adapted from a full reproducer which can be found here: 24f08c3bdfSopenharmony_ci * https://github.com/tr3ee/CVE-2022-23222 25f08c3bdfSopenharmony_ci * 26f08c3bdfSopenharmony_ci * It's recommended to disable unprivileged eBPF by setting 27f08c3bdfSopenharmony_ci * /proc/sys/kernel/unprivileged_bpf_disabled. Also there is a 28f08c3bdfSopenharmony_ci * specific fix for this issue: 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * commit 64620e0a1e712a778095bd35cbb277dc2259281f 31f08c3bdfSopenharmony_ci * Author: Daniel Borkmann <daniel@iogearbox.net> 32f08c3bdfSopenharmony_ci * Date: Tue Jan 11 14:43:41 2022 +0000 33f08c3bdfSopenharmony_ci * 34f08c3bdfSopenharmony_ci * bpf: Fix out of bounds access for ringbuf helpers 35f08c3bdfSopenharmony_ci */ 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci#include <stdint.h> 38f08c3bdfSopenharmony_ci#include <stdio.h> 39f08c3bdfSopenharmony_ci#include <string.h> 40f08c3bdfSopenharmony_ci#include <inttypes.h> 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci#include "config.h" 43f08c3bdfSopenharmony_ci#include "tst_test.h" 44f08c3bdfSopenharmony_ci#include "tst_taint.h" 45f08c3bdfSopenharmony_ci#include "tst_capability.h" 46f08c3bdfSopenharmony_ci#include "lapi/bpf.h" 47f08c3bdfSopenharmony_ci#include "bpf_common.h" 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic const char MSG[] = "Ahoj!"; 50f08c3bdfSopenharmony_cistatic char *msg; 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistatic int map_fd; 53f08c3bdfSopenharmony_cistatic uint32_t *key; 54f08c3bdfSopenharmony_cistatic uint64_t *val; 55f08c3bdfSopenharmony_cistatic char *log; 56f08c3bdfSopenharmony_cistatic union bpf_attr *attr; 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_cistatic int load_prog(void) 59f08c3bdfSopenharmony_ci{ 60f08c3bdfSopenharmony_ci int ret; 61f08c3bdfSopenharmony_ci const struct bpf_insn prog_insn[] = { 62f08c3bdfSopenharmony_ci // r0 = bpf_ringbuf_reserve(ctx->ringbuf_fd, PAGE_SIZE, 0) 63f08c3bdfSopenharmony_ci BPF_LD_MAP_FD(BPF_REG_1, map_fd), 64f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_2, getpagesize()), 65f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_3, 0x00), 66f08c3bdfSopenharmony_ci BPF_EMIT_CALL(BPF_FUNC_ringbuf_reserve), 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 69f08c3bdfSopenharmony_ci BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1), 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci // if (r0 != NULL) { ringbuf_discard(r0, 1); exit(2); } 72f08c3bdfSopenharmony_ci BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5), 73f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 74f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_2, 1), 75f08c3bdfSopenharmony_ci BPF_EMIT_CALL(BPF_FUNC_ringbuf_discard), 76f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_0, 2), 77f08c3bdfSopenharmony_ci BPF_EXIT_INSN(), 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci // *(sp + 4*r1) = INT32_MAX 80f08c3bdfSopenharmony_ci BPF_ALU64_IMM(BPF_MUL, BPF_REG_1, 8), 81f08c3bdfSopenharmony_ci BPF_MOV64_REG(BPF_REG_3, BPF_REG_10), 82f08c3bdfSopenharmony_ci BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -8), 83f08c3bdfSopenharmony_ci BPF_ALU64_REG(BPF_ADD, BPF_REG_3, BPF_REG_1), 84f08c3bdfSopenharmony_ci BPF_ST_MEM(BPF_DW, BPF_REG_3, 0, INT32_MAX), 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci /* exit(0) */ 87f08c3bdfSopenharmony_ci BPF_MOV64_IMM(BPF_REG_0, 0), 88f08c3bdfSopenharmony_ci BPF_EXIT_INSN() 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci }; 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci bpf_init_prog_attr(attr, prog_insn, sizeof(prog_insn), log, BUFSIZE); 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)), 95f08c3bdfSopenharmony_ci TST_RETVAL_GE0); 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci if (ret >= 0) 98f08c3bdfSopenharmony_ci return ret; 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci if (ret != -1) 101f08c3bdfSopenharmony_ci tst_brk(TBROK, "Invalid bpf() return value: %d", ret); 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci if (log[0] != 0) 104f08c3bdfSopenharmony_ci tst_printf("%s\n", log); 105f08c3bdfSopenharmony_ci 106f08c3bdfSopenharmony_ci return ret; 107f08c3bdfSopenharmony_ci} 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_cistatic void setup(void) 110f08c3bdfSopenharmony_ci{ 111f08c3bdfSopenharmony_ci rlimit_bump_memlock(); 112f08c3bdfSopenharmony_ci memcpy(msg, MSG, sizeof(MSG)); 113f08c3bdfSopenharmony_ci} 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_cistatic void run(void) 116f08c3bdfSopenharmony_ci{ 117f08c3bdfSopenharmony_ci int prog_fd; 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci map_fd = bpf_map_create(&(union bpf_attr){ 120f08c3bdfSopenharmony_ci .map_type = BPF_MAP_TYPE_RINGBUF, 121f08c3bdfSopenharmony_ci .key_size = 0, 122f08c3bdfSopenharmony_ci .value_size = 0, 123f08c3bdfSopenharmony_ci .max_entries = getpagesize() 124f08c3bdfSopenharmony_ci }); 125f08c3bdfSopenharmony_ci 126f08c3bdfSopenharmony_ci tst_res(TINFO, "Trying to load eBPF with OOB write"); 127f08c3bdfSopenharmony_ci prog_fd = load_prog(); 128f08c3bdfSopenharmony_ci if (prog_fd == -1) { 129f08c3bdfSopenharmony_ci tst_res(TPASS, "Failed verification"); 130f08c3bdfSopenharmony_ci return; 131f08c3bdfSopenharmony_ci } 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci tst_res(TFAIL, "Loaded program with OOB write"); 134f08c3bdfSopenharmony_ci tst_res(TINFO, "Running eBPF with OOB"); 135f08c3bdfSopenharmony_ci bpf_run_prog(prog_fd, msg, sizeof(MSG)); 136f08c3bdfSopenharmony_ci tst_res(TINFO, "Ran eBPF"); 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_ci SAFE_CLOSE(prog_fd); 139f08c3bdfSopenharmony_ci} 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_cistatic struct tst_test test = { 142f08c3bdfSopenharmony_ci .setup = setup, 143f08c3bdfSopenharmony_ci .test_all = run, 144f08c3bdfSopenharmony_ci .min_kver = "5.8", 145f08c3bdfSopenharmony_ci .taint_check = TST_TAINT_W | TST_TAINT_D, 146f08c3bdfSopenharmony_ci .caps = (struct tst_cap []) { 147f08c3bdfSopenharmony_ci TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN), 148f08c3bdfSopenharmony_ci TST_CAP(TST_CAP_DROP, CAP_BPF), 149f08c3bdfSopenharmony_ci {} 150f08c3bdfSopenharmony_ci }, 151f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 152f08c3bdfSopenharmony_ci {&key, .size = sizeof(*key)}, 153f08c3bdfSopenharmony_ci {&val, .size = sizeof(*val)}, 154f08c3bdfSopenharmony_ci {&log, .size = BUFSIZE}, 155f08c3bdfSopenharmony_ci {&attr, .size = sizeof(*attr)}, 156f08c3bdfSopenharmony_ci {&msg, .size = sizeof(MSG)}, 157f08c3bdfSopenharmony_ci {} 158f08c3bdfSopenharmony_ci }, 159f08c3bdfSopenharmony_ci .tags = (const struct tst_tag[]) { 160f08c3bdfSopenharmony_ci {"linux-git", "64620e0a1e71"}, 161f08c3bdfSopenharmony_ci {"CVE", "CVE-2022-23222"}, 162f08c3bdfSopenharmony_ci {} 163f08c3bdfSopenharmony_ci } 164f08c3bdfSopenharmony_ci}; 165