Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

libbpf: Fix BTF header parsing checks

Original code assumed fixed and correct BTF header length. That's not
always the case, though, so fix this bug with a proper additional check.
And use actual header length instead of sizeof(struct btf_header) in
sanity checks.

Fixes: 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")
Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211023003157.726961-2-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
c825f5fe 5245dafe

+9 -3
+9 -3
tools/lib/bpf/btf.c
··· 236 236 } 237 237 btf_bswap_hdr(hdr); 238 238 } else if (hdr->magic != BTF_MAGIC) { 239 - pr_debug("Invalid BTF magic:%x\n", hdr->magic); 239 + pr_debug("Invalid BTF magic: %x\n", hdr->magic); 240 240 return -EINVAL; 241 241 } 242 242 243 - meta_left = btf->raw_size - sizeof(*hdr); 243 + if (btf->raw_size < hdr->hdr_len) { 244 + pr_debug("BTF header len %u larger than data size %u\n", 245 + hdr->hdr_len, btf->raw_size); 246 + return -EINVAL; 247 + } 248 + 249 + meta_left = btf->raw_size - hdr->hdr_len; 244 250 if (meta_left < (long long)hdr->str_off + hdr->str_len) { 245 - pr_debug("Invalid BTF total size:%u\n", btf->raw_size); 251 + pr_debug("Invalid BTF total size: %u\n", btf->raw_size); 246 252 return -EINVAL; 247 253 } 248 254