diff mbox series

[v2] ptrace: fix ptrace_unfreeze_traced() race with rt-lock

Message ID 20201103113901.GA29800@redhat.com
State Superseded
Headers show
Series [v2] ptrace: fix ptrace_unfreeze_traced() race with rt-lock | expand

Commit Message

Oleg Nesterov Nov. 3, 2020, 11:39 a.m. UTC
The commit 6362e6476cd7 ("ptrace: fix ptrace vs tasklist_lock race")
changed ptrace_freeze_traced() to take task->saved_state into account,
but ptrace_unfreeze_traced() has the same problem and needs a similar
fix: it should check/update both ->state and ->saved_state.

Reported-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>
Fixes: 6362e6476cd7 ("ptrace: fix ptrace vs tasklist_lock race")
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 kernel/ptrace.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

Comments

Sebastian Andrzej Siewior Nov. 3, 2020, 5:01 p.m. UTC | #1
On 2020-11-03 12:39:01 [+0100], Oleg Nesterov wrote:
> The commit 6362e6476cd7 ("ptrace: fix ptrace vs tasklist_lock race")

> changed ptrace_freeze_traced() to take task->saved_state into account,

> but ptrace_unfreeze_traced() has the same problem and needs a similar

> fix: it should check/update both ->state and ->saved_state.

> 

> Reported-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>

> Fixes: 6362e6476cd7 ("ptrace: fix ptrace vs tasklist_lock race")

> Signed-off-by: Oleg Nesterov <oleg@redhat.com>


Applied, thank you.

Sebastian
diff mbox series

Patch

diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 3075006d720e..3f7156f06b6c 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -197,8 +197,8 @@  static bool ptrace_freeze_traced(struct task_struct *task)
 
 static void ptrace_unfreeze_traced(struct task_struct *task)
 {
-	if (task->state != __TASK_TRACED)
-		return;
+	unsigned long flags;
+	bool frozen = true;
 
 	WARN_ON(!task->ptrace || task->parent != current);
 
@@ -207,12 +207,19 @@  static void ptrace_unfreeze_traced(struct task_struct *task)
 	 * Recheck state under the lock to close this race.
 	 */
 	spin_lock_irq(&task->sighand->siglock);
-	if (task->state == __TASK_TRACED) {
-		if (__fatal_signal_pending(task))
-			wake_up_state(task, __TASK_TRACED);
-		else
-			task->state = TASK_TRACED;
-	}
+
+	raw_spin_lock_irqsave(&task->pi_lock, flags);
+	if (task->state == __TASK_TRACED)
+		task->state = TASK_TRACED;
+	else if (task->saved_state == __TASK_TRACED)
+		task->saved_state = TASK_TRACED;
+	else
+		frozen = false;
+	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
+
+	if (frozen && __fatal_signal_pending(task))
+		wake_up_state(task, __TASK_TRACED);
+
 	spin_unlock_irq(&task->sighand->siglock);
 }