If a CRC is set in provided BTF, verify it. Suggested-by: Alexei Starovoitov <alexei.starovoitov@xxxxxxxxx> Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx> --- kernel/bpf/btf.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 554b5b795d6f..96c553e40b43 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -25,6 +25,7 @@ #include <linux/bsearch.h> #include <linux/kobject.h> #include <linux/sysfs.h> +#include <linux/crc32.h> #include <net/netfilter/nf_bpf_link.h> @@ -5354,6 +5355,20 @@ static int btf_parse_hdr(struct btf_verifier_env *env) return -ENOTSUPP; } + if (hdr->flags & BTF_FLAG_CRC_SET) { + __u32 check, crc = hdr->crc; + struct btf_header *h = btf->data; + + h->crc = 0; + check = crc32(0xffffffff, btf->data, btf_data_size); + check ^= ~0; + h->crc = crc; + if (check != crc) { + btf_verifier_log(env, "Invalid CRC; expected 0x%x ; actual 0x%x", + crc, check); + return -EINVAL; + } + } if (!btf->base_btf && btf_data_size == hdr->hdr_len) { btf_verifier_log(env, "No data"); return -EINVAL; -- 2.31.1