Verify BPF signed loader at load time
Subsystems (8)
Daniel Borkmann
maintainerv2 cover Verify BPF signed loader at load time
The BPF signing scheme signs a light skeleton's loader program and lets the loader vouch for everything else: bpftool bakes the SHA256 of the metadata map into the loader's instructions, signs the instructions, and the loader compares the (frozen, exclusive) map against that hash from within BPF once it runs. The construction is sound as a trusted hash chain, but the kernel itself never attests the metadata, and that split has been the recurring objection from the LSM / integrity side since the scheme was proposed.
This proposal closes both gaps by having the kernel verify the metadata at BPF_PROG_LOAD time, before the LSM admission hook and before the verifier, /without/ growing the UAPI. A signed loader binds its metadata map(s) through the existing fd_array/fd_array_cnt, and exclusive maps are already bound to the loader's digest via excl_prog_hash. When a signature is present, the kernel collects the exclusive maps from the fd_array and appends their frozen contents to the instructions before PKCS#7 verification, so the signature covers ...
insns || metadata_0 || metadata_1 || [...]
... in fd_array order. The in-loader hash check is dropped from the gen_loader entirely: generated loaders carry no verification logic anymore, and signing or verifying a skeleton becomes an ordinary CMS operation over bytes that sit verbatim in the skeleton, reproducible offline. A signed program is either BPF_SIG_UNSIGNED or BPF_SIG_VERIFIED with nothing in between.
There is no new UAPI, we now have a single signature scheme, no LSM code reaching into BPF internals, no new LSM hook, and unsigned loads are completely unaffected. It is also less complex since the loader does not need to deal with BTF, an extra kfunc, etc, as proposed in an earlier series [0]. Tested against full BPF CI which came back green. For more details and examples, see the documentation patch in this series.
[0] https://lore.kernel.org/bpf/[email protected]/
v1 -> v2:
- Addressed both sashiko complaints, the TOCTOU bug regarding fd_array processing, as well as exclusive map checking to only allow array maps. The validation is now moved into the verifier before the main verification work happens. This also gives the opportunity to utilize the verifier log.
Daniel Borkmann (5):
bpf: Verify signed loader metadata at load time
libbpf: Drop in-loader metadata check for load-time verification
bpftool: Cover loader metadata with the program signature selftests/bpf: Verify load-time signed loader metadata Documentation/bpf: Add BPF signing and enforcement doc
Documentation/bpf/index.rst | 1 + Documentation/bpf/signing.rst | 490 ++++++++++++++ include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 81 +-- kernel/bpf/verifier.c | 163 ++++- tools/bpf/bpftool/gen.c | 2 + tools/bpf/bpftool/sign.c | 15 +- tools/lib/bpf/bpf_gen_internal.h | 1 - tools/lib/bpf/gen_loader.c | 76 +-- tools/lib/bpf/skel_internal.h | 27 +- .../selftests/bpf/prog_tests/signed_loader.c | 597 +++++++++++++----- 11 files changed, 1114 insertions(+), 340 deletions(-) create mode 100644 Documentation/bpf/signing.rst
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH v2 1/5 bpf: Verify signed loader metadata at load time
A signed gen_loader program carries the programs, maps and relocations it installs in a metadata array map. The loader instructions are covered by the PKCS#7 signature, but the metadata map is not: Today the loader compares the map contents from within BPF against a hash baked into its (signed) instructions, using the kernel-cached map hash. The kernel itself never actually attests that the metadata the loader installs is the metadata that was signed.
This split is the core of the long-standing objection to the BPF signing scheme from the LSM / integrity side: the integrity check of a light skeleton only completes once the loader program runs, that is, after the security_bpf_prog_load() hook, so at admission time an LSM observes a program whose payload has not yet been verified [0]. Auditing the chain link is also not a purely cryptographic operation: whoever signs or reviews an lskel has to disassemble the loader's preamble to convince themselves that the embedded hash check is present and correct [1][2]. Two acceptable fixes were identified in those threads: Complete the integrity check before the admission hook fires, or add a second hook that collects the verification result after the loader ran [3]. Let's implement the former, without growing the UAPI.
A signed loader binds its metadata map(s) through the existing fd_array, and an exclusive map is already bound to a program digest (excl_prog_hash). So when a signature is present, collect the exclusive maps from fd_array and append their frozen contents to the instructions before verification:
the signature now covers insns || metadata_0 || metadata_1 || [...] in the fd_array order, and verification completes in bpf_check(), once the fd_array maps are resolved into used_maps, before the LSM admission hook and the rest of verification.
A program is either BPF_SIG_UNSIGNED or BPF_SIG_VERIFIED, with nothing in between. While folding the fd_array maps, a non-exclusive map bound to a signed program is rejected, so every map folded into the signature is exclusive. A signed loader that fails to cover its metadata thus does not load, and BPF_SIG_VERIFIED always means the instructions and every exclusive map are authentic.
The maps must be frozen so the hashed bytes cannot change before the loader runs; the map <-> program digest binding is enforced by the verifier for every used map. Binding maps through fd_array_cnt makes the verifier resolve and excl-check them (excl_prog_sha vs prog->digest) before it would otherwise compute the digest, so compute prog->digest up front in bpf_check(), over the unmodified instructions the signature covers, for a load that folds metadata.
Unsigned programs are not affected. Note, signed loaders generated by older libbpf/bpftool versions need to be regenerated; some of the recent fixes we've had on the signed loader side require the latter already to close gaps.
--- include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 76 +--------------- kernel/bpf/verifier.c | 163 ++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 75 deletions(-)
include/linux/bpf_verifier.h +1 -0
@@ -939,6 +939,7 @@ struct bpf_verifier_env {bool bypass_spec_v4;bool seen_direct_write;bool seen_exception; +bool check_signature;struct bpf_insn_aux_data *insn_aux_data;/* array of per-insn state */ const struct bpf_line_info *prev_linfo;struct bpf_verifier_log log;
kernel/bpf/syscall.c +2 -74
100 lines not on this page · raw message
kernel/bpf/verifier.c +162 -1
204 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] TOCTOU race in BPF signed program verification allows bypassing signature via concurrent…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] TOCTOU race in BPF signed program verification allows bypassing signature via concurrent modification of user-space fd_array. --
commit e61d684bd0467f62e035ea0592a8066b9c7246fb
Author: Daniel Borkmann <[email protected]>
bpf: Verify signed loader metadata at load time
This patch modifies the BPF verifier to perform integrity checking of a signed program's metadata array maps before the LSM admission hook fires. It achieves this by resolving the maps from the fd_array, folding their frozen contents into the signature verification process, and validating the map count.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 20 lines
kernel/bpf/verifier.c +11
-0@@ -19895,11 +19895,28 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_unlock; +if (env->check_signature) { + ret =bpf_prog_calc_tag (env->prog); +if (ret <0 ) + goto skip_full_check; + } ret =process_fd_array (env, attr, uattr);if (ret) goto skip_full_check; +if (env->check_signature) { + ret =bpf_prog_verify_signature (env, attr, uattr.is_kernel); +if (ret) + goto skip_full_check; + signed_map_cnt = env->used_map_cnt; + }
[ ... ]
kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · line 19970
Quoted 7 lines
kernel/bpf/verifier.c +2
-1@@ -19966,7 +19966,14 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =check_and_resolve_insns (env);if (ret <0 ) goto skip_full_check; - +if (env->prog->aux->sig.verdict ==BPF_SIG_VERIFIED && + (env->used_map_cnt != signed_map_cnt || env->used_btf_cnt)) {
[Severity: High]
Is there a TOCTOU race with how fd_array is processed between these checks?
If an attacker concurrently modifies the fd_array in user memory after process_fd_array() runs but before check_and_resolve_insns() reads it again:
1. process_fd_array() reads the array from user space and populates env->used_maps with MAP_A and MAP_B.
2. bpf_prog_verify_signature() successfully verifies the signature over these specific maps, and signed_map_cnt is set to 2.
3. The attacker modifies the user-space fd_array memory (e.g., swapping the FDs for MAP_A and MAP_B, or duplicating MAP_B).
4. check_and_resolve_insns() reads the modified fd_array from user space again via copy_from_bpfptr_offset(&fd, env->fd_array, ...) and binds the swapped maps to the program's pseudo instructions.
5. This final integrity check (env->used_map_cnt != signed_map_cnt) still passes because the total map count remains unchanged, but the cryptographic bindings have been completely bypassed.
Would it be safer to cache the fd_array in kernel memory during the initial process_fd_array() call so that check_and_resolve_insns() does not need to read from user space a second time?
Quoted 6 lines
kernel/bpf/verifier.c +6
-0+verbose (env,"signed program uses %s not covered by the signature\n" , + env->used_map_cnt != signed_map_cnt ? + (env->used_btf_cnt ?"maps and BTF" :"maps" ) :"BTF" ); + ret = -EACCES ; + goto skip_full_check; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 2 lines · On 6/24/26 4:48 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] TOCTOU race in BPF signed program verification allows bypassing signature via concurrent modification of user-spacefd_array.
For the v3, I'll rework this slightly, and I'm looking into potentially caching the fd_array entries into kernel memory at process_fd_array() time. As it stands, the TOCTOU as described could only swap maps that were part of the signature and had the exclusive-digest binding, and given the loader only uses 1 metadata map today, there is nothing to permute. But in either case, I'll address it.
Paul Moore
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
In reply to Daniel BorkmannQuoted 61 lines · Daniel Borkmann
A signed
gen_loaderprogram carries the programs, maps and relocations it installs in a metadata array map. The loader instructions are covered by the PKCS#7 signature, but the metadata map is not: Today the loader compares the map contents from within BPF against a hash baked into its (signed) instructions, using the kernel-cached map hash. The kernel itself never actually attests that the metadata the loader installs is the metadata that was signed.This split is the core of the long-standing objection to the BPF signing scheme from the LSM / integrity side: the integrity check of a light skeleton only completes once the loader program runs, that is, after the
security_bpf_prog_load()hook, so at admission time an LSM observes a program whose payload has not yet been verified [0]. Auditing the chain link is also not a purely cryptographic operation: whoever signs or reviews an lskel has to disassemble the loader's preamble to convince themselves that the embedded hash check is present and correct [1][2]. Two acceptable fixes were identified in those threads: Complete the integrity check before the admission hook fires, or add a second hook that collects the verification result after the loader ran [3]. Let's implement the former, without growing the UAPI.A signed loader binds its metadata map(s) through the existing
fd_array, and an exclusive map is already bound to a program digest (excl_prog_hash). So when a signature is present, collect the exclusive maps fromfd_arrayand append their frozen contents to the instructions before verification:
the signature now covers insns ||metadata_0||metadata_1|| [...] in thefd_arrayorder, and verification completes inbpf_check(), once thefd_arraymaps are resolved intoused_maps, before the LSM admission hook and the rest of verification.A program is either
BPF_SIG_UNSIGNEDorBPF_SIG_VERIFIED, with nothing in between. While folding thefd_arraymaps, a non-exclusive map bound to a signed program is rejected, so every map folded into the signature is exclusive. A signed loader that fails to cover its metadata thus does not load, andBPF_SIG_VERIFIEDalways means the instructions and every exclusive map are authentic.The maps must be frozen so the hashed bytes cannot change before the loader runs; the map <-> program digest binding is enforced by the verifier for every used map. Binding maps through
fd_array_cntmakes the verifier resolve and excl-check them (excl_prog_shavsprog->digest) before it would otherwise compute the digest, so computeprog->digestup front inbpf_check(), over the unmodified instructions the signature covers, for a load that folds metadata.Unsigned programs are not affected. Note, signed loaders generated by older libbpf/bpftool versions need to be regenerated; some of the recent fixes we've had on the signed loader side require the latter already to close gaps.
Signed-off-by Daniel Borkmann <[email protected]>Link https://lore.kernel.org/bpf/CAHC9VhSDkwGgPfrBUh7EgBKEJj_JjnY68c0YAmuuLT_i--GskQ@mail.gmail.com [0] open hereLink https://lore.kernel.org/bpf/2f71d6c03698eb17d51f7247efde777627ee578a.camel@HansenPartnership.com [1] open here--- include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 76 +--------------- kernel/bpf/verifier.c | 163 ++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 75 deletions(-)
...
Quoted 5 lines
kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at · line 3126
Quoted 10 lines
kernel/bpf/syscall.c +0
-4@@ -3189,10 +3121,6 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_atif (err <0 ) goto free_prog; - err =security_bpf_prog_load (prog, attr, token, uattr.is_kernel); -if (err) - goto free_prog; -/* run eBPF verifier */ err =bpf_check (&prog, attr, uattr, attr_log);if (err <0 )
We must preserve the existing location of the call into the security_bpf_prog_load() hook as some users rely on this hook being called *before* the verifier runs.
Quoted 19 lines
kernel/bpf/verifier.c +6
-0@@ -19758,11 +19895,28 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_unlock; +if (env->check_signature) { + ret =bpf_prog_calc_tag (env->prog); +if (ret <0 ) + goto skip_full_check; + } ret =process_fd_array (env, attr, uattr);if (ret) goto skip_full_check; +if (env->check_signature) {
kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · line 19918
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ ret =bpf_prog_verify_signature (env, attr, uattr.is_kernel); +if (ret) + goto skip_full_check; + signed_map_cnt = env->used_map_cnt; + } + + ret =security_bpf_prog_load (env->prog, attr, env->prog->aux->token, + uattr.is_kernel); +if (ret) + goto skip_full_check;
We can always create a new LSM hook for this call site, e.g. security_bpf_prog_verify_signature(...).
Quoted 3 lines
kernel/bpf/verifier.c +0
-0mark_verifier_state_clean (env);if (IS_ERR (btf_vmlinux)) {
Signature · 1 line
paul-moore.com
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Hi Paul,
Quoted 1 line · Paul Moore
On Wed, Jun 24, 2026 at 10:03 AM Daniel Borkmann <[email protected]> wrote:
[...]
Quoted 15 lines
Quoted 4 lines
include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 76 +--------------- kernel/bpf/verifier.c | 163 ++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 75 deletions(-)...
Quoted 8 lines
kernel/bpf/syscall.c +0
-0@@ -3189,10 +3121,6 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_atif (err <0 ) goto free_prog;
Quoted 11 lines
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at · line 3126
Quoted 7 lines
kernel/bpf/syscall.c +0
-4- err =security_bpf_prog_load (prog, attr, token, uattr.is_kernel); -if (err) - goto free_prog; -/* run eBPF verifier */ err =bpf_check (&prog, attr, uattr, attr_log);if (err <0 )We must preserve the existing location of the call into the
security_bpf_prog_load()hook as some users rely on this hook being called *before* the verifier runs.
Keep in mind that the verifier /at this point/ of the new location did _not_ verify anything. So there is no heavy-duty work happening yet at security_bpf_prog_load. The work that is done before security_bpf_prog_load is basically setting up the env, initializing the verifier log, and doing the process_fd_array which is resolving the map/BTF objects. But it did not walk any instructions etc, so semantics of the security_bpf_prog_load hook did not change from a user PoV.
Quoted 37 lines
Quoted 19 lines
kernel/bpf/verifier.c +6
-0@@ -19758,11 +19895,28 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_unlock; +if (env->check_signature) { + ret =bpf_prog_calc_tag (env->prog); +if (ret <0 ) + goto skip_full_check; + } ret =process_fd_array (env, attr, uattr);if (ret) goto skip_full_check; +if (env->check_signature) {kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · line 19918
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ ret =bpf_prog_verify_signature (env, attr, uattr.is_kernel); +if (ret) + goto skip_full_check; + signed_map_cnt = env->used_map_cnt; + } + + ret =security_bpf_prog_load (env->prog, attr, env->prog->aux->token, + uattr.is_kernel); +if (ret) + goto skip_full_check;We can always create a new LSM hook for this call site, e.g.
security_bpf_prog_verify_signature(...).Quoted 3 lines
kernel/bpf/verifier.c +0
-0mark_verifier_state_clean (env);if (IS_ERR (btf_vmlinux)) {
Paul Moore
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 30 lines · Daniel Borkmann
Quoted 1 line · Paul Moore
On Wed, Jun 24, 2026 at 10:03 AM Daniel Borkmann <[email protected]> wrote:
[...]
Quoted 15 lines
Quoted 4 lines
include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 76 +--------------- kernel/bpf/verifier.c | 163 ++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 75 deletions(-)...
Quoted 8 lines
kernel/bpf/syscall.c +0
-0@@ -3189,10 +3121,6 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_atif (err <0 ) goto free_prog;Quoted 11 lines
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at · line 3126
Quoted 7 lines
kernel/bpf/syscall.c +0
-4- err =security_bpf_prog_load (prog, attr, token, uattr.is_kernel); -if (err) - goto free_prog; -/* run eBPF verifier */ err =bpf_check (&prog, attr, uattr, attr_log);if (err <0 )We must preserve the existing location of the call into the
security_bpf_prog_load()hook as some users rely on this hook being called *before* the verifier runs.
Quoted 7 lines
Keep in mind that the verifier /at this point/ of the new location did _not_ verify anything. So there is no heavy-duty work happening yet at
security_bpf_prog_load. The work that is done beforesecurity_bpf_prog_loadis basically setting up the env, initializing the verifier log, and doing theprocess_fd_arraywhich is resolving the map/BTF objects. But it did not walk any instructions etc, so semantics of thesecurity_bpf_prog_loadhook did not change from a user PoV.
There is still a reasonable amount of work between the existing and new call sites, and the existing location outside of bpf_check() offers an additional robustness benefit that future verifier changes are less likely to impact the hook. If I'm completely honest, I also need to consider the events of the past year and a half; I'm now much less inclined to support LSM hook changes in the BPF subsystem because I'm very concerned about our ability to revert/modify those changes in the future if needed. That doesn't mean I won't support LSM hook changes in BPF, but such changes are going to need to have a *very* strong advantage from a LSM perspective to offset the risk associated with the current BPF subsystem.
Based on what I see in this patchset, the security_bpf_prog_load() call should remain in the current location. If you need an additional hook after the bpf_prog_verify_signature() call I'm happy to work with you on that.
I also have to bring up the same question I asked back in your v1
posting: have you discussed this signature approach with Alexei? Your patches abandon and remove KP's signature scheme in favor of what is effectively Blaise's signature scheme from last fall; Alexei argued very strongly against these changes in the past. I'd hate to spend a lot more time reviewing and discussing patches that Alexei is simply going to NACK once again.
Quoted 36 lines
Quoted 36 lines
Quoted 19 lines
kernel/bpf/verifier.c +6
-0@@ -19758,11 +19895,28 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_unlock; +if (env->check_signature) { + ret =bpf_prog_calc_tag (env->prog); +if (ret <0 ) + goto skip_full_check; + } ret =process_fd_array (env, attr, uattr);if (ret) goto skip_full_check; +if (env->check_signature) {kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · line 19918
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ ret =bpf_prog_verify_signature (env, attr, uattr.is_kernel); +if (ret) + goto skip_full_check; + signed_map_cnt = env->used_map_cnt; + } + + ret =security_bpf_prog_load (env->prog, attr, env->prog->aux->token, + uattr.is_kernel); +if (ret) + goto skip_full_check;We can always create a new LSM hook for this call site, e.g.
security_bpf_prog_verify_signature(...).Quoted 3 lines
kernel/bpf/verifier.c +0
-0mark_verifier_state_clean (env);if (IS_ERR (btf_vmlinux)) {
Signature · 1 line
paul-moore.com
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 39 lines · Paul Moore
Quoted 30 lines · Daniel Borkmann
Quoted 1 line · Paul Moore
On Wed, Jun 24, 2026 at 10:03 AM Daniel Borkmann <[email protected]> wrote:
[...]
Quoted 15 lines
Quoted 4 lines
include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 76 +--------------- kernel/bpf/verifier.c | 163 ++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 75 deletions(-)...
Quoted 8 lines
kernel/bpf/syscall.c +0
-0@@ -3189,10 +3121,6 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_atif (err <0 ) goto free_prog;Quoted 11 lines
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at · line 3126
Quoted 7 lines
kernel/bpf/syscall.c +0
-4- err =security_bpf_prog_load (prog, attr, token, uattr.is_kernel); -if (err) - goto free_prog; -/* run eBPF verifier */ err =bpf_check (&prog, attr, uattr, attr_log);if (err <0 )We must preserve the existing location of the call into the
security_bpf_prog_load()hook as some users rely on this hook being called *before* the verifier runs.Quoted 7 lines
Keep in mind that the verifier /at this point/ of the new location did _not_ verify anything. So there is no heavy-duty work happening yet at
security_bpf_prog_load. The work that is done beforesecurity_bpf_prog_loadis basically setting up the env, initializing the verifier log, and doing theprocess_fd_arraywhich is resolving the map/BTF objects. But it did not walk any instructions etc, so semantics of thesecurity_bpf_prog_loadhook did not change from a user PoV.
Quoted 11 lines
There is still a reasonable amount of work between the existing and new call sites, and the existing location outside of
bpf_check()offers an additional robustness benefit that future verifier changes are less likely to impact the hook. If I'm completely honest, I also need to consider the events of the past year and a half; I'm now much less inclined to support LSM hook changes in the BPF subsystem because I'm very concerned about our ability to revert/modify those changes in the future if needed. That doesn't mean I won't support LSM hook changes in BPF, but such changes are going to need to have a *very* strong advantage from a LSM perspective to offset the risk associated with the current BPF subsystem.
From where you sit with regards to LSMs that is a natural stance towards all kernel code, but coming back to the LSM hook, to me this is way too excessive that we should add *yet another* LSM hook. So, just for loading a *single* BPF program we would then need to pass through *four* layers of LSM hooks:
1) security_bpf (cmd=PROG_LOAD): for gating various bpf subcmds
2) security_bpf_prog_load: historical admission hook (CAP/token, prog_type, attach point), pre-verification
3) security_bpf_prog_verify_signature: newly asked admission hook, same role as 2), plus the BPF signature verdict
4) security_bpf_prog: gate handing the prog fd back to userspace, verification done & signature verified
The use-cases of 2) and 3) conflate. I strongly prefer to just keep a total of 3 LSM hooks (as-is today): 3) makes 2) incoherent given they are the /same class/ of hook, that is, access-control admission on the load and split only by _what_ they can see. Worse, with the split, for a signed BPF program security_bpf_prog_load 2) admits a program whose signature has not been checked, so a policy gating at 2) is structurally unable to express "admit only verified" and every such policy is forced onto 3) *anyway*. In other words, you don't get two complementary hooks, but rather, you get one real admission hook aka 3) plus a now-degraded /legacy/ hook 2) that can't answer the question operators actually want to ask. So, no, we're not adding yet another LSM hook.
Quoted 4 lines
Based on what I see in this patchset, the
security_bpf_prog_load()call should remain in the current location. If you need an additional hook after thebpf_prog_verify_signature()call I'm happy to work with you on that.
See above.
Quoted 7 lines
I also have to bring up the same question I asked back in your v1
posting: have you discussed this signature approach with Alexei? Your patches abandon and remove KP's signature scheme in favor of what is effectively Blaise's signature scheme from last fall; Alexei argued very strongly against these changes in the past. I'd hate to spend a lot more time reviewing and discussing patches that Alexei is simply going to NACK once again.
I think last time I already stated that this is not "effectively Blaise's signature scheme" for couple of reasons: i) we sign over the raw bytes, not the derived hash anymore, so the hashing is only used in the context to tie the map to the loader program, but not anymore for the signature. ii) its one /single scheme/ and not a parallel branch, so the main loader is built upon the updated signing scheme rather than having this as an option on the side; in other words, this replaces the in-loader check and there's a single PKCS#7-over-bytes path, not an 'if (signature_maps_size)' fork; and iii) given we expose the verification result in the BPF prog, we also don't need a new LSM hook and can just piggy back on the existing security_bpf_prog which also has the possibility to still reject late at this point.
From where you started out back then, it was the stance that while the original KP approach generically addresses all the use cases for loading BPF related to relocations via the lskel loader, Blaise proposed a parallel scheme which would only allow static programs (only insns, no maps) which is 1% of use cases it covers for the BPF ecosystem and users are stuck on figuring out which approach they need to go with. So when I took over the BTF series with the extra kfunc that KP proposed, I was mainly looking at that series trying to figure out how we can get away without pulling in BTF complexity for the signed loader and with a compromise that would potentially satisfy all parties under a unified signature scheme and having the kernel side (rather than loader side) providing the hard guarantee. So I cannot directly speak for Alexei/KP, but I think this proposal should satisfy all parties under one roof. I've build out user space tooling in addition to test real world BPF program to make sure they work in combination with maps and BTF under this scheme as well as map-less BPF programs.
Quoted 37 lines
Quoted 36 lines
Quoted 36 lines
Quoted 19 lines
kernel/bpf/verifier.c +6
-0@@ -19758,11 +19895,28 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_unlock; +if (env->check_signature) { + ret =bpf_prog_calc_tag (env->prog); +if (ret <0 ) + goto skip_full_check; + } ret =process_fd_array (env, attr, uattr);if (ret) goto skip_full_check; +if (env->check_signature) {kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · line 19918
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ ret =bpf_prog_verify_signature (env, attr, uattr.is_kernel); +if (ret) + goto skip_full_check; + signed_map_cnt = env->used_map_cnt; + } + + ret =security_bpf_prog_load (env->prog, attr, env->prog->aux->token, + uattr.is_kernel); +if (ret) + goto skip_full_check;We can always create a new LSM hook for this call site, e.g.
security_bpf_prog_verify_signature(...).Quoted 3 lines
kernel/bpf/verifier.c +0
-0mark_verifier_state_clean (env);if (IS_ERR (btf_vmlinux)) {
Paul Moore
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 52 lines · Daniel Borkmann
Quoted 39 lines · Paul Moore
Quoted 30 lines · Daniel Borkmann
Quoted 1 line · Paul Moore
On Wed, Jun 24, 2026 at 10:03 AM Daniel Borkmann <[email protected]> wrote:
[...]
Quoted 15 lines
Quoted 4 lines
include/linux/bpf_verifier.h | 1 + kernel/bpf/syscall.c | 76 +--------------- kernel/bpf/verifier.c | 163 ++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 75 deletions(-)...
Quoted 8 lines
kernel/bpf/syscall.c +0
-0@@ -3189,10 +3121,6 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_atif (err <0 ) goto free_prog;Quoted 11 lines
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at · line 3126
Quoted 7 lines
kernel/bpf/syscall.c +0
-4- err =security_bpf_prog_load (prog, attr, token, uattr.is_kernel); -if (err) - goto free_prog; -/* run eBPF verifier */ err =bpf_check (&prog, attr, uattr, attr_log);if (err <0 )We must preserve the existing location of the call into the
security_bpf_prog_load()hook as some users rely on this hook being called *before* the verifier runs.Quoted 7 lines
Keep in mind that the verifier /at this point/ of the new location did _not_ verify anything. So there is no heavy-duty work happening yet at
security_bpf_prog_load. The work that is done beforesecurity_bpf_prog_loadis basically setting up the env, initializing the verifier log, and doing theprocess_fd_arraywhich is resolving the map/BTF objects. But it did not walk any instructions etc, so semantics of thesecurity_bpf_prog_loadhook did not change from a user PoV.Quoted 11 lines
There is still a reasonable amount of work between the existing and new call sites, and the existing location outside of
bpf_check()offers an additional robustness benefit that future verifier changes are less likely to impact the hook. If I'm completely honest, I also need to consider the events of the past year and a half; I'm now much less inclined to support LSM hook changes in the BPF subsystem because I'm very concerned about our ability to revert/modify those changes in the future if needed. That doesn't mean I won't support LSM hook changes in BPF, but such changes are going to need to have a *very* strong advantage from a LSM perspective to offset the risk associated with the current BPF subsystem.
Quoted 3 lines
From where you sit with regards to LSMs that is a natural stance towards
all kernel code, but coming back to the LSM hook, to me this is way too excessive that we should add *yet another* LSM hook ...
For all the reasons I gave previously, I can't support moving the existing security_bpf_prog_load() hook at this point in time. However, unlike Alexei, I am willing to work with you to develop a new LSM hook to meet your new needs for enforcing signed BPF program policies via an LSM (BPF or otherwise). If, as you say, you are not willing to add a new hook, you will need to find a way to make it work with the existing hooks/placements.
Quoted 10 lines
Quoted 7 lines
I also have to bring up the same question I asked back in your v1
posting: have you discussed this signature approach with Alexei? Your patches abandon and remove KP's signature scheme in favor of what is effectively Blaise's signature scheme from last fall; Alexei argued very strongly against these changes in the past. I'd hate to spend a lot more time reviewing and discussing patches that Alexei is simply going to NACK once again.I think last time I already stated that this is not "effectively Blaise's signature scheme" for couple of reasons ...
I already responded to these three points in your last patchset. It was sent to you directly, as well as to all of the relevant lists (it was a reply-all), but here is a lore link if you haven't read it:
Quoted 4 lines
From where you started out back then, it was the stance that while the
original KP approach generically addresses all the use cases for loading BPF related to relocations via the lskel loader, Blaise proposed a parallel scheme which would only allow static programs (only insns, no maps) ...
I'm guessing you still haven't looked at Blaise's patchset from last September. You were CC'd on the original posting, and I sent you a lore link in our discussion of your v1 patchset, but I guess it's easy to get busy/distracted and lose track of things. Regardless, here is another link to Blaise's patchset:
https://lore.kernel.org/linux-security-module/[email protected]/
Blaise's patchset proposed a scheme which ran the PKCS7 signature over the lskel loader and maps in a way *very* similar to what you are proposing. Blaise's patchset also supported the same key selection as KP's scheme so user and session signatures were supported without issue.
Quoted 3 lines
... So I cannot
directly speak for Alexei/KP, but I think this proposal should satisfy all parties under one roof.
Considering the striking similarities between what you are proposing and what Blaise proposed last September I *strongly* suggest getting a basic thumbs-up or thumbs-down from Alexei on this new/old approach on-list. As you can see from the lore archives, he has vehemently opposed the approach you are proposing for quite a while. If he has changed his mind to understand the value in Blaise's approach of running the PKCS7 signature over both the lskel loader and maps that's great, but I worry that will not be the case.
Signature · 1 line
paul-moore.com
Re: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 3 lines · Paul Moore
For all the reasons I gave previously, I can't support moving the existing
security_bpf_prog_load()hook at this point in time.
Paul,
it's not up to you to approve or deny where security_bpf_prog_load() is called within bpf subsystem as long as it doesn't affect behavior. Daniel's patch doesn't change observable state from LSMs pov. It merely moves the call from syscall.c to verifier.c.
So we're going to proceed.
Quoted 2 lines
I'm guessing you still haven't looked at Blaise's patchset from last September.
Blaise approach was Nacked because you guys ignored TOCTOU issue. I pointed it a year ago before AI was a thing. Then sashiko pointed it again and the bot explained it in detail. It was again ignored.
Daniel's v1 sadly had the same issue and sashiko spotted it too. Hence v2 is moving the location of security_bpf_prog_load().
Quoted 2 lines
on-list. As you can see from the lore archives, he has vehemently opposed the approach you are proposing for quite a while.
Exactly, because you kept ignoring TOCTOU issue. Claiming support for signed bpf that can be easily defeated is a shameless security scam.
Paul Moore
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 10 lines · Alexei Starovoitov
Quoted 3 lines · Paul Moore
For all the reasons I gave previously, I can't support moving the existing
security_bpf_prog_load()hook at this point in time.Paul,
it's not up to you to approve or deny wheresecurity_bpf_prog_load()is called within bpf subsystem as long as it doesn't affect behavior. Daniel's patch doesn't change observable state from LSMs pov. It merely moves the call fromsyscall.ctoverifier.c.
Alexei,
It is my responsibility to speak up and voice my opinion about LSM hook placement; arguably that is one of the LSM maintainer's larger responsibilities. Non-trivial work, including several allocations (which can be quite large in some cases), occurs between the current placement of security_bpf_prog_load() and Daniel's proposed location. We must preserve the existing security_bpf_prog_load() call site.
Quoted 1 line
So we're going to proceed.
Oh goodie, will the fun ever stop?
Signature · 1 line
paul-moore.com
Re: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 14 lines · Paul Moore
Quoted 10 lines · Alexei Starovoitov
Quoted 3 lines · Paul Moore
For all the reasons I gave previously, I can't support moving the existing
security_bpf_prog_load()hook at this point in time.Paul,
it's not up to you to approve or deny wheresecurity_bpf_prog_load()is called within bpf subsystem as long as it doesn't affect behavior. Daniel's patch doesn't change observable state from LSMs pov. It merely moves the call fromsyscall.ctoverifier.c.
Quoted 7 lines
Alexei,
It is my responsibility to speak up and voice my opinion about LSM hook placement; arguably that is one of the LSM maintainer's larger responsibilities. Non-trivial work, including several allocations (which can be quite large in some cases), occurs between the current placement ofsecurity_bpf_prog_load()and Daniel's proposed location. We must preserve the existingsecurity_bpf_prog_load()call site.
I don't think you read the patch because you're saying nonsense.
Paul Moore
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 23 lines · Alexei Starovoitov
Quoted 14 lines · Paul Moore
Quoted 10 lines · Alexei Starovoitov
Quoted 3 lines · Paul Moore
For all the reasons I gave previously, I can't support moving the existing
security_bpf_prog_load()hook at this point in time.Paul,
it's not up to you to approve or deny wheresecurity_bpf_prog_load()is called within bpf subsystem as long as it doesn't affect behavior. Daniel's patch doesn't change observable state from LSMs pov. It merely moves the call fromsyscall.ctoverifier.c.Quoted 7 lines
Alexei,
It is my responsibility to speak up and voice my opinion about LSM hook placement; arguably that is one of the LSM maintainer's larger responsibilities. Non-trivial work, including several allocations (which can be quite large in some cases), occurs between the current placement ofsecurity_bpf_prog_load()and Daniel's proposed location. We must preserve the existingsecurity_bpf_prog_load()call site.
Quoted 1 line
I don't think you read the patch because you're saying nonsense.
I've read the patch, as well as the code between the existing and proposed call sites that is outside the patch's context, that is the basis of my comment.
--
paul-moore.com
Re: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
In reply to Alexei StarovoitovQuoted 13 lines · Alexei Starovoitov
Quoted 3 lines · Paul Moore
For all the reasons I gave previously, I can't support moving the existing
security_bpf_prog_load()hook at this point in time.Paul,
it's not up to you to approve or deny wheresecurity_bpf_prog_load()is called within bpf subsystem as long as it doesn't affect behavior. Daniel's patch doesn't change observable state from LSMs pov. It merely moves the call fromsyscall.ctoverifier.c.
So we're going to proceed.
Quoted 8 lines
Quoted 2 lines
I'm guessing you still haven't looked at Blaise's patchset from last September.
Blaise approach was Nacked because you guys ignored TOCTOU issue. I pointed it a year ago before AI was a thing. Then sashiko pointed it again and the bot explained it in detail. It was again ignored.
Agreed, with Alexei: I like Daniel's solution because it avoids the complexity of machine independent BTF for loader programs and still addresses the signing goal, without the TOCTOU. I am okay with the current implementation as well. I think having kfuncs in loader programs would be useful, but we don't need to rush it or tie it to the signing effort.
- KP
Quoted 10 lines
Daniel's v1 sadly had the same issue and sashiko spotted it too. Hence v2 is moving the location of
security_bpf_prog_load().Quoted 2 lines
on-list. As you can see from the lore archives, he has vehemently opposed the approach you are proposing for quite a while.
Exactly, because you kept ignoring TOCTOU issue. Claiming support for signed bpf that can be easily defeated is a shameless security scam.
Paul Moore
maintainerRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
Quoted 22 lines · KP Singh
Quoted 12 lines · Alexei Starovoitov
Quoted 3 lines · Paul Moore
For all the reasons I gave previously, I can't support moving the existing
security_bpf_prog_load()hook at this point in time.Paul,
it's not up to you to approve or deny wheresecurity_bpf_prog_load()is called within bpf subsystem as long as it doesn't affect behavior. Daniel's patch doesn't change observable state from LSMs pov. It merely moves the call fromsyscall.ctoverifier.c.
So we're going to proceed.Quoted 7 lines
Quoted 2 lines
I'm guessing you still haven't looked at Blaise's patchset from last September.
Blaise approach was Nacked because you guys ignored TOCTOU issue. I pointed it a year ago before AI was a thing. Then sashiko pointed it again and the bot explained it in detail. It was again ignored.
Quoted 6 lines
Agreed, with Alexei: I like Daniel's solution because it avoids the complexity of machine independent BTF for loader programs and still addresses the signing goal, without the TOCTOU. I am okay with the current implementation as well. I think having kfuncs in loader programs would be useful, but we don't need to rush it or tie it to the signing effort.
As I've said a couple of times now in Daniel's approach, I like the basic idea in Daniel's approach; it aligns with the same scheme we have been advocating for all along: run the PKCS7 signature over both the lskel loader and the maps. I know you keep mentioning a TOCTOU bug in that September patchset, but the main objection to all the different variants of Blaise's patches was always the same basic idea:
you and Alexei wanted to verify the maps in the lskel loader BPF code, while we wanted to directly verify the maps as part of the PKCS7 signature. Any TOCTOU issues were implementation details; the difference in the signature scheme was the sticking point in many of the arguments. For whatever it is worth, you and Alexei were not immune to TOCTOU issues either. The frozen map problem was mentioned on-list and ignored. We had to submit a PoC/report to security@kernel to get it taken seriously and patched.
Regardless, of how we got here and whether or not you recognize Daniel's signature scheme as a retread of Blaise's scheme, the point is that you've come around to a concept we have advocated for over a year. If you plan to merge it, good (although I do wonder what Linus will think of the userspace breakage caused by abandoning the signature scheme that shipped in v6.18, maybe it's not a big deal, who knows). However, and this is important, we can't move the security_bpf_prog_load() function to where Daniel proposed in his v2 patchset. The issue is that non-trivial work happens, e.g. the potential allocation of a buffer to hold up to one million eBPF instructions, and we want to ensure we have an LSM access control point before that happens. If you, Daniel, and Alexei want to move the bpf_prog_verify_signature() call into the verifier that's fine. However, the security_bpf_prog_load() call needs to stay where it is. It served an important purpose well before signed BPF programs were supported, and adding BPF signatures doesn't invalidate that original purpose. If you want an LSM hook *after* the bpf_prog_verify_signature() call and you oppose adding a new hook for this new functionality, use the security_bpf_prog() hook. It isn't quite as conceptually clean of a solution as adding a new hook, but it should work to allow you to control access to a BPF program based on the signature verification result.
I'll support Daniel's patchset as long as you don't tamper with the security_bpf_prog_load() hook. If you move the hook to where it is currently proposed in Daniel's v2 patchset I will have to object. Do we really want to keep going around on this stupid, petty carousel of bickering and constant NACKs?
Signature · 1 line
paul-moore.com
[email protected]
AI reviewRe: [PATCH bpf-next v2 1/5] bpf: Verify signed loader metadata at load time
In reply to Daniel BorkmannThis reworks the machinery added by 63a673e8a411 ("bpf: Expose signature verdict via bpf_prog_aux"), which introduced the prog->aux->sig verdict and ran verification before security_bpf_prog_load()…
Quoted 45 lines
A signed
gen_loaderprogram carries the programs, maps and relocations it installs in a metadata array map. The loader instructions are covered by the PKCS#7 signature, but the metadata map is not: Today the loader compares the map contents from within BPF against a hash baked into its (signed) instructions, using the kernel-cached map hash. The kernel itself never actually attests that the metadata the loader installs is the metadata that was signed.This split is the core of the long-standing objection to the BPF signing scheme from the LSM / integrity side: the integrity check of a light skeleton only completes once the loader program runs, that is, after the
security_bpf_prog_load()hook, so at admission time an LSM observes a program whose payload has not yet been verified [0]. Auditing the chain link is also not a purely cryptographic operation: whoever signs or reviews an lskel has to disassemble the loader's preamble to convince themselves that the embedded hash check is present and correct [1][2]. Two acceptable fixes were identified in those threads: Complete the integrity check before the admission hook fires, or add a second hook that collects the verification result after the loader ran [3]. Let's implement the former, without growing the UAPI.A signed loader binds its metadata map(s) through the existing
fd_array, and an exclusive map is already bound to a program digest (excl_prog_hash). So when a signature is present, collect the exclusive maps fromfd_arrayand append their frozen contents to the instructions before verification:
the signature now covers insns ||metadata_0||metadata_1|| [...] in thefd_arrayorder, and verification completes inbpf_check(), once thefd_arraymaps are resolved intoused_maps, before the LSM admission hook and the rest of verification.A program is either
BPF_SIG_UNSIGNEDorBPF_SIG_VERIFIED, with nothing in between. While folding thefd_arraymaps, a non-exclusive map bound to a signed program is rejected, so every map folded into the signature is exclusive. A signed loader that fails to cover its metadata thus does not load, andBPF_SIG_VERIFIEDalways means the instructions and every exclusive map are authentic.The maps must be frozen so the hashed bytes cannot change before the loader runs; the map <-> program digest binding is enforced by the verifier for every used map. Binding maps through
fd_array_cntmakes the verifier resolve and excl-check them (excl_prog_shavsprog->digest) before it would otherwise compute the digest, so computeprog->digestup front inbpf_check(), over the unmodified instructions the signature covers, for a load that folds metadata.
Quoted 6 lines
Unsigned programs are not affected. Note, signed loaders generated by older libbpf/bpftool versions need to be regenerated; some of the recent fixes we've had on the signed loader side require the latter already to close gaps.
Signed-off-by Daniel Borkmann <[email protected]>
This reworks the machinery added by 63a673e8a411 ("bpf: Expose signature verdict via bpf_prog_aux"), which introduced the prog->aux->sig verdict and ran verification before security_bpf_prog_load() so the hook would observe only either UNSIGNED or VERIFIED.
Since this moves the verdict assignment and signature verification into bpf_check() and folds the exclusive metadata maps into the signed payload ahead of the admission hook, should it carry:
---
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/28106955037
Daniel Borkmann
maintainerPATCH v2 2/5 libbpf: Drop in-loader metadata check for load-time verification
In reply to Daniel BorkmannThe signed gen_loader used to police its own metadata map from within
BPF: emit_signature_match() read the kernel-cached map->sha[] back through hardcoded struct bpf_map offsets and compared it against a hash that compute_sha_update_offsets() baked into the signed instructions, after a BPF_OBJ_GET_INFO_BY_FD round-trip to populate map->sha[].
The kernel now verifies the metadata at BPF_PROG_LOAD time by folding the frozen contents of the loader's exclusive fd_array maps into the signature, so the loader no longer checks anything itself. Generated loaders thus carry no verification logic of their own anymore: Nothing in the signing chain depends on emitted loader bytecode doing the right thing.
On the loading side, skel_internal.h now sets fd_array_cnt for a signed load so the kernel scans fd_array for the exclusive metadata map - still frozen, as the kernel requires - and the BPF_OBJ_GET_INFO_BY_FD round-trip to populate map->sha[] is gone. The struct bpf_map layout BUILD_BUG_ON()s on the kernel side are removed as well: they only pinned the ABI for the in-BPF read of map->sha[] that is no longer needed. Note: gen_hash is retained; it still marks a loader as signed so an untrusted host cannot re-dimension maps or override initial values now covered by the signature.
--- kernel/bpf/syscall.c | 5 --- tools/lib/bpf/bpf_gen_internal.h | 1 - tools/lib/bpf/gen_loader.c | 76 +++----------------------------- tools/lib/bpf/skel_internal.h | 27 +----------- 4 files changed, 9 insertions(+), 100 deletions(-)
kernel/bpf/syscall.c +0 -5
@@ -1599,11 +1599,6 @@ static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_ver goto free_map; } -/* See libbpf: emit_signature_match() */ -BUILD_BUG_ON (offsetof(struct bpf_map , excl) !=SHA256_DIGEST_SIZE ); -BUILD_BUG_ON (!__same_type (map->excl, u32)); -BUILD_BUG_ON (offsetof(struct bpf_map , sha) !=0 ); -BUILD_BUG_ON (!__same_type (map->sha, u8[SHA256_DIGEST_SIZE ])); map->excl =1 ; }else if (attr->excl_prog_hash_size) {bpf_log (log,"Invalid excl_prog_hash_size.\n" );
tools/lib/bpf/bpf_gen_internal.h +0 -1
@@ -51,7 +51,6 @@ struct bpf_gen {__u32 nr_ksyms;int fd_array;int nr_fd_array; -int hash_insn_offset[SHA256_DWORD_SIZE ]; };void bpf_gen__init (struct bpf_gen *gen,int log_level,int nr_progs,int nr_maps);
tools/lib/bpf/gen_loader.c +7 -69
124 lines not on this page · raw message
tools/lib/bpf/skel_internal.h +2 -25
@@ -320,25 +320,6 @@ static inline int skel_link_create(int prog_fd, int target_fd,return skel_sys_bpf (BPF_LINK_CREATE , &attr, attr_sz); } -static inline int skel_obj_get_info_by_fd (int fd) -{ - const size_t attr_sz = offsetofend(union bpf_attr, info); -__u8 sha[SHA256_DIGEST_LENGTH ]; -struct bpf_map_info info; -__u32 info_len =sizeof (info); -union bpf_attr attr; - -memset (&info,0 ,sizeof (info)); - info.hash = (long ) &sha; - info.hash_size =SHA256_DIGEST_LENGTH ; - -memset (&attr,0 , attr_sz); - attr.info.bpf_fd = fd; - attr.info.info = (long ) &info; - attr.info.info_len = info_len; -return skel_sys_bpf (BPF_OBJ_GET_INFO_BY_FD , &attr, attr_sz); -} -static inline int skel_map_freeze (int fd) { const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
@@ -384,12 +365,6 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) set_err; goto out; } - err =skel_obj_get_info_by_fd (map_fd); -if (err <0 ) { - opts->errstr ="failed to fetch obj info" ; - set_err; - goto out; - }#endif memset (&attr,0 , prog_load_attr_sz);
@@ -400,6 +375,8 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)#ifndef __KERNEL__ attr.signature = (long ) opts->signature; attr.signature_size = opts->signature_sz; +if (opts->signature) + attr.fd_array_cnt =1 ;#else if (opts->signature || opts->signature_sz)pr_warn ("signatures are not supported from bpf_preload\n" );
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH v2 3/5 bpftool: Cover loader metadata with the program signature
In reply to Daniel Borkmannbpftool_prog_sign() signed only the loader instructions. The metadata blob the loader installs was left to an in-loader hash check, which the kernel now performs at load time over insns || metadata.
Sign that same concatenation: pass the metadata blob (gen_loader_opts data) through to bpftool_prog_sign() and feed insns || metadata to CMS_final(). The excl_prog_hash stays a digest of the instructions alone; it binds the metadata map to the loader and is matched against prog->digest by the verifier, independent of what the signature covers.
The signed artifact is now plain data: both bytes the signature covers are embedded verbatim in the generated skeleton, so signing and verifying an lskel is an ordinary CMS operation that a signer or auditor can perform (or reproduce) offline, without analyzing loader bytecode to establish what the signature actually attests to [0].
--- tools/bpf/bpftool/gen.c | 2 ++ tools/bpf/bpftool/sign.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
tools/bpf/bpftool/gen.c +2 -0
@@ -793,6 +793,8 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *hif (sign_progs) { sopts.insns = opts.insns; sopts.insns_sz = opts.insns_sz; + sopts.data = opts.data; + sopts.data_sz = opts.data_sz; sopts.excl_prog_hash = prog_sha; sopts.excl_prog_hash_sz =sizeof (prog_sha); sopts.signature = sig_buf;
tools/bpf/bpftool/sign.c +14 -1
@@ -135,9 +135,21 @@ int bpftool_prog_sign(struct bpf_load_and_run_opts *opts)CMS_ContentInfo *cms =NULL ;long actual_sig_len =0 ;X509 *x509 =NULL ; +void *data =NULL ; +size_t data_sz;int err =0 ; - bd_in =BIO_new_mem_buf (opts->insns, opts->insns_sz); + data_sz = (size_t )opts->insns_sz + opts->data_sz; + data =malloc (data_sz); +if (!data) { + err = -ENOMEM ; + goto cleanup; + } +memcpy (data, opts->insns, opts->insns_sz); +if (opts->data_sz) +memcpy ((char *)data + opts->insns_sz, opts->data, opts->data_sz); + + bd_in =BIO_new_mem_buf (data, data_sz);if (!bd_in) { err = -ENOMEM ; goto cleanup;
@@ -212,6 +224,7 @@ int bpftool_prog_sign(struct bpf_load_and_run_opts *opts)X509_free (x509);EVP_PKEY_free (private_key);BIO_free (bd_in); +free (data);DISPLAY_OSSL_ERR (err <0 );return err; }
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH v2 4/5 selftests/bpf: Verify load-time signed loader metadata
In reply to Daniel BorkmannThe signed gen_loader no longer checks its metadata map from within BPF; the kernel does it at BPF_PROG_LOAD by folding the loader's frozen exclusive fd_array maps into the signature. Exercise that path end to end. Extend with more test cases (e.g. map-less program, asserting the LSM admission hook observes BPF_SIG_UNSIGNED and BPF_SIG_VERIFIED), and retire the subtests that asserted the old in-loader check, which no longer exists.
# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t signed_loader [...]
#410/1 signed_loader/loadtime_no_map:OK #410/2 signed_loader/loadtime_with_map:OK #410/3 signed_loader/metadata_match:OK #410/4 signed_loader/signature_enforced:OK #410/5 signed_loader/signed_nonexcl_fd_array_rejected:OK #410/6 signed_loader/signed_nonarray_fd_array_rejected:OK #410/7 signed_loader/signed_btf_fd_array_rejected:OK #410/8 signed_loader/signature_failure_logs:OK #410/9 signed_loader/signature_too_large:OK #410/10 signed_loader/signature_bad_keyring:OK #410/11 signed_loader/metadata_ctx_max_entries_ignored:OK #410/12 signed_loader/metadata_ctx_initial_value_ignored:OK #410/13 signed_loader/signature_authenticates_insns:OK #410/14 signed_loader/hash_requires_frozen:OK #410/15 signed_loader/no_update_after_freeze:OK #410/16 signed_loader/freeze_writable_mmap:OK #410/17 signed_loader/no_writable_mmap_frozen:OK #410/18 signed_loader/map_hash_matches_libbpf:OK #410/19 signed_loader/map_hash_multi_element:OK #410/20 signed_loader/map_hash_bad_size:OK #410/21 signed_loader/map_hash_unsupported_type:OK #410/22 signed_loader/lsm_signature_verdict:OK #410 signed_loader:OK
Summary: 1/22 PASSED, 0 SKIPPED, 0 FAILED
--- .../selftests/bpf/prog_tests/signed_loader.c | 597 +++++++++++++----- 1 file changed, 433 insertions(+), 164 deletions(-)
tools/testing/selftests/bpf/prog_tests/signed_loader.c +433 -164
@@ -11,6 +11,8 @@#include <linux/keyctl.h> #include <linux/bpf.h> +#include <bpf/btf.h> +#include "bpf/libbpf_internal.h" /* for libbpf_sha256() */ #include "bpf/skel_internal.h" /* for loader ctx layout (bpf_loader_ctx etc) */
@@ -19,8 +21,6 @@#include "test_signed_loader_data.skel.h" #include "test_signed_loader_lsm.skel.h" -#define SIG_MATCH_INSNS 33/* excl (5) + 4 * sha-dword (7) */ -enum {BPF_SIG_UNSIGNED =0 ,BPF_SIG_VERIFIED ,
@@ -35,7 +35,8 @@ enum { };static int load_loader (const void *insns,__u32 insns_sz,int map_fd, -const void *sig,__u32 sig_sz,__s32 keyring_id) +const void *sig,__u32 sig_sz,__s32 keyring_id, +__u32 fd_array_cnt) {union bpf_attr attr;int fd;
@@ -52,6 +53,7 @@ static int load_loader(const void *insns, __u32 insns_sz, int map_fd, attr.signature_size = sig_sz; attr.keyring_id = keyring_id; } + attr.fd_array_cnt = fd_array_cnt;memcpy (attr.prog_name,"__loader.prog" ,sizeof ("__loader.prog" )); fd =syscall (__NR_bpf,BPF_PROG_LOAD , &attr, offsetofend(union bpf_attr, keyring_id));
@@ -62,14 +64,12 @@ static int run_gen_loader(const void *insns, __u32 insns_sz, const void *data, __u32 data_sz, const void *excl, __u32 excl_sz, const void *sig, __u32 sig_sz, - bool get_hash, void *ctx, __u32 ctx_sz, bool *loader_ran) + void *ctx, __u32 ctx_sz, bool *loader_ran) {LIBBPF_OPTS (bpf_map_create_opts ,mopts , .excl_prog_hash = excl, .excl_prog_hash_size = excl_sz); -__u8 hbuf[SHA256_DIGEST_LENGTH ]; -struct bpf_map_info info; -__u32 ilen =sizeof (info), key =0 ; +__u32 key =0 ;union bpf_attr attr;int map_fd, prog_fd, ret;
@@ -87,15 +87,6 @@ static int run_gen_loader(const void *insns, __u32 insns_sz, ret = -errno; goto out_map; } -if (get_hash) { -memset (&info,0 ,sizeof (info)); - info.hash =ptr_to_u64 (hbuf); - info.hash_size =sizeof (hbuf); -if (bpf_map_get_info_by_fd (map_fd, &info, &ilen)) { - ret = -errno; - goto out_map; - } - }memset (&attr,0 ,sizeof (attr)); attr.prog_type =BPF_PROG_TYPE_SYSCALL ;
@@ -108,6 +99,7 @@ static int run_gen_loader(const void *insns, __u32 insns_sz, attr.signature =ptr_to_u64 (sig); attr.signature_size = sig_sz; attr.keyring_id =KEY_SPEC_SESSION_KEYRING ; + attr.fd_array_cnt =1 ; }memcpy (attr.prog_name,"__loader.prog" ,sizeof ("__loader.prog" )); prog_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr,
@@ -236,79 +228,6 @@ static int sign_buf(const char *dir, const void *buf, __u32 len,return ret; } -static void check_sig_match_shape (const struct bpf_insn *in,int n) -{ -int a =-1 , cleanup =-1 , i, base, t, br[5 ], nb =0 ; - -/* BPF_PSEUDO_MAP_IDX (the struct bpf_map * form) is used only here. */ -for (i =0 ; i +1 < n; i++) { -if (in[i].code == (BPF_LD |BPF_IMM |BPF_DW ) && - in[i].src_reg ==BPF_PSEUDO_MAP_IDX ) { - a = i; -break ; - } - } -if (!ASSERT_GE (a,0 ,"emit_signature_match present" )) -return ; -if (!ASSERT_LE (a +SIG_MATCH_INSNS , n,"block fits in program" )) -return ; - -/* excl check: r2 = *(u32 *)(map + 32); if r2 != 1 goto cleanup */ -ASSERT_EQ (in[a +2 ].code, (BPF_LDX |BPF_MEM |BPF_W ),"excl load width" ); -ASSERT_EQ (in[a +2 ].off,SHA256_DIGEST_LENGTH ,"excl field offset" ); -ASSERT_EQ (in[a +4 ].code, (BPF_JMP |BPF_JNE |BPF_K ),"excl branch op" ); -ASSERT_EQ (in[a +4 ].imm,1 ,"excl compared to 1" ); - br[nb++] = a +4 ; - -/* 4 sha-dword checks: r2 = *(u64 *)(map + i*8); if r2 != r3 goto cleanup */ -for (i =0 ; i <4 ; i++) { - base = a +5 + i *7 ; -ASSERT_EQ (in[base +2 ].code, (BPF_LDX |BPF_MEM |BPF_DW ),"sha load width" ); -ASSERT_EQ (in[base +2 ].off, i *8 ,"sha dword offset" ); -ASSERT_EQ (in[base +3 ].code, (BPF_LD |BPF_IMM |BPF_DW ),"sha imm64 (H_meta)" ); -ASSERT_EQ (in[base +6 ].code, (BPF_JMP |BPF_JNE |BPF_X ),"sha branch op" ); - br[nb++] = base +6 ; - } - -/* -* Locate the real cleanup label so we can pin the exact jump target, -* not just "some backward label". bpf_gen__init() emits the cleanup -* block as a prog-fd close loop whose first instruction is the label -* every error branch jumps to. -*/ -for (i =0 ; i +2 < a; i++) { -if (in[i].code == (BPF_LDX |BPF_MEM |BPF_W ) && - in[i].dst_reg ==BPF_REG_1 && in[i].src_reg ==BPF_REG_10 && - in[i +1 ].code == (BPF_JMP |BPF_JSLE |BPF_K ) && - in[i +1 ].dst_reg ==BPF_REG_1 && in[i +1 ].imm ==0 && - in[i +1 ].off ==1 && - in[i +2 ].code == (BPF_JMP |BPF_CALL ) && - in[i +2 ].imm == BPF_FUNC_sys_close) { - cleanup = i; -break ; - } - } -if (!ASSERT_GE (cleanup,0 ,"cleanup label located" )) -return ; -for (i =0 ; i < nb; i++) { - t = br[i] +1 + in[br[i]].off; -ASSERT_EQ (t, cleanup,"sig-match lands on cleanup" ); - } -/* -* Same invariant for every other cleanup-bound jump in the program: -* emit_check_err() is the only source of "if (r7 < 0) goto cleanup", -* so each of those must also resolve exactly to cleanup. -*/ -for (i =0 , t =0 ; i < n; i++) { -if (in[i].code != (BPF_JMP |BPF_JSLT |BPF_K ) || - in[i].dst_reg !=BPF_REG_7 || in[i].imm !=0 || in[i].off >=0 ) -continue ; -ASSERT_EQ (i +1 + in[i].off, cleanup,"err-check lands on cleanup" ); - t++; - } -ASSERT_GT (t,0 ,"found emit_check_err jumps" ); -} -struct gen_loader_fixture {struct test_signed_loader *skel;struct gen_loader_opts gopts;
@@ -372,16 +291,6 @@ static void gen_loader_fixture_fini(struct gen_loader_fixture *f)test_signed_loader__destroy (f->skel); } -static void metadata_check_shape (void ) -{ -struct gen_loader_fixture f; - -if (gen_loader_fixture_init (&f) ==0 ) -check_sig_match_shape ((const struct bpf_insn *)f.gopts.insns, - f.gopts.insns_sz /sizeof (struct bpf_insn )); -gen_loader_fixture_fini (&f); -} -static void metadata_match (void ) {struct gen_loader_fixture f;
@@ -391,94 +300,221 @@ static void metadata_match(void)if (gen_loader_fixture_init (&f) ==0 ) { r =run_gen_loader (f.gopts.insns, f.gopts.insns_sz, f.blob, f.data_sz, f.excl,sizeof (f.excl),NULL ,0 , - true, f.ctx, f.ctx_sz, &ran); + f.ctx, f.ctx_sz, &ran);ASSERT_TRUE (ran,"loader ran" );ASSERT_EQ (r,0 ,"honest loader retval" ); }gen_loader_fixture_fini (&f); } -static void metadata_sha_mismatch (void ) +static void signature_enforced (void ) { +static const __u8 junk[64 ] = {0x30 ,0x42 ,0x13 ,0x37 , };struct gen_loader_fixture f; -bool ran; -int r; +int fd;if (gen_loader_fixture_init (&f) ==0 ) {/* -* blob[0] lives in the loader's fd_array scratch (first add_data in -* bpf_gen__init); a 0-map program never reads it, so flipping it -* changes only map->sha. The metadata check is the only thing that -* can notice -> isolates emit_signature_match. +* A present-but-invalid signature (the cert bytes are not a +* PKCS#7 signature) must be rejected at load: the signature +* path is honored, not ignored. (The valid path is covered by +* the signed lskels.) */ - f.blob[0 ] ^=0xff ; - r =run_gen_loader (f.gopts.insns, f.gopts.insns_sz, f.blob, - f.data_sz, f.excl,sizeof (f.excl),NULL ,0 , - true, f.ctx, f.ctx_sz, &ran); -ASSERT_TRUE (ran,"loader ran" ); -ASSERT_EQ (r, -EINVAL ,"tampered blob rejected by emit_signature_match" ); + fd =load_loader (f.gopts.insns, f.gopts.insns_sz,-1 , junk, +sizeof (junk),KEY_SPEC_SESSION_KEYRING ,0 ); +ASSERT_LT (fd,0 ,"invalid signature rejected at load" ); }gen_loader_fixture_fini (&f); } -static void metadata_not_exclusive (void ) +static void signed_nonexcl_fd_array_rejected (void ) { +static const __u8 junk[64 ] = {0x30 ,0x42 ,0x13 ,0x37 , };struct gen_loader_fixture f; -bool ran; -int r; +int map_fd, fd;if (gen_loader_fixture_init (&f) ==0 ) {/* -* Correct blob but a non-exclusive metadata map: the verifier does -* not reject (excl_prog_sha unset), so the runtime map->excl == 1 -* check in the loader must. +* A signed program may only bind exclusive maps through fd_array +* (their contents are folded into the signature). Binding a +* non-exclusive map is rejected, before the signature is even +* examined. */ - r =run_gen_loader (f.gopts.insns, f.gopts.insns_sz, f.blob, - f.data_sz,NULL ,0 ,NULL ,0 , true, f.ctx, - f.ctx_sz, &ran); -ASSERT_TRUE (ran,"loader ran" ); -ASSERT_EQ (r, -EINVAL ,"non-exclusive metadata map rejected" ); + map_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"nonexcl" ,4 , + f.data_sz,1 ,NULL ); +if (ASSERT_OK_FD (map_fd,"nonexcl_map" )) { + fd =load_loader (f.gopts.insns, f.gopts.insns_sz, map_fd, + junk,sizeof (junk), +KEY_SPEC_SESSION_KEYRING ,1 ); +ASSERT_EQ (fd, -EPERM , +"non-exclusive map in signed fd_array rejected" ); +if (fd >=0 ) +close (fd); +close (map_fd); + } }gen_loader_fixture_fini (&f); } -static void metadata_hash_not_computed (void ) +static void signed_nonarray_fd_array_rejected (void ) { +static const __u8 junk[64 ] = {0x30 ,0x42 ,0x13 ,0x37 , }; +LIBBPF_OPTS (bpf_map_create_opts, mopts);struct gen_loader_fixture f; -bool ran; -int r; +int map_fd, fd;if (gen_loader_fixture_init (&f) ==0 ) {/* -* Correct, exclusive, frozen map, but its hash was never computed -* (no OBJ_GET_INFO_BY_FD), so map->sha stays zero. The loader must -* fail closed rather than treat an unset hash as a match. +* Only a plain BPF_MAP_TYPE_ARRAY may be folded into the +* signature. An exclusive map of any other type is rejected +* (-EINVAL) rather than folded - this is the type gate that +* keeps arena maps (map_direct_value_addr() returns a user +* address) and insn-array maps (buffer smaller than value_size) +* out of the hashed region, where the old code would have +* memcpy()'d from them. A hash map stands in here: it is +* exclusive (bound to the loader digest) but not an array. */ - r =run_gen_loader (f.gopts.insns, f.gopts.insns_sz, f.blob, - f.data_sz, f.excl,sizeof (f.excl),NULL ,0 , - false, f.ctx, f.ctx_sz, &ran); -ASSERT_TRUE (ran,"loader ran" ); -ASSERT_EQ (r, -EINVAL ,"uncomputed metadata hash rejected" ); + mopts.excl_prog_hash = f.excl; + mopts.excl_prog_hash_size =sizeof (f.excl); + map_fd =bpf_map_create (BPF_MAP_TYPE_HASH ,"excl_hash" ,4 ,4 ,1 , + &mopts); +if (ASSERT_OK_FD (map_fd,"excl_hash_map" )) { + fd =load_loader (f.gopts.insns, f.gopts.insns_sz, map_fd, + junk,sizeof (junk), +KEY_SPEC_SESSION_KEYRING ,1 ); +ASSERT_EQ (fd, -EINVAL , +"non-array map in signed fd_array rejected" ); +if (fd >=0 ) +close (fd); +close (map_fd); + } }gen_loader_fixture_fini (&f); } -static void signature_enforced (void ) +static int setup_meta_map (const struct gen_loader_fixture *f); + +static void signed_btf_fd_array_rejected (void ) +{ +char dir_tmpl[] ="/tmp/signed_loader_btfXXXXXX" , *dir =NULL ; +__u32 sig_sz =8192 ; +int map_fd =-1 , prog_fd =-1 ; +unsigned char *buf =NULL ; +struct gen_loader_fixture f; +bool have_fixture = false; +struct btf *btf =NULL ; +union bpf_attr attr; +int fds[2 ]; +__u8 sig[8192 ]; + +syscall (__NR_request_key,"keyring" ,"_uid.0" ,NULL , +KEY_SPEC_SESSION_KEYRING ); + dir =mkdtemp (dir_tmpl); +if (!ASSERT_OK_PTR (dir,"mkdtemp" )) +return ; +if (!ASSERT_OK (run_setup ("setup" , dir),"verify_sig_setup" )) { +rmdir (dir); +return ; + } +if (gen_loader_fixture_init (&f) !=0 ) + goto out; + have_fixture = true; + +/* +* fd_array binds maps and BTFs alike, but only exclusive array maps are +* folded into the signature. Build a genuinely signed load - insns || +* metadata, exclusive frozen map at fd_array[0] - so it reaches +* BPF_SIG_VERIFIED, then smuggle an extra BTF into fd_array[1]. The BTF +* is not foldable and carries no excl_prog_sha, so it is not covered; +* the post-verifier coverage check (used_btf_cnt != 0) must reject the +* verified program with -EACCES. +*/ + buf =malloc ((size_t )f.gopts.insns_sz + f.data_sz); +if (!ASSERT_OK_PTR (buf,"signbuf" )) + goto out; +memcpy (buf, f.gopts.insns, f.gopts.insns_sz); +memcpy (buf + f.gopts.insns_sz, f.blob, f.data_sz); +if (!ASSERT_OK (sign_buf (dir, buf, f.gopts.insns_sz + f.data_sz, sig, + &sig_sz),"sign insns||metadata" )) + goto out; + + map_fd =setup_meta_map (&f); +if (!ASSERT_OK_FD (map_fd,"meta_map" )) + goto out; + btf =btf__new_empty (); +if (!ASSERT_OK_PTR (btf,"btf_new_empty" )) + goto out; +btf__add_int (btf,"int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_OK (btf__load_into_kernel (btf),"btf_load" )) + goto out; + + fds[0 ] = map_fd; + fds[1 ] =btf__fd (btf); +memset (&attr,0 ,sizeof (attr)); + attr.prog_type =BPF_PROG_TYPE_SYSCALL ; + attr.insns =ptr_to_u64 (f.gopts.insns); + attr.insn_cnt = f.gopts.insns_sz /sizeof (struct bpf_insn ); + attr.license =ptr_to_u64 ("Dual BSD/GPL" ); + attr.prog_flags =BPF_F_SLEEPABLE ; + attr.fd_array =ptr_to_u64 (fds); + attr.fd_array_cnt =2 ; + attr.signature =ptr_to_u64 (sig); + attr.signature_size = sig_sz; + attr.keyring_id =KEY_SPEC_SESSION_KEYRING ; +memcpy (attr.prog_name,"__loader.prog" ,sizeof ("__loader.prog" )); + prog_fd =syscall (__NR_bpf,BPF_PROG_LOAD , &attr, + offsetofend(union bpf_attr, keyring_id)); +ASSERT_EQ (prog_fd <0 ? -errno : prog_fd, -EACCES , +"BTF in signed fd_array rejected post-verifier" ); +if (prog_fd >=0 ) +close (prog_fd); +out: +if (btf) +btf__free (btf); +if (map_fd >=0 ) +close (map_fd); +if (have_fixture) +gen_loader_fixture_fini (&f); +if (dir) +run_setup ("cleanup" , dir); +free (buf); +} + +static void signature_failure_logs (void ) {static const __u8 junk[64 ] = {0x30 ,0x42 ,0x13 ,0x37 , }; +char log_buf[1024 ] = {};struct gen_loader_fixture f; +union bpf_attr attr;int fd;if (gen_loader_fixture_init (&f) ==0 ) {/* -* A present-but-invalid signature (the cert bytes are not a -* PKCS#7 signature) must be rejected at load: the signature -* path is honored, not ignored. (The valid path is covered by -* the signed lskels.) +* Signature verification now runs inside bpf_check(), so a +* failure is reported through the verifier log. A present-but- +* invalid signature is rejected and the log says why. */ - fd =load_loader (f.gopts.insns, f.gopts.insns_sz,-1 , junk, -sizeof (junk),KEY_SPEC_SESSION_KEYRING ); +memset (&attr,0 ,sizeof (attr)); + attr.prog_type =BPF_PROG_TYPE_SYSCALL ; + attr.insns =ptr_to_u64 (f.gopts.insns); + attr.insn_cnt = f.gopts.insns_sz /sizeof (struct bpf_insn ); + attr.license =ptr_to_u64 ("Dual BSD/GPL" ); + attr.prog_flags =BPF_F_SLEEPABLE ; + attr.signature =ptr_to_u64 (junk); + attr.signature_size =sizeof (junk); + attr.keyring_id =KEY_SPEC_SESSION_KEYRING ; + attr.log_level =1 ; + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size =sizeof (log_buf); +memcpy (attr.prog_name,"__loader.prog" ,sizeof ("__loader.prog" )); + + fd =syscall (__NR_bpf,BPF_PROG_LOAD , &attr, + offsetofend(union bpf_attr, keyring_id));ASSERT_LT (fd,0 ,"invalid signature rejected at load" ); +if (fd >=0 ) +close (fd); +ASSERT_HAS_SUBSTR (log_buf,"signature verification failed" , +"verifier logs signature failure" ); }gen_loader_fixture_fini (&f); }
@@ -495,7 +531,7 @@ static void signature_too_large(void)* is rejected before the buffer is read. */ fd =load_loader (f.gopts.insns, f.gopts.insns_sz,-1 , junk, -64 <<20 ,KEY_SPEC_SESSION_KEYRING ); +64 <<20 ,KEY_SPEC_SESSION_KEYRING ,0 );ASSERT_EQ (fd, -EINVAL ,"oversized signature rejected" ); }gen_loader_fixture_fini (&f);
@@ -515,7 +551,7 @@ static void signature_bad_keyring(void)* large positive serial takes the user-keyring path and won't exist. */ fd =load_loader (f.gopts.insns, f.gopts.insns_sz,-1 , junk, -sizeof (junk),INT_MAX ); +sizeof (junk),INT_MAX ,0 );ASSERT_EQ (fd, -EINVAL ,"signature with bad keyring_id rejected" ); }gen_loader_fixture_fini (&f);
@@ -575,7 +611,7 @@ static void metadata_ctx_max_entries_ignored(void)memcpy (blob, gopts.data, data_sz); r =run_gen_loader (gopts.insns, gopts.insns_sz, blob, data_sz, - excl,sizeof (excl),NULL ,0 , true, ctx, ctx_sz, &ran); + excl,sizeof (excl),NULL ,0 , ctx, ctx_sz, &ran);if (!ASSERT_TRUE (ran,"loader ran" ) || !ASSERT_EQ (r,0 ,"loader retval" )) goto free_blob;
@@ -661,7 +697,7 @@ static void metadata_ctx_initial_value_ignored(void)memcpy (blob, gopts.data, data_sz); r =run_gen_loader (gopts.insns, gopts.insns_sz, blob, data_sz, - excl,sizeof (excl),NULL ,0 , true, ctx, ctx_sz, &ran); + excl,sizeof (excl),NULL ,0 , ctx, ctx_sz, &ran);if (!ASSERT_TRUE (ran,"loader ran" ) || !ASSERT_EQ (r,0 ,"loader retval" )) goto free_blob;
@@ -714,6 +750,7 @@ static void signature_authenticates_insns(void)__u8 excl[SHA256_DIGEST_LENGTH ], sig[8192 ];__u32 sig_sz =sizeof (sig), insns_sz, data_sz, ctx_sz;unsigned char *insns =NULL , *tampered =NULL , *blob =NULL ; +unsigned char *signbuf =NULL ;int nr_maps =0 , nr_progs =0 , r;struct bpf_program *p;struct bpf_map *m;
@@ -760,29 +797,46 @@ static void signature_authenticates_insns(void)memcpy (blob, gopts.data, data_sz);libbpf_sha256 (insns, insns_sz, excl); -if (!ASSERT_OK (sign_buf (dir, insns, insns_sz, sig, &sig_sz),"sign-file" )) + signbuf =malloc ((size_t )insns_sz + data_sz); +if (!ASSERT_OK_PTR (signbuf,"signbuf" )) + goto cleanup; +memcpy (signbuf, insns, insns_sz); +memcpy (signbuf + insns_sz, blob, data_sz); +if (!ASSERT_OK (sign_buf (dir, signbuf, insns_sz + data_sz, sig, &sig_sz), +"sign-file" )) goto cleanup;memset (ctx,0 , ctx_sz); ((struct bpf_loader_ctx *)ctx)->sz = ctx_sz; r =run_gen_loader (insns, insns_sz, blob, data_sz, excl,sizeof (excl), - sig, sig_sz, true, ctx, ctx_sz, &ran); + sig, sig_sz, ctx, ctx_sz, &ran);ASSERT_TRUE (ran,"valid signature: loader loaded and ran" );ASSERT_EQ (r,0 ,"valid signature accepted" );close_loader_ctx_fds (ctx, nr_maps, nr_progs);memcpy (tampered, insns, insns_sz); tampered[insns_sz /2 ] ^=0xff ; +/* +* Bind the metadata map to the tampered loader's own digest, so the +* verifier's exclusive-map check (excl_prog_sha == prog->digest) passes +* and the signature - verified after the maps are resolved - is what +* rejects the load. This is the attacker's best case: even after +* re-binding the exclusive map to their tampered loader, the signature +* over the original insns || metadata still fails. (Leaving the map +* bound to the original digest would instead trip the excl check first.) +*/ +libbpf_sha256 (tampered, insns_sz, excl);memset (ctx,0 , ctx_sz); ((struct bpf_loader_ctx *)ctx)->sz = ctx_sz; r =run_gen_loader (tampered, insns_sz, blob, data_sz, excl,sizeof (excl), - sig, sig_sz, true, ctx, ctx_sz, &ran); + sig, sig_sz, ctx, ctx_sz, &ran);ASSERT_FALSE (ran,"tampered loader rejected before run" );ASSERT_EQ (r, -EKEYREJECTED ,"signature is bound to the instructions" ); cleanup:free (insns);free (tampered);free (blob); +free (signbuf);free (ctx);test_signed_loader__destroy (skel);run_setup ("cleanup" , dir);
@@ -1007,10 +1061,11 @@ static void lsm_signature_verdict(void) {char dir_tmpl[] ="/tmp/signed_loader_lsmXXXXXX" , *dir =NULL ;struct test_signed_loader_lsm *lsm =NULL ; +__u32 sig_sz =8192 , msig_sz =8192 ;int map_fd =-1 , prog_fd =-1 ;bool have_fixture = false;struct gen_loader_fixture f; -__u32 sig_sz =8192 ; +unsigned char *buf;__s32 ses_serial;__u8 sig[8192 ];
@@ -1029,7 +1084,7 @@ static void lsm_signature_verdict(void)if (!ASSERT_OK_FD (map_fd,"meta_map_unsigned" )) goto out; lsm->bss->seen =0 ; - prog_fd =load_loader (f.gopts.insns, f.gopts.insns_sz, map_fd,NULL ,0 ,0 ); + prog_fd =load_loader (f.gopts.insns, f.gopts.insns_sz, map_fd,NULL ,0 ,0 ,0 );close (map_fd); map_fd =-1 ;if (!ASSERT_OK_FD (prog_fd,"unsigned loader load" ))
@@ -1062,22 +1117,51 @@ static void lsm_signature_verdict(void) goto out; lsm->bss->seen =0 ; prog_fd =load_loader (f.gopts.insns, f.gopts.insns_sz, map_fd, sig, - sig_sz,KEY_SPEC_SESSION_KEYRING ); + sig_sz,KEY_SPEC_SESSION_KEYRING ,0 );close (map_fd); map_fd =-1 ; -if (!ASSERT_OK_FD (prog_fd,"signed loader load" )) - goto out; -close (prog_fd); +ASSERT_EQ (prog_fd, -EACCES ,"unfolded metadata rejected" ); +if (prog_fd >=0 ) +close (prog_fd); prog_fd =-1 ; ses_serial =syscall (__NR_keyctl,KEYCTL_GET_KEYRING_ID ,KEY_SPEC_SESSION_KEYRING ,0 );ASSERT_EQ (lsm->bss->seen,1 ,"signed: one observed load" ); -ASSERT_EQ (lsm->bss->sig_verdict,BPF_SIG_VERIFIED ,"signed verdict" ); +ASSERT_EQ (lsm->bss->sig_verdict,BPF_SIG_VERIFIED , +"admission saw a valid signature" );ASSERT_EQ (lsm->bss->sig_keyring_type,BPF_SIG_KEYRING_USER ,"signed keyring type" );ASSERT_GT (ses_serial,0 ,"session keyring serial resolved" );ASSERT_EQ (lsm->bss->sig_keyring_serial, ses_serial,"signed: validated against session keyring" ); + + buf =malloc ((size_t )f.gopts.insns_sz + f.data_sz); +if (!ASSERT_OK_PTR (buf,"meta_signbuf" )) + goto out; +memcpy (buf, f.gopts.insns, f.gopts.insns_sz); +memcpy (buf + f.gopts.insns_sz, f.blob, f.data_sz); +if (!ASSERT_OK (sign_buf (dir, buf, f.gopts.insns_sz + f.data_sz, + sig, &msig_sz),"sign insns||metadata" )) { +free (buf); + goto out; + } +free (buf); + + map_fd =setup_meta_map (&f); +if (!ASSERT_OK_FD (map_fd,"meta_map_bound" )) + goto out; + lsm->bss->seen =0 ; + prog_fd =load_loader (f.gopts.insns, f.gopts.insns_sz, map_fd, sig, + msig_sz,KEY_SPEC_SESSION_KEYRING ,1 ); +close (map_fd); + map_fd =-1 ; +if (!ASSERT_OK_FD (prog_fd,"metadata-bound loader load" )) + goto out; +close (prog_fd); + prog_fd =-1 ; +ASSERT_EQ (lsm->bss->seen,1 ,"metadata: one observed load" ); +ASSERT_EQ (lsm->bss->sig_verdict,BPF_SIG_VERIFIED , +"metadata-bound verdict" ); out:if (map_fd >=0 )close (map_fd);
@@ -1090,20 +1174,205 @@ static void lsm_signature_verdict(void)test_signed_loader_lsm__destroy (lsm); } +/* +* Load-time metadata verification: the kernel folds the frozen metadata map +* into the signature (insns || metadata) and checks it at BPF_PROG_LOAD via +* fd_array_cnt, rather than the loader checking from within BPF. Sign that +* concatenation, hand the kernel the map, and confirm the signed loader loads, +* runs, and installs its target. +*/ +static int loadtime_drive (const char *dir,const void *insns,__u32 insns_sz, +const void *data,__u32 data_sz,const __u8 *excl, +void *ctx,__u32 ctx_sz,int *load_ret,bool *ran) +{ +LIBBPF_OPTS (bpf_map_create_opts, mopts, + .excl_prog_hash = excl, + .excl_prog_hash_size = SHA256_DIGEST_LENGTH); +__u32 sig_sz =8192 , key =0 ; +unsigned char *buf =NULL ; +int map_fd, prog_fd, ret =0 ; +union bpf_attr attr; +__u8 sig[8192 ]; + + *ran = false; + *load_ret =0 ; + +/* +* Metadata map, bound to the loader digest and frozen, exactly as +* skel_internal.h's bpf_load_and_run() sets it up. +*/ + map_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"__loader.map" ,4 , + data_sz,1 , &mopts); +if (map_fd <0 ) +return -errno; +if (bpf_map_update_elem (map_fd, &key, data,0 ) ||bpf_map_freeze (map_fd)) { + ret = -errno; + goto out_map; + } + +/* Sign insns || metadata, the same bytes the kernel reconstructs. */ + buf =malloc ((size_t )insns_sz + data_sz); +if (!buf) { + ret = -ENOMEM ; + goto out_map; + } +memcpy (buf, insns, insns_sz); +memcpy (buf + insns_sz, data, data_sz); + ret =sign_buf (dir, buf, insns_sz + data_sz, sig, &sig_sz); +if (ret) + goto out_map; + +memset (&attr,0 ,sizeof (attr)); + attr.prog_type =BPF_PROG_TYPE_SYSCALL ; + attr.insns =ptr_to_u64 (insns); + attr.insn_cnt = insns_sz /sizeof (struct bpf_insn ); + attr.license =ptr_to_u64 ("Dual BSD/GPL" ); + attr.prog_flags =BPF_F_SLEEPABLE ; + attr.fd_array =ptr_to_u64 (&map_fd); + attr.signature =ptr_to_u64 (sig); + attr.signature_size = sig_sz; + attr.keyring_id =KEY_SPEC_SESSION_KEYRING ; + attr.fd_array_cnt =1 ; +memcpy (attr.prog_name,"__loader.prog" ,sizeof ("__loader.prog" )); + prog_fd =syscall (__NR_bpf,BPF_PROG_LOAD , &attr, + offsetofend(union bpf_attr, keyring_id)); +if (prog_fd <0 ) { + *load_ret = -errno; + ret = -errno; + goto out_map; + } + +memset (&attr,0 ,sizeof (attr)); + attr.test.prog_fd = prog_fd; + attr.test.ctx_in =ptr_to_u64 (ctx); + attr.test.ctx_size_in = ctx_sz; +if (syscall (__NR_bpf,BPF_PROG_RUN , &attr, + offsetofend(union bpf_attr, test)) < 0) { + ret = -errno; + goto out_prog; + } + *ran = true; + ret = (int )attr.test.retval; +out_prog: +close (prog_fd); +out_map: +free (buf); +close (map_fd); +return ret; +} + +static void loadtime_verify (struct bpf_object *obj,int expect_maps) +{ + LIBBPF_OPTS(gen_loader_opts, gopts, .gen_hash = true); +char dir_tmpl[] ="/tmp/signed_loader_ltXXXXXX" , *dir =NULL ; +int nr_maps =0 , nr_progs =0 , load_ret =0 , r; +__u8 excl[SHA256_DIGEST_LENGTH ]; +struct bpf_prog_desc *pd; +struct bpf_map_desc *md; +unsigned char *blob =NULL ; +struct bpf_program *p; +struct bpf_map *m; +__u32 ctx_sz, data_sz; +void *ctx =NULL ; +bool ran = false; + +syscall (__NR_request_key,"keyring" ,"_uid.0" ,NULL , +KEY_SPEC_SESSION_KEYRING ); + dir =mkdtemp (dir_tmpl); +if (!ASSERT_OK_PTR (dir,"mkdtemp" )) +return ; +if (!ASSERT_OK (run_setup ("setup" , dir),"verify_sig_setup" )) { +rmdir (dir); +return ; + } + +if (!ASSERT_OK (bpf_object__gen_loader (obj, &gopts),"gen_loader" )) + goto out; +if (!ASSERT_OK (bpf_object__load (obj),"gen_load" )) + goto out; + +bpf_object__for_each_program (p, obj) + nr_progs++; +bpf_object__for_each_map (m, obj) + nr_maps++; +if (!ASSERT_EQ (nr_maps, expect_maps,"fixture map count" )) + goto out; + + ctx_sz =sizeof (struct bpf_loader_ctx ) + + nr_maps *sizeof (struct bpf_map_desc ) + + nr_progs *sizeof (struct bpf_prog_desc ); + ctx =calloc (1 , ctx_sz); +if (!ASSERT_OK_PTR (ctx,"ctx_alloc" )) + goto out; + ((struct bpf_loader_ctx *)ctx)->sz = ctx_sz; + + data_sz = gopts.data_sz; + blob =malloc (data_sz); +if (!ASSERT_OK_PTR (blob,"blob_alloc" )) + goto out; +memcpy (blob, gopts.data, data_sz); + +/* excl_prog_hash = SHA256(loader insns) == the loader's prog->digest. */ +libbpf_sha256 (gopts.insns, gopts.insns_sz, excl); + + r =loadtime_drive (dir, gopts.insns, gopts.insns_sz, blob, data_sz, + excl, ctx, ctx_sz, &load_ret, &ran); +ASSERT_OK (load_ret,"signed loader loaded (insns || metadata)" ); +ASSERT_TRUE (ran,"loader ran" ); +ASSERT_EQ (r,0 ,"loader installed its target" ); + + md = (struct bpf_map_desc *)((char *)ctx +sizeof (struct bpf_loader_ctx )); + pd = (struct bpf_prog_desc *)(md + nr_maps); +ASSERT_GT (pd[0 ].prog_fd,0 ,"target program installed" ); +if (nr_maps) +ASSERT_GT (md[0 ].map_fd,0 ,"target map installed" ); + +close_loader_ctx_fds (ctx, nr_maps, nr_progs); +out: +free (blob); +free (ctx); +if (dir) +run_setup ("cleanup" , dir); +} + +static void loadtime_no_map (void ) +{ +struct test_signed_loader *skel =test_signed_loader__open (); + +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; +loadtime_verify (skel->obj,0 ); +test_signed_loader__destroy (skel); +} + +static void loadtime_with_map (void ) +{ +struct test_signed_loader_map *skel =test_signed_loader_map__open (); + +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; +loadtime_verify (skel->obj,1 ); +test_signed_loader_map__destroy (skel); +} +void test_signed_loader (void ) { -if (test__start_subtest ("metadata_check_shape" )) -metadata_check_shape (); +if (test__start_subtest ("loadtime_no_map" )) +loadtime_no_map (); +if (test__start_subtest ("loadtime_with_map" )) +loadtime_with_map ();if (test__start_subtest ("metadata_match" ))metadata_match (); -if (test__start_subtest ("metadata_sha_mismatch" )) -metadata_sha_mismatch (); -if (test__start_subtest ("metadata_not_exclusive" )) -metadata_not_exclusive (); -if (test__start_subtest ("metadata_hash_not_computed" )) -metadata_hash_not_computed ();if (test__start_subtest ("signature_enforced" ))signature_enforced (); +if (test__start_subtest ("signed_nonexcl_fd_array_rejected" )) +signed_nonexcl_fd_array_rejected (); +if (test__start_subtest ("signed_nonarray_fd_array_rejected" )) +signed_nonarray_fd_array_rejected (); +if (test__start_subtest ("signed_btf_fd_array_rejected" )) +signed_btf_fd_array_rejected (); +if (test__start_subtest ("signature_failure_logs" )) +signature_failure_logs ();if (test__start_subtest ("signature_too_large" ))signature_too_large ();if (test__start_subtest ("signature_bad_keyring" ))
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH v2 5/5 Documentation/bpf: Add BPF signing and enforcement doc
In reply to Daniel BorkmannDescribe the BPF signing design end to end: why a trusted loader is needed, the signature(insns || metadata) contract, load-time verification via fd_array (exclusive + frozen maps), the binary BPF_SIG_{UNSIGNED,VERIFIED} verdict, and how [BPF] LSMs can enforce policy on it.
This writes down the contract on the discussion points with the LSM / integrity folks [0][1]: by the time security_bpf_prog_load() is called, signature verification has fully completed and covers the instructions plus the frozen contents of every bound exclusive map; there is no intermediate "loader verified, payload pending" state to reason about; and what BPF_SIG_VERIFIED means at each hook is spelled out explicitly, including the post-verifier coverage check that keeps the verdict binary.
--- Documentation/bpf/index.rst | 1 + Documentation/bpf/signing.rst | 490 ++++++++++++++++++++++++++++++++++ 2 files changed, 491 insertions(+) create mode 100644 Documentation/bpf/signing.rst
Documentation/bpf/index.rst +1 -0
@@ -28,6 +28,7 @@ that goes into great technical depth about the BPF Architecture. classic_vs_extended.rst bpf_iterators bpf_licensing + signing test_debug clang-notes linux-notes
Documentation/bpf/signing.rst new file +490 -0
490 lines not on this page · raw message
Signature · 1 line
2.43.0