Re: [PATCH v12 bpf-next 6/8] btf: support kernel parsing of BTF with layout info

[email protected]
Newsgroups org.kernel.vger.dwarves,org.kernel.vger.bpf
Message-ID <a1a841b4bd59afe171bf85b06e723705403e1092c5c17a0466feb7114d189296@mail.kernel.org>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c

[ ... ]

> +static int btf_parse_layout_sec(struct btf_verifier_env *env)
> +{
> +	const struct btf_header *hdr = &env->btf->hdr;
> +	struct btf *btf = env->btf;
> +	void *start, *end;
> +
> +	if (hdr->hdr_len < sizeof(struct btf_header) ||
> +	    hdr->layout_len == 0)
> +		return 0;
> +
> +	/* Layout section must align to 4 bytes */
> +	if (hdr->layout_off & (sizeof(u32) - 1)) {
> +		btf_verifier_log(env, "Unaligned layout_off");
> +		return -EINVAL;
> +	}

When one of these validation errors fires, btf_verifier_log_hdr()
has already printed the header dump.  That function currently logs
type_off, type_len, str_off, and str_len, but not layout_off or
layout_len:

kernel/bpf/btf.c:btf_verifier_log_hdr() {
    ...
    __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
    __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
    __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
    __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
    __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
}

Should layout_off and layout_len be added to the header log, at
least when hdr_len >= sizeof(struct btf_header)?  Without them a
user who hits "Unaligned layout_off" or "Layout section is too
small" cannot see the actual values in the verifier output.

> +	start = btf->nohdr_data + hdr->layout_off;
> +	end = start + hdr->layout_len;
> +
> +	if (hdr->layout_len < sizeof(struct btf_layout)) {
> +		btf_verifier_log(env, "Layout section is too small");
> +		return -EINVAL;
> +	}
> +	if (hdr->layout_len % sizeof(struct btf_layout) != 0) {
> +		btf_verifier_log(env, "layout_len is not multiple of %zu",
> +				 sizeof(struct btf_layout));
> +		return -EINVAL;
> +	}
> +	if (end > btf->data + btf->data_size) {
> +		btf_verifier_log(env, "Layout section is too big");
> +		return -EINVAL;
> +	}
> +	btf->layout = start;
> +
> +	return 0;
> +}


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22637324186
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.