Skip to contentSkip to conversation

ptrace_stop: fix the race with ptrace detach+attach

PATCH In review Oleg Nesterov 3 participants 4 messages Nov 22, 2007 – Dec 11, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ptrace_stop: fix the race with ptrace detach+attach

If the tracer went away (may_ptrace_stop() failed), ptrace_stop() drops tasklist and then changes the ->state from TASK_TRACED to TASK_RUNNING.

This can fool another tracer which attaches to us in between. Change the ->state under tasklist_lock to ensure that ptrace_check_attach() can't wrongly succeed. Also, remove the unnecessary mb().

kernel/signal.c renamed from kernel/signal.c~1_ptrace_stop +3 -3
@@ -1628,11 +1628,11 @@ static void ptrace_stop(int exit_code, i
 	} else {
 		/*
 		 * By the time we got the lock, our tracer went away.
-		 * Don't stop here.
+		 * Don't drop the lock yet, another tracer may come.
 		 */
-		read_unlock(&tasklist_lock);
-		set_current_state(TASK_RUNNING);
+		__set_current_state(TASK_RUNNING);
 		current->exit_code = nostop_code;
+		read_unlock(&tasklist_lock);
 	}
 
 	/*
#1 lore raw
Quoted 5 lines · Oleg Nesterov

If the tracer went away (may_ptrace_stop() failed), ptrace_stop() drops tasklist and then changes the ->state from TASK_TRACED to TASK_RUNNING.

This can fool another tracer which attaches to us in between. Change the ->state under tasklist_lock to ensure that ptrace_check_attach() can't wrongly succeed.

ptrace_check_attach? Both do read_lock -- can run in parallel, so how can it help?

Quoted 17 lines
kernel/signal.c renamed from kernel/signal.c~1_ptrace_stop +3 -3
@@ -1628,11 +1628,11 @@ static void ptrace_stop(int exit_code, i
 	} else {
 		/*
 		 * By the time we got the lock, our tracer went away.
-		 * Don't stop here.
+		 * Don't drop the lock yet, another tracer may come.
 		 */
-		read_unlock(&tasklist_lock);
-		set_current_state(TASK_RUNNING);
+		__set_current_state(TASK_RUNNING);
 		current->exit_code = nostop_code;
+		read_unlock(&tasklist_lock);
 	}
 
 	/*
#2 lore raw
Quoted 9 lines · Alexey Dobriyan
Quoted 5 lines · Oleg Nesterov

If the tracer went away (may_ptrace_stop() failed), ptrace_stop() drops tasklist and then changes the ->state from TASK_TRACED to TASK_RUNNING.

This can fool another tracer which attaches to us in between. Change the ->state under tasklist_lock to ensure that ptrace_check_attach() can't wrongly succeed.

ptrace_check_attach? Both do read_lock -- can run in parallel,

Yep.

Quoted 1 line

so how can it help?

read_lock prevents ptrace_attach(), so the new tracer can't attach and then do ptrace_check_attach().

Oleg.