Skip to content

Commit 3d2786d

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
bpf: correctly handle malformed BPF_CORE_TYPE_ID_LOCAL relos
In case of malformed relocation record of kind BPF_CORE_TYPE_ID_LOCAL referencing a non-existing BTF type, function bpf_core_calc_relo_insn would cause a null pointer deference. Fix this by adding a proper check upper in call stack, as malformed relocation records could be passed from user space. Simplest reproducer is a program: r0 = 0 exit With a single relocation record: .insn_off = 0, /* patch first instruction */ .type_id = 100500, /* this type id does not exist */ .access_str_off = 6, /* offset of string "0" */ .kind = BPF_CORE_TYPE_ID_LOCAL, See the link for original reproducer or next commit for a test case. Fixes: 74753e1 ("libbpf: Replace btf__type_by_id() with btf_type_by_id().") Reported-by: Liu RuiTong <[email protected]> Closes: https://lore.kernel.org/bpf/CAK55_s6do7C+DVwbwY_7nKfUz0YLDoiA1v6X3Y9+p0sWzipFSA@mail.gmail.com/ Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b6ab509 commit 3d2786d

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-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;

0 commit comments

Comments
 (0)