]> pilppa.com Git - linux-2.6-omap-h63xx.git/commitdiff
[PATCH] use ptrace_get_task_struct in various places
authorChristoph Hellwig <hch@lst.de>
Sun, 8 Jan 2006 09:02:33 +0000 (01:02 -0800)
committerLinus Torvalds <torvalds@g5.osdl.org>
Mon, 9 Jan 2006 04:13:51 +0000 (20:13 -0800)
The ptrace_get_task_struct() helper that I added as part of the ptrace
consolidation is useful in variety of places that currently opencode it.
Switch them to the common helpers.

Add a ptrace_traceme() helper that needs to be explicitly called, and simplify
the ptrace_get_task_struct() interface.  We don't need the request argument
now, and we return the task_struct directly, using ERR_PTR() for error
returns.  It's a bit more code in the callers, but we have two sane routines
that do one thing well now.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
12 files changed:
arch/alpha/kernel/ptrace.c
arch/ia64/ia32/sys_ia32.c
arch/ia64/kernel/ptrace.c
arch/m32r/kernel/ptrace.c
arch/mips/kernel/ptrace32.c
arch/powerpc/kernel/ptrace32.c
arch/s390/kernel/ptrace.c
arch/sparc/kernel/ptrace.c
arch/sparc64/kernel/ptrace.c
arch/x86_64/ia32/ptrace32.c
include/linux/ptrace.h
kernel/ptrace.c

index bbd37536d14ef98458b3142bd532f4065a4dd8ec..9969d212e94d9d4b16da2f66921f8b206822612e 100644 (file)
@@ -265,30 +265,16 @@ do_sys_ptrace(long request, long pid, long addr, long data,
        lock_kernel();
        DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
                      request, pid, addr, data));
-       ret = -EPERM;
        if (request == PTRACE_TRACEME) {
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED)
-                       goto out_notsk;
-               ret = security_ptrace(current->parent, current);
-               if (ret)
-                       goto out_notsk;
-               /* set the ptrace bit in the process ptrace flags. */
-               current->ptrace |= PT_PTRACED;
-               ret = 0;
+               ret = ptrace_traceme();
                goto out_notsk;
        }
-       if (pid == 1)           /* you may not mess with init */
-               goto out_notsk;
 
-       ret = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (!child)
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
                goto out_notsk;
+       }
 
        if (request == PTRACE_ATTACH) {
                ret = ptrace_attach(child);
index dc282710421a18b71053a3ecb86f9ae8f778f129..9f8e8d5588731066475d8dc235a3f8bc7d608e34 100644 (file)
@@ -1761,21 +1761,15 @@ sys32_ptrace (int request, pid_t pid, unsigned int addr, unsigned int data)
 
        lock_kernel();
        if (request == PTRACE_TRACEME) {
-               ret = sys_ptrace(request, pid, addr, data);
+               ret = ptrace_traceme();
                goto out;
        }
 
-       ret = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (!child)
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
                goto out;
-       ret = -EPERM;
-       if (pid == 1)           /* no messing around with init! */
-               goto out_tsk;
+       }
 
        if (request == PTRACE_ATTACH) {
                ret = sys_ptrace(request, pid, addr, data);
index 4b19d04106326db6b01cfa3735421d07c27a4190..8d88eeea02d12fa762a1d54ba71ce15be73529c9 100644 (file)
@@ -1422,14 +1422,7 @@ sys_ptrace (long request, pid_t pid, unsigned long addr, unsigned long data)
        lock_kernel();
        ret = -EPERM;
        if (request == PTRACE_TRACEME) {
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED)
-                       goto out;
-               ret = security_ptrace(current->parent, current);
-               if (ret)
-                       goto out;
-               current->ptrace |= PT_PTRACED;
-               ret = 0;
+               ret = ptrace_traceme();
                goto out;
        }
 
index 078d2a0e71c2669f3ce47673bc49e5857a03b3fc..9b75caaf5cec49ba98a0357312f4a4cdbb35042f 100644 (file)
@@ -762,28 +762,16 @@ asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
        int ret;
 
        lock_kernel();
-       ret = -EPERM;
        if (request == PTRACE_TRACEME) {
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED)
-                       goto out;
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
-               ret = 0;
+               ret = ptrace_traceme();
                goto out;
        }
-       ret = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (!child)
-               goto out;
 
-       ret = -EPERM;
-       if (pid == 1)           /* you may not mess with init */
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
                goto out;
+       }
 
        if (request == PTRACE_ATTACH) {
                ret = ptrace_attach(child);
index 9a9b049721327cb28fca14dffd8b6bf5f5d47ec1..7e55457a491f3c71b730dd5c3286030db571f02f 100644 (file)
@@ -57,30 +57,16 @@ asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
               (unsigned long) data);
 #endif
        lock_kernel();
-       ret = -EPERM;
        if (request == PTRACE_TRACEME) {
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED)
-                       goto out;
-               if ((ret = security_ptrace(current->parent, current)))
-                       goto out;
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
-               ret = 0;
+               ret = ptrace_traceme();
                goto out;
        }
-       ret = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (!child)
-               goto out;
 
-       ret = -EPERM;
-       if (pid == 1)           /* you may not mess with init */
-               goto out_tsk;
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
+               goto out;
+       }
 
        if (request == PTRACE_ATTACH) {
                ret = ptrace_attach(child);
index 61762640b8775ea1d1bfb7acb9f22c5b865708b5..826ee3d056de09c8618b8671e8684a5e1b1c3edd 100644 (file)
@@ -45,33 +45,19 @@ long compat_sys_ptrace(int request, int pid, unsigned long addr,
                       unsigned long data)
 {
        struct task_struct *child;
-       int ret = -EPERM;
+       int ret;
 
        lock_kernel();
        if (request == PTRACE_TRACEME) {
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED)
-                       goto out;
-               ret = security_ptrace(current->parent, current);
-               if (ret)
-                       goto out;
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
-               ret = 0;
+               ret = ptrace_traceme();
                goto out;
        }
-       ret = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (!child)
-               goto out;
 
-       ret = -EPERM;
-       if (pid == 1)           /* you may not mess with init */
-               goto out_tsk;
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
+               goto out;
+       }
 
        if (request == PTRACE_ATTACH) {
                ret = ptrace_attach(child);
index 8ecda6d66de4c0b2e9a1a7b3b32c4dbc49dea891..cc02232aa96e93be846ceac801908fc08e070432 100644 (file)
@@ -712,35 +712,18 @@ sys_ptrace(long request, long pid, long addr, long data)
        int ret;
 
        lock_kernel();
-
        if (request == PTRACE_TRACEME) {
-               /* are we already being traced? */
-               ret = -EPERM;
-               if (current->ptrace & PT_PTRACED)
-                       goto out;
-               ret = security_ptrace(current->parent, current);
-               if (ret)
-                       goto out;
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
-               goto out;
+                ret = ptrace_traceme();
+                goto out;
        }
 
-       ret = -EPERM;
-       if (pid == 1)           /* you may not mess with init */
-               goto out;
-
-       ret = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (!child)
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
                goto out;
+       }
 
        ret = do_ptrace(child, request, addr, data);
-
        put_task_struct(child);
 out:
        unlock_kernel();
index 475c4c13462c8c81b79e7594422817246f4a20d2..fc470c0e9dc6bd453ce6b14143610d70225622d5 100644 (file)
@@ -286,40 +286,17 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
                               s, (int) request, (int) pid, addr, data, addr2);
        }
 #endif
-       if (request == PTRACE_TRACEME) {
-               int my_ret;
-
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED) {
-                       pt_error_return(regs, EPERM);
-                       goto out;
-               }
-               my_ret = security_ptrace(current->parent, current);
-               if (my_ret) {
-                       pt_error_return(regs, -my_ret);
-                       goto out;
-               }
 
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
+       if (request == PTRACE_TRACEME) {
+               ret = ptrace_traceme();
                pt_succ_return(regs, 0);
                goto out;
        }
-#ifndef ALLOW_INIT_TRACING
-       if (pid == 1) {
-               /* Can't dork with init. */
-               pt_error_return(regs, EPERM);
-               goto out;
-       }
-#endif
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
 
-       if (!child) {
-               pt_error_return(regs, ESRCH);
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
+               pt_error_return(regs, -ret);
                goto out;
        }
 
index 774ecbb8a0319061c909c9ad72b39d32b11a2737..84d3df2264cb7148e4a5390f05a0ce0baf618844 100644 (file)
@@ -198,39 +198,15 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
        }
 #endif
        if (request == PTRACE_TRACEME) {
-               int ret;
-
-               /* are we already being traced? */
-               if (current->ptrace & PT_PTRACED) {
-                       pt_error_return(regs, EPERM);
-                       goto out;
-               }
-               ret = security_ptrace(current->parent, current);
-               if (ret) {
-                       pt_error_return(regs, -ret);
-                       goto out;
-               }
-
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
+               ret = ptrace_traceme();
                pt_succ_return(regs, 0);
                goto out;
        }
-#ifndef ALLOW_INIT_TRACING
-       if (pid == 1) {
-               /* Can't dork with init. */
-               pt_error_return(regs, EPERM);
-               goto out;
-       }
-#endif
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
 
-       if (!child) {
-               pt_error_return(regs, ESRCH);
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
+               pt_error_return(regs, -ret);
                goto out;
        }
 
index 2a925e2af390bddc1ac34ba37882f8138703de1a..5f4cdfa56901ef4528c4850a8a225f246a672a13 100644 (file)
@@ -196,36 +196,6 @@ static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
 
 #undef R32
 
-static struct task_struct *find_target(int request, int pid, int *err)
-{ 
-       struct task_struct *child;
-
-       *err = -EPERM; 
-       if (pid == 1)
-               return NULL; 
-
-       *err = -ESRCH;
-       read_lock(&tasklist_lock);
-       child = find_task_by_pid(pid);
-       if (child)
-               get_task_struct(child);
-       read_unlock(&tasklist_lock);
-       if (child) { 
-               *err = -EPERM;
-               if (child->pid == 1) 
-                       goto out;
-               *err = ptrace_check_attach(child, request == PTRACE_KILL); 
-               if (*err < 0) 
-                       goto out;
-               return child; 
-       } 
- out:
-       if (child)
-       put_task_struct(child);
-       return NULL; 
-       
-} 
-
 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
 {
        struct task_struct *child;
@@ -254,9 +224,16 @@ asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
                break;
        } 
 
-       child = find_target(request, pid, &ret);
-       if (!child)
-               return ret;
+       if (request == PTRACE_TRACEME)
+               return ptrace_traceme();
+
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child))
+               return PTR_ERR(child);
+
+       ret = ptrace_check_attach(child, request == PTRACE_KILL);
+       if (ret < 0)
+               goto out;
 
        childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs)); 
 
@@ -373,6 +350,7 @@ asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
                break;
        }
 
+ out:
        put_task_struct(child);
        return ret;
 }
index b2b3dba1298d1fef7f1b4e4ecfba22fe142c88d2..864791996b5fa8efaafb777dff7f74cc623c3f47 100644 (file)
@@ -80,6 +80,8 @@
 
 
 extern long arch_ptrace(struct task_struct *child, long request, long addr, long data);
+extern struct task_struct *ptrace_get_task_struct(pid_t pid);
+extern int ptrace_traceme(void);
 extern int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len);
 extern int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len);
 extern int ptrace_attach(struct task_struct *tsk);
index 656476eedb1bfe9a4de0286d7dd35aad8023c8d1..cceaf09ac413a4da1b9ea3d27d7655cb405e785c 100644 (file)
@@ -408,54 +408,62 @@ int ptrace_request(struct task_struct *child, long request,
        return ret;
 }
 
-#ifndef __ARCH_SYS_PTRACE
-static int ptrace_get_task_struct(long request, long pid,
-               struct task_struct **childp)
+/**
+ * ptrace_traceme  --  helper for PTRACE_TRACEME
+ *
+ * Performs checks and sets PT_PTRACED.
+ * Should be used by all ptrace implementations for PTRACE_TRACEME.
+ */
+int ptrace_traceme(void)
 {
-       struct task_struct *child;
        int ret;
 
        /*
-        * Callers use child == NULL as an indication to exit early even
-        * when the return value is 0, so make sure it is non-NULL here.
+        * Are we already being traced?
+        */
+       if (current->ptrace & PT_PTRACED)
+               return -EPERM;
+       ret = security_ptrace(current->parent, current);
+       if (ret)
+               return -EPERM;
+       /*
+        * Set the ptrace bit in the process ptrace flags.
         */
-       *childp = NULL;
+       current->ptrace |= PT_PTRACED;
+       return 0;
+}
 
-       if (request == PTRACE_TRACEME) {
-               /*
-                * Are we already being traced?
-                */
-               if (current->ptrace & PT_PTRACED)
-                       return -EPERM;
-               ret = security_ptrace(current->parent, current);
-               if (ret)
-                       return -EPERM;
-               /*
-                * Set the ptrace bit in the process ptrace flags.
-                */
-               current->ptrace |= PT_PTRACED;
-               return 0;
-       }
+/**
+ * ptrace_get_task_struct  --  grab a task struct reference for ptrace
+ * @pid:       process id to grab a task_struct reference of
+ *
+ * This function is a helper for ptrace implementations.  It checks
+ * permissions and then grabs a task struct for use of the actual
+ * ptrace implementation.
+ *
+ * Returns the task_struct for @pid or an ERR_PTR() on failure.
+ */
+struct task_struct *ptrace_get_task_struct(pid_t pid)
+{
+       struct task_struct *child;
 
        /*
-        * You may not mess with init
+        * Tracing init is not allowed.
         */
        if (pid == 1)
-               return -EPERM;
+               return ERR_PTR(-EPERM);
 
-       ret = -ESRCH;
        read_lock(&tasklist_lock);
        child = find_task_by_pid(pid);
        if (child)
                get_task_struct(child);
        read_unlock(&tasklist_lock);
        if (!child)
-               return -ESRCH;
-
-       *childp = child;
-       return 0;
+               return ERR_PTR(-ESRCH);
+       return child;
 }
 
+#ifndef __ARCH_SYS_PTRACE
 asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
 {
        struct task_struct *child;
@@ -465,9 +473,16 @@ asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
         * This lock_kernel fixes a subtle race with suid exec
         */
        lock_kernel();
-       ret = ptrace_get_task_struct(request, pid, &child);
-       if (!child)
+       if (request == PTRACE_TRACEME) {
+               ret = ptrace_traceme();
                goto out;
+       }
+
+       child = ptrace_get_task_struct(pid);
+       if (IS_ERR(child)) {
+               ret = PTR_ERR(child);
+               goto out;
+       }
 
        if (request == PTRACE_ATTACH) {
                ret = ptrace_attach(child);