Skip to content

Commit 5148f19

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-null-pointer-access-for-malformed-bpf_core_type_id_local-relos'
Eduard Zingerman says: ==================== bpf: fix null pointer access for malformed BPF_CORE_TYPE_ID_LOCAL relos Liu RuiTong reported an in-kernel null pointer derefence when processing BPF_CORE_TYPE_ID_LOCAL relocations referencing non-existing BTF types. Fix this by adding proper id checks. Changes v2->v3: - selftest update suggested by Andrii: avoid memset(0) for log buffer and do memset(0) for bpf_attr. Changes v1->v2: - moved check from bpf_core_calc_relo_insn() to bpf_core_apply() now both in kernel and in libbpf relocation type id is guaranteed to exist when bpf_core_calc_relo_insn() is called; - added a test case. v1: https://lore.kernel.org/bpf/[email protected]/ v2: https://lore.kernel.org/bpf/[email protected]/ ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents b6ab509 + 110bbd3 commit 5148f19

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

kernel/bpf/btf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8910,6 +8910,7 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
89108910
struct bpf_core_cand_list cands = {};
89118911
struct bpf_core_relo_res targ_res;
89128912
struct bpf_core_spec *specs;
8913+
const struct btf_type *type;
89138914
int err;
89148915

89158916
/* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5"
@@ -8919,6 +8920,13 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
89198920
if (!specs)
89208921
return -ENOMEM;
89218922

8923+
type = btf_type_by_id(ctx->btf, relo->type_id);
8924+
if (!type) {
8925+
bpf_log(ctx->log, "relo #%u: bad type id %u\n",
8926+
relo_idx, relo->type_id);
8927+
return -EINVAL;
8928+
}
8929+
89228930
if (need_cands) {
89238931
struct bpf_cand_cache *cc;
89248932
int i;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/* Test cases that can't load programs using libbpf and need direct
4+
* BPF syscall access
5+
*/
6+
7+
#include <sys/syscall.h>
8+
#include <bpf/libbpf.h>
9+
#include <bpf/btf.h>
10+
11+
#include "test_progs.h"
12+
#include "test_btf.h"
13+
#include "bpf/libbpf_internal.h"
14+
15+
static char log[16 * 1024];
16+
17+
/* Check that verifier rejects BPF program containing relocation
18+
* pointing to non-existent BTF type.
19+
*/
20+
static void test_bad_local_id(void)
21+
{
22+
struct test_btf {
23+
struct btf_header hdr;
24+
__u32 types[15];
25+
char strings[128];
26+
} raw_btf = {
27+
.hdr = {
28+
.magic = BTF_MAGIC,
29+
.version = BTF_VERSION,
30+
.hdr_len = sizeof(struct btf_header),
31+
.type_off = 0,
32+
.type_len = sizeof(raw_btf.types),
33+
.str_off = offsetof(struct test_btf, strings) -
34+
offsetof(struct test_btf, types),
35+
.str_len = sizeof(raw_btf.strings),
36+
},
37+
.types = {
38+
BTF_PTR_ENC(0), /* [1] void* */
39+
BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [2] int */
40+
BTF_FUNC_PROTO_ENC(2, 1), /* [3] int (*)(void*) */
41+
BTF_FUNC_PROTO_ARG_ENC(8, 1),
42+
BTF_FUNC_ENC(8, 3) /* [4] FUNC 'foo' type_id=2 */
43+
},
44+
.strings = "\0int\0 0\0foo\0"
45+
};
46+
__u32 log_level = 1 | 2 | 4;
47+
LIBBPF_OPTS(bpf_btf_load_opts, opts,
48+
.log_buf = log,
49+
.log_size = sizeof(log),
50+
.log_level = log_level,
51+
);
52+
struct bpf_insn insns[] = {
53+
BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0),
54+
BPF_EXIT_INSN(),
55+
};
56+
struct bpf_func_info funcs[] = {
57+
{
58+
.insn_off = 0,
59+
.type_id = 4,
60+
}
61+
};
62+
struct bpf_core_relo relos[] = {
63+
{
64+
.insn_off = 0, /* patch first instruction (r0 = 0) */
65+
.type_id = 100500, /* !!! this type id does not exist */
66+
.access_str_off = 6, /* offset of "0" */
67+
.kind = BPF_CORE_TYPE_ID_LOCAL,
68+
}
69+
};
70+
union bpf_attr attr;
71+
int saved_errno;
72+
int prog_fd = -1;
73+
int btf_fd = -1;
74+
75+
btf_fd = bpf_btf_load(&raw_btf, sizeof(raw_btf), &opts);
76+
saved_errno = errno;
77+
if (btf_fd < 0 || env.verbosity > VERBOSE_NORMAL) {
78+
printf("-------- BTF load log start --------\n");
79+
printf("%s", log);
80+
printf("-------- BTF load log end ----------\n");
81+
}
82+
if (btf_fd < 0) {
83+
PRINT_FAIL("bpf_btf_load() failed, errno=%d\n", saved_errno);
84+
return;
85+
}
86+
87+
log[0] = 0;
88+
memset(&attr, 0, sizeof(attr));
89+
attr.prog_btf_fd = btf_fd;
90+
attr.prog_type = BPF_TRACE_RAW_TP;
91+
attr.license = (__u64)"GPL";
92+
attr.insns = (__u64)&insns;
93+
attr.insn_cnt = sizeof(insns) / sizeof(*insns);
94+
attr.log_buf = (__u64)log;
95+
attr.log_size = sizeof(log);
96+
attr.log_level = log_level;
97+
attr.func_info = (__u64)funcs;
98+
attr.func_info_cnt = sizeof(funcs) / sizeof(*funcs);
99+
attr.func_info_rec_size = sizeof(*funcs);
100+
attr.core_relos = (__u64)relos;
101+
attr.core_relo_cnt = sizeof(relos) / sizeof(*relos);
102+
attr.core_relo_rec_size = sizeof(*relos);
103+
prog_fd = sys_bpf_prog_load(&attr, sizeof(attr), 1);
104+
saved_errno = errno;
105+
if (prog_fd < 0 || env.verbosity > VERBOSE_NORMAL) {
106+
printf("-------- program load log start --------\n");
107+
printf("%s", log);
108+
printf("-------- program load log end ----------\n");
109+
}
110+
if (prog_fd >= 0) {
111+
PRINT_FAIL("sys_bpf_prog_load() expected to fail\n");
112+
goto out;
113+
}
114+
ASSERT_HAS_SUBSTR(log, "relo #0: bad type id 100500", "program load log");
115+
116+
out:
117+
close(prog_fd);
118+
close(btf_fd);
119+
}
120+
121+
void test_core_reloc_raw(void)
122+
{
123+
if (test__start_subtest("bad_local_id"))
124+
test_bad_local_id();
125+
}

0 commit comments

Comments
 (0)