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 * ringbuf_submit takes a pointer to a ringbuf record, but not the
10f08c3bdfSopenharmony_ci * size of this record. The verifier only validates offset ptrs[1] passed
11f08c3bdfSopenharmony_ci * to functions if the function has a size parameter. So we can
12f08c3bdfSopenharmony_ci * perform a wide range of ptr arithmetic on this record ptr.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * ringbuf_submit updates some data (i.e. the length) in the
15f08c3bdfSopenharmony_ci * ringbuf header which is calculated from the record ptr. So this can
16f08c3bdfSopenharmony_ci * be used to corrupt memory.
17f08c3bdfSopenharmony_ci *
18f08c3bdfSopenharmony_ci * This test does not try to cause a crash. Howver it does run the
19f08c3bdfSopenharmony_ci * eBPF if it can. This will result in an instant crash or memory
20f08c3bdfSopenharmony_ci * corruption which may later cause a crash.
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci * This test is adapted from a full reproducer which can be found here:
23f08c3bdfSopenharmony_ci * https://github.com/tr3ee/CVE-2021-4204
24f08c3bdfSopenharmony_ci *
25f08c3bdfSopenharmony_ci * It's recommended to disable unprivileged eBPF by setting
26f08c3bdfSopenharmony_ci * /proc/sys/kernel/unprivileged_bpf_disabled. Also there is a
27f08c3bdfSopenharmony_ci * specific fix for this issue:
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci * commit 64620e0a1e712a778095bd35cbb277dc2259281f
30f08c3bdfSopenharmony_ci * Author: Daniel Borkmann <daniel@iogearbox.net>
31f08c3bdfSopenharmony_ci * Date:   Tue Jan 11 14:43:41 2022 +0000
32f08c3bdfSopenharmony_ci *
33f08c3bdfSopenharmony_ci *  bpf: Fix out of bounds access for ringbuf helpers
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci * [1]: Depending on the ptr/reg type
36f08c3bdfSopenharmony_ci */
37f08c3bdfSopenharmony_ci
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, 0xff0, 0)
63f08c3bdfSopenharmony_ci		BPF_LD_MAP_FD(BPF_REG_1, map_fd),
64f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_2, 0xff0),
65f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_3, 0x00),
66f08c3bdfSopenharmony_ci		BPF_EMIT_CALL(BPF_FUNC_ringbuf_reserve),
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci		// if (r0 == NULL) exit(2)
69f08c3bdfSopenharmony_ci		BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
70f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_0, 2),
71f08c3bdfSopenharmony_ci		BPF_EXIT_INSN(),
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci		// r0 = BPF_FUNC_ringbuf_submit(r0-(0x3008-0x38), BPF_RB_NO_WAKEUP)
74f08c3bdfSopenharmony_ci		BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, (0x3008-0x38)),
75f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
76f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_2, 1),
77f08c3bdfSopenharmony_ci		BPF_EMIT_CALL(BPF_FUNC_ringbuf_submit),
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci		/* exit(0) */
80f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_0, 0),
81f08c3bdfSopenharmony_ci		BPF_EXIT_INSN()
82f08c3bdfSopenharmony_ci	};
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	bpf_init_prog_attr(attr, prog_insn, sizeof(prog_insn), log, BUFSIZE);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)),
87f08c3bdfSopenharmony_ci			     TST_RETVAL_GE0);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	if (ret >= 0)
90f08c3bdfSopenharmony_ci		return ret;
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	if (ret != -1)
93f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Invalid bpf() return value: %d", ret);
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	if (log[0] != 0)
96f08c3bdfSopenharmony_ci		tst_printf("%s\n", log);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	return ret;
99f08c3bdfSopenharmony_ci}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_cistatic void setup(void)
102f08c3bdfSopenharmony_ci{
103f08c3bdfSopenharmony_ci	rlimit_bump_memlock();
104f08c3bdfSopenharmony_ci	memcpy(msg, MSG, sizeof(MSG));
105f08c3bdfSopenharmony_ci}
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_cistatic void run(void)
108f08c3bdfSopenharmony_ci{
109f08c3bdfSopenharmony_ci	int prog_fd;
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	map_fd = bpf_map_create(&(union bpf_attr){
112f08c3bdfSopenharmony_ci			.map_type = BPF_MAP_TYPE_RINGBUF,
113f08c3bdfSopenharmony_ci			.key_size = 0,
114f08c3bdfSopenharmony_ci			.value_size = 0,
115f08c3bdfSopenharmony_ci			.max_entries = getpagesize()
116f08c3bdfSopenharmony_ci		});
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	tst_res(TINFO, "Trying to load eBPF with OOB write");
119f08c3bdfSopenharmony_ci	prog_fd = load_prog();
120f08c3bdfSopenharmony_ci	if (prog_fd == -1) {
121f08c3bdfSopenharmony_ci		tst_res(TPASS, "Failed verification");
122f08c3bdfSopenharmony_ci		return;
123f08c3bdfSopenharmony_ci	}
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	tst_res(TFAIL, "Loaded program with OOB write");
126f08c3bdfSopenharmony_ci	tst_res(TINFO, "Running eBPF with OOB");
127f08c3bdfSopenharmony_ci	bpf_run_prog(prog_fd, msg, sizeof(MSG));
128f08c3bdfSopenharmony_ci	tst_res(TINFO, "Ran eBPF");
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci	SAFE_CLOSE(prog_fd);
131f08c3bdfSopenharmony_ci}
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_cistatic struct tst_test test = {
134f08c3bdfSopenharmony_ci	.setup = setup,
135f08c3bdfSopenharmony_ci	.test_all = run,
136f08c3bdfSopenharmony_ci	.min_kver = "5.8",
137f08c3bdfSopenharmony_ci	.taint_check = TST_TAINT_W | TST_TAINT_D,
138f08c3bdfSopenharmony_ci	.caps = (struct tst_cap []) {
139f08c3bdfSopenharmony_ci		TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN),
140f08c3bdfSopenharmony_ci		TST_CAP(TST_CAP_DROP, CAP_BPF),
141f08c3bdfSopenharmony_ci		{}
142f08c3bdfSopenharmony_ci	},
143f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
144f08c3bdfSopenharmony_ci		{&key, .size = sizeof(*key)},
145f08c3bdfSopenharmony_ci		{&val, .size = sizeof(*val)},
146f08c3bdfSopenharmony_ci		{&log, .size = BUFSIZE},
147f08c3bdfSopenharmony_ci		{&attr, .size = sizeof(*attr)},
148f08c3bdfSopenharmony_ci		{&msg, .size = sizeof(MSG)},
149f08c3bdfSopenharmony_ci		{}
150f08c3bdfSopenharmony_ci	},
151f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
152f08c3bdfSopenharmony_ci		{"linux-git", "64620e0a1e71"},
153f08c3bdfSopenharmony_ci		{"CVE", "CVE-2021-4204"},
154f08c3bdfSopenharmony_ci		{}
155f08c3bdfSopenharmony_ci	}
156f08c3bdfSopenharmony_ci};
157