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

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