1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Richard Palethorpe <rpalethorpe@suse.com>
4f08c3bdfSopenharmony_ci * Original byte code was provided by jannh@google.com
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * Check for the bug fixed by 95a762e2c8c942780948091f8f2a4f32fce1ac6f
7f08c3bdfSopenharmony_ci * "bpf: fix incorrect sign extension in check_alu_op()"
8f08c3bdfSopenharmony_ci * CVE-2017-16995
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * This test is very similar to the reproducer found here:
11f08c3bdfSopenharmony_ci * https://bugs.chromium.org/p/project-zero/issues/detail?id=1454
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * However it has been modified to try to corrupt the map struct instead of
14f08c3bdfSopenharmony_ci * writing to a noncanonical pointer. This appears to be more reliable at
15f08c3bdfSopenharmony_ci * producing stack traces and confirms we would be able to overwrite the ops
16f08c3bdfSopenharmony_ci * function pointers, as suggested by Jan.
17f08c3bdfSopenharmony_ci *
18f08c3bdfSopenharmony_ci * If the eBPF code is loaded then this is considered a failure regardless of
19f08c3bdfSopenharmony_ci * whether it is able to cause any visible damage.
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include <string.h>
23f08c3bdfSopenharmony_ci#include <stdio.h>
24f08c3bdfSopenharmony_ci#include <inttypes.h>
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include "config.h"
27f08c3bdfSopenharmony_ci#include "tst_test.h"
28f08c3bdfSopenharmony_ci#include "tst_capability.h"
29f08c3bdfSopenharmony_ci#include "bpf_common.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#define LOG_SIZE (1024 * 1024)
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#define CHECK_BPF_RET(x) ((x) >= 0 || ((x) == -1 && errno != EPERM))
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic const char MSG[] = "Ahoj!";
36f08c3bdfSopenharmony_cistatic char *msg;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic char *log;
39f08c3bdfSopenharmony_cistatic uint32_t *key;
40f08c3bdfSopenharmony_cistatic uint64_t *val;
41f08c3bdfSopenharmony_cistatic union bpf_attr *attr;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic int load_prog(int fd)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	int ret;
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	struct bpf_insn insn[] = {
48f08c3bdfSopenharmony_ci		BPF_LD_MAP_FD(BPF_REG_1, fd),
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci		// fill r0 with pointer to map value
51f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_8, BPF_REG_10),
52f08c3bdfSopenharmony_ci		BPF_ALU64_IMM(BPF_ADD, BPF_REG_8, -4), // allocate 4 bytes stack
53f08c3bdfSopenharmony_ci		BPF_MOV32_IMM(BPF_REG_2, 0),
54f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_2, 0),
55f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_2, BPF_REG_8),
56f08c3bdfSopenharmony_ci		BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
57f08c3bdfSopenharmony_ci		BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
58f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_0, 0), // prepare exit
59f08c3bdfSopenharmony_ci		BPF_EXIT_INSN(), // exit
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci		// r1 = 0xffff'ffff, mistreated as 0xffff'ffff'ffff'ffff
62f08c3bdfSopenharmony_ci		BPF_MOV32_IMM(BPF_REG_1, -1),
63f08c3bdfSopenharmony_ci		// r1 = 0x1'0000'0000, mistreated as 0
64f08c3bdfSopenharmony_ci		BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
65f08c3bdfSopenharmony_ci		// r1 = 64, mistreated as 0
66f08c3bdfSopenharmony_ci		BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 26),
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci		// Write actual value of r1 to map for debugging this test
69f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci		// Corrupt the map meta-data which comes before the map data
72f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
73f08c3bdfSopenharmony_ci		BPF_ALU64_REG(BPF_SUB, BPF_REG_2, BPF_REG_1),
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_3, 0xdeadbeef),
76f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0),
77f08c3bdfSopenharmony_ci		BPF_ALU64_REG(BPF_SUB, BPF_REG_2, BPF_REG_1),
78f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0),
79f08c3bdfSopenharmony_ci		BPF_ALU64_REG(BPF_SUB, BPF_REG_2, BPF_REG_1),
80f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0),
81f08c3bdfSopenharmony_ci		BPF_ALU64_REG(BPF_SUB, BPF_REG_2, BPF_REG_1),
82f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0),
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci		// terminate to make the verifier happy
85f08c3bdfSopenharmony_ci		BPF_MOV32_IMM(BPF_REG_0, 0),
86f08c3bdfSopenharmony_ci		BPF_EXIT_INSN()
87f08c3bdfSopenharmony_ci	};
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	bpf_init_prog_attr(attr, insn, sizeof(insn), log, LOG_SIZE);
90f08c3bdfSopenharmony_ci	ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)),
91f08c3bdfSopenharmony_ci		CHECK_BPF_RET);
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	if (ret < -1)
94f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Invalid bpf() return value %d", ret);
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	if (ret >= 0) {
97f08c3bdfSopenharmony_ci		tst_res(TINFO, "Verification log:");
98f08c3bdfSopenharmony_ci		fputs(log, stderr);
99f08c3bdfSopenharmony_ci		return ret;
100f08c3bdfSopenharmony_ci	}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	if (log[0] == 0)
103f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "Failed to load program");
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	tst_res(TPASS | TERRNO, "Failed verification");
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 map_fd, prog_fd;
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci	map_fd = bpf_map_array_create(32);
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	memset(attr, 0, sizeof(*attr));
122f08c3bdfSopenharmony_ci	attr->map_fd = map_fd;
123f08c3bdfSopenharmony_ci	attr->key = ptr_to_u64(key);
124f08c3bdfSopenharmony_ci	attr->value = ptr_to_u64(val);
125f08c3bdfSopenharmony_ci	attr->flags = BPF_ANY;
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	TEST(bpf(BPF_MAP_UPDATE_ELEM, attr, sizeof(*attr)));
128f08c3bdfSopenharmony_ci	if (TST_RET == -1)
129f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "Failed to lookup map element");
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	prog_fd = load_prog(map_fd);
132f08c3bdfSopenharmony_ci	if (prog_fd == -1)
133f08c3bdfSopenharmony_ci		goto exit;
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci	tst_res(TFAIL, "Loaded bad eBPF, now we will run it and maybe crash");
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	bpf_run_prog(prog_fd, msg, sizeof(MSG));
138f08c3bdfSopenharmony_ci	SAFE_CLOSE(prog_fd);
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci	*key = 0;
141f08c3bdfSopenharmony_ci	bpf_map_array_get(map_fd, key, val);
142f08c3bdfSopenharmony_ci	tst_res(TINFO, "Pointer offset was 0x%"PRIx64, *val);
143f08c3bdfSopenharmony_ciexit:
144f08c3bdfSopenharmony_ci	SAFE_CLOSE(map_fd);
145f08c3bdfSopenharmony_ci}
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_cistatic struct tst_test test = {
148f08c3bdfSopenharmony_ci	.setup = setup,
149f08c3bdfSopenharmony_ci	.test_all = run,
150f08c3bdfSopenharmony_ci	.min_kver = "3.18",
151f08c3bdfSopenharmony_ci	.caps = (struct tst_cap []) {
152f08c3bdfSopenharmony_ci		TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN),
153f08c3bdfSopenharmony_ci		{}
154f08c3bdfSopenharmony_ci	},
155f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
156f08c3bdfSopenharmony_ci		{&key, .size = sizeof(*key)},
157f08c3bdfSopenharmony_ci		{&val, .size = sizeof(*val)},
158f08c3bdfSopenharmony_ci		{&log, .size = LOG_SIZE},
159f08c3bdfSopenharmony_ci		{&attr, .size = sizeof(*attr)},
160f08c3bdfSopenharmony_ci		{&msg, .size = sizeof(MSG)},
161f08c3bdfSopenharmony_ci		{},
162f08c3bdfSopenharmony_ci	},
163f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
164f08c3bdfSopenharmony_ci		{"linux-git", "95a762e2c8c9"},
165f08c3bdfSopenharmony_ci		{"CVE", "2017-16995"},
166f08c3bdfSopenharmony_ci		{}
167f08c3bdfSopenharmony_ci	}
168f08c3bdfSopenharmony_ci};
169