linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / arch / x86_64 / kernel / ptrace.c
index fb883d3..5320562 100644 (file)
  * in exit.c or in signal.c.
  */
 
-/* determines which flags the user has access to. */
-/* 1 = access 0 = no access */
-#define FLAG_MASK 0x44dd5UL
+/*
+ * Determines which flags the user has access to [1 = access, 0 = no access].
+ * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
+ * Also masks reserved bits (63-22, 15, 5, 3, 1).
+ */
+#define FLAG_MASK 0x54dd5UL
 
 /* set's the trap flag. */
 #define TRAP_FLAG 0x100UL
@@ -64,12 +67,6 @@ static inline unsigned long get_stack_long(struct task_struct *task, int offset)
        return (*((unsigned long *)stack));
 }
 
-static inline struct pt_regs *get_child_regs(struct task_struct *task)
-{
-       struct pt_regs *regs = (void *)task->thread.rsp0;
-       return regs - 1;
-}
-
 /*
  * this routine will put a word on the processes privileged stack. 
  * the offset is how far from the base addr as stored in the TSS.  
@@ -167,7 +164,7 @@ static int is_at_popf(struct task_struct *child, struct pt_regs *regs)
 
 static void set_singlestep(struct task_struct *child)
 {
-       struct pt_regs *regs = get_child_regs(child);
+       struct pt_regs *regs = task_pt_regs(child);
 
        /*
         * Always set TIF_SINGLESTEP - this guarantees that
@@ -205,7 +202,7 @@ static void clear_singlestep(struct task_struct *child)
 
        /* But touch TF only if it was set by us.. */
        if (child->ptrace & PT_DTRACE) {
-               struct pt_regs *regs = get_child_regs(child);
+               struct pt_regs *regs = task_pt_regs(child);
                regs->eflags &= ~TRAP_FLAG;
                child->ptrace &= ~PT_DTRACE;
        }
@@ -257,12 +254,12 @@ static int putreg(struct task_struct *child,
                        value &= 0xffff;
                        return 0;
                case offsetof(struct user_regs_struct,fs_base):
-                       if (value >= TASK_SIZE)
+                       if (value >= TASK_SIZE_OF(child))
                                return -EIO;
                        child->thread.fs = value;
                        return 0;
                case offsetof(struct user_regs_struct,gs_base):
-                       if (value >= TASK_SIZE)
+                       if (value >= TASK_SIZE_OF(child))
                                return -EIO;
                        child->thread.gs = value;
                        return 0;
@@ -279,7 +276,7 @@ static int putreg(struct task_struct *child,
                        break;
                case offsetof(struct user_regs_struct, rip):
                        /* Check if the new RIP address is canonical */
-                       if (value >= TASK_SIZE)
+                       if (value >= TASK_SIZE_OF(child))
                                return -EIO;
                        break;
        }
@@ -313,50 +310,11 @@ static unsigned long getreg(struct task_struct *child, unsigned long regno)
 
 }
 
-asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data)
+long arch_ptrace(struct task_struct *child, long request, long addr, long data)
 {
-       struct task_struct *child;
        long i, ret;
        unsigned ui;
 
-       /* This lock_kernel fixes a subtle race with suid exec */
-       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;
-               /* set the ptrace bit in the process flags. */
-               current->ptrace |= PT_PTRACED;
-               ret = 0;
-               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;
-       if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
-               goto out_tsk;
-
-       ret = -EPERM;
-       if (pid == 1)           /* you may not mess with init */
-               goto out_tsk;
-
-       if (request == PTRACE_ATTACH) {
-               ret = ptrace_attach(child);
-               goto out_tsk;
-       }
-       ret = ptrace_check_attach(child, request == PTRACE_KILL); 
-       if (ret < 0) 
-               goto out_tsk;
-
        switch (request) {
        /* when I and D space are separate, these will need to be fixed. */
        case PTRACE_PEEKTEXT: /* read word at location addr. */ 
@@ -421,6 +379,8 @@ asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data
                break;
 
        case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
+       {
+               int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
                ret = -EIO;
                if ((addr & 7) ||
                    addr > sizeof(struct user) - 7)
@@ -432,22 +392,22 @@ asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data
                        break;
                /* Disallows to set a breakpoint into the vsyscall */
                case offsetof(struct user, u_debugreg[0]):
-                       if (data >= TASK_SIZE-7) break;
+                       if (data >= TASK_SIZE_OF(child) - dsize) break;
                        child->thread.debugreg0 = data;
                        ret = 0;
                        break;
                case offsetof(struct user, u_debugreg[1]):
-                       if (data >= TASK_SIZE-7) break;
+                       if (data >= TASK_SIZE_OF(child) - dsize) break;
                        child->thread.debugreg1 = data;
                        ret = 0;
                        break;
                case offsetof(struct user, u_debugreg[2]):
-                       if (data >= TASK_SIZE-7) break;
+                       if (data >= TASK_SIZE_OF(child) - dsize) break;
                        child->thread.debugreg2 = data;
                        ret = 0;
                        break;
                case offsetof(struct user, u_debugreg[3]):
-                       if (data >= TASK_SIZE-7) break;
+                       if (data >= TASK_SIZE_OF(child) - dsize) break;
                        child->thread.debugreg3 = data;
                        ret = 0;
                        break;
@@ -471,6 +431,7 @@ asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data
                  break;
                }
                break;
+       }
        case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
        case PTRACE_CONT:    /* restart after signal. */
 
@@ -607,10 +568,6 @@ asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data
                ret = ptrace_request(child, request, addr, data);
                break;
        }
-out_tsk:
-       put_task_struct(child);
-out:
-       unlock_kernel();
        return ret;
 }