1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2021 SUSE LLC <rpalethorpe@suse.com>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Compare the effects of 32-bit div/mod by zero with the "expected"
10f08c3bdfSopenharmony_ci * behaviour.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * The commit "bpf: fix subprog verifier bypass by div/mod by 0
13f08c3bdfSopenharmony_ci * exception", changed div/mod by zero from exiting the current
14f08c3bdfSopenharmony_ci * program to setting the destination register to zero (div) or
15f08c3bdfSopenharmony_ci * leaving it untouched (mod).
16f08c3bdfSopenharmony_ci *
17f08c3bdfSopenharmony_ci * This solved one verfier bug which allowed dodgy pointer values, but
18f08c3bdfSopenharmony_ci * it turned out that the source register was being 32-bit truncated
19f08c3bdfSopenharmony_ci * when it should not be. Also the destination register for mod was
20f08c3bdfSopenharmony_ci * not being truncated when it should be.
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci * So then we have the following two fixes:
23f08c3bdfSopenharmony_ci * "bpf: Fix 32 bit src register truncation on div/mod"
24f08c3bdfSopenharmony_ci * "bpf: Fix truncation handling for mod32 dst reg wrt zero"
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * Testing for all of these issues is a problem. Not least because
27f08c3bdfSopenharmony_ci * division by zero is undefined, so in theory any result is
28f08c3bdfSopenharmony_ci * acceptable so long as the verifier and runtime behaviour
29f08c3bdfSopenharmony_ci * match.
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * However to keep things simple we just check if the source and
32f08c3bdfSopenharmony_ci * destination register runtime values match the current upstream
33f08c3bdfSopenharmony_ci * behaviour at the time of writing.
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci * If the test fails you may have one or more of the above patches
36f08c3bdfSopenharmony_ci * missing. In this case it is possible that you are not vulnerable
37f08c3bdfSopenharmony_ci * depending on what other backports and fixes have been applied. If
38f08c3bdfSopenharmony_ci * upstream changes the behaviour of division by zero, then the test
39f08c3bdfSopenharmony_ci * will need updating.
40f08c3bdfSopenharmony_ci *
41f08c3bdfSopenharmony_ci * Note that we use r6 as the src register and r7 as the dst. w6 and
42f08c3bdfSopenharmony_ci * w7 are the same registers treated as 32bit.
43f08c3bdfSopenharmony_ci */
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci#include <stdio.h>
46f08c3bdfSopenharmony_ci#include <string.h>
47f08c3bdfSopenharmony_ci#include <inttypes.h>
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci#include "config.h"
50f08c3bdfSopenharmony_ci#include "tst_test.h"
51f08c3bdfSopenharmony_ci#include "tst_taint.h"
52f08c3bdfSopenharmony_ci#include "tst_capability.h"
53f08c3bdfSopenharmony_ci#include "bpf_common.h"
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic const char MSG[] = "Ahoj!";
56f08c3bdfSopenharmony_cistatic char *msg;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic int map_fd;
59f08c3bdfSopenharmony_cistatic uint32_t *key;
60f08c3bdfSopenharmony_cistatic uint64_t *val;
61f08c3bdfSopenharmony_cistatic char *log;
62f08c3bdfSopenharmony_cistatic union bpf_attr *attr;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_cistatic void ensure_ptr_arithmetic(void)
65f08c3bdfSopenharmony_ci{
66f08c3bdfSopenharmony_ci	const struct bpf_insn prog_insn[] = {
67f08c3bdfSopenharmony_ci		/* r2 = r10
68f08c3bdfSopenharmony_ci		 * r3 = -1
69f08c3bdfSopenharmony_ci		 * r2 += r3
70f08c3bdfSopenharmony_ci		 * *(char *)r2 = 0
71f08c3bdfSopenharmony_ci		 */
72f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
73f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_3, -1),
74f08c3bdfSopenharmony_ci		BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
75f08c3bdfSopenharmony_ci		BPF_ST_MEM(BPF_B, BPF_REG_2, 0, 0),
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci		/* exit(0) */
78f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_0, 0),
79f08c3bdfSopenharmony_ci		BPF_EXIT_INSN()
80f08c3bdfSopenharmony_ci	};
81f08c3bdfSopenharmony_ci	int ret;
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	bpf_init_prog_attr(attr, prog_insn, sizeof(prog_insn), log, BUFSIZE);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)),
86f08c3bdfSopenharmony_ci				       TST_RETVAL_GE0);
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	if (ret >= 0) {
89f08c3bdfSopenharmony_ci		tst_res(TINFO, "Have pointer arithmetic");
90f08c3bdfSopenharmony_ci		SAFE_CLOSE(ret);
91f08c3bdfSopenharmony_ci		return;
92f08c3bdfSopenharmony_ci	}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	if (ret != -1)
95f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Invalid bpf() return value: %d", ret);
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	if (log[0] != 0)
98f08c3bdfSopenharmony_ci		tst_brk(TCONF | TERRNO, "No pointer arithmetic:\n %s", log);
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	tst_brk(TBROK | TERRNO, "Failed to load program");
101f08c3bdfSopenharmony_ci}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_cistatic int load_prog(void)
104f08c3bdfSopenharmony_ci{
105f08c3bdfSopenharmony_ci	const struct bpf_insn prog_insn[] = {
106f08c3bdfSopenharmony_ci		/* r6 = 1 << 32
107f08c3bdfSopenharmony_ci		 * r7 = -1
108f08c3bdfSopenharmony_ci		 */
109f08c3bdfSopenharmony_ci		BPF_LD_IMM64(BPF_REG_6, 1ULL << 32),
110f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_7, -1LL),
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci		/* w7 /= w6 */
113f08c3bdfSopenharmony_ci		BPF_ALU32_REG(BPF_DIV, BPF_REG_7, BPF_REG_6),
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci		/* map[1] = r6
116f08c3bdfSopenharmony_ci		 * map[2] = r7
117f08c3bdfSopenharmony_ci		 */
118f08c3bdfSopenharmony_ci		BPF_MAP_ARRAY_STX(map_fd, 0, BPF_REG_6),
119f08c3bdfSopenharmony_ci		BPF_MAP_ARRAY_STX(map_fd, 1, BPF_REG_7),
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci		/* r6 = 1 << 32
122f08c3bdfSopenharmony_ci		 * r7 = -1
123f08c3bdfSopenharmony_ci		 */
124f08c3bdfSopenharmony_ci		BPF_LD_IMM64(BPF_REG_6, 1ULL << 32),
125f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_7, -1LL),
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci		/* w7 %= w6 */
128f08c3bdfSopenharmony_ci		BPF_ALU32_REG(BPF_MOD, BPF_REG_7, BPF_REG_6),
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci		/* map[3] = r6
131f08c3bdfSopenharmony_ci		 * map[4] = r7
132f08c3bdfSopenharmony_ci		 */
133f08c3bdfSopenharmony_ci		BPF_MAP_ARRAY_STX(map_fd, 2, BPF_REG_6),
134f08c3bdfSopenharmony_ci		BPF_MAP_ARRAY_STX(map_fd, 3, BPF_REG_7),
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci		/* exit(0) */
137f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_0, 0),
138f08c3bdfSopenharmony_ci		BPF_EXIT_INSN()
139f08c3bdfSopenharmony_ci	};
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	bpf_init_prog_attr(attr, prog_insn, sizeof(prog_insn), log, BUFSIZE);
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci	return bpf_load_prog(attr, log);
144f08c3bdfSopenharmony_ci}
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_cistatic void expect_reg_val(const char *const reg_name,
147f08c3bdfSopenharmony_ci			   const uint64_t expected_val)
148f08c3bdfSopenharmony_ci{
149f08c3bdfSopenharmony_ci	bpf_map_array_get(map_fd, key, val);
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci	(*key)++;
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci	if (*val != expected_val) {
154f08c3bdfSopenharmony_ci		tst_res(TFAIL,
155f08c3bdfSopenharmony_ci			"%s = %"PRIu64", but should be %"PRIu64,
156f08c3bdfSopenharmony_ci			reg_name, *val, expected_val);
157f08c3bdfSopenharmony_ci	} else {
158f08c3bdfSopenharmony_ci		tst_res(TPASS, "%s = %"PRIu64, reg_name, *val);
159f08c3bdfSopenharmony_ci	}
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_cistatic void setup(void)
163f08c3bdfSopenharmony_ci{
164f08c3bdfSopenharmony_ci	rlimit_bump_memlock();
165f08c3bdfSopenharmony_ci	memcpy(msg, MSG, sizeof(MSG));
166f08c3bdfSopenharmony_ci}
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_cistatic void run(void)
169f08c3bdfSopenharmony_ci{
170f08c3bdfSopenharmony_ci	int prog_fd;
171f08c3bdfSopenharmony_ci
172f08c3bdfSopenharmony_ci	map_fd = bpf_map_array_create(8);
173f08c3bdfSopenharmony_ci
174f08c3bdfSopenharmony_ci	ensure_ptr_arithmetic();
175f08c3bdfSopenharmony_ci	prog_fd = load_prog();
176f08c3bdfSopenharmony_ci	bpf_run_prog(prog_fd, msg, sizeof(MSG));
177f08c3bdfSopenharmony_ci	SAFE_CLOSE(prog_fd);
178f08c3bdfSopenharmony_ci
179f08c3bdfSopenharmony_ci	*key = 0;
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_ci	tst_res(TINFO, "Check w7(-1) /= w6(0) [r7 = -1, r6 = 1 << 32]");
182f08c3bdfSopenharmony_ci	expect_reg_val("src(r6)", 1ULL << 32);
183f08c3bdfSopenharmony_ci	expect_reg_val("dst(r7)", 0);
184f08c3bdfSopenharmony_ci
185f08c3bdfSopenharmony_ci	tst_res(TINFO, "Check w7(-1) %%= w6(0) [r7 = -1, r6 = 1 << 32]");
186f08c3bdfSopenharmony_ci	expect_reg_val("src(r6)", 1ULL << 32);
187f08c3bdfSopenharmony_ci	expect_reg_val("dst(r7)", (uint32_t)-1);
188f08c3bdfSopenharmony_ci
189f08c3bdfSopenharmony_ci	SAFE_CLOSE(map_fd);
190f08c3bdfSopenharmony_ci}
191f08c3bdfSopenharmony_ci
192f08c3bdfSopenharmony_cistatic struct tst_test test = {
193f08c3bdfSopenharmony_ci	.setup = setup,
194f08c3bdfSopenharmony_ci	.test_all = run,
195f08c3bdfSopenharmony_ci	.min_kver = "3.18",
196f08c3bdfSopenharmony_ci	.taint_check = TST_TAINT_W | TST_TAINT_D,
197f08c3bdfSopenharmony_ci	.caps = (struct tst_cap []) {
198f08c3bdfSopenharmony_ci		TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN),
199f08c3bdfSopenharmony_ci		TST_CAP(TST_CAP_DROP, CAP_BPF),
200f08c3bdfSopenharmony_ci		{}
201f08c3bdfSopenharmony_ci	},
202f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
203f08c3bdfSopenharmony_ci		{&key, .size = sizeof(*key)},
204f08c3bdfSopenharmony_ci		{&val, .size = sizeof(*val)},
205f08c3bdfSopenharmony_ci		{&log, .size = BUFSIZE},
206f08c3bdfSopenharmony_ci		{&attr, .size = sizeof(*attr)},
207f08c3bdfSopenharmony_ci		{&msg, .size = sizeof(MSG)},
208f08c3bdfSopenharmony_ci		{}
209f08c3bdfSopenharmony_ci	},
210f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
211f08c3bdfSopenharmony_ci		{"linux-git", "f6b1b3bf0d5f"},
212f08c3bdfSopenharmony_ci		{"linux-git", "468f6eafa6c4"},
213f08c3bdfSopenharmony_ci		{"linux-git", "e88b2c6e5a4d"},
214f08c3bdfSopenharmony_ci		{"linux-git", "9b00f1b78809"},
215f08c3bdfSopenharmony_ci		{"CVE", "CVE-2021-3444"},
216f08c3bdfSopenharmony_ci		{}
217f08c3bdfSopenharmony_ci	}
218f08c3bdfSopenharmony_ci};
219