[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#704437: ptrace of stopped process fails



Package: src:linux-2.6
Version: 2.6.32-48squeeze1
Severity: important
Tags: patch

I noticed on the Ubuntu kernel list that there was a regression
reported to be caused by their fix for CVE-2013-0871, which we also
used.  The test case that detected this is part of the AppArmor
test suite, but I was able to write a standalone test case:

/* BEGIN */
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    int child = fork();
    int status;
    int rc;

    assert(child >= 0);

    if (child == 0) {
	kill(getpid(), SIGSTOP);
    } else {
	sleep(1);
	rc = ptrace(PTRACE_ATTACH, child, 0, 0);
	assert(rc >= 0);
	while (waitpid(child, &status, 0) == child) {
	    if (WIFSTOPPED(status)) {
		rc = ptrace(PTRACE_SINGLESTEP, child, 0, 0);
		if (rc < 0) {
		    perror("ptrace");
		    return 1;
		}
	    } else if (WIFSIGNALED(status)) {
		printf("signalled: %d\n", WTERMSIG(status));
		break;
	    } else if (WIFEXITED(status)) {
		printf("exited: %d\n", WEXITSTATUS(status));
		break;
	    }
	}
    }

    return 0;
}
/* END */

This should print 'exited: 0', but on 2.6.32-48squeeze1 it
prints 'ptrace: No such process'.

Attaching the Ubuntu patch - I haven't yet tested it but it
applies cleanly and looks entirely reasonable.

Ben.

-- System Information:
Debian Release: 7.0
  APT prefers unstable
  APT policy: (500, 'unstable'), (500, 'testing'), (1, 'experimental')
Architecture: i386 (x86_64)
Foreign Architectures: amd64

Kernel: Linux 3.2.0-4-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_GB.utf8, LC_CTYPE=en_GB.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
From: John Johansen <john.johansen@canonical.com>
Date: Thu, 21 Mar 2013 05:04:13 -0700
Subject: [PATCH] UBUNTU: SAUCE: Fix ptrace when task is in task_is_stopped()
 state

This patch fixes a regression in ptrace, introduced by commit 9e74eb39
(backport of 9899d11f) which makes assumptions about ptrace behavior
which are not true in the 2.6.32 kernel.

BugLink: http://bugs.launchpad.net/bugs/1145234

9899d11f makes the assumption that task_is_stopped() is not a valid state
in ptrace because it is built on top of a series of patches which change
how the TASK_STOPPED state is tracked (321fb561 which requires d79fdd6d
and several other patches).

Because Lucid does not have the set of patches that make task_is_stopped()
an invalid state in ptrace_check_attach, partially revert 9e74eb39 so
that ptrace_check_attach() correctly handles task_is_stopped(). However
we must replace the assignment of TASK_TRACED with __TASK_TRACED to
ensure TASK_WAKEKILL is cleared.

Signed-off-by: John Johansen <john.johansen@canonical.com>
---
 kernel/ptrace.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index d0036f0..d9c8c47 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -81,14 +81,18 @@ void __ptrace_unlink(struct task_struct *child)
 }
 
 /* Ensure that nothing can wake it up, even SIGKILL */
-static bool ptrace_freeze_traced(struct task_struct *task)
+static bool ptrace_freeze_traced(struct task_struct *task, int kill)
 {
-	bool ret = false;
+	bool ret = true;
 
 	spin_lock_irq(&task->sighand->siglock);
-	if (task_is_traced(task) && !__fatal_signal_pending(task)) {
+	if (task_is_stopped(task) && !__fatal_signal_pending(task))
 		task->state = __TASK_TRACED;
-		ret = true;
+	else if (!kill) {
+		if (task_is_traced(task) && !__fatal_signal_pending(task))
+			task->state = __TASK_TRACED;
+		else
+			ret = false;
 	}
 	spin_unlock_irq(&task->sighand->siglock);
 
@@ -131,7 +135,7 @@ int ptrace_check_attach(struct task_struct *child, int kill)
 		 * child->sighand can't be NULL, release_task()
 		 * does ptrace_unlink() before __exit_signal().
 		 */
-		if (kill || ptrace_freeze_traced(child))
+		if (ptrace_freeze_traced(child, kill))
 			ret = 0;
 	}
 	read_unlock(&tasklist_lock);
-- 
1.8.1.2



Reply to: