[PATCH bpf-next v5 2/7] exec: Clear bprm->mm before dropping its reference
From: Anastasios Papagiannis
Date: Mon Sep 07 2026 - 13:00:53 EST
Once mmput() drops the final reference to bprm->mm, the pointer must no
longer remain accessible through struct linux_binprm.
The successful exec path and the bprm initialization error path already
clear bprm->mm when ownership is transferred or released. Do the same in
free_bprm() before calling mmput().
This is required for BPF kfuncs where bprm->mm is either NULL or points
to a live mm_struct to ensure safe access.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@xxxxxxxxx>
Reviewed-by: Sun Jian <sun.jian.kdev@xxxxxxxxx>
---
fs/exec.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index 745f6eb5279e..4ddd403fd91c 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1456,9 +1456,12 @@ void bprm_drop_loader(struct linux_binprm *bprm)
static void free_bprm(struct linux_binprm *bprm)
{
- if (bprm->mm) {
+ struct mm_struct *mm = bprm->mm;
+
+ if (mm) {
acct_arg_size(bprm, 0);
- mmput(bprm->mm);
+ bprm->mm = NULL;
+ mmput(mm);
}
if (bprm->user_ns)
put_user_ns(bprm->user_ns);
--
2.55.0