]> git.baikalelectronics.ru Git - kernel.git/commit
bpf: Fix potential memleak and UAF in the verifier.
authorHe Fengqing <hefengqing@huawei.com>
Wed, 14 Jul 2021 10:18:15 +0000 (10:18 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 15 Jul 2021 01:31:24 +0000 (18:31 -0700)
commit3f3dad2d2661df9ebb6b512e5b7973bd3c917f7c
treeabeb89b61f020596ba339199c539b93fff3565e1
parent3ec481b0bb4a880e814784cfaeba3212bfccfc41
bpf: Fix potential memleak and UAF in the verifier.

In bpf_patch_insn_data(), we first use the bpf_patch_insn_single() to
insert new instructions, then use adjust_insn_aux_data() to adjust
insn_aux_data. If the old env->prog have no enough room for new inserted
instructions, we use bpf_prog_realloc to construct new_prog and free the
old env->prog.

There have two errors here. First, if adjust_insn_aux_data() return
ENOMEM, we should free the new_prog. Second, if adjust_insn_aux_data()
return ENOMEM, bpf_patch_insn_data() will return NULL, and env->prog has
been freed in bpf_prog_realloc, but we will use it in bpf_check().

So in this patch, we make the adjust_insn_aux_data() never fails. In
bpf_patch_insn_data(), we first pre-malloc memory for the new
insn_aux_data, then call bpf_patch_insn_single() to insert new
instructions, at last call adjust_insn_aux_data() to adjust
insn_aux_data.

Fixes: 6ab69d505f8d ("bpf: adjust insn_aux_data when patching insns")
Signed-off-by: He Fengqing <hefengqing@huawei.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20210714101815.164322-1-hefengqing@huawei.com
kernel/bpf/verifier.c