1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2018 Jann Horn <jannh@google.com>
4f08c3bdfSopenharmony_ci * Copyright (c) 2020 SUSE LLC <mdoucha@suse.cz>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * CVE 2018-18445
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Check that eBPF verifier correctly handles 32-bit arithmetic, in particular
11f08c3bdfSopenharmony_ci * the right bit shift instruction. It is an error if the BPF program passes
12f08c3bdfSopenharmony_ci * verification regardless of whether it then causes any actual damage. Kernel
13f08c3bdfSopenharmony_ci * bug fixed in:
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *  commit b799207e1e1816b09e7a5920fbb2d5fcf6edd681
16f08c3bdfSopenharmony_ci *  Author: Jann Horn <jannh@google.com>
17f08c3bdfSopenharmony_ci *  Date:   Fri Oct 5 18:17:59 2018 +0200
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci *  bpf: 32-bit RSH verification must truncate input before the ALU op
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include <stdio.h>
23f08c3bdfSopenharmony_ci#include <string.h>
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include "config.h"
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci#include "tst_taint.h"
28f08c3bdfSopenharmony_ci#include "tst_capability.h"
29f08c3bdfSopenharmony_ci#include "bpf_common.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#define CHECK_BPF_RET(x) ((x) >= 0 || ((x) == -1 && errno != EACCES))
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic const char MSG[] = "Ahoj!";
34f08c3bdfSopenharmony_cistatic char *msg;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic char *log;
37f08c3bdfSopenharmony_cistatic union bpf_attr *attr;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic int load_prog(int fd)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	int ret;
42f08c3bdfSopenharmony_ci	struct bpf_insn insn[] = {
43f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_8, 2),
44f08c3bdfSopenharmony_ci		BPF_ALU64_IMM(BPF_LSH, BPF_REG_8, 31),
45f08c3bdfSopenharmony_ci		BPF_ALU32_IMM(BPF_RSH, BPF_REG_8, 31),
46f08c3bdfSopenharmony_ci		BPF_ALU32_IMM(BPF_SUB, BPF_REG_8, 2),
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci		// store r8 into map
49f08c3bdfSopenharmony_ci		BPF_LD_MAP_FD(BPF_REG_1, fd),
50f08c3bdfSopenharmony_ci		BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
51f08c3bdfSopenharmony_ci		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
52f08c3bdfSopenharmony_ci		BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
53f08c3bdfSopenharmony_ci		BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
54f08c3bdfSopenharmony_ci		BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
55f08c3bdfSopenharmony_ci		BPF_EXIT_INSN(),
56f08c3bdfSopenharmony_ci		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_8),
57f08c3bdfSopenharmony_ci		BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_8, 0),
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci		BPF_MOV64_IMM(BPF_REG_0, 0),
60f08c3bdfSopenharmony_ci		BPF_EXIT_INSN()
61f08c3bdfSopenharmony_ci	};
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	bpf_init_prog_attr(attr, insn, sizeof(insn), log, BUFSIZE);
64f08c3bdfSopenharmony_ci	ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)),
65f08c3bdfSopenharmony_ci		CHECK_BPF_RET);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (ret >= 0) {
68f08c3bdfSopenharmony_ci		tst_res(TINFO, "Verification log:");
69f08c3bdfSopenharmony_ci		fputs(log, stderr);
70f08c3bdfSopenharmony_ci		return ret;
71f08c3bdfSopenharmony_ci	}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	if (ret < -1)
74f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Invalid bpf() return value %d", ret);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	if (!*log)
77f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "Failed to load BPF program");
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	tst_res(TPASS | TERRNO, "BPF program failed verification");
80f08c3bdfSopenharmony_ci	return ret;
81f08c3bdfSopenharmony_ci}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic void setup(void)
84f08c3bdfSopenharmony_ci{
85f08c3bdfSopenharmony_ci	rlimit_bump_memlock();
86f08c3bdfSopenharmony_ci	memcpy(msg, MSG, sizeof(MSG));
87f08c3bdfSopenharmony_ci}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_cistatic void run(void)
90f08c3bdfSopenharmony_ci{
91f08c3bdfSopenharmony_ci	int map_fd, prog_fd;
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	map_fd = bpf_map_array_create(1);
94f08c3bdfSopenharmony_ci	prog_fd = load_prog(map_fd);
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	if (prog_fd >= 0) {
97f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Malicious eBPF code passed verification. "
98f08c3bdfSopenharmony_ci			"Now let's try crashing the kernel.");
99f08c3bdfSopenharmony_ci		bpf_run_prog(prog_fd, msg, sizeof(MSG));
100f08c3bdfSopenharmony_ci	}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	if (prog_fd >= 0)
103f08c3bdfSopenharmony_ci		SAFE_CLOSE(prog_fd);
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	SAFE_CLOSE(map_fd);
106f08c3bdfSopenharmony_ci}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_cistatic struct tst_test test = {
109f08c3bdfSopenharmony_ci	.setup = setup,
110f08c3bdfSopenharmony_ci	.test_all = run,
111f08c3bdfSopenharmony_ci	.min_kver = "3.18",
112f08c3bdfSopenharmony_ci	.taint_check = TST_TAINT_W | TST_TAINT_D,
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		{&log, .size = BUFSIZE},
119f08c3bdfSopenharmony_ci		{&attr, .size = sizeof(*attr)},
120f08c3bdfSopenharmony_ci		{&msg, .size = sizeof(MSG)},
121f08c3bdfSopenharmony_ci		{}
122f08c3bdfSopenharmony_ci	},
123f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
124f08c3bdfSopenharmony_ci		{"linux-git", "b799207e1e18"},
125f08c3bdfSopenharmony_ci		{"CVE", "2018-18445"},
126f08c3bdfSopenharmony_ci		{}
127f08c3bdfSopenharmony_ci	}
128f08c3bdfSopenharmony_ci};
129