* [PATCH bpf-next 1/5] mm: Add copy_remote_mm_str()
2026-08-11 11:21 [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
@ 2026-08-11 11:21 ` Anastasios Papagiannis
2026-08-11 12:40 ` bot+bpf-ci
2026-08-11 11:21 ` [PATCH bpf-next 2/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
` (4 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Anastasios Papagiannis @ 2026-08-11 11:21 UTC (permalink / raw)
To: bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis
copy_remote_vm_str() gets the target address space from a struct
task_struct. This does not work for an address space that exists but is
not yet associated with a task_struct, such as the mm held by struct
linux_binprm during exec.
This patch adds copy_remote_mm_str(), which operates directly on a struct
mm_struct.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
include/linux/mm.h | 2 ++
mm/memory.c | 25 ++++++++++++++++++++++++-
mm/nommu.c | 25 ++++++++++++++++++++++++-
3 files changed, 50 insertions(+), 2 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 485df9c2dbdd..eede435bf4a3 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3222,6 +3222,8 @@ extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
void *buf, int len, unsigned int gup_flags);
#ifdef CONFIG_BPF_SYSCALL
+extern int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
+ void *buf, int len, unsigned int gup_flags);
extern int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr,
void *buf, int len, unsigned int gup_flags);
#endif
diff --git a/mm/memory.c b/mm/memory.c
index 6b8280cfc1db..03cbd3b02bef 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -7218,6 +7218,29 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr,
return buf - old_buf;
}
+/**
+ * copy_remote_mm_str - copy a string from a remote address space.
+ * @mm: the remote address space
+ * @addr: start address to read from
+ * @buf: destination buffer
+ * @len: number of bytes to copy
+ * @gup_flags: flags modifying lookup behaviour
+ *
+ * The caller must hold a reference on @mm.
+ *
+ * Return: number of bytes copied from @addr (source) to @buf (destination),
+ * not including the trailing NUL. Always guarantees a NUL-terminated buffer.
+ * On any error, return -EFAULT.
+ */
+int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
+ void *buf, int len, unsigned int gup_flags)
+{
+ if (unlikely(len == 0))
+ return 0;
+
+ return __copy_remote_vm_str(mm, addr, buf, len, gup_flags);
+}
+
/**
* copy_remote_vm_str - copy a string from another process's address space.
* @tsk: the task of the target address space
@@ -7247,7 +7270,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr,
return -EFAULT;
}
- ret = __copy_remote_vm_str(mm, addr, buf, len, gup_flags);
+ ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags);
mmput(mm);
diff --git a/mm/nommu.c b/mm/nommu.c
index ed3934bc2de4..8fbe6306d074 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1752,6 +1752,29 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr,
return ret;
}
+/**
+ * copy_remote_mm_str - copy a string from a remote address space.
+ * @mm: the remote address space
+ * @addr: start address to read from
+ * @buf: destination buffer
+ * @len: number of bytes to copy
+ * @gup_flags: flags modifying lookup behaviour (unused)
+ *
+ * The caller must hold a reference on @mm.
+ *
+ * Return: number of bytes copied from @addr (source) to @buf (destination),
+ * not including the trailing NUL. Always guarantees a NUL-terminated buffer.
+ * On any error, return -EFAULT.
+ */
+int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
+ void *buf, int len, unsigned int gup_flags)
+{
+ if (unlikely(len == 0))
+ return 0;
+
+ return __copy_remote_vm_str(mm, addr, buf, len);
+}
+
/**
* copy_remote_vm_str - copy a string from another process's address space.
* @tsk: the task of the target address space
@@ -1781,7 +1804,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr,
return -EFAULT;
}
- ret = __copy_remote_vm_str(mm, addr, buf, len);
+ ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags);
mmput(mm);
--
2.55.0
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next 1/5] mm: Add copy_remote_mm_str()
2026-08-11 11:21 ` [PATCH bpf-next 1/5] mm: Add copy_remote_mm_str() Anastasios Papagiannis
@ 2026-08-11 12:40 ` bot+bpf-ci
0 siblings, 0 replies; 10+ messages in thread
From: bot+bpf-ci @ 2026-08-11 12:40 UTC (permalink / raw)
To: tasos.papagiannnis, bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis, ast,
andrii, daniel, martin.lau, eddyz87, yonghong.song, clm,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3206 bytes --]
> mm: Add copy_remote_mm_str()
>
> copy_remote_vm_str() gets the target address space from a struct
> task_struct. This does not work for an address space that exists but is
> not yet associated with a task_struct, such as the mm held by struct
> linux_binprm during exec.
>
> This patch adds copy_remote_mm_str(), which operates directly on a struct
> mm_struct.
This isn't a bug, but could this last paragraph be reworded in the
imperative, e.g. "Add copy_remote_mm_str(), which operates directly on a
struct mm_struct", to match the changelog style asked for in
submitting-patches.rst?
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 485df9c2dbddb..eede435bf4a3f 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -3222,6 +3222,8 @@ extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
> void *buf, int len, unsigned int gup_flags);
>
> #ifdef CONFIG_BPF_SYSCALL
> +extern int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
> + void *buf, int len, unsigned int gup_flags);
> extern int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr,
> void *buf, int len, unsigned int gup_flags);
> #endif
> diff --git a/mm/memory.c b/mm/memory.c
> index 6b8280cfc1dbe..03cbd3b02befb 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -7218,6 +7218,29 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr,
> return buf - old_buf;
> }
>
> +/**
> + * copy_remote_mm_str - copy a string from a remote address space.
> + * @mm: the remote address space
> + * @addr: start address to read from
> + * @buf: destination buffer
> + * @len: number of bytes to copy
> + * @gup_flags: flags modifying lookup behaviour
This isn't a bug, but would it be worth using a tab after the parameter
colons here, to match the copy_remote_vm_str and access_remote_vm
kernel-doc blocks in the same files? This is the only kernel-doc block in
mm/memory.c that aligns parameter descriptions with spaces rather than a
tab.
> + *
> + * The caller must hold a reference on @mm.
> + *
> + * Return: number of bytes copied from @addr (source) to @buf (destination),
> + * not including the trailing NUL. Always guarantees a NUL-terminated buffer.
> + * On any error, return -EFAULT.
> + */
> +int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
> + void *buf, int len, unsigned int gup_flags)
> +{
> + if (unlikely(len == 0))
> + return 0;
> +
> + return __copy_remote_vm_str(mm, addr, buf, len, gup_flags);
> +}
> +
> /**
> * copy_remote_vm_str - copy a string from another process's address space.
> * @tsk: the task of the target address space
> @@ -7247,7 +7270,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr,
> return -EFAULT;
> }
>
> - ret = __copy_remote_vm_str(mm, addr, buf, len, gup_flags);
> + ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags);
>
> mmput(mm);
>
[ ... ]
---
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/31488001112
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next 2/5] bpf: Add user memory access kfuncs for mm_struct
2026-08-11 11:21 [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
2026-08-11 11:21 ` [PATCH bpf-next 1/5] mm: Add copy_remote_mm_str() Anastasios Papagiannis
@ 2026-08-11 11:21 ` Anastasios Papagiannis
2026-08-11 12:40 ` bot+bpf-ci
2026-08-11 11:21 ` [PATCH bpf-next 3/5] bpf: Mark linux_binprm->mm as trusted-or-null Anastasios Papagiannis
` (3 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Anastasios Papagiannis @ 2026-08-11 11:21 UTC (permalink / raw)
To: bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis
When security_bprm_check runs, the arg and env strings for the exec have
been copied into bprm->mm. The new address space has not been associated
yet with a task_struct until exec_mmap(), so existing BPF user memory
helpers can only read from the calling task's old address space.
Add bpf_copy_from_user_mm() and bpf_copy_from_user_mm_str() kfuncs. Both
take a struct mm_struct pointer directly, allowing callers to access
trusted address spaces that are not associated with a task_struct.
bpf_copy_from_user_mm() has similar semantics to
bpf_copy_from_user_task(). bpf_copy_from_user_mm_str() copies one
NUL-terminated string and returns its size including the NUL terminator.
It accepts BPF_F_PAD_ZEROS to clear unused destination bytes on success.
Register both kfuncs with the common BPF_EVENTS kfunc set and mark them
KF_SLEEPABLE because accessing the remote address space can fault. The next
patch marks linux_binprm->mm as trusted-or-null, allowing BPF LSM programs
attached to security_bprm_check to read arguments beginning at bprm->p
after a NULL check and reject an exec based on its command-line arguments.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
kernel/bpf/helpers.c | 2 +
kernel/trace/bpf_trace.c | 83 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6388b6b23e49..9ba3362832cd 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4931,6 +4931,8 @@ BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
BTF_ID_FLAGS(func, bpf_local_irq_save)
BTF_ID_FLAGS(func, bpf_local_irq_restore)
#ifdef CONFIG_BPF_EVENTS
+BTF_ID_FLAGS(func, bpf_copy_from_user_mm, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_copy_from_user_mm_str, KF_SLEEPABLE)
BTF_ID_FLAGS(func, bpf_probe_read_user_dynptr)
BTF_ID_FLAGS(func, bpf_probe_read_kernel_dynptr)
BTF_ID_FLAGS(func, bpf_probe_read_user_str_dynptr)
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 891897f8a1b3..8931b3b2fc9d 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -24,6 +24,7 @@
#include <linux/key.h>
#include <linux/namei.h>
#include <linux/file.h>
+#include <linux/mm.h>
#include <net/bpf_sk_storage.h>
@@ -3619,6 +3620,88 @@ __bpf_kfunc int bpf_send_signal_task(struct task_struct *task, int sig, enum pid
return bpf_send_signal_common(sig, type, task, value);
}
+/**
+ * bpf_copy_from_user_mm() - Copy data from an address space
+ * @dst: Destination address, in kernel space
+ * @dst__sz: Number of bytes to copy
+ * @unsafe_ptr__ign: Source address in the address space
+ * @mm: Address space to copy from
+ * @flags: Reserved for future use; must be zero
+ *
+ * Copies data from the user address space associated with @mm. The destination
+ * is zeroed if the requested number of bytes cannot be copied in full.
+ *
+ * Return: 0 on success, -EINVAL if @flags is non-zero, or -EFAULT if the copy
+ * fails or is partial.
+ */
+__bpf_kfunc int bpf_copy_from_user_mm(void *dst, u32 dst__sz,
+ const void __user *unsafe_ptr__ign,
+ struct mm_struct *mm, u64 flags)
+{
+ int ret;
+
+ if (unlikely(flags))
+ return -EINVAL;
+
+ if (unlikely(!dst__sz))
+ return 0;
+
+ ret = access_remote_vm(mm, (unsigned long)unsafe_ptr__ign,
+ dst, dst__sz, 0);
+ if (ret == dst__sz)
+ return 0;
+
+ memset(dst, 0, dst__sz);
+ return ret < 0 ? ret : -EFAULT;
+}
+
+/**
+ * bpf_copy_from_user_mm_str() - Copy a string from an address space
+ * @dst: Destination address, in kernel space. This buffer must be
+ * at least @dst__sz bytes long
+ * @dst__sz: Maximum number of bytes to copy, including the trailing NUL
+ * @unsafe_ptr__ign: Source address in the address space
+ * @mm: Address space to copy from
+ * @flags: The only supported flag is BPF_F_PAD_ZEROS
+ *
+ * Copies a NUL-terminated string from the user address space associated with
+ * @mm. If the string is too long, @dst is still NUL-terminated unless @dst__sz
+ * is zero.
+ *
+ * If BPF_F_PAD_ZEROS is set, the unused portion of @dst is cleared on success
+ * and all of @dst is cleared on failure.
+ *
+ * Return: The number of copied bytes including the NUL terminator on success,
+ * or a negative error code on failure.
+ */
+__bpf_kfunc int bpf_copy_from_user_mm_str(void *dst, u32 dst__sz,
+ const void __user *unsafe_ptr__ign,
+ struct mm_struct *mm, u64 flags)
+{
+ int ret;
+
+ if (unlikely(flags & ~BPF_F_PAD_ZEROS))
+ return -EINVAL;
+
+ if (unlikely(!dst__sz))
+ return 0;
+
+ ret = copy_remote_mm_str(mm, (unsigned long)unsafe_ptr__ign,
+ dst, dst__sz, 0);
+ if (ret < 0)
+ goto err;
+
+ if (flags & BPF_F_PAD_ZEROS)
+ memset(dst + ret, 0, dst__sz - ret);
+
+ return ret + 1;
+
+err:
+ if (flags & BPF_F_PAD_ZEROS)
+ memset(dst, 0, dst__sz);
+ return ret;
+}
+
__bpf_kfunc int bpf_probe_read_user_dynptr(const struct bpf_dynptr *dptr, u64 off,
u64 size, const void __user *unsafe_ptr__ign)
{
--
2.55.0
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next 2/5] bpf: Add user memory access kfuncs for mm_struct
2026-08-11 11:21 ` [PATCH bpf-next 2/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
@ 2026-08-11 12:40 ` bot+bpf-ci
0 siblings, 0 replies; 10+ messages in thread
From: bot+bpf-ci @ 2026-08-11 12:40 UTC (permalink / raw)
To: tasos.papagiannnis, bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis, ast,
andrii, daniel, martin.lau, eddyz87, yonghong.song, clm,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3382 bytes --]
> Register both kfuncs with the common BPF_EVENTS kfunc set and mark them
This isn't a bug, but could the changelog be more precise here? The
kfuncs go into common_btf_ids, which is registered for
BPF_PROG_TYPE_UNSPEC, so they are reachable from any sleepable program
rather than from a tracing-specific set.
Relatedly, was gating them on CONFIG_BPF_EVENTS intentional? Nothing in
either function depends on tracing, and the analogous
bpf_copy_from_user_task*() kfuncs in kernel/bpf/helpers.c are available
unconditionally.
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 6388b6b23e490..9ba3362832cd0 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -4931,6 +4931,8 @@ BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
> BTF_ID_FLAGS(func, bpf_local_irq_save)
> BTF_ID_FLAGS(func, bpf_local_irq_restore)
> #ifdef CONFIG_BPF_EVENTS
> +BTF_ID_FLAGS(func, bpf_copy_from_user_mm, KF_SLEEPABLE)
> +BTF_ID_FLAGS(func, bpf_copy_from_user_mm_str, KF_SLEEPABLE)
> BTF_ID_FLAGS(func, bpf_probe_read_user_dynptr)
> BTF_ID_FLAGS(func, bpf_probe_read_kernel_dynptr)
> BTF_ID_FLAGS(func, bpf_probe_read_user_str_dynptr)
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 891897f8a1b35..8931b3b2fc9d7 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -24,6 +24,7 @@
> #include <linux/key.h>
> #include <linux/namei.h>
> #include <linux/file.h>
> +#include <linux/mm.h>
>
> #include <net/bpf_sk_storage.h>
>
[ ... ]
> +__bpf_kfunc int bpf_copy_from_user_mm(void *dst, u32 dst__sz,
> + const void __user *unsafe_ptr__ign,
> + struct mm_struct *mm, u64 flags)
> +{
> + int ret;
> +
> + if (unlikely(flags))
> + return -EINVAL;
> +
> + if (unlikely(!dst__sz))
> + return 0;
> +
> + ret = access_remote_vm(mm, (unsigned long)unsafe_ptr__ign,
> + dst, dst__sz, 0);
> + if (ret == dst__sz)
> + return 0;
> +
> + memset(dst, 0, dst__sz);
> + return ret < 0 ? ret : -EFAULT;
> +}
[ ... ]
> +__bpf_kfunc int bpf_copy_from_user_mm_str(void *dst, u32 dst__sz,
> + const void __user *unsafe_ptr__ign,
> + struct mm_struct *mm, u64 flags)
> +{
> + int ret;
> +
> + if (unlikely(flags & ~BPF_F_PAD_ZEROS))
> + return -EINVAL;
> +
> + if (unlikely(!dst__sz))
> + return 0;
> +
> + ret = copy_remote_mm_str(mm, (unsigned long)unsafe_ptr__ign,
> + dst, dst__sz, 0);
> + if (ret < 0)
> + goto err;
> +
> + if (flags & BPF_F_PAD_ZEROS)
> + memset(dst + ret, 0, dst__sz - ret);
> +
> + return ret + 1;
> +
> +err:
> + if (flags & BPF_F_PAD_ZEROS)
> + memset(dst, 0, dst__sz);
> + return ret;
> +}
This isn't a bug, but since copy_remote_vm_str() in mm/memory.c is
already just a get_task_mm()/mmput() wrapper around copy_remote_mm_str(),
would it be cleaner to have bpf_copy_from_user_task_str() delegate to the
new mm-based kfunc (or share a static helper) rather than keeping two
near-identical copies of the flag/PAD_ZEROS/return-value logic in
different files?
The new copy also uses a 'goto err' where the existing one inlines the
error path, so the two now diverge in style too.
---
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/31488001112
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next 3/5] bpf: Mark linux_binprm->mm as trusted-or-null
2026-08-11 11:21 [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
2026-08-11 11:21 ` [PATCH bpf-next 1/5] mm: Add copy_remote_mm_str() Anastasios Papagiannis
2026-08-11 11:21 ` [PATCH bpf-next 2/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
@ 2026-08-11 11:21 ` Anastasios Papagiannis
2026-08-11 12:40 ` bot+bpf-ci
2026-08-11 11:21 ` [PATCH bpf-next 4/5] selftests/bpf: Test trusted-or-null linux_binprm->mm Anastasios Papagiannis
` (2 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Anastasios Papagiannis @ 2026-08-11 11:21 UTC (permalink / raw)
To: bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis
Mark linux_binprm->mm as a trusted-or-null nested pointer so BPF programs
can pass it to kfuncs after a NULL check. The linux_binprm owns this mm
throughout the bprm_check_security hook and clears the pointer only after
exec_mmap() installs the new address space.
This patch also updates the existing LSM selftest to NULL-check bprm->mm
before dereferencing it.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
kernel/bpf/verifier.c | 5 +++++
tools/testing/selftests/bpf/progs/lsm.c | 5 ++++-
2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index add3affc5703..8bdcb65618d0 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5682,6 +5682,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry) {
struct inode *d_inode;
};
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct linux_binprm) {
+ struct mm_struct *mm;
+};
+
BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
struct sock *sk;
};
@@ -5732,6 +5736,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
{
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
+ BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct linux_binprm));
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct));
return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
diff --git a/tools/testing/selftests/bpf/progs/lsm.c b/tools/testing/selftests/bpf/progs/lsm.c
index 7de173daf27b..7441d66c080c 100644
--- a/tools/testing/selftests/bpf/progs/lsm.c
+++ b/tools/testing/selftests/bpf/progs/lsm.c
@@ -113,6 +113,7 @@ int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
{
__u32 pid = bpf_get_current_pid_tgid() >> 32;
struct inner_map *inner_map;
+ struct mm_struct *mm;
char args[64];
__u32 key = 0;
__u64 *value;
@@ -121,7 +122,9 @@ int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
bprm_count++;
bpf_copy_from_user(args, sizeof(args), (void *)bprm->vma->vm_mm->arg_start);
- bpf_copy_from_user(args, sizeof(args), (void *)bprm->mm->arg_start);
+ mm = bprm->mm;
+ if (mm)
+ bpf_copy_from_user(args, sizeof(args), (void *)mm->arg_start);
value = bpf_map_lookup_elem(&array, &key);
if (value)
--
2.55.0
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next 3/5] bpf: Mark linux_binprm->mm as trusted-or-null
2026-08-11 11:21 ` [PATCH bpf-next 3/5] bpf: Mark linux_binprm->mm as trusted-or-null Anastasios Papagiannis
@ 2026-08-11 12:40 ` bot+bpf-ci
0 siblings, 0 replies; 10+ messages in thread
From: bot+bpf-ci @ 2026-08-11 12:40 UTC (permalink / raw)
To: tasos.papagiannnis, bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis, ast,
andrii, daniel, martin.lau, eddyz87, yonghong.song, clm,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 4199 bytes --]
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index add3affc57035..8bdcb65618d01 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -5682,6 +5682,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry) {
> struct inode *d_inode;
> };
>
> +BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct linux_binprm) {
> + struct mm_struct *mm;
> +};
> +
Does this break the BPF load-compatibility guarantee documented in
Documentation/bpf/bpf_design_QA.rst?
Before this patch, walking bprm->mm produced a dereferenceable register
in every program type. In sleepable programs, in_rcu_cs() is false, so
check_ptr_to_btf_access() sets flag = PTR_UNTRUSTED. In non-sleepable
programs, in_rcu_cs() is true but mm is not in any __safe_rcu* list, so
clear_trusted_flags() produces a plain PTR_TO_BTF_ID.
Both PTR_TO_BTF_ID and PTR_TO_BTF_ID|PTR_UNTRUSTED satisfy the guard in
check_mem_access():
} else if (base_type(reg->type) == PTR_TO_BTF_ID &&
!type_may_be_null(reg->type)) {
err = check_ptr_to_btf_access(env, regs, reg, argno, off, size, t,
value_regno);
so bprm->mm->arg_start loads with no NULL check.
After this patch, btf_nested_type_is_trusted() matches
linux_binprm__safe_trusted_or_null.mm, so check_ptr_to_btf_access() sets
flag |= PTR_TRUSTED | PTR_MAYBE_NULL. Now type_may_be_null() is true, the
arm above is skipped, and control falls to the final else:
} else {
verbose(env, "%s invalid mem access '%s'\n", reg_arg_name(env, argno),
reg_type_str(env, reg->type));
return -EACCES;
}
Any existing program that reads through bprm->mm without an explicit NULL
check now fails bpf_prog_load() with -EACCES.
> @@ -5732,6 +5736,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
> {
> BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
> BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
> + BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct linux_binprm));
> BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct));
>
> return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
> diff --git a/tools/testing/selftests/bpf/progs/lsm.c b/tools/testing/selftests/bpf/progs/lsm.c
> index 7de173daf27b6..7441d66c080c9 100644
> --- a/tools/testing/selftests/bpf/progs/lsm.c
> +++ b/tools/testing/selftests/bpf/progs/lsm.c
> @@ -113,6 +113,7 @@ int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
> {
> __u32 pid = bpf_get_current_pid_tgid() >> 32;
> struct inner_map *inner_map;
> + struct mm_struct *mm;
> char args[64];
> __u32 key = 0;
> __u64 *value;
> @@ -121,7 +122,9 @@ int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
> bprm_count++;
>
> bpf_copy_from_user(args, sizeof(args), (void *)bprm->vma->vm_mm->arg_start);
> - bpf_copy_from_user(args, sizeof(args), (void *)bprm->mm->arg_start);
> + mm = bprm->mm;
> + if (mm)
> + bpf_copy_from_user(args, sizeof(args), (void *)mm->arg_start);
>
> value = bpf_map_lookup_elem(&array, &key);
> if (value)
The selftest rewrite above shows the problem: the old form
bpf_copy_from_user(args, sizeof(args), (void *)bprm->mm->arg_start) now
requires a NULL check. Out-of-tree BPF LSM and tracing programs that read
bprm->mm the same way will stop loading on kernel upgrade with no source
change on their side.
The bpf_design_QA.rst compatibility guarantee states: "The bpf development
process guarantees that the future kernel versions will accept all bpf
programs that were accepted by the earlier versions."
The tracing-program exception in that document does not cover this case:
struct linux_binprm's layout is unchanged, the program source is unchanged,
and the in-tree casualty is a BPF LSM program, not a bpf_probe_read()-based
tracing program.
Should the changelog mention the compatibility impact and document that
programs dereferencing bprm->mm now need a NULL check?
---
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/31488001112
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next 4/5] selftests/bpf: Test trusted-or-null linux_binprm->mm
2026-08-11 11:21 [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
` (2 preceding siblings ...)
2026-08-11 11:21 ` [PATCH bpf-next 3/5] bpf: Mark linux_binprm->mm as trusted-or-null Anastasios Papagiannis
@ 2026-08-11 11:21 ` Anastasios Papagiannis
2026-08-11 11:21 ` [PATCH bpf-next 5/5] selftests/bpf: Test mm_struct user memory kfuncs with linux_binprm Anastasios Papagiannis
2026-08-11 13:28 ` [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
5 siblings, 0 replies; 10+ messages in thread
From: Anastasios Papagiannis @ 2026-08-11 11:21 UTC (permalink / raw)
To: bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis
Add verifier coverage for the linux_binprm->mm trusted-or-null
annotation. Verify that bpf_copy_from_user_mm() accepts the pointer
after a NULL check and rejects it without one.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
.../selftests/bpf/progs/verifier_lsm.c | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/verifier_lsm.c b/tools/testing/selftests/bpf/progs/verifier_lsm.c
index c724bf389f5c..4fc835432af0 100644
--- a/tools/testing/selftests/bpf/progs/verifier_lsm.c
+++ b/tools/testing/selftests/bpf/progs/verifier_lsm.c
@@ -5,6 +5,10 @@
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
+extern int bpf_copy_from_user_mm(void *dst, u32 dst__sz,
+ const void *unsafe_ptr__ign,
+ struct mm_struct *mm, u64 flags) __ksym;
+
SEC("lsm/file_permission")
__description("lsm bpf prog with -4095~0 retval. test 1")
__success
@@ -212,4 +216,31 @@ __naked int retval_load_resets_bounds(void *ctx)
::: __clobber_all);
}
+SEC("lsm.s/bprm_check_security")
+__description("null checking trusted-or-null linux_binprm mm")
+__success
+int BPF_PROG(copy_from_user_mm_with_null_check, struct linux_binprm *bprm, int ret)
+{
+ struct mm_struct *mm;
+ char dst[8];
+
+ mm = bprm->mm;
+ if (!mm)
+ return 0;
+
+ bpf_copy_from_user_mm(dst, sizeof(dst), (void *)bprm->p, mm, 0);
+ return 0;
+}
+
+SEC("lsm.s/bprm_check_security")
+__description("not null checking trusted-or-null linux_binprm mm")
+__failure __msg("Possibly NULL pointer passed to trusted R4")
+int BPF_PROG(copy_from_user_mm_without_null_check, struct linux_binprm *bprm, int ret)
+{
+ char dst[8];
+
+ bpf_copy_from_user_mm(dst, sizeof(dst), (void *)bprm->p, bprm->mm, 0);
+ return 0;
+}
+
char _license[] SEC("license") = "GPL";
--
2.55.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH bpf-next 5/5] selftests/bpf: Test mm_struct user memory kfuncs with linux_binprm
2026-08-11 11:21 [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
` (3 preceding siblings ...)
2026-08-11 11:21 ` [PATCH bpf-next 4/5] selftests/bpf: Test trusted-or-null linux_binprm->mm Anastasios Papagiannis
@ 2026-08-11 11:21 ` Anastasios Papagiannis
2026-08-11 13:28 ` [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
5 siblings, 0 replies; 10+ messages in thread
From: Anastasios Papagiannis @ 2026-08-11 11:21 UTC (permalink / raw)
To: bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis
Add a sleepable BPF LSM program attached to bprm_check_security to test
bpf_copy_from_user_mm() and bpf_copy_from_user_mm_str() using the trusted
mm_struct from linux_binprm.
After checking bprm->mm for NULL, start at bprm->p and verify that
bpf_copy_from_user_mm() can copy the contiguous NUL-separated argument
data. Then use bpf_copy_from_user_mm_str() to read each argument separately
by advancing the offset based on the returned length. Also verify that
bpf_copy_from_user_mm() rejects unsupported flags.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
.../bpf/prog_tests/copy_from_user_bprm.c | 52 +++++++++++++
.../selftests/bpf/progs/copy_from_user_bprm.c | 77 +++++++++++++++++++
2 files changed, 129 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c
create mode 100644 tools/testing/selftests/bpf/progs/copy_from_user_bprm.c
diff --git a/tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c b/tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c
new file mode 100644
index 000000000000..3d5080a3975e
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/copy_from_user_bprm.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <errno.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include <test_progs.h>
+
+#include "copy_from_user_bprm.skel.h"
+
+void test_copy_from_user_bprm(void)
+{
+ struct copy_from_user_bprm *skel;
+ pid_t child;
+ int status;
+
+ skel = copy_from_user_bprm__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ if (!ASSERT_OK(copy_from_user_bprm__attach(skel), "attach"))
+ goto out;
+
+ child = fork();
+ if (!ASSERT_GE(child, 0, "fork"))
+ goto out;
+
+ if (!child) {
+ char *const argv[] = { "first", "second-argument", NULL };
+
+ skel->bss->monitored_pid = getpid();
+ execv("/bin/true", argv);
+ _exit(errno);
+ }
+
+ if (!ASSERT_EQ(waitpid(child, &status, 0), child, "waitpid"))
+ goto out;
+
+ if (ASSERT_TRUE(WIFEXITED(status), "child_exited"))
+ ASSERT_EQ(WEXITSTATUS(status), EPERM, "exec_errno");
+
+ ASSERT_EQ(skel->bss->bprm_argc, 2, "bprm_argc");
+ ASSERT_EQ(skel->bss->invalid_flags_ret, -EINVAL, "invalid_flags_ret");
+ ASSERT_EQ(skel->bss->copy_ret, 0, "copy_ret");
+ ASSERT_EQ(skel->bss->str_arg0_ret, 6, "str_arg0_ret");
+ ASSERT_EQ(skel->bss->str_arg1_ret, 16, "str_arg1_ret");
+ ASSERT_EQ(skel->bss->args_match, 1, "args_match");
+ ASSERT_EQ(skel->bss->str_args_match, 1, "str_args_match");
+
+out:
+ copy_from_user_bprm__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/copy_from_user_bprm.c b/tools/testing/selftests/bpf/progs/copy_from_user_bprm.c
new file mode 100644
index 000000000000..d7132bca6edc
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/copy_from_user_bprm.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <errno.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+static const char expected_args[] = "first\0second-argument";
+static const char expected_arg0[] = "first";
+static const char expected_arg1[] = "second-argument";
+
+int monitored_pid;
+int bprm_argc;
+int invalid_flags_ret;
+int copy_ret;
+int str_arg0_ret;
+int str_arg1_ret;
+int args_match;
+int str_args_match;
+
+extern int bpf_copy_from_user_mm(void *dst, u32 dst__sz,
+ const void *unsafe_ptr__ign,
+ struct mm_struct *mm, u64 flags) __ksym;
+
+extern int bpf_copy_from_user_mm_str(void *dst, u32 dst__sz,
+ const void *unsafe_ptr__ign,
+ struct mm_struct *mm, u64 flags) __ksym;
+
+SEC("lsm.s/bprm_check_security")
+int BPF_PROG(check_exec_args, struct linux_binprm *bprm, int ret)
+{
+ u32 pid = bpf_get_current_pid_tgid() >> 32;
+ struct mm_struct *mm;
+ char args[sizeof(expected_args)];
+ char arg0[32];
+ char arg1[32];
+
+ if (ret || pid != monitored_pid)
+ return ret;
+
+ mm = bprm->mm;
+ if (!mm)
+ return 0;
+
+ bprm_argc = bprm->argc;
+
+ invalid_flags_ret = bpf_copy_from_user_mm(args, sizeof(args),
+ (void *)bprm->p, mm, 1);
+
+ copy_ret = bpf_copy_from_user_mm(args, sizeof(args),
+ (void *)bprm->p, mm, 0);
+ if (copy_ret)
+ return 0;
+
+ args_match = !__builtin_memcmp(args, expected_args, sizeof(expected_args));
+
+ str_arg0_ret = bpf_copy_from_user_mm_str(arg0, sizeof(arg0),
+ (void *)bprm->p, mm,
+ BPF_F_PAD_ZEROS);
+ if (str_arg0_ret != sizeof(expected_arg0))
+ return 0;
+
+ str_arg1_ret = bpf_copy_from_user_mm_str(arg1, sizeof(arg1),
+ (void *)(bprm->p + str_arg0_ret),
+ mm, BPF_F_PAD_ZEROS);
+ if (str_arg1_ret != sizeof(expected_arg1))
+ return 0;
+
+ str_args_match = !__builtin_memcmp(arg0, expected_arg0, sizeof(expected_arg0)) &&
+ !__builtin_memcmp(arg1, expected_arg1, sizeof(expected_arg1));
+
+ return args_match && str_args_match ? -EPERM : 0;
+}
--
2.55.0
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct
2026-08-11 11:21 [PATCH bpf-next 0/5] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
` (4 preceding siblings ...)
2026-08-11 11:21 ` [PATCH bpf-next 5/5] selftests/bpf: Test mm_struct user memory kfuncs with linux_binprm Anastasios Papagiannis
@ 2026-08-11 13:28 ` Anastasios Papagiannis
5 siblings, 0 replies; 10+ messages in thread
From: Anastasios Papagiannis @ 2026-08-11 13:28 UTC (permalink / raw)
To: bpf
Cc: linux-kernel, linux-mm, akpm, david, ast, daniel, andrii,
eddyz87, memxor, kpsingh, matt, song, tasos.papagiannnis
After looking at the output from
https://sashiko.dev/#/patchset/20260811112154.94053-1-tasos.papagiannnis%40gmail.com
and https://github.com/kernel-patches/bpf/pull/13211 I believe that fixing
those issues in this series would complicate things.
For this reason, please disregard this series. I will send another version
separately.
Thanks,
Anastasios
^ permalink raw reply [flat|nested] 10+ messages in thread