Skip to content

eBPF-based security platform

libbpf: read a packed enum value without shifting a __u64 by 64

Daniel Borkmann3051bccf5371
diff --git a/testing/fuzz/corpus/rodata/misaligned-enum b/testing/fuzz/corpus/rodata/misaligned-enum
new file mode 100644
index 00000000..1c0d5f48
Binary files /dev/null and b/testing/fuzz/corpus/rodata/misaligned-enum differ
diff --git a/vendor/libbpf/README.md b/vendor/libbpf/README.md
index 55d46e4c..b2980566 100644
--- a/vendor/libbpf/README.md
+++ b/vendor/libbpf/README.md
@@ -33,7 +33,10 @@ commit: `MAX_USED_PROGS` is 64 instead of 32, as required by the
kernel-obs package. In addition `gen_loader.c` also supports reusing a
signer-selected map FD supplied through the loader context.
`bpf/btf_dump.c` preserves complete native GNU attribute spellings without
-wrapping them twice. `include/uapi/linux/types.h` defines `__s128` and
+wrapping them twice, and reads an enum value at an address its size does
+not divide by copying it to an aligned buffer, as it reads such an int,
+where upstream reads it as a bitfield of no bits and shifts a `__u64` by
+64. `include/uapi/linux/types.h` defines `__s128` and
`__u128` as the kernel's own `include/uapi/linux/types.h` does: it
shadows the system's copy, and the arm64 `asm/sigcontext.h` of current
kernel headers, which `<signal.h>` pulls in, uses `__u128`. In the
diff --git a/vendor/libbpf/bpf/btf_dump.c b/vendor/libbpf/bpf/btf_dump.c
index b7ac879e..5002338e 100644
--- a/vendor/libbpf/bpf/btf_dump.c
+++ b/vendor/libbpf/bpf/btf_dump.c
@@ -2326,16 +2326,12 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
__s64 *value)
{
bool is_signed = btf_kflag(t);
+ __u64 buf;
- if (!ptr_is_aligned(d->btf, id, data)) {
- __u64 val;
- int err;
-
- err = btf_dump_get_bitfield_value(d, t, data, 0, 0, &val);
- if (err)
- return err;
- *value = (__s64)val;
- return 0;
+ /* packed enum data, as btf_dump_int_data() handles packed ints */
+ if (!ptr_is_aligned(d->btf, id, data) && t->size <= sizeof(buf)) {
+ memcpy(&buf, data, t->size);
+ data = &buf;
}
switch (t->size) {