Skip to contentSkip to conversation

Fix check after use in kernel/exit.c

PATCH Applied WANG Cong 2 participants 7 messages Mar 8, 2008 – Mar 10, 2008

Subsystems (3)
lore
Share & tools
lore

WANG Cong

author
#0 lore raw

PATCH 1/1 Fix check after use in kernel/exit.c

mm_release won't check the mm_struct pointer, so it should be checked before this call.

This patch was first posted here[1], and I don't know why it was not accepted yet.

Thanks to "G. Vaughan" to remind me about this.

1. http://lkml.org/lkml/2007/11/15/520

Cc Ingo Molnar <[email protected]>
Cc "G. Vaughan" <[email protected]>
---
 kernel/exit.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/exit.c +1 -1
index cd20bf0..0cb9cce 100644
@@ -582,9 +582,9 @@ static void exit_mm(struct task_struct * tsk)
 {
 	struct mm_struct *mm = tsk->mm;
 
-	mm_release(tsk, mm);
 	if (!mm)
 		return;
+	mm_release(tsk, mm);
 	/*
 	 * Serialize with any possible pending coredump.
 	 * We must hold mmap_sem around checking core_waiters
Signature · 1 line
1.5.2.4
Applied
#1 lore raw

kernel/exit.c static void exit_mm(struct task_struct * tsk) · line 587

Quoted 11 lines · WANG Cong

mm_release won't check the mm_struct pointer, so it should be checked before this call.

kernel/exit.c +1 -1
@@ -582,9 +582,9 @@ static void exit_mm(struct task_struct * tsk)
 {
 	struct mm_struct *mm = tsk->mm;
 
-	mm_release(tsk, mm);
 	if (!mm)
 		return;
+	mm_release(tsk, mm);

thanks, applied. I'm wondering why this never seems to hit in practice.

Ingo

#2 lore raw
Quoted 11 lines · Ingo Molnar

kernel/exit.c static void exit_mm(struct task_struct * tsk) · line 587

Quoted 8 lines
kernel/exit.c +1 -1
@@ -582,9 +582,9 @@ static void exit_mm(struct task_struct * tsk)
 {
 	struct mm_struct *mm = tsk->mm;
 
-	mm_release(tsk, mm);
 	if (!mm)
 		return;
+	mm_release(tsk, mm);

thanks, applied. I'm wondering why this never seems to hit in practice.

actually, i unapplied it again because the patch is wrong: mm_release() has side-effects for kernel threads such as the deactivate_mm() [which is important even if the user-mm is NULL]. If the NULL mm dereference can really trigger then it should be avoided within mm_release().

Ingo

author
#3 lore raw

From: Ingo Molnar <[email protected]>
Date: Sat, 8 Mar 2008 09:53:22 +0100

Quoted 15 lines · Ingo Molnar
Quoted 5 lines
Quoted 5 lines
kernel/exit.c +0 -0
@@ -582,9 +582,9 @@ static void exit_mm(struct task_struct * tsk)
 {
 	struct mm_struct *mm = tsk->mm;
 
Quoted 7 lines

kernel/exit.c static void exit_mm(struct task_struct * tsk) · line 587

Quoted 4 lines
kernel/exit.c +1 -1
-	mm_release(tsk, mm);
 	if (!mm)
 		return;
+	mm_release(tsk, mm);

thanks, applied. I'm wondering why this never seems to hit in practice.

Quoted 5 lines

actually, i unapplied it again because the patch is wrong: mm_release() has side-effects for kernel threads such as the deactivate_mm() [which is important even if the user-mm is NULL]. If the NULL mm dereference can really trigger then it should be avoided within mm_release().

Do you mean that the NULL check should be moved into mm_release()?

Thanks!

#4 lore raw
Quoted 7 lines · WANG Cong
Quoted 5 lines

actually, i unapplied it again because the patch is wrong:
mm_release() has side-effects for kernel threads such as the deactivate_mm() [which is important even if the user-mm is NULL]. If the NULL mm dereference can really trigger then it should be avoided within mm_release().

Do you mean that the NULL check should be moved into mm_release()?

yes - we need to run deactivate_mm() for kernel threads.

Ingo

author
#5 lore raw

From: Ingo Molnar <[email protected]>
Date: Sun, 9 Mar 2008 12:48:44 +0100

Quoted 9 lines · WANG Cong
Quoted 7 lines
Quoted 6 lines

actually, i unapplied it again because the patch is wrong:
mm_release() has side-effects for kernel threads such as the deactivate_mm() [which is important even if the user-mm is NULL]. If the NULL mm dereference can really trigger then it should be avoided within mm_release().

Quoted 3 lines
Quoted 1 line

Do you mean that the NULL check should be moved into mm_release()?

yes - we need to run deactivate_mm() for kernel threads.

Thank you for your kind hints.

Here is an updated version. Please check.

---->

From: WANG Cong <[email protected]>
Subject: [PATCH] Fix check after use in kernel/exit.c

As suggested by Ingo, the NULL deference check should be moved into mm_release().

---
 kernel/exit.c |    2 --
 kernel/fork.c |    2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
kernel/exit.c +0 -2
index 53872bf..b88244a 100644
@@ -583,8 +583,6 @@ static void exit_mm(struct task_struct * tsk)
 	struct mm_struct *mm = tsk->mm;
 
 	mm_release(tsk, mm);
-	if (!mm)
-		return;
 	/*
 	 * Serialize with any possible pending coredump.
 	 * We must hold mmap_sem around checking core_waiters
kernel/fork.c +2 -0
index dd249c3..7683c5f 100644
@@ -464,6 +464,8 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
 {
 	struct completion *vfork_done = tsk->vfork_done;
 
+	if (!mm)
+		return;
 	/* Get rid of any cached register state */
 	deactivate_mm(tsk, mm);
 
Signature · 1 line
1.5.2.4
#6 lore raw
Quoted 10 lines · WANG Cong
kernel/exit.c +0 -2
@@ -583,8 +583,6 @@ static void exit_mm(struct task_struct * tsk)
 	struct mm_struct *mm = tsk->mm;
 
 	mm_release(tsk, mm);
-	if (!mm)
-		return;
 

kernel/exit.c void mm_release(struct task_struct *tsk, struct mm_struct *mm) · line 470

Quoted 7 lines
kernel/exit.c +2 -0
@@ -464,6 +464,8 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
 {
 	struct completion *vfork_done = tsk->vfork_done;
 
+	if (!mm)
+		return;
 	/* Get rid of any cached register state */
 	deactivate_mm(tsk, mm);

no, this is buggy in the same way - we wont do a deactivate_mm() for kernel threads. The check should be left alone in exit_mm(), we should at most add a check for NULL mm to this place:

        if (tsk->clear_child_tid
            && !(tsk->flags & PF_SIGNALED)
            && atomic_read(&mm->mm_users) > 1) {

but ... can mm in fact ever be NULL if tsk->clear_child_tid is set?

Ingo