root/kernel/ptrace.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_task
  2. get_stack_long
  3. put_stack_long
  4. get_long
  5. put_long
  6. find_vma
  7. read_long
  8. write_long
  9. sys_ptrace
  10. syscall_trace

   1 /* ptrace.c */
   2 /* By Ross Biro 1/23/92 */
   3 /* edited by Linus Torvalds */
   4 
   5 #include <linux/head.h>
   6 #include <linux/kernel.h>
   7 #include <linux/sched.h>
   8 #include <linux/mm.h>
   9 #include <linux/errno.h>
  10 #include <linux/ptrace.h>
  11 #include <linux/user.h>
  12 
  13 #include <asm/segment.h>
  14 #include <asm/system.h>
  15 #include <linux/debugreg.h>
  16 
  17 /*
  18  * does not yet catch signals sent when the child dies.
  19  * in exit.c or in signal.c.
  20  */
  21 
  22 /* determines which flags the user has access to. */
  23 /* 1 = access 0 = no access */
  24 #define FLAG_MASK 0x00044dd5
  25 
  26 /* set's the trap flag. */
  27 #define TRAP_FLAG 0x100
  28 
  29 /*
  30  * this is the number to subtract from the top of the stack. To find
  31  * the local frame.
  32  */
  33 #define MAGICNUMBER 68
  34 
  35 /* change a pid into a task struct. */
  36 static inline struct task_struct * get_task(int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38         int i;
  39 
  40         for (i = 1; i < NR_TASKS; i++) {
  41                 if (task[i] != NULL && (task[i]->pid == pid))
  42                         return task[i];
  43         }
  44         return NULL;
  45 }
  46 
  47 /*
  48  * this routine will get a word off of the processes priviledged stack. 
  49  * the offset is how far from the base addr as stored in the TSS.  
  50  * this routine assumes that all the priviledged stacks are in our
  51  * data space.
  52  */   
  53 static inline int get_stack_long(struct task_struct *task, int offset)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55         unsigned char *stack;
  56 
  57         stack = (unsigned char *)task->tss.esp0;
  58         stack += offset;
  59         return (*((int *)stack));
  60 }
  61 
  62 /*
  63  * this routine will put a word on the processes priviledged stack. 
  64  * the offset is how far from the base addr as stored in the TSS.  
  65  * this routine assumes that all the priviledged stacks are in our
  66  * data space.
  67  */
  68 static inline int put_stack_long(struct task_struct *task, int offset,
     /* [previous][next][first][last][top][bottom][index][help] */
  69         unsigned long data)
  70 {
  71         unsigned char * stack;
  72 
  73         stack = (unsigned char *) task->tss.esp0;
  74         stack += offset;
  75         *(unsigned long *) stack = data;
  76         return 0;
  77 }
  78 
  79 /*
  80  * This routine gets a long from any process space by following the page
  81  * tables. NOTE! You should check that the long isn't on a page boundary,
  82  * and that it is in the task area before calling this: this routine does
  83  * no checking.
  84  */
  85 static unsigned long get_long(struct vm_area_struct * vma, unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         unsigned long page;
  88 
  89 repeat:
  90         page = *PAGE_DIR_OFFSET(vma->vm_task->tss.cr3, addr);
  91         if (page & PAGE_PRESENT) {
  92                 page &= PAGE_MASK;
  93                 page += PAGE_PTR(addr);
  94                 page = *((unsigned long *) page);
  95         }
  96         if (!(page & PAGE_PRESENT)) {
  97                 do_no_page(vma, addr, 0);
  98                 goto repeat;
  99         }
 100 /* this is a hack for non-kernel-mapped video buffers and similar */
 101         if (page >= high_memory)
 102                 return 0;
 103         page &= PAGE_MASK;
 104         page += addr & ~PAGE_MASK;
 105         return *(unsigned long *) page;
 106 }
 107 
 108 /*
 109  * This routine puts a long into any process space by following the page
 110  * tables. NOTE! You should check that the long isn't on a page boundary,
 111  * and that it is in the task area before calling this: this routine does
 112  * no checking.
 113  *
 114  * Now keeps R/W state of page so that a text page stays readonly
 115  * even if a debugger scribbles breakpoints into it.  -M.U-
 116  */
 117 static void put_long(struct vm_area_struct * vma, unsigned long addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 118         unsigned long data)
 119 {
 120         unsigned long page, pte = 0;
 121         int readonly = 0;
 122 
 123 repeat:
 124         page = *PAGE_DIR_OFFSET(vma->vm_task->tss.cr3, addr);
 125         if (page & PAGE_PRESENT) {
 126                 page &= PAGE_MASK;
 127                 page += PAGE_PTR(addr);
 128                 pte = page;
 129                 page = *((unsigned long *) page);
 130         }
 131         if (!(page & PAGE_PRESENT)) {
 132                 do_no_page(vma, addr, 0 /* PAGE_RW */);
 133                 goto repeat;
 134         }
 135         if (!(page & PAGE_RW)) {
 136                 if (!(page & PAGE_COW))
 137                         readonly = 1;
 138                 do_wp_page(vma, addr, PAGE_RW | PAGE_PRESENT);
 139                 goto repeat;
 140         }
 141 /* this is a hack for non-kernel-mapped video buffers and similar */
 142         if (page >= high_memory)
 143                 return;
 144 /* we're bypassing pagetables, so we have to set the dirty bit ourselves */
 145         *(unsigned long *) pte |= (PAGE_DIRTY|PAGE_COW);
 146         page &= PAGE_MASK;
 147         page += addr & ~PAGE_MASK;
 148         *(unsigned long *) page = data;
 149         if (readonly) {
 150                 *(unsigned long *) pte &=~ (PAGE_RW|PAGE_COW);
 151                 invalidate();
 152         } 
 153 }
 154 
 155 static struct vm_area_struct * find_vma(struct task_struct * tsk, unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157         struct vm_area_struct * vma;
 158 
 159         addr &= PAGE_MASK;
 160         for (vma = tsk->mm->mmap ; ; vma = vma->vm_next) {
 161                 if (!vma)
 162                         return NULL;
 163                 if (vma->vm_end > addr)
 164                         break;
 165         }
 166         if (vma->vm_start <= addr)
 167                 return vma;
 168         if (!(vma->vm_flags & VM_GROWSDOWN))
 169                 return NULL;
 170         if (vma->vm_end - addr > tsk->rlim[RLIMIT_STACK].rlim_cur)
 171                 return NULL;
 172         vma->vm_offset -= vma->vm_start - addr;
 173         vma->vm_start = addr;
 174         return vma;
 175 }
 176 
 177 /*
 178  * This routine checks the page boundaries, and that the offset is
 179  * within the task area. It then calls get_long() to read a long.
 180  */
 181 static int read_long(struct task_struct * tsk, unsigned long addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 182         unsigned long * result)
 183 {
 184         struct vm_area_struct * vma = find_vma(tsk, addr);
 185 
 186         if (!vma)
 187                 return -EIO;
 188         if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
 189                 unsigned long low,high;
 190                 struct vm_area_struct * vma_high = vma;
 191 
 192                 if (addr + sizeof(long) >= vma->vm_end) {
 193                         vma_high = vma->vm_next;
 194                         if (!vma_high || vma_high->vm_start != vma->vm_end)
 195                                 return -EIO;
 196                 }
 197                 low = get_long(vma, addr & ~(sizeof(long)-1));
 198                 high = get_long(vma_high, (addr+sizeof(long)) & ~(sizeof(long)-1));
 199                 switch (addr & (sizeof(long)-1)) {
 200                         case 1:
 201                                 low >>= 8;
 202                                 low |= high << 24;
 203                                 break;
 204                         case 2:
 205                                 low >>= 16;
 206                                 low |= high << 16;
 207                                 break;
 208                         case 3:
 209                                 low >>= 24;
 210                                 low |= high << 8;
 211                                 break;
 212                 }
 213                 *result = low;
 214         } else
 215                 *result = get_long(vma, addr);
 216         return 0;
 217 }
 218 
 219 /*
 220  * This routine checks the page boundaries, and that the offset is
 221  * within the task area. It then calls put_long() to write a long.
 222  */
 223 static int write_long(struct task_struct * tsk, unsigned long addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 224         unsigned long data)
 225 {
 226         struct vm_area_struct * vma = find_vma(tsk, addr);
 227 
 228         if (!vma)
 229                 return -EIO;
 230         if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
 231                 unsigned long low,high;
 232                 struct vm_area_struct * vma_high = vma;
 233 
 234                 if (addr + sizeof(long) >= vma->vm_end) {
 235                         vma_high = vma->vm_next;
 236                         if (!vma_high || vma_high->vm_start != vma->vm_end)
 237                                 return -EIO;
 238                 }
 239                 low = get_long(vma, addr & ~(sizeof(long)-1));
 240                 high = get_long(vma_high, (addr+sizeof(long)) & ~(sizeof(long)-1));
 241                 switch (addr & (sizeof(long)-1)) {
 242                         case 0: /* shouldn't happen, but safety first */
 243                                 low = data;
 244                                 break;
 245                         case 1:
 246                                 low &= 0x000000ff;
 247                                 low |= data << 8;
 248                                 high &= ~0xff;
 249                                 high |= data >> 24;
 250                                 break;
 251                         case 2:
 252                                 low &= 0x0000ffff;
 253                                 low |= data << 16;
 254                                 high &= ~0xffff;
 255                                 high |= data >> 16;
 256                                 break;
 257                         case 3:
 258                                 low &= 0x00ffffff;
 259                                 low |= data << 24;
 260                                 high &= ~0xffffff;
 261                                 high |= data >> 8;
 262                                 break;
 263                 }
 264                 put_long(vma, addr & ~(sizeof(long)-1),low);
 265                 put_long(vma_high, (addr+sizeof(long)) & ~(sizeof(long)-1),high);
 266         } else
 267                 put_long(vma, addr, data);
 268         return 0;
 269 }
 270 
 271 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
     /* [previous][next][first][last][top][bottom][index][help] */
 272 {
 273         struct task_struct *child;
 274         struct user * dummy;
 275         int i;
 276 
 277         dummy = NULL;
 278 
 279         if (request == PTRACE_TRACEME) {
 280                 /* are we already being traced? */
 281                 if (current->flags & PF_PTRACED)
 282                         return -EPERM;
 283                 /* set the ptrace bit in the proccess flags. */
 284                 current->flags |= PF_PTRACED;
 285                 return 0;
 286         }
 287         if (pid == 1)           /* you may not mess with init */
 288                 return -EPERM;
 289         if (!(child = get_task(pid)))
 290                 return -ESRCH;
 291         if (request == PTRACE_ATTACH) {
 292                 if (child == current)
 293                         return -EPERM;
 294                 if ((!child->dumpable || (current->uid != child->euid) ||
 295                     (current->gid != child->egid)) && !suser())
 296                         return -EPERM;
 297                 /* the same process cannot be attached many times */
 298                 if (child->flags & PF_PTRACED)
 299                         return -EPERM;
 300                 child->flags |= PF_PTRACED;
 301                 if (child->p_pptr != current) {
 302                         REMOVE_LINKS(child);
 303                         child->p_pptr = current;
 304                         SET_LINKS(child);
 305                 }
 306                 send_sig(SIGSTOP, child, 1);
 307                 return 0;
 308         }
 309         if (!(child->flags & PF_PTRACED))
 310                 return -ESRCH;
 311         if (child->state != TASK_STOPPED) {
 312                 if (request != PTRACE_KILL)
 313                         return -ESRCH;
 314         }
 315         if (child->p_pptr != current)
 316                 return -ESRCH;
 317 
 318         switch (request) {
 319         /* when I and D space are seperate, these will need to be fixed. */
 320                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
 321                 case PTRACE_PEEKDATA: {
 322                         unsigned long tmp;
 323                         int res;
 324 
 325                         res = read_long(child, addr, &tmp);
 326                         if (res < 0)
 327                                 return res;
 328                         res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
 329                         if (!res)
 330                                 put_fs_long(tmp,(unsigned long *) data);
 331                         return res;
 332                 }
 333 
 334         /* read the word at location addr in the USER area. */
 335                 case PTRACE_PEEKUSR: {
 336                         unsigned long tmp;
 337                         int res;
 338 
 339                         if ((addr & 3) || addr < 0 || 
 340                             addr > sizeof(struct user) - 3)
 341                                 return -EIO;
 342 
 343                         res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
 344                         if (res)
 345                                 return res;
 346                         tmp = 0;  /* Default return condition */
 347                         if(addr < 17*sizeof(long)) {
 348                           addr = addr >> 2; /* temporary hack. */
 349 
 350                           tmp = get_stack_long(child, sizeof(long)*addr - MAGICNUMBER);
 351                           if (addr == DS || addr == ES ||
 352                               addr == FS || addr == GS ||
 353                               addr == CS || addr == SS)
 354                             tmp &= 0xffff;
 355                         };
 356                         if(addr >= (long) &dummy->u_debugreg[0] &&
 357                            addr <= (long) &dummy->u_debugreg[7]){
 358                                 addr -= (long) &dummy->u_debugreg[0];
 359                                 addr = addr >> 2;
 360                                 tmp = child->debugreg[addr];
 361                         };
 362                         put_fs_long(tmp,(unsigned long *) data);
 363                         return 0;
 364                 }
 365 
 366       /* when I and D space are seperate, this will have to be fixed. */
 367                 case PTRACE_POKETEXT: /* write the word at location addr. */
 368                 case PTRACE_POKEDATA:
 369                         return write_long(child,addr,data);
 370 
 371                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
 372                         if ((addr & 3) || addr < 0 || 
 373                             addr > sizeof(struct user) - 3)
 374                                 return -EIO;
 375 
 376                         addr = addr >> 2; /* temproary hack. */
 377 
 378                         if (addr == ORIG_EAX)
 379                                 return -EIO;
 380                         if (addr == DS || addr == ES ||
 381                             addr == FS || addr == GS ||
 382                             addr == CS || addr == SS) {
 383                                 data &= 0xffff;
 384                                 if (data && (data & 3) != 3)
 385                                         return -EIO;
 386                         }
 387                         if (addr == EFL) {   /* flags. */
 388                                 data &= FLAG_MASK;
 389                                 data |= get_stack_long(child, EFL*sizeof(long)-MAGICNUMBER)  & ~FLAG_MASK;
 390                         }
 391                   /* Do not allow the user to set the debug register for kernel
 392                      address space */
 393                   if(addr < 17){
 394                           if (put_stack_long(child, sizeof(long)*addr-MAGICNUMBER, data))
 395                                 return -EIO;
 396                         return 0;
 397                         };
 398 
 399                   /* We need to be very careful here.  We implicitly
 400                      want to modify a portion of the task_struct, and we
 401                      have to be selective about what portions we allow someone
 402                      to modify. */
 403 
 404                   addr = addr << 2;  /* Convert back again */
 405                   if(addr >= (long) &dummy->u_debugreg[0] &&
 406                      addr <= (long) &dummy->u_debugreg[7]){
 407 
 408                           if(addr == (long) &dummy->u_debugreg[4]) return -EIO;
 409                           if(addr == (long) &dummy->u_debugreg[5]) return -EIO;
 410                           if(addr < (long) &dummy->u_debugreg[4] &&
 411                              ((unsigned long) data) >= 0xbffffffd) return -EIO;
 412                           
 413                           if(addr == (long) &dummy->u_debugreg[7]) {
 414                                   data &= ~DR_CONTROL_RESERVED;
 415                                   for(i=0; i<4; i++)
 416                                           if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
 417                                                   return -EIO;
 418                           };
 419 
 420                           addr -= (long) &dummy->u_debugreg;
 421                           addr = addr >> 2;
 422                           child->debugreg[addr] = data;
 423                           return 0;
 424                   };
 425                   return -EIO;
 426 
 427                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
 428                 case PTRACE_CONT: { /* restart after signal. */
 429                         long tmp;
 430 
 431                         if ((unsigned long) data > NSIG)
 432                                 return -EIO;
 433                         if (request == PTRACE_SYSCALL)
 434                                 child->flags |= PF_TRACESYS;
 435                         else
 436                                 child->flags &= ~PF_TRACESYS;
 437                         child->exit_code = data;
 438                         child->state = TASK_RUNNING;
 439         /* make sure the single step bit is not set. */
 440                         tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) & ~TRAP_FLAG;
 441                         put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
 442                         return 0;
 443                 }
 444 
 445 /*
 446  * make the child exit.  Best I can do is send it a sigkill. 
 447  * perhaps it should be put in the status that it want's to 
 448  * exit.
 449  */
 450                 case PTRACE_KILL: {
 451                         long tmp;
 452 
 453                         child->state = TASK_RUNNING;
 454                         child->exit_code = SIGKILL;
 455         /* make sure the single step bit is not set. */
 456                         tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) & ~TRAP_FLAG;
 457                         put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
 458                         return 0;
 459                 }
 460 
 461                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
 462                         long tmp;
 463 
 464                         if ((unsigned long) data > NSIG)
 465                                 return -EIO;
 466                         child->flags &= ~PF_TRACESYS;
 467                         tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) | TRAP_FLAG;
 468                         put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
 469                         child->state = TASK_RUNNING;
 470                         child->exit_code = data;
 471         /* give it a chance to run. */
 472                         return 0;
 473                 }
 474 
 475                 case PTRACE_DETACH: { /* detach a process that was attached. */
 476                         long tmp;
 477 
 478                         if ((unsigned long) data > NSIG)
 479                                 return -EIO;
 480                         child->flags &= ~(PF_PTRACED|PF_TRACESYS);
 481                         child->state = TASK_RUNNING;
 482                         child->exit_code = data;
 483                         REMOVE_LINKS(child);
 484                         child->p_pptr = child->p_opptr;
 485                         SET_LINKS(child);
 486                         /* make sure the single step bit is not set. */
 487                         tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) & ~TRAP_FLAG;
 488                         put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
 489                         return 0;
 490                 }
 491 
 492                 default:
 493                         return -EIO;
 494         }
 495 }
 496 
 497 asmlinkage void syscall_trace(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499         if ((current->flags & (PF_PTRACED|PF_TRACESYS))
 500                         != (PF_PTRACED|PF_TRACESYS))
 501                 return;
 502         current->exit_code = SIGTRAP;
 503         current->state = TASK_STOPPED;
 504         notify_parent(current);
 505         schedule();
 506         /*
 507          * this isn't the same as continuing with a signal, but it will do
 508          * for normal use.  strace only continues with a signal if the
 509          * stopping signal is not SIGTRAP.  -brl
 510          */
 511         if (current->exit_code)
 512                 current->signal |= (1 << (current->exit_code - 1));
 513         current->exit_code = 0;
 514 }

/* [previous][next][first][last][top][bottom][index][help] */