libbpf: Support sanitization of BTF layout for older kernels

Add a FEAT_BTF_LAYOUT feature check which checks if the
kernel supports BTF layout information.  Also sanitize
BTF if it contains layout data but the kernel does not
support it.  The sanitization requires rewriting raw
BTF data to update the header and eliminate the layout
section (since it lies between the types and strings),
so refactor sanitization to do the raw BTF retrieval
and creation of updated BTF, returning that new BTF
on success.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20260326145444.2076244-7-alan.maguire@oracle.com
This commit is contained in:
Alan Maguire authored and Andrii Nakryiko committed 2026-04-09 15:12:21 -07:00
1 parent c95ad95d0a
commit fd04b4eae1
4 files changed
+131 -40

No files matched your search

+29
View File
@@ -589,6 +589,32 @@ static int probe_uprobe_syscall(int token_fd)
}
#endif
static int probe_kern_btf_layout(int token_fd)
{
static const char strs[] = "\0int";
__u32 types[] = {
/* int */
BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
};
struct btf_layout layout[] = {
{ 0, 0, 0 },
{ sizeof(__u32), 0, 0 },
};
struct btf_header hdr = {
.magic = BTF_MAGIC,
.version = BTF_VERSION,
.hdr_len = sizeof(struct btf_header),
.type_len = sizeof(types),
.str_off = sizeof(types) + sizeof(layout),
.str_len = sizeof(strs),
.layout_off = sizeof(types),
.layout_len = sizeof(layout),
};
return probe_fd(libbpf__load_raw_btf_hdr(&hdr, (char *)types, strs,
(char *)layout, token_fd));
}
typedef int (*feature_probe_fn)(int /* token_fd */);
static struct kern_feature_cache feature_cache;
@@ -670,6 +696,9 @@ static struct kern_feature_desc {
[FEAT_UPROBE_SYSCALL] = {
"kernel supports uprobe syscall", probe_uprobe_syscall,
},
[FEAT_BTF_LAYOUT] = {
"kernel supports BTF layout", probe_kern_btf_layout,
},
};
bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id)