* [PATCH] mm_access: simplify the security checks
@ 2026-05-30 13:56 Oleg Nesterov
2026-05-30 14:10 ` Oleg Nesterov
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Oleg Nesterov @ 2026-05-30 13:56 UTC (permalink / raw)
To: Andrew Morton
Cc: Christian Brauner, David Hildenbrand, Jann Horn, Kees Cook,
Lorenzo Stoakes, Michal Hocko, linux-kernel, linux-mm
1. Shift the fast-path "mm == current->mm" check from may_access_mm()
to mm_access(), and do it locklessly.
task->mm is not stable but we do not care. We can race with exec,
but in this case we pin/return current->mm. This doesn't differ
from the case where the target execs after we drop exec_update_lock.
All we need for correctness is READ_ONCE() to ensure the compiler
won't reload task->mm. This is not enough for KCSAN, but we already
have other lockless ->mm LOAD's. We should probably change exec_mmap/
exit_mm to use WRITE_ONCE().
2. With the change above may_access_mm() doesn't need the "mm" argument,
so we do not need to call get_task_mm() beforehand, we can call it
only if may_access_mm() suceeds.
2. With the change above, may_access_mm() doesn't need the "mm" argument,
so we do not need to call get_task_mm() beforehand. We can call it
only if may_access_mm() succeeds.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/fork.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index b8b651abce8b..3239380ab93b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1381,10 +1381,8 @@ struct mm_struct *get_task_mm(struct task_struct *task)
}
EXPORT_SYMBOL_GPL(get_task_mm);
-static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsigned int mode)
+static bool may_access_mm(struct task_struct *task, unsigned int mode)
{
- if (mm == current->mm)
- return true;
if (ptrace_may_access(task, mode))
return true;
if ((mode & PTRACE_MODE_READ) && perfmon_capable())
@@ -1394,20 +1392,24 @@ static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsign
struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
{
- struct mm_struct *mm;
- int err;
+ struct mm_struct *mm = READ_ONCE(task->mm);
- err = down_read_killable(&task->signal->exec_update_lock);
- if (err)
- return ERR_PTR(err);
+ if (!mm || (task->flags & PF_KTHREAD))
+ return ERR_PTR(-ESRCH);
- mm = get_task_mm(task);
- if (!mm) {
- mm = ERR_PTR(-ESRCH);
- } else if (!may_access_mm(mm, task, mode)) {
- mmput(mm);
- mm = ERR_PTR(-EACCES);
+ if (mm == current->mm) {
+ mmget(mm);
+ return mm;
}
+
+ if (down_read_killable(&task->signal->exec_update_lock))
+ return ERR_PTR(-EINTR);
+
+ if (may_access_mm(task, mode))
+ mm = get_task_mm(task) ?: ERR_PTR(-ESRCH);
+ else
+ mm = ERR_PTR(-EACCES);
+
up_read(&task->signal->exec_update_lock);
return mm;
--
2.52.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm_access: simplify the security checks
2026-05-30 13:56 [PATCH] mm_access: simplify the security checks Oleg Nesterov
@ 2026-05-30 14:10 ` Oleg Nesterov
2026-05-30 14:12 ` [PATCH v2] " Oleg Nesterov
2026-05-30 15:00 ` [PATCH] " Oleg Nesterov
2 siblings, 0 replies; 4+ messages in thread
From: Oleg Nesterov @ 2026-05-30 14:10 UTC (permalink / raw)
To: Andrew Morton
Cc: Christian Brauner, David Hildenbrand, Jann Horn, Kees Cook,
Lorenzo Stoakes, Michal Hocko, linux-kernel, linux-mm
On 05/30, Oleg Nesterov wrote:
>
> 2. With the change above may_access_mm() doesn't need the "mm" argument,
> so we do not need to call get_task_mm() beforehand, we can call it
> only if may_access_mm() suceeds.
>
> 2. With the change above, may_access_mm() doesn't need the "mm" argument,
> so we do not need to call get_task_mm() beforehand. We can call it
> only if may_access_mm() succeeds.
OOPS. Somehow I wrote the same twice. Will fix the changelog and send V2
in reply to this patch...
Oleg.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] mm_access: simplify the security checks
2026-05-30 13:56 [PATCH] mm_access: simplify the security checks Oleg Nesterov
2026-05-30 14:10 ` Oleg Nesterov
@ 2026-05-30 14:12 ` Oleg Nesterov
2026-05-30 15:00 ` [PATCH] " Oleg Nesterov
2 siblings, 0 replies; 4+ messages in thread
From: Oleg Nesterov @ 2026-05-30 14:12 UTC (permalink / raw)
To: Andrew Morton
Cc: Christian Brauner, David Hildenbrand, Jann Horn, Kees Cook,
Lorenzo Stoakes, Michal Hocko, linux-kernel, linux-mm
1. Shift the fast-path "mm == current->mm" check from may_access_mm()
to mm_access(), and do it locklessly.
task->mm is not stable but we do not care. We can race with exec,
but in this case we pin/return current->mm. This doesn't differ
from the case where the target execs after we drop exec_update_lock.
All we need for correctness is READ_ONCE() to ensure the compiler
won't reload task->mm. This is not enough for KCSAN, but we already
have other lockless ->mm LOAD's. We should probably change exec_mmap/
exit_mm to use WRITE_ONCE().
2. With the change above, may_access_mm() doesn't need the "mm" argument,
so we do not need to call get_task_mm() beforehand. We can call it
only if may_access_mm() succeeds.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/fork.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index b8b651abce8b..3239380ab93b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1381,10 +1381,8 @@ struct mm_struct *get_task_mm(struct task_struct *task)
}
EXPORT_SYMBOL_GPL(get_task_mm);
-static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsigned int mode)
+static bool may_access_mm(struct task_struct *task, unsigned int mode)
{
- if (mm == current->mm)
- return true;
if (ptrace_may_access(task, mode))
return true;
if ((mode & PTRACE_MODE_READ) && perfmon_capable())
@@ -1394,20 +1392,24 @@ static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsign
struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
{
- struct mm_struct *mm;
- int err;
+ struct mm_struct *mm = READ_ONCE(task->mm);
- err = down_read_killable(&task->signal->exec_update_lock);
- if (err)
- return ERR_PTR(err);
+ if (!mm || (task->flags & PF_KTHREAD))
+ return ERR_PTR(-ESRCH);
- mm = get_task_mm(task);
- if (!mm) {
- mm = ERR_PTR(-ESRCH);
- } else if (!may_access_mm(mm, task, mode)) {
- mmput(mm);
- mm = ERR_PTR(-EACCES);
+ if (mm == current->mm) {
+ mmget(mm);
+ return mm;
}
+
+ if (down_read_killable(&task->signal->exec_update_lock))
+ return ERR_PTR(-EINTR);
+
+ if (may_access_mm(task, mode))
+ mm = get_task_mm(task) ?: ERR_PTR(-ESRCH);
+ else
+ mm = ERR_PTR(-EACCES);
+
up_read(&task->signal->exec_update_lock);
return mm;
--
2.52.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm_access: simplify the security checks
2026-05-30 13:56 [PATCH] mm_access: simplify the security checks Oleg Nesterov
2026-05-30 14:10 ` Oleg Nesterov
2026-05-30 14:12 ` [PATCH v2] " Oleg Nesterov
@ 2026-05-30 15:00 ` Oleg Nesterov
2 siblings, 0 replies; 4+ messages in thread
From: Oleg Nesterov @ 2026-05-30 15:00 UTC (permalink / raw)
To: Andrew Morton
Cc: Christian Brauner, David Hildenbrand, Jann Horn, Kees Cook,
Lorenzo Stoakes, Michal Hocko, linux-kernel, linux-mm
On 05/30, Oleg Nesterov wrote:
>
> 1. Shift the fast-path "mm == current->mm" check from may_access_mm()
> to mm_access(), and do it locklessly.
Heh ;) sashiko has some concerns, see
https://sashiko.dev/#/patchset/ahrsfJE3NkKjShEX%40redhat.com
I still think this is fine, but I'll recheck tomorrow.
Oleg.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-05-30 15:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-30 13:56 [PATCH] mm_access: simplify the security checks Oleg Nesterov
2026-05-30 14:10 ` Oleg Nesterov
2026-05-30 14:12 ` [PATCH v2] " Oleg Nesterov
2026-05-30 15:00 ` [PATCH] " Oleg Nesterov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox