Linux Security Modules development
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: roblabla <linux@roblab.la>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Paul Moore <paul@paul-moore.com>,
	Daan De Meyer <daan@amutable.com>
Cc: bpf@vger.kernel.org, linux-security-module@vger.kernel.org,
	paul.mabileau@harfanglab.fr, linux-kernel@vger.kernel.org,
	Andrii Nakryiko <andrii@kernel.org>
Subject: Re: [BUG] bpf, lsm: new first argument in 7.4 inode hooks silently breaks BPF LSMs
Date: Tue, 17 Nov 2026 17:52:38 -0700	[thread overview]
Message-ID: <9695ec3d-7b81-488d-989f-0bd411d673f2@linux.dev> (raw)
In-Reply-To: <9d6bdc31-e57c-4187-be78-29d82471f710@app.fastmail.com>

On 11/9/26 8:07 AM, roblabla wrote:
> Hi,
> 
> Thanks for the replies!
> 
>> [...]
> 
> So what would be really nice is a way to make eBPF code robust against
> kernel changes such that it either works, or fails to load, but doesn't
> do the halfway "loads but silently misbehaves". Here are two ideas that
> have been in the back of my mind that could help solve this issue (though
> I understand that they may be rather large changes, I'm putting them out
> there mostly as a conversation starter):
> 
> 1. Could libbpf/the kernel have a mismatch check on hook arguments? I
>    understand that libbpf and the verifier don't know the types a
>    program expects, since BPF_PROG() only casts u64s. But the macro
>    does know the types at compile time. Could it (or an opt-in variant)
>    record the expected prototype in the program's BTF, so that libbpf
>    can compare it against the LSM hook's prototype in vmlinux BTF and
>    refuse to load on a mismatch?
> 
> 2. Since kernel BTF has parameter names for the bpf_lsm_* functions,
>    could we maybe have CO-RE-style relocation to resolve "the argument
>    named dentry" to the argument index at load time? This way, adding a
>    new argument could happen anywhere in the argument list without
>    breaking eBPFs, and removing an argument will (correctly) cause our
>    eBPF to stop loading. It does mean the eBPF programs must provide the
>    same argument names, but maybe that's an acceptable tradeoff?

Hi everyone.

roblabla's comments got me curious, and I spent some time with my
helpful assistants poking at the problem.

There is another workaround in addition to what Andrii suggested.
A program can already make itself fail to load on a signature
mismatch:

	union security_list_options___mine {
		int (*inode_create)(struct inode *dir,
				    struct dentry *dentry, umode_t mode);
	};

	extern void lsm_proto_mismatch__inode_create(void) __ksym __weak;

	SEC("lsm/inode_create")
	int BPF_PROG(deny_create, struct inode *dir, struct dentry *dentry)
	{
		if (!bpf_core_type_matches(union security_list_options___mine))
			lsm_proto_mismatch__inode_create();
		...
	}

bpf_core_type_matches() checks the expected prototype against the
hook's member of union security_list_options in kernel BTF.
On a mismatch the call to the unresolved weak kfunc stays reachable,
so the verifier rejects the program, and libbpf reports:

  kfunc 'lsm_proto_mismatch__inode_create' is referenced but wasn't resolved

It's positional and needs the exact argument count, but it turns
silent misbehavior into a load failure. The same should work for
tp_btf with the btf_trace_<tp> typedefs.

But of course it's just a workaround..

A bit more generally, expanding on roblabla's second idea, I think libbpf
could resolve hook arguments by name with the existing CO-RE machinery,
without kernel, verifier or LSM changes.

The BPF side could look like this:

	SEC("lsm/inode_create")
	int BPF_PROG_ARGS(deny_create, struct inode *, dir,
			  struct dentry *, dentry)
	{ ... }

BPF_PROG_ARGS() (name TBD) takes (type, name) pairs like BPF_PROG2(),
but instead of casting ctx[0], ctx[1], ... it reads each argument
through a local preserve_access_index-marked struct, so every argument
access is a regular CO-RE field relocation:

	struct bpf_func_args___deny_create {
		const struct mnt_idmap *idmap;	/* 7.4+ */
		struct inode *dir;
		struct dentry *dentry;
	} __attribute__((preserve_access_index));

	SEC("lsm/inode_create")
	int deny_create(u64 *ctx)
	{
		struct bpf_func_args___deny_create *args = (void *)ctx;

		if (bpf_core_field_exists(args->idmap))
			/* use args->idmap */;

		return check(args->dir, args->dentry);
	}

Nothing changes on the loader side:

	skel = deny_create__open_and_load();
	if (!skel)	/* mismatch: load fails, libbpf logs why */
		return -errno;
	err = deny_create__attach(skel);

To achieve this libbpf would need to do the following at load time:

  - treat bpf_func_args___* as a reserved type in CO-RE relocations;
  - find the program's attach target in kernel (or module) BTF:
    bpf_lsm_<hook>, the fentry/fexit target, or the tracepoint stub for
    tp_btf;
  - build a struct from the target's prototype: one member per
    parameter, named after it, at the parameter's ctx offset, plus 'ret'
    for fexit, fmod_ret and LSM;
  - resolve the relocations against it by name, with a strict type check
    (pointee types must match, qualifiers ignored), since CO-RE normally
    treats any two pointers as compatible.

A missing argument or a type mismatch is a normal CO-RE relocation
failure: the program fails to load with an error naming the argument,
unless the access is guarded by bpf_core_field_exists().

And this is not LSM specific, fentry/fexit/fmod_ret and tp_btf would
work the same way.

It doesn't help if a hook keeps its signature but changes semantics,
or if the attach point goes away (that already fails to attach).

Here is a vibe-coded prototype of the above:
https://github.com/kernel-patches/bpf/compare/bpf-next_base...theihor:bpf:bpf-named-args

It's an entirely new libbpf feature though.

Andrii, what do you think of it?

---

And just my 2c re argument ordering:

Paul, with all due respect, prioritizing code style and personal
preferences over backwards compatibility is unreasonable even for
unimportant applications. And here we are talking about apps
implementing security policies, which many organizations (not just
companies) in the world rely on. Given the recent flood of
cybersecurity issues (in Linux and beyond), I don't see why would we,
as in kernel devs, intentionally make the defenders' job harder.


> 
> Thanks,
> roblabla


      parent reply	other threads:[~2026-11-18  0:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <d85ece6e-27f9-4d9f-83af-4101bc76fef6@app.fastmail.com>
2026-11-06 13:38 ` [BUG] bpf, lsm: new first argument in 7.4 inode hooks silently breaks BPF LSMs roblabla
2026-11-06 15:41   ` Paul Moore
2026-11-06 19:33     ` Andrii Nakryiko
2026-11-09 15:07       ` roblabla
2026-11-12 23:00         ` Andrii Nakryiko
2026-11-13  2:41           ` Paul Moore
2026-11-17 16:51             ` Andrii Nakryiko
2026-11-17 19:30               ` Paul Moore
2026-11-17 21:44                 ` Andrii Nakryiko
2026-11-17 23:04                   ` Paul Moore
2026-11-18  0:52         ` Ihor Solodrai [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9695ec3d-7b81-488d-989f-0bd411d673f2@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daan@amutable.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux@roblab.la \
    --cc=paul.mabileau@harfanglab.fr \
    --cc=paul@paul-moore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox