Skip to contentSkip to conversation

sched: Fix NULL mm dereference in sched_mm_cid_after_execve()

PATCH Applied Cong Wang 6 participants 8 messages Dec 23, 2025 – Jan 9 Scheduler

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 sched: Fix NULL mm dereference in sched_mm_cid_after_execve()

From: Cong Wang <[email protected]>

sched_mm_cid_after_execve() is called in bprm_execve()'s cleanup path even when exec_binprm() fails. For the init task's first execve, this causes a problem:

1. current->mm is NULL (kernel threads don't have an mm)
2. sched_mm_cid_before_execve() exits early because mm is NULL
3. exec_binprm() fails (e.g., ENOENT for missing script interpreter)
4. sched_mm_cid_after_execve() is called with mm still NULL
5. sched_mm_cid_fork() is called unconditionally, triggering WARN_ON

This is easily reproduced by booting with an init that is a shell script (#!/bin/sh) where the interpreter doesn't exist in the initramfs.

Fix this by checking if t->mm is NULL before calling sched_mm_cid_fork(), matching the behavior of sched_mm_cid_before_execve() which already handles this case via sched_mm_cid_exit()'s early return.

Fixes b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value")
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/core.c +3 -2
index 41ba0be16911..60afadb6eede 100644
@@ -10694,10 +10694,11 @@ void sched_mm_cid_before_execve(struct task_struct *t)
 	sched_mm_cid_exit(t);
 }
 
-/* Reactivate MM CID after successful execve() */
+/* Reactivate MM CID after execve() */
 void sched_mm_cid_after_execve(struct task_struct *t)
 {
-	sched_mm_cid_fork(t);
+	if (t->mm)
+		sched_mm_cid_fork(t);
 }
 
 static void mm_cid_work_fn(struct work_struct *work)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Cong Wang

From: Cong Wang <[email protected]>

sched_mm_cid_after_execve() is called in bprm_execve()'s cleanup path even when exec_binprm() fails. For the init task's first execve, this causes a problem:

1. current->mm is NULL (kernel threads don't have an mm)
2. sched_mm_cid_before_execve() exits early because mm is NULL
3. exec_binprm() fails (e.g., ENOENT for missing script interpreter)
4. sched_mm_cid_after_execve() is called with mm still NULL
5. sched_mm_cid_fork() is called unconditionally, triggering WARN_ON

This is easily reproduced by booting with an init that is a shell script (#!/bin/sh) where the interpreter doesn't exist in the initramfs.

Quoted 7 lines

Fix this by checking if t->mm is NULL before calling sched_mm_cid_fork(), matching the behavior of sched_mm_cid_before_execve() which already handles this case via sched_mm_cid_exit()'s early return.

Fixes b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value")
Cc Thomas Gleixner <[email protected]>

Thanks for the detailed explanation.

Indeed, the offending commit removes a pre-existing NULL mm check:

void sched_mm_cid_after_execve(struct task_struct *t) {
- struct mm_struct *mm = t->mm;
-
- if (!mm)
- return;

Thomas, Peter, Ingo, can we fast-track this fix for upstream ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#2 lore raw
In reply to Cong Wang
Quoted 6 lines

  void sched_mm_cid_after_execve(struct task_struct *t) {
-    sched_mm_cid_fork(t);
+    if (t->mm)
+        sched_mm_cid_fork(t);
  }

Hi,

It's a correct solution, but I have a small suggestion that putting the 'mm' checking into sched_mm_cid_fork(), just like sched_mm_cid_exit().

Best regards,
Qing Wang

Acked-by on v1 1/1
#3 lore raw
In reply to Cong Wang
Quoted 34 lines · Cong Wang

From: Cong Wang <[email protected]>

sched_mm_cid_after_execve() is called in bprm_execve()'s cleanup path even when exec_binprm() fails. For the init task's first execve, this causes a problem:

1. current->mm is NULL (kernel threads don't have an mm)
2. sched_mm_cid_before_execve() exits early because mm is NULL
3. exec_binprm() fails (e.g., ENOENT for missing script interpreter)
4. sched_mm_cid_after_execve() is called with mm still NULL
5. sched_mm_cid_fork() is called unconditionally, triggering WARN_ON

This is easily reproduced by booting with an init that is a shell script (#!/bin/sh) where the interpreter doesn't exist in the initramfs.

Fix this by checking if t->mm is NULL before calling sched_mm_cid_fork(), matching the behavior of sched_mm_cid_before_execve() which already handles this case via sched_mm_cid_exit()'s early return.

Fixes b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value")
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index 41ba0be16911..60afadb6eede 100644
@@ -10694,10 +10694,11 @@ void sched_mm_cid_before_execve(struct task_struct *t)
 	sched_mm_cid_exit(t);
 }
 

kernel/sched/core.c void sched_mm_cid_before_execve(struct task_struct *t) · line 10704

Quoted 10 lines
kernel/sched/core.c +3 -2
-/* Reactivate MM CID after successful execve() */
+/* Reactivate MM CID after execve() */
 void sched_mm_cid_after_execve(struct task_struct *t)
 {
-	sched_mm_cid_fork(t);
+	if (t->mm)
+		sched_mm_cid_fork(t);
 }
 
 static void mm_cid_work_fn(struct work_struct *work)

This addresses a panic reported on arm64 when trying to execute x86 binaries using TCG on an Apple device:

https://lore.kernel.org/all/[email protected]/

so:

Please can we land this for 6.19?

Cheers,

Will

#4 lore raw
Quoted 49 lines · Will Deacon
Quoted 34 lines · Cong Wang

From: Cong Wang <[email protected]>

sched_mm_cid_after_execve() is called in bprm_execve()'s cleanup path even when exec_binprm() fails. For the init task's first execve, this causes a problem:

1. current->mm is NULL (kernel threads don't have an mm)
2. sched_mm_cid_before_execve() exits early because mm is NULL
3. exec_binprm() fails (e.g., ENOENT for missing script interpreter)
4. sched_mm_cid_after_execve() is called with mm still NULL
5. sched_mm_cid_fork() is called unconditionally, triggering WARN_ON

This is easily reproduced by booting with an init that is a shell script (#!/bin/sh) where the interpreter doesn't exist in the initramfs.

Fix this by checking if t->mm is NULL before calling sched_mm_cid_fork(), matching the behavior of sched_mm_cid_before_execve() which already handles this case via sched_mm_cid_exit()'s early return.

Fixes b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value")
Cc Thomas Gleixner <[email protected]>
---
  kernel/sched/core.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index 41ba0be16911..60afadb6eede 100644
@@ -10694,10 +10694,11 @@ void sched_mm_cid_before_execve(struct task_struct *t)
  	sched_mm_cid_exit(t);
  }
 

kernel/sched/core.c void sched_mm_cid_before_execve(struct task_struct *t) · line 10704

Quoted 10 lines
kernel/sched/core.c +3 -2
-/* Reactivate MM CID after successful execve() */
+/* Reactivate MM CID after execve() */
  void sched_mm_cid_after_execve(struct task_struct *t)
  {
-	sched_mm_cid_fork(t);
+	if (t->mm)
+		sched_mm_cid_fork(t);
  }
 
  static void mm_cid_work_fn(struct work_struct *work)

This addresses a panic reported on arm64 when trying to execute x86 binaries using TCG on an Apple device:

Quoted 7 lines

https://lore.kernel.org/all/[email protected]/

so:

Please can we land this for 6.19?

Yes, please. I gave my Reviewed-by already 2 weeks ago:

https://lore.kernel.org/lkml/[email protected]/

I guess the relevant maintainers are gradually coming back from the holiday break. I will ask it again here: can we fast-track this fix for upstream ?

Thanks,

Mathieu

Quoted 4 lines

Cheers,

Will

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#5 lore raw
Quoted 3 lines · Mathieu Desnoyers

On 2026-01-07 13:00, Will Deacon wrote:
I guess the relevant maintainers are gradually coming back from the holiday break. I will ask it again here: can we fast-track this fix for upstream ?

Yes people are coming back from vacation and are picking up stuff.

#6 lore raw
Quoted 6 lines · Thomas Gleixner
Quoted 3 lines · Mathieu Desnoyers

On 2026-01-07 13:00, Will Deacon wrote:
I guess the relevant maintainers are gradually coming back from the holiday break. I will ask it again here: can we fast-track this fix for upstream ?

Yes people are coming back from vacation and are picking up stuff.

That's what I figured. Thanks for picking this up!

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
merge bot Applied
#7 lore raw

[tip: sched/urgent] sched/mm_cid: Prevent NULL mm dereference in sched_mm_cid_after_execve()

In reply to Cong Wang
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     2bdf777410dc6e022d1081885ff34673b5dfee99
Gitweb:        https://git.kernel.org/tip/2bdf777410dc6e022d1081885ff34673b5dfee99
Author:        Cong Wang <[email protected]>
AuthorDate:    Tue, 23 Dec 2025 13:51:13 -08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 09 Jan 2026 13:02:57 +01:00

sched/mm_cid: Prevent NULL mm dereference in sched_mm_cid_after_execve()

sched_mm_cid_after_execve() is called in bprm_execve()'s cleanup path even when exec_binprm() fails. For the init task's first execve(), this causes a problem:

1. current->mm is NULL (kernel threads don't have an mm)
2. sched_mm_cid_before_execve() exits early because mm is NULL
3. exec_binprm() fails (e.g., ENOENT for missing script interpreter)
4. sched_mm_cid_after_execve() is called with mm still NULL
5. sched_mm_cid_fork() is called unconditionally, triggering WARN_ON

This is easily reproduced by booting with an init that is a shell script (#!/bin/sh) where the interpreter doesn't exist in the initramfs.

Fix this by checking if t->mm is NULL before calling sched_mm_cid_fork(), matching the behavior of sched_mm_cid_before_execve() which already handles this case via sched_mm_cid_exit()'s early return.

---
 kernel/sched/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/core.c +3 -2
index 41ba0be..60afadb 100644
@@ -10694,10 +10694,11 @@ void sched_mm_cid_before_execve(struct task_struct *t)
 	sched_mm_cid_exit(t);
 }
 
-/* Reactivate MM CID after successful execve() */
+/* Reactivate MM CID after execve() */
 void sched_mm_cid_after_execve(struct task_struct *t)
 {
-	sched_mm_cid_fork(t);
+	if (t->mm)
+		sched_mm_cid_fork(t);
 }
 
 static void mm_cid_work_fn(struct work_struct *work)