bpf: add bpf_real_inode() kfunc
Christian Brauner
maintainerPATCH v2 1/1 bpf: add bpf_real_inode() kfunc
Add a sleepable BPF kfunc that resolves the real inode backing a dentry via d_real_inode(). On overlay/union filesystems the inode attached to the dentry is the overlay inode which does not carry the underlying device information. d_real_inode() resolves through the overlay and returns the inode from the lower, real filesystem.
This is used in the RestrictFilesytemAccess bpf program that has been merged into systemd a little while ago.
---
Changes in v2:
- Tag bpf_real_inode() with KF_RET_NULL and document that it returns
NULL for negative dentries; LSM hooks such as security_inode_create()
pass negative dentries, and without the flag the verifier marks the
return as PTR_TRUSTED and skips the NULL check.
- Link to v1: https://patch.msgid.link/[email protected]
--- fs/bpf_fs_kfuncs.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
fs/bpf_fs_kfuncs.c +16 -0
@@ -353,6 +353,21 @@ __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s }#endif /* CONFIG_CGROUPS */ +/** +* bpf_real_inode - get the real inode backing a dentry +* @dentry: dentry to resolve +* +* If the dentry is on a union/overlay filesystem, return the underlying, real +* inode that hosts the data. Otherwise return the inode attached to the +* dentry itself. +* +* Return: The real inode backing the dentry, or NULL for a negative dentry. +*/ +__bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) +{ +return d_real_inode (dentry); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)
@@ -363,6 +378,7 @@ BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_real_inode, KF_SLEEPABLE | KF_RET_NULL)BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
---
base-commit: 1f318b96cc84d7c2ab792fcc0bfd42a7ca890681
change-id: 20260326-work-bpf-verity-a43f28baa242
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] bpf_real_inode() fails to resolve the real underlying inode for directories and non-regular files…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] bpf_real_inode() fails to resolve the real underlying inode for directories and non-regular files on overlayfs.
- [High] bpf_real_inode() can be exploited to trigger a kernel warning (DoS) and bypass security policies via pending signals.
- [High] bpf_real_inode() exposes overlayfs internal locks to BPF, allowing trivial self-deadlocks.
--
commit ea7b29442723e30589a55df64a725a8aaaaeddd8
Author: Christian Brauner <[email protected]>
bpf: add bpf_real_inode() kfunc
Add a sleepable BPF kfunc that resolves the real inode backing a dentry via d_real_inode(). On overlay/union filesystems the inode attached to the dentry is the overlay inode which does not carry the underlying device information. d_real_inode() resolves through the overlay and returns the inode from the lower, real filesystem.
Quoted 11 lines
fs/bpf_fs_kfuncs.c +3
-0@@ -353,6 +353,21 @@ __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s }#endif /* CONFIG_CGROUPS */ +/** +* bpf_real_inode - get the real inode backing a dentry +* @dentry: dentry to resolve
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s · line 368
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+* +* If the dentry is on a union/overlay filesystem, return the underlying, real +* inode that hosts the data. Otherwise return the inode attached to the +* dentry itself. +* +* Return: The real inode backing the dentry, or NULL for a negative dentry. +*/ +__bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) +{ +return d_real_inode (dentry);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Severity: High]
Does this code fail to resolve the real underlying inode for directories and non-regular files on overlayfs?
When d_real_inode() is called, it invokes d_real(dentry, D_REAL_DATA), which maps to ovl_d_real() in overlayfs. However, ovl_d_real() contains an explicit short-circuit check:
if (!d_is_reg(dentry))
return dentry;This means it will return the overlayfs dentry (and therefore the overlayfs inode) for directories, sockets, and symlinks.
Since BPF LSM policies using bpf_real_inode() to restrict access based on the underlying real filesystem will receive the overlayfs inode for directories, could this cause inconsistent policy enforcement and allow security bypasses for non-regular files?
[Severity: High]
Can this implementation be exploited to trigger a kernel warning and bypass security policies via pending signals?
When bpf_real_inode() is called on an overlayfs regular file, it delegates to ovl_d_real() which calls ovl_verify_lowerdata(). This attempts a lazy lookup using ovl_maybe_lookup_lowerdata(), which then calls ovl_inode_lock_interruptible(inode) to acquire a mutex.
If the user process triggering the BPF hook has a pending signal, the lock acquisition fails and returns -EINTR.
Because ovl_d_real() treats any failure from ovl_verify_lowerdata() as a critical bug, it will trigger a WARN(1, ...) and fall back to returning the overlayfs dentry.
Does this allow an unprivileged user to intentionally trigger this path with a pending signal, reliably spamming kernel warnings, and returning the overlayfs inode instead of the real one to bypass BPF LSM restrictions?
[Severity: High]
Does exposing these internal overlayfs locks to BPF allow for trivial self-deadlocks?
Because bpf_real_inode() is marked as KF_SLEEPABLE, it can be called from sleepable BPF programs such as LSM hooks or fentry.s tracing programs.
When operating on an overlayfs dentry, this attempts to acquire the non-recursive mutex OVL_I(inode)->lock via ovl_inode_lock_interruptible() during the lazy lowerdata lookup.
If a sleepable BPF program attaches to a kernel function or LSM hook that executes while OVL_I(inode)->lock is already held by the current thread (for example, an fentry.s trace on ovl_do_setattr or ovl_do_copy_up), calling bpf_real_inode() on the same dentry will cause the thread to wait on the non-recursive mutex it already holds.
Could this result in a self-deadlock, completely freezing the thread and breaking the verifier's safety guarantees?
Quoted 1 line
fs/bpf_fs_kfuncs.c +1
-0+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christian Brauner
maintainerQuoted 53 lines · On Tue, May 26, 2026 at 01:05:17PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High]bpf_real_inode()fails to resolve the real underlying inode for directories and non-regular files on overlayfs.
- [High]bpf_real_inode()can be exploited to trigger a kernel warning (DoS) and bypass security policies via pending signals.
- [High]bpf_real_inode()exposes overlayfs internal locks to BPF, allowing trivial self-deadlocks.
--commit
ea7b29442723e30589a55df64a725a8aaaaeddd8
Author: Christian Brauner <[email protected]>bpf: add
bpf_real_inode()kfuncAdd a sleepable BPF kfunc that resolves the real inode backing a dentry via
d_real_inode(). On overlay/union filesystems the inode attached to the dentry is the overlay inode which does not carry the underlying device information.d_real_inode()resolves through the overlay and returns the inode from the lower, real filesystem.Quoted 11 lines
fs/bpf_fs_kfuncs.c +3
-0@@ -353,6 +353,21 @@ __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s }#endif /* CONFIG_CGROUPS */ +/** +* bpf_real_inode - get the real inode backing a dentry +* @dentry: dentry to resolve fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s · line 368
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+* +* If the dentry is on a union/overlay filesystem, return the underlying, real +* inode that hosts the data. Otherwise return the inode attached to the +* dentry itself. +* +* Return: The real inode backing the dentry, or NULL for a negative dentry. +*/ +__bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) +{ +return d_real_inode (dentry);^^^^^^^^^^^^^^^^^^^^^^^^^^^^[Severity: High]
Does this code fail to resolve the real underlying inode for directories and non-regular files on overlayfs?When
d_real_inode()is called, it invokesd_real(dentry, D_REAL_DATA), which maps toovl_d_real()in overlayfs. However,ovl_d_real()contains an explicit short-circuit check:if (!d_is_reg(dentry)) return dentry;
Quoted 7 lines
This means it will return the overlayfs dentry (and therefore the overlayfs inode) for directories, sockets, and symlinks.
Since BPF LSM policies using
bpf_real_inode()to restrict access based on the underlying real filesystem will receive the overlayfs inode for directories, could this cause inconsistent policy enforcement and allow security bypasses for non-regular files?
No, this is expected.
Quoted 9 lines
[Severity: High]
Can this implementation be exploited to trigger a kernel warning and bypass security policies via pending signals?When
bpf_real_inode()is called on an overlayfs regular file, it delegates toovl_d_real()which callsovl_verify_lowerdata(). This attempts a lazy lookup usingovl_maybe_lookup_lowerdata(), which then callsovl_inode_lock_interruptible(inode)to acquire a mutex.
Quoted 6 lines
If the user process triggering the BPF hook has a pending signal, the lock acquisition fails and returns
-EINTR.Because
ovl_d_real()treats any failure fromovl_verify_lowerdata()as a critical bug, it will trigger aWARN(1, ...)and fall back to returning the overlayfs dentry.
This is at least strange behavior on overlayfs. But it wouldn't be an issue. The overlayfs indoe would be returned and execution would be denied. So it would prevent execution, it wouldn't escalate privileges.
Quoted 13 lines
Does this allow an unprivileged user to intentionally trigger this path with a pending signal, reliably spamming kernel warnings, and returning the overlayfs inode instead of the real one to bypass BPF LSM restrictions?
[Severity: High]
Does exposing these internal overlayfs locks to BPF allow for trivial self-deadlocks?Because
bpf_real_inode()is marked asKF_SLEEPABLE, it can be called from sleepable BPF programs such as LSM hooks or fentry.s tracing programs.
Quoted 7 lines
When operating on an overlayfs dentry, this attempts to acquire the non-recursive mutex OVL_I(inode)->lock via
ovl_inode_lock_interruptible()during the lazy lowerdata lookup.If a sleepable BPF program attaches to a kernel function or LSM hook that executes while OVL_I(inode)->lock is already held by the current thread (for example, an fentry.s trace on
ovl_do_setattrorovl_do_copy_up), calling
Fwiw, ovl_do_setattr() doesn't exist iirc.
Quoted 5 lines
bpf_real_inode()on the same dentry will cause the thread to wait on the non-recursive mutex it already holds.Could this result in a self-deadlock, completely freezing the thread and breaking the verifier's safety guarantees?
Does the verifier protect against calling
bpf_set_dentry_xattr() from:
security_inode_setxattr()?
Because that's a trivial deadlock right there already.
I have not idea what promises the verifier gives but if it's supposed to extend to avoiding deadlocks then we probably have quite a few issues right now.
Quoted 36 lines · Christian Brauner
Add a sleepable BPF kfunc that resolves the real inode backing a dentry via
d_real_inode(). On overlay/union filesystems the inode attached to the dentry is the overlay inode which does not carry the underlying device information.d_real_inode()resolves through the overlay and returns the inode from the lower, real filesystem.This is used in the RestrictFilesytemAccess bpf program that has been merged into systemd a little while ago.
Signed-off-by Christian Brauner (Amutable) <[email protected]>---
Changes in v2:
- Tagbpf_real_inode()withKF_RET_NULLand document that it returns
NULL for negative dentries; LSM hooks such assecurity_inode_create()
pass negative dentries, and without the flag the verifier marks the
return asPTR_TRUSTEDand skips the NULL check.
- Link to v1: https://patch.msgid.link/[email protected]--- fs/bpf_fs_kfuncs.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)fs/bpf_fs_kfuncs.c +5
-0@@ -353,6 +353,21 @@ __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s }#endif /* CONFIG_CGROUPS */ +/** +* bpf_real_inode - get the real inode backing a dentry +* @dentry: dentry to resolve +* +* If the dentry is on a union/overlay filesystem, return the underlying, real
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__s · line 370
Quoted 10 lines
fs/bpf_fs_kfuncs.c +10
-0+* inode that hosts the data. Otherwise return the inode attached to the +* dentry itself. +* +* Return: The real inode backing the dentry, or NULL for a negative dentry. +*/ +__bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) +{ +return d_real_inode (dentry); +} +
Sorry for noticing late, but pause to consider.
I see that both EVM and IMA open code the would be helper d_real_metadata_inode()
while for d_real_inode(), d_real_data_inode() would have been a more descriptive name.
The name d_real_inode() is from before the introduction of the D_REAL_ types.
Perhaps for the kfunc we want to use the more descriptive name bpf_real_data_inode().
Could also add d_real_data_inode() wrapper or rename all the vfs call sites.
Up to you. Anyway feel free to add:
Thanks,
Amir.
Quoted 8 lines · Christian Brauner
Add a sleepable BPF kfunc that resolves the real inode backing a dentry via
d_real_inode(). On overlay/union filesystems the inode attached to the dentry is the overlay inode which does not carry the underlying device information.d_real_inode()resolves through the overlay and returns the inode from the lower, real filesystem.This is used in the RestrictFilesytemAccess bpf program that has been merged into systemd a little while ago.
I still have objections to this program as it really should not do the poking through two layers of inodes and then look at the potential incorrect device that should be hidden. Please add proper VFS APIs for your integrity verification instead of piling up these hacks.