selftests/bpf: Test signed loader error paths

The positive path for signed BPF loaders is covered today by the
signed lskels (fentry_test, fexit_test, atomics).

But the runtime metadata check the generated loader performs (libbpf
gen_loader's emit_signature_match), the map content hash it relies
on, the load-time signature, and the immutability invariants of its
metadata map are not yet covered.

Thus, add a new, extensive test suite which drives libbpf's gen_loader
(bpf_object__gen_loader, gen_hash=true), the same machinery which
bpftool uses for signed light skeletons, and exercise corner cases
so that we can assert this in BPF CI:

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t signed_loader
  [...]
  [    1.840842] clocksource: Switched to clocksource tsc
  #405/1   signed_loader/metadata_check_shape:OK
  #405/2   signed_loader/metadata_match:OK
  #405/3   signed_loader/metadata_sha_mismatch:OK
  #405/4   signed_loader/metadata_not_exclusive:OK
  #405/5   signed_loader/metadata_hash_not_computed:OK
  #405/6   signed_loader/signature_enforced:OK
  #405/7   signed_loader/signature_too_large:OK
  #405/8   signed_loader/signature_bad_keyring:OK
  #405/9   signed_loader/metadata_ctx_max_entries_ignored:OK
  #405/10  signed_loader/metadata_ctx_initial_value_ignored:OK
  #405/11  signed_loader/signature_authenticates_insns:OK
  #405/12  signed_loader/hash_requires_frozen:OK
  #405/13  signed_loader/no_update_after_freeze:OK
  #405/14  signed_loader/freeze_writable_mmap:OK
  #405/15  signed_loader/no_writable_mmap_frozen:OK
  #405/16  signed_loader/map_hash_matches_libbpf:OK
  #405/17  signed_loader/map_hash_multi_element:OK
  #405/18  signed_loader/map_hash_bad_size:OK
  #405/19  signed_loader/map_hash_unsupported_type:OK
  #405     signed_loader:OK
  Summary: 1/19 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20260603211658.471212-2-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Daniel Borkmann 2026-06-03 23:16:58 +02:00 committed by Alexei Starovoitov
parent e87d898bc7
commit 5b88319e47
4 changed files with 1079 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
/*
* Minimal, map-less program. Driven through libbpf's gen_loader (gen_hash)
* by prog_tests/signed_loader.c so the generated light-skeleton loader (with
* the emit_signature_match metadata check) can be exercised against good
* and tampered metadata. A socket filter needs no load-time attach resolution,
* and having no maps keeps the generated loader's ctx trivial (0 maps, 1 prog).
*/
SEC("socket")
int probe(void *ctx)
{
return 0;
}
char _license[] SEC("license") = "GPL";

View File

@ -0,0 +1,20 @@
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
/*
* A single initialized global, so the generated loader has one internal
* (.data) map that it seeds with an initial value while loading.
* prog_tests/signed_loader.c uses this to check that a signed loader
* keeps the attested contents and ignores a ctx-supplied initial_value:
* the host cannot re-seed a signed program's maps through the loader ctx.
*/
__u64 magic = 0x5eed1234abad1deaULL;
SEC("socket")
int probe(void *ctx)
{
return (int)magic;
}
char _license[] SEC("license") = "GPL";

View File

@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
/*
* One explicit array map and no global variables, so the generated loader
* has exactly one map to create (no .rodata/.bss). prog_tests/signed_loader.c
* uses this to check that a signed loader ignores ctx-supplied max_entries:
* the map must keep its attested size (4), not whatever the host puts in
* the loader ctx.
*/
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 4);
__type(key, __u32);
__type(value, __u64);
} amap SEC(".maps");
SEC("socket")
int probe(void *ctx)
{
__u32 key = 0;
__u64 *val = bpf_map_lookup_elem(&amap, &key);
return val ? (int)*val : 0;
}
char _license[] SEC("license") = "GPL";