bpf: bound trusted fields to their parent's lifetime
Subsystems (4)
cover bpf: bound trusted fields to their parent's lifetime
Howdy folks,
Two fixes to the lifetime of trusted fields.
A field in the __safe_trusted or __safe_trusted_or_null sets becomes PTR_TRUSTED regardless of how its parent is kept alive: nothing ties the resulting pointer to its parent's lifetime. There are two separate cases, RCU-protected parents and referenced parents.
First, an RCU-protected parent, e.g. mm->exe_file read under bpf_rcu_read_lock(), is MEM_RCU. It can hand out file->f_inode as fully trusted, and the inode pointer survives bpf_rcu_read_unlock(). It remains dereferenceable without PROBE_MEM and passable to kfuncs expecting trusted arguments after the grace period.
Second, a referenced parent has the same lifetime problem with release kfuncs. A trusted field walked out of an acquired object stays usable even after the reference to its parent is dropped. An in-tree example:
the file acquired from bpf_get_task_exe_file() is released at bpf_put_file(), but an f_inode taken out of it stays trusted after the object may be freed.
Fix both by making the walked field inherit its parent's bound. Patch 1 substitutes MEM_RCU for PTR_TRUSTED when the parent is MEM_RCU, so the field is invalidated with the critical section like every other RCU pointer. Patch 3 sets the walked field's parent_id to the parent's reference. Programs that relied on either escape will no longer load, but the behavior they relied on was essentially a use-after-free being papered over with deferred free mechanisms and refcounts held outside BPF.
Patches 2 and 4 add reject and accept selftests for each.
Justin
Justin Suess (4):
bpf: Carry MEM_RCU for trusted fields with MEM_RCU parents selftests/bpf: Test MEM_RCU carry-over for trusted fields
bpf: Bind trusted fields to their parent's reference selftests/bpf: Test trusted-field invalidation on parent release
kernel/bpf/verifier.c | 11 ++++- .../selftests/bpf/progs/verifier_vfs_accept.c | 40 +++++++++++++++ .../selftests/bpf/progs/verifier_vfs_reject.c | 49 +++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-)
base-commit: 763187dbbf83764a57d5811168ef9de7b9a60e2d
Signature · 1 line
2.55.0
PATCH 1/4 bpf: Carry MEM_RCU for trusted fields with MEM_RCU parents
Substitute MEM_RCU instead of PTR_TRUSTED for fields declared as trusted when the parent pointer is MEM_RCU.
Today, walking a field in the __safe_trusted or __safe_trusted_or_null set invariably yields PTR_TRUSTED if its parent is trusted. If the parent is instead RCU protected, the resulting pointer survives bpf_rcu_read_unlock() as fully trusted: it can be dereferenced without PROBE_MEM and passed to kfuncs expecting trusted arguments after the parent's RCU grace period, e.g. mm->exe_file (MEM_RCU) followed by file->f_inode escapes the RCU CS as a trusted inode. Limit the child field's lifetime to that of the parent.
--- kernel/bpf/verifier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +2 -2
@@ -6684,9 +6684,9 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,* trusted if we are in an RCU CS. Such pointer can be NULL. */ if (type_is_trusted (env, reg, field_name, btf_id)) { - flag |=PTR_TRUSTED ; + flag |=is_rcu_reg (reg) ?MEM_RCU :PTR_TRUSTED ; }else if (type_is_trusted_or_null (env, reg, field_name, btf_id)) { - flag |=PTR_TRUSTED |PTR_MAYBE_NULL ; + flag |= (is_rcu_reg (reg) ?MEM_RCU :PTR_TRUSTED ) |PTR_MAYBE_NULL ; }else if (in_rcu_cs (env) && !type_may_be_null (reg->type)) {if (type_is_rcu (env, reg, field_name, btf_id)) {/* ignore __rcu tag and mark it MEM_RCU */
Signature · 1 line
2.55.0
PATCH 2/4 selftests/bpf: Test MEM_RCU carry-over for trusted fields
In reply to Justin SuessWalking file->f_inode out of an RCU-protected mm->exe_file must yield an RCU pointer rather than a trusted one. Add a reject test where the inode is used as a trusted argument after bpf_rcu_read_unlock() and an accept test where it is dereferenced inside the RCU critical section.
--- .../selftests/bpf/progs/verifier_vfs_accept.c | 23 ++++++++++++++ .../selftests/bpf/progs/verifier_vfs_reject.c | 30 +++++++++++++++++++ 2 files changed, 53 insertions(+)
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +23 -0
@@ -100,4 +100,27 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,return 0 ; } +SEC("lsm.s/bprm_committed_creds") +__success +int BPF_PROG (trusted_field_used_in_rcu_cs ,struct linux_binprm *bprm) +{ +struct mm_struct *mm = bprm->mm; +struct inode *inode; +struct file *file; +ino_t ino =0 ; + +if (!mm) +return 0 ; + +bpf_rcu_read_lock (); + file = mm->exe_file; +if (file) { + inode = file->f_inode; + ino = inode->i_ino; + } +bpf_rcu_read_unlock (); +__sink (ino); +return 0 ; +} + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +30 -0
@@ -173,4 +173,34 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,return -EACCES ;return 0 ; } + +__weak int subprog_trusted_inode(struct inode *inode __arg_trusted) +{ +return inode->i_ino; +} + +SEC("lsm.s/bprm_committed_creds") +__failure __msg("R1 type=untrusted_ptr_ expected=ptr_, trusted_ptr_, rcu_ptr_") +int BPF_PROG(trusted_field_escapes_rcu_cs, struct linux_binprm *bprm) +{ +struct mm_struct *mm = bprm->mm; +struct inode *inode; +struct file *file; + +if (!mm) +return 0 ; + +bpf_rcu_read_lock (); + file = mm->exe_file; +if (!file) { +bpf_rcu_read_unlock (); +return 0 ; + } + inode = file->f_inode; +bpf_rcu_read_unlock (); + +/* inode inherited MEM_RCU from file and is untrusted after unlock. */ +subprog_trusted_inode (inode); +return 0 ; +} char _license[] SEC("license") = "GPL";
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v1 2/4] selftests/bpf: Test MEM_RCU carry-over for trusted fields
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: unhandled NULL deref of f_inode due to SLAB_TYPESAFE_BY_RCU --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: unhandled NULL deref of f_inode due to SLAB_TYPESAFE_BY_RCU --
commit 3b6fbe50569bfdf0fb6b88ab1c38f2e6056c0f68
Author: Justin Suess <[email protected]>
selftests/bpf: Test MEM_RCU carry-over for trusted fields
This commit adds selftests to verify the behavior of the BPF verifier when accessing fields from RCU-protected pointers. It includes an accept test for dereferencing an inode within an RCU critical section, and a reject test for using it after the critical section ends.
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +0
-0
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +7
-0+SEC("lsm.s/bprm_committed_creds") +__success +int BPF_PROG (trusted_field_used_in_rcu_cs ,struct linux_binprm *bprm) +{ +struct mm_struct *mm = bprm->mm; +struct inode *inode; +struct file *file;
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry, · line 119
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +10
-0+ino_t ino =0 ; + +if (!mm) +return 0 ; + +bpf_rcu_read_lock (); + file = mm->exe_file; +if (file) { + inode = file->f_inode; + ino = inode->i_ino;
[Severity: High]
Can this result in an unhandled NULL pointer dereference leading to a kernel Oops?
Because struct file is allocated with SLAB_TYPESAFE_BY_RCU in files_init() in fs/file_table.c, the file object can be concurrently freed and reallocated while an RCU reader holds a pointer to it. During this reallocation process, alloc_empty_file() calls init_file(), which zeroes out f_inode.
In check_ptr_to_btf_access() in the verifier, the PTR_MAYBE_NULL flag is omitted for trusted fields read from MEM_RCU pointers. This prevents the verifier from enforcing a NULL check on the resulting inode pointer. Additionally, the dereference bypasses the exception table rewrite that would normally handle memory faults for untrusted pointers.
If a file is closed and reallocated concurrently while the BPF program runs, could inode be NULL when inode->i_ino is accessed? Is it safe to codify this pattern as an accept test without requiring a NULL check for f_inode?
Quoted 7 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +6
-0+ } +bpf_rcu_read_unlock (); +__sink (ino); +return 0 ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf-next v1 2/4] selftests/bpf: Test MEM_RCU carry-over for trusted fields
Quoted 32 lines · On Wed, Oct 14, 2026 at 05:43:34PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: unhandled NULL deref off_inodedue toSLAB_TYPESAFE_BY_RCU--commit
3b6fbe50569bfdf0fb6b88ab1c38f2e6056c0f68
Author: Justin Suess <[email protected]>selftests/bpf: Test
MEM_RCUcarry-over for trusted fieldsThis commit adds selftests to verify the behavior of the BPF verifier when accessing fields from RCU-protected pointers. It includes an accept test for dereferencing an inode within an RCU critical section, and a reject test for using it after the critical section ends.
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +0
-0[ ... ]
Quoted 13 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +13
-0+SEC("lsm.s/bprm_committed_creds") +__success +int BPF_PROG (trusted_field_used_in_rcu_cs ,struct linux_binprm *bprm) +{ +struct mm_struct *mm = bprm->mm; +struct inode *inode; +struct file *file; +ino_t ino =0 ; + +if (!mm) +return 0 ; + +bpf_rcu_read_lock ();
Quoted 9 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry, · line 119
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +4
-0+ file = mm->exe_file; +if (file) { + inode = file->f_inode; + ino = inode->i_ino;[Severity: High]
Can this result in an unhandled NULL pointer dereference leading to a kernel Oops?
No because this test never executes. It's a verifier load selftest.
Justin
Quoted 25 lines
Because struct file is allocated with
SLAB_TYPESAFE_BY_RCUinfiles_init()infs/file_table.c, the file object can be concurrently freed and reallocated while an RCU reader holds a pointer to it. During this reallocation process,alloc_empty_file()callsinit_file(), which zeroes outf_inode.In
check_ptr_to_btf_access()in the verifier, thePTR_MAYBE_NULLflag is omitted for trusted fields read fromMEM_RCUpointers. This prevents the verifier from enforcing a NULL check on the resulting inode pointer. Additionally, the dereference bypasses the exception table rewrite that would normally handle memory faults for untrusted pointers.If a file is closed and reallocated concurrently while the BPF program runs, could inode be NULL when
inode->i_inois accessed? Is it safe to codify this pattern as an accept test without requiring a NULL check forf_inode?Quoted 7 lines
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +6
-0+ } +bpf_rcu_read_unlock (); +__sink (ino); +return 0 ; +} + char _license[] SEC("license") = "GPL";Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/4 bpf: Bind trusted fields to their parent's reference
In reply to Justin SuessA field in the __safe_trusted or __safe_trusted_or_null set walked out of a referenced parent yields PTR_TRUSTED with nothing tying it to the reference that keeps it alive. Releasing the parent invalidates the parent's own register but not the walked field, which stays fully trusted after the object may already be freed:
file = bpf_get_task_exe_file(task); /* acquired */ inode = file->f_inode; /* __safe_trusted */ bpf_put_file(file); /* inode stays trusted */
Set the walked field's parent_id to the parent's reference id, or to the parent's own parent_id so a chain of derived fields stays bound to the original reference. release_reference() already invalidates every register whose parent_id matches, recursively, and regsafe() already maps parent_id through check_ids(), so no other verifier changes are needed. An RCU-protected parent keeps bounding its fields to the RCU critical section through MEM_RCU instead.
--- kernel/bpf/verifier.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/bpf/verifier.c +7 -0
@@ -6562,6 +6562,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,const char *tname =btf_name_by_offset (reg->btf, t->name_off);const char *field_name =NULL ;enum bpf_type_flag flag =0 ; +u32 parent_id =0 ;u32 btf_id =0 ;s64 min_off;int ret;
@@ -6716,6 +6717,11 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,*/ flag =PTR_UNTRUSTED ; } + +/* A trusted field is pinned by whatever pins its parent. */ +if (flag &PTR_TRUSTED ) + parent_id =reg_is_referenced (env, reg) ? reg->id : + reg->parent_id; }else {/* Old compat. Deprecated */ clear_trusted_flags (&flag);
@@ -6725,6 +6731,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env, ret =mark_btf_ld_reg (env, regs, value_regno, ret, reg->btf, btf_id, flag);if (ret <0 )return ret; + regs[value_regno].parent_id = parent_id; }return 0 ;
Signature · 1 line
2.55.0
PATCH 4/4 selftests/bpf: Test trusted-field invalidation on parent release
In reply to Justin SuessWalking file->f_inode out of an acquired file must not outlive bpf_put_file(). Add a reject test where the inode is used as a trusted argument after the file's reference is released and an accept test where it is read before.
--- .../selftests/bpf/progs/verifier_vfs_accept.c | 17 +++++++++++++++++ .../selftests/bpf/progs/verifier_vfs_reject.c | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+)
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +17 -0
@@ -123,4 +123,21 @@ int BPF_PROG(trusted_field_used_in_rcu_cs, struct linux_binprm *bprm)return 0 ; } +SEC("lsm.s/file_open") +__success +int BPF_PROG (trusted_field_used_before_put ) +{ +struct task_struct *task =bpf_get_current_task_btf (); +struct file *file =bpf_get_task_exe_file (task); +ino_t ino; + +if (!file) +return 0 ; + + ino = file->f_inode->i_ino; +bpf_put_file (file); +__sink (ino); +return 0 ; +} + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +19 -0
@@ -203,4 +203,23 @@ int BPF_PROG(trusted_field_escapes_rcu_cs, struct linux_binprm *bprm)subprog_trusted_inode (inode);return 0 ; } + +SEC("lsm.s/file_open") +__failure __msg("R1 type=scalar expected=ptr_, trusted_ptr_, rcu_ptr_") +int BPF_PROG(trusted_field_outlives_parent_reference) +{ +struct task_struct *task =bpf_get_current_task_btf (); +struct file *file =bpf_get_task_exe_file (task); +struct inode *inode; + +if (!file) +return 0 ; + + inode = file->f_inode; +bpf_put_file (file); + +/* inode was pinned by file's reference and died with it. */ +subprog_trusted_inode (inode); +return 0 ; +} char _license[] SEC("license") = "GPL";
Signature · 1 line
2.55.0