Substitute MEM_RCU | PTR_MAYBE_NULL 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. An RCU-protected parent also guarantees less about the field's value than a trusted one. __safe_trusted promises a non-NULL field for as long as the parent object is valid, but RCU only keeps the parent's memory valid: struct file is allocated SLAB_TYPESAFE_BY_RCU, so a file read under RCU can be freed and reused within the same CS. Mark the field PTR_MAYBE_NULL as well, as is already done for __rcu tagged fields, so programs check it before dereferencing. Fixes: 6fcd486b3a0a ("bpf: Refactor RCU enforcement in the verifier.") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/bpf/sashiko-outbox-171590@kernel.org/ Signed-off-by: Justin Suess --- kernel/bpf/verifier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 2f190abe3448..534200c0d27d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -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_MAYBE_NULL : 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 */ -- 2.55.0 Walking file->f_inode out of an RCU-protected mm->exe_file must yield an RCU pointer that may be NULL rather than a trusted one. Add reject tests where the inode is dereferenced without a NULL check and where it is used as a trusted argument after bpf_rcu_read_unlock(), and an accept test where it is checked and dereferenced inside the RCU critical section. Signed-off-by: Justin Suess --- .../selftests/bpf/progs/verifier_vfs_accept.c | 24 +++++++++ .../selftests/bpf/progs/verifier_vfs_reject.c | 51 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c index 55398c04290a..34919abcff04 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c @@ -100,4 +100,28 @@ 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; + if (inode) + ino = inode->i_ino; + } + bpf_rcu_read_unlock(); + __sink(ino); + return 0; +} + char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c index b5f456d57669..7ca0d8c3edf1 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c @@ -173,4 +173,55 @@ 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("invalid mem access 'rcu_ptr_or_null_'") +int BPF_PROG(trusted_field_of_rcu_parent_unchecked, struct linux_binprm *bprm) +{ + struct mm_struct *mm = bprm->mm; + struct file *file; + ino_t ino = 0; + + if (!mm) + return 0; + + bpf_rcu_read_lock(); + file = mm->exe_file; + /* The file may have been recycled under RCU: f_inode can be NULL. */ + if (file) + ino = file->f_inode->i_ino; + bpf_rcu_read_unlock(); + __sink(ino); + return 0; +} + +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"; -- 2.55.0 A 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. Fixes: 6fcd486b3a0a ("bpf: Refactor RCU enforcement in the verifier.") Cc: stable@vger.kernel.org Signed-off-by: Justin Suess --- kernel/bpf/verifier.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 534200c0d27d..c85c83a6ee63 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -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; -- 2.55.0 Walking 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. Signed-off-by: Justin Suess --- .../selftests/bpf/progs/verifier_vfs_accept.c | 17 +++++++++++++++++ .../selftests/bpf/progs/verifier_vfs_reject.c | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c index 34919abcff04..53441391bcae 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c @@ -124,4 +124,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"; diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c index 7ca0d8c3edf1..c2be63ad8055 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c @@ -224,4 +224,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"; -- 2.55.0 An element returned by an iterator's next() kfunc is kept alive by the iterator alone. bpf_iter_task_vma_next() returns a snapshot that lives in the iterator's own allocation and holds the vma's vm_file reference only until the next step or destroy; bpf_iter_kmem_cache_next() and bpf_iter_dmabuf_next() hand out objects whose reference the iterator drops the same way. The verifier marks every such element PTR_TRUSTED with nothing tying it to the iterator, so a pointer saved across bpf_iter__destroy() stays fully trusted after the memory or the reference behind it is gone: bpf_iter_task_vma_new(&it, task, 0); vma = bpf_iter_task_vma_next(&it); file = vma->vm_file; /* __safe_trusted_or_null */ bpf_iter_task_vma_destroy(&it); /* frees snapshot, fput() */ start = vma->vm_start; /* vma, file still trusted */ Set the element's parent_id to the iterator's reference id, as is already done for the PTR_TO_MEM elements of bpf_iter_num_next(), so that destroying the iterator invalidates the element together with every trusted field acquired from it. Elements of an RCU-protected iterator keep MEM_RCU and stay bounded by the critical section instead: they point at RCU-managed objects rather than iterator state. Fixes: 4ac454682158 ("bpf: Introduce task_vma open-coded iterator kfuncs") Cc: stable@vger.kernel.org Signed-off-by: Justin Suess --- Documentation/bpf/bpf_iterators.rst | 6 +++++- kernel/bpf/verifier.c | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Documentation/bpf/bpf_iterators.rst b/Documentation/bpf/bpf_iterators.rst index 189e3ec1c6c8..08c9c15b59ac 100644 --- a/Documentation/bpf/bpf_iterators.rst +++ b/Documentation/bpf/bpf_iterators.rst @@ -79,7 +79,11 @@ Constructor/next/destructor implementation contract is as follows: will eventually return NULL when elements are exhausted. Once NULL is returned, subsequent next calls *should keep returning NULL*. Next method is marked with KF_ITER_NEXT (and should also have KF_RET_NULL as - NULL-returning kfunc, of course). + NULL-returning kfunc, of course). An element is only guaranteed to be + valid until the iterator is destroyed, so the verifier invalidates it, + and every trusted pointer derived from it, at `bpf_iter__destroy()`. + Elements of an RCU-protected iterator (see KF_RCU_PROTECTED) are instead + valid until the end of the RCU critical section. - destructor, `bpf_iter__destroy()`, is always called once. Even if constructor failed or next returned nothing. Destructor frees up any diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c85c83a6ee63..9ce5517dc1a0 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15504,6 +15504,19 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0].btf = desc_btf; regs[BPF_REG_0].type = type; regs[BPF_REG_0].btf_id = ptr_type_id; + + /* + * A trusted element is pinned by the iterator that + * produced it and dies with it, like the memory + * returned by bpf_iter_num_next() above. An RCU + * element is bounded by the critical section instead. + */ + if (bpf_is_iter_next_kfunc(&meta) && (type & PTR_TRUSTED)) { + err = validate_ref_obj(env, &meta.ref_obj); + if (err) + return err; + regs[BPF_REG_0].parent_id = meta.ref_obj.id; + } } if (is_kfunc_ret_null(&meta)) { -- 2.55.0 Save a task_vma element and a trusted field walked out of it across bpf_iter_task_vma_destroy() and check that both uses are rejected, and that the existing operations already allowed before destroy are accepted. Signed-off-by: Justin Suess --- .../selftests/bpf/progs/iters_task_failure.c | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/iters_task_failure.c b/tools/testing/selftests/bpf/progs/iters_task_failure.c index 566a1d3dffea..f9c149b4a2d5 100644 --- a/tools/testing/selftests/bpf/progs/iters_task_failure.c +++ b/tools/testing/selftests/bpf/progs/iters_task_failure.c @@ -14,6 +14,74 @@ void bpf_cgroup_release(struct cgroup *p) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; +__weak int subprog_trusted_file(struct file *file __arg_trusted) +{ + return file->f_inode->i_ino; +} + +SEC("?raw_tp") +__failure __msg("invalid mem access 'scalar'") +int iter_task_vma_use_after_destroy(const void *ctx) +{ + struct task_struct *task = bpf_get_current_task_btf(); + struct bpf_iter_task_vma it; + struct vm_area_struct *vma; + unsigned long start; + + bpf_iter_task_vma_new(&it, task, 0); + vma = bpf_iter_task_vma_next(&it); + if (!vma) { + bpf_iter_task_vma_destroy(&it); + return 0; + } + bpf_iter_task_vma_destroy(&it); + + /* The vma snapshot lives in the iterator and died with it. */ + start = vma->vm_start; + return start; +} + +SEC("?raw_tp") +__failure __msg("R1 type=scalar expected=ptr_, trusted_ptr_, rcu_ptr_") +int iter_task_vma_file_after_destroy(const void *ctx) +{ + struct task_struct *task = bpf_get_current_task_btf(); + struct bpf_iter_task_vma it; + struct vm_area_struct *vma; + struct file *file; + + bpf_iter_task_vma_new(&it, task, 0); + vma = bpf_iter_task_vma_next(&it); + file = vma ? vma->vm_file : NULL; + if (!file) { + bpf_iter_task_vma_destroy(&it); + return 0; + } + bpf_iter_task_vma_destroy(&it); + + /* vm_file was pinned through the vma by the iterator. */ + return subprog_trusted_file(file); +} + +SEC("?raw_tp") +__success +int iter_task_vma_file_before_destroy(const void *ctx) +{ + struct task_struct *task = bpf_get_current_task_btf(); + struct bpf_iter_task_vma it; + struct vm_area_struct *vma; + struct file *file; + int ret = 0; + + bpf_iter_task_vma_new(&it, task, 0); + vma = bpf_iter_task_vma_next(&it); + file = vma ? vma->vm_file : NULL; + if (file) + ret = subprog_trusted_file(file); + bpf_iter_task_vma_destroy(&it); + return ret; +} + SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") __failure __msg("kernel func bpf_iter_task_new requires RCU critical section protection") int BPF_PROG(iter_tasks_without_lock) -- 2.55.0