~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86_64/kernel/ptrace.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.12 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.55 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.136 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.191 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* ptrace.c */
  2 /* By Ross Biro 1/23/92 */
  3 /*
  4  * Pentium III FXSR, SSE support
  5  *      Gareth Hughes <gareth@valinux.com>, May 2000
  6  * 
  7  * x86-64 port 2000-2002 Andi Kleen
  8  */
  9 
 10 #include <linux/kernel.h>
 11 #include <linux/sched.h>
 12 #include <linux/mm.h>
 13 #include <linux/smp.h>
 14 #include <linux/smp_lock.h>
 15 #include <linux/errno.h>
 16 #include <linux/ptrace.h>
 17 #include <linux/user.h>
 18 #include <linux/security.h>
 19 
 20 #include <asm/uaccess.h>
 21 #include <asm/pgtable.h>
 22 #include <asm/system.h>
 23 #include <asm/processor.h>
 24 #include <asm/i387.h>
 25 #include <asm/debugreg.h>
 26 #include <asm/ldt.h>
 27 #include <asm/desc.h>
 28 #include <asm/proto.h>
 29 #include <asm/ia32.h>
 30 
 31 /*
 32  * does not yet catch signals sent when the child dies.
 33  * in exit.c or in signal.c.
 34  */
 35 
 36 /* determines which flags the user has access to. */
 37 /* 1 = access 0 = no access */
 38 #define FLAG_MASK 0x44dd5UL
 39 
 40 /* set's the trap flag. */
 41 #define TRAP_FLAG 0x100UL
 42 
 43 /*
 44  * eflags and offset of eflags on child stack..
 45  */
 46 #define EFLAGS offsetof(struct pt_regs, eflags)
 47 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
 48 
 49 /*
 50  * this routine will get a word off of the processes privileged stack. 
 51  * the offset is how far from the base addr as stored in the TSS.  
 52  * this routine assumes that all the privileged stacks are in our
 53  * data space.
 54  */   
 55 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
 56 {
 57         unsigned char *stack;
 58 
 59         stack = (unsigned char *)task->thread.rsp0;
 60         stack += offset;
 61         return (*((unsigned long *)stack));
 62 }
 63 
 64 /*
 65  * this routine will put a word on the processes privileged stack. 
 66  * the offset is how far from the base addr as stored in the TSS.  
 67  * this routine assumes that all the privileged stacks are in our
 68  * data space.
 69  */
 70 static inline long put_stack_long(struct task_struct *task, int offset,
 71         unsigned long data)
 72 {
 73         unsigned char * stack;
 74 
 75         stack = (unsigned char *) task->thread.rsp0;
 76         stack += offset;
 77         *(unsigned long *) stack = data;
 78         return 0;
 79 }
 80 
 81 /*
 82  * Called by kernel/ptrace.c when detaching..
 83  *
 84  * Make sure the single step bit is not set.
 85  */
 86 void ptrace_disable(struct task_struct *child)
 87 { 
 88         long tmp;
 89 
 90         tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
 91         put_stack_long(child, EFL_OFFSET, tmp);
 92 }
 93 
 94 static int putreg(struct task_struct *child,
 95         unsigned long regno, unsigned long value)
 96 {
 97         unsigned long tmp; 
 98         
 99         /* Some code in the 64bit emulation may not be 64bit clean.
100            Don't take any chances. */
101         if (test_tsk_thread_flag(child, TIF_IA32))
102                 value &= 0xffffffff;
103         switch (regno) {
104                 case offsetof(struct user_regs_struct,fs):
105                         if (value && (value & 3) != 3)
106                                 return -EIO;
107                         child->thread.fsindex = value & 0xffff; 
108                         return 0;
109                 case offsetof(struct user_regs_struct,gs):
110                         if (value && (value & 3) != 3)
111                                 return -EIO;
112                         child->thread.gsindex = value & 0xffff;
113                         return 0;
114                 case offsetof(struct user_regs_struct,ds):
115                         if (value && (value & 3) != 3)
116                                 return -EIO;
117                         child->thread.ds = value & 0xffff;
118                         return 0;
119                 case offsetof(struct user_regs_struct,es): 
120                         if (value && (value & 3) != 3)
121                                 return -EIO;
122                         child->thread.es = value & 0xffff;
123                         return 0;
124                 case offsetof(struct user_regs_struct,ss):
125                         if ((value & 3) != 3)
126                                 return -EIO;
127                         value &= 0xffff;
128                         return 0;
129                 case offsetof(struct user_regs_struct,fs_base):
130                         if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
131                                 return -EIO; 
132                         child->thread.fs = value;
133                         return 0;
134                 case offsetof(struct user_regs_struct,gs_base):
135                         if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
136                                 return -EIO; 
137                         child->thread.gs = value;
138                         return 0;
139                 case offsetof(struct user_regs_struct, eflags):
140                         value &= FLAG_MASK;
141                         tmp = get_stack_long(child, EFL_OFFSET); 
142                         tmp &= ~FLAG_MASK; 
143                         value |= tmp;
144                         break;
145                 case offsetof(struct user_regs_struct,cs): 
146                         if ((value & 3) != 3)
147                                 return -EIO;
148                         value &= 0xffff;
149                         break;
150         }
151         put_stack_long(child, regno - sizeof(struct pt_regs), value);
152         return 0;
153 }
154 
155 static unsigned long getreg(struct task_struct *child, unsigned long regno)
156 {
157         unsigned long val;
158         switch (regno) {
159                 case offsetof(struct user_regs_struct, fs):
160                         return child->thread.fsindex;
161                 case offsetof(struct user_regs_struct, gs):
162                         return child->thread.gsindex;
163                 case offsetof(struct user_regs_struct, ds):
164                         return child->thread.ds;
165                 case offsetof(struct user_regs_struct, es):
166                         return child->thread.es; 
167                 case offsetof(struct user_regs_struct, fs_base):
168                         return child->thread.fs;
169                 case offsetof(struct user_regs_struct, gs_base):
170                         return child->thread.gs;
171                 default:
172                         regno = regno - sizeof(struct pt_regs);
173                         val = get_stack_long(child, regno);
174                         if (test_tsk_thread_flag(child, TIF_IA32))
175                                 val &= 0xffffffff;
176                         return val;
177         }
178 
179 }
180 
181 asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data)
182 {
183         struct task_struct *child;
184         long i, ret;
185         unsigned ui;
186 
187         /* This lock_kernel fixes a subtle race with suid exec */
188         lock_kernel();
189         ret = -EPERM;
190         if (request == PTRACE_TRACEME) {
191                 /* are we already being traced? */
192                 if (current->ptrace & PT_PTRACED)
193                         goto out;
194                 ret = security_ptrace(current->parent, current);
195                 if (ret)
196                         goto out;
197                 /* set the ptrace bit in the process flags. */
198                 current->ptrace |= PT_PTRACED;
199                 ret = 0;
200                 goto out;
201         }
202         ret = -ESRCH;
203         read_lock(&tasklist_lock);
204         child = find_task_by_pid(pid);
205         if (child)
206                 get_task_struct(child);
207         read_unlock(&tasklist_lock);
208         if (!child)
209                 goto out;
210 
211         ret = -EPERM;
212         if (pid == 1)           /* you may not mess with init */
213                 goto out_tsk;
214 
215         if (request == PTRACE_ATTACH) {
216                 ret = ptrace_attach(child);
217                 goto out_tsk;
218         }
219         ret = ptrace_check_attach(child, request == PTRACE_KILL); 
220         if (ret < 0) 
221                 goto out_tsk;
222 
223         switch (request) {
224         /* when I and D space are separate, these will need to be fixed. */
225         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
226         case PTRACE_PEEKDATA: {
227                 unsigned long tmp;
228                 int copied;
229 
230                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
231                 ret = -EIO;
232                 if (copied != sizeof(tmp))
233                         break;
234                 ret = put_user(tmp,(unsigned long *) data);
235                 break;
236         }
237 
238         /* read the word at location addr in the USER area. */
239         case PTRACE_PEEKUSR: {
240                 unsigned long tmp;
241 
242                 ret = -EIO;
243                 if ((addr & 7) ||
244                     addr > sizeof(struct user) - 7)
245                         break;
246 
247                 switch (addr) { 
248                 case 0 ... sizeof(struct user_regs_struct):
249                         tmp = getreg(child, addr);
250                         break;
251                 case offsetof(struct user, u_debugreg[0]):
252                         tmp = child->thread.debugreg0;
253                         break;
254                 case offsetof(struct user, u_debugreg[1]):
255                         tmp = child->thread.debugreg1;
256                         break;
257                 case offsetof(struct user, u_debugreg[2]):
258                         tmp = child->thread.debugreg2;
259                         break;
260                 case offsetof(struct user, u_debugreg[3]):
261                         tmp = child->thread.debugreg3;
262                         break;
263                 case offsetof(struct user, u_debugreg[6]):
264                         tmp = child->thread.debugreg6;
265                         break;
266                 case offsetof(struct user, u_debugreg[7]):
267                         tmp = child->thread.debugreg7;
268                         break;
269                 default:
270                         tmp = 0;
271                         break;
272                 }
273                 ret = put_user(tmp,(unsigned long *) data);
274                 break;
275         }
276 
277         /* when I and D space are separate, this will have to be fixed. */
278         case PTRACE_POKETEXT: /* write the word at location addr. */
279         case PTRACE_POKEDATA:
280                 ret = 0;
281                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
282                         break;
283                 ret = -EIO;
284                 break;
285 
286         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
287                 ret = -EIO;
288                 if ((addr & 7) ||
289                     addr > sizeof(struct user) - 7)
290                         break;
291 
292                 switch (addr) { 
293                 case 0 ... sizeof(struct user_regs_struct): 
294                         ret = putreg(child, addr, data);
295                         break;
296                 /* Disallows to set a breakpoint into the vsyscall */
297                 case offsetof(struct user, u_debugreg[0]):
298                         if (data >= TASK_SIZE-7) break;
299                         child->thread.debugreg0 = data;
300                         ret = 0;
301                         break;
302                 case offsetof(struct user, u_debugreg[1]):
303                         if (data >= TASK_SIZE-7) break;
304                         child->thread.debugreg1 = data;
305                         ret = 0;
306                         break;
307                 case offsetof(struct user, u_debugreg[2]):
308                         if (data >= TASK_SIZE-7) break;
309                         child->thread.debugreg2 = data;
310                         ret = 0;
311                         break;
312                 case offsetof(struct user, u_debugreg[3]):
313                         if (data >= TASK_SIZE-7) break;
314                         child->thread.debugreg3 = data;
315                         ret = 0;
316                         break;
317                 case offsetof(struct user, u_debugreg[6]):
318                                   if (data >> 32)
319                                 break; 
320                         child->thread.debugreg6 = data;
321                         ret = 0;
322                         break;
323                 case offsetof(struct user, u_debugreg[7]):
324                                   data &= ~DR_CONTROL_RESERVED;
325                                   for(i=0; i<4; i++)
326                                           if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
327                                         break;
328                         if (i == 4) {
329                                 child->thread.debugreg7 = data;
330                           ret = 0;
331                   }
332                   break;
333                 }
334                 break;
335         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
336         case PTRACE_CONT: { /* restart after signal. */
337                 long tmp;
338 
339                 ret = -EIO;
340                 if ((unsigned long) data > _NSIG)
341                         break;
342                 if (request == PTRACE_SYSCALL)
343                         set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
344                 else
345                         clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
346                 child->exit_code = data;
347         /* make sure the single step bit is not set. */
348                 tmp = get_stack_long(child, EFL_OFFSET);
349                 tmp &= ~TRAP_FLAG;
350                 put_stack_long(child, EFL_OFFSET,tmp);
351                 wake_up_process(child);
352                 ret = 0;
353                 break;
354         }
355 
356 #ifdef CONFIG_IA32_EMULATION
357                 /* This makes only sense with 32bit programs. Allow a
358                    64bit debugger to fully examine them too. Better
359                    don't use it against 64bit processes, use
360                    PTRACE_ARCH_PRCTL instead. */
361         case PTRACE_SET_THREAD_AREA: {
362                 int old; 
363                 get_user(old,  &((struct user_desc *)data)->entry_number); 
364                 put_user(addr, &((struct user_desc *)data)->entry_number);
365                 ret = do_set_thread_area(&child->thread, 
366                                          (struct user_desc *)data);
367                 put_user(old,  &((struct user_desc *)data)->entry_number); 
368                 break;
369         case PTRACE_GET_THREAD_AREA:
370                 get_user(old,  &((struct user_desc *)data)->entry_number); 
371                 put_user(addr, &((struct user_desc *)data)->entry_number);
372                 ret = do_get_thread_area(&child->thread, 
373                                          (struct user_desc *)data);
374                 put_user(old,  &((struct user_desc *)data)->entry_number); 
375                 break;
376         } 
377 #endif
378                 /* normal 64bit interface to access TLS data. 
379                    Works just like arch_prctl, except that the arguments
380                    are reversed. */
381         case PTRACE_ARCH_PRCTL: 
382                 ret = do_arch_prctl(child, data, addr);
383                 break;
384 
385 /*
386  * make the child exit.  Best I can do is send it a sigkill. 
387  * perhaps it should be put in the status that it wants to 
388  * exit.
389  */
390         case PTRACE_KILL: {
391                 long tmp;
392 
393                 ret = 0;
394                 if (child->state == TASK_ZOMBIE)        /* already dead */
395                         break;
396                 child->exit_code = SIGKILL;
397                 /* make sure the single step bit is not set. */
398                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
399                 put_stack_long(child, EFL_OFFSET, tmp);
400                 wake_up_process(child);
401                 break;
402         }
403 
404         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
405                 long tmp;
406 
407                 ret = -EIO;
408                 if ((unsigned long) data > _NSIG)
409                         break;
410                 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
411                 if ((child->ptrace & PT_DTRACE) == 0) {
412                         /* Spurious delayed TF traps may occur */
413                         child->ptrace |= PT_DTRACE;
414                 }
415                 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
416                 put_stack_long(child, EFL_OFFSET, tmp);
417                 child->exit_code = data;
418                 /* give it a chance to run. */
419                 wake_up_process(child);
420                 ret = 0;
421                 break;
422         }
423 
424         case PTRACE_DETACH:
425                 /* detach a process that was attached. */
426                 ret = ptrace_detach(child, data);
427                 break;
428 
429         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
430                 if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE)) {
431                         ret = -EIO;
432                         break;
433                 }
434                 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
435                         __put_user(getreg(child, ui),(unsigned long *) data);
436                         data += sizeof(long);
437                 }
438                 ret = 0;
439                 break;
440         }
441 
442         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
443                 unsigned long tmp;
444                 if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE)) {
445                         ret = -EIO;
446                         break;
447                 }
448                 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
449                         __get_user(tmp, (unsigned long *) data);
450                         putreg(child, ui, tmp);
451                         data += sizeof(long);
452                 }
453                 ret = 0;
454                 break;
455         }
456 
457         case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
458                 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
459                                sizeof(struct user_i387_struct))) {
460                         ret = -EIO;
461                         break;
462                 }
463                 ret = get_fpregs((struct user_i387_struct *)data, child);
464                 break;
465         }
466 
467         case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
468                 if (!access_ok(VERIFY_READ, (unsigned *)data,
469                                sizeof(struct user_i387_struct))) {
470                         ret = -EIO;
471                         break;
472                 }
473                 child->used_math = 1;
474                 ret = set_fpregs(child, (struct user_i387_struct *)data);
475                 break;
476         }
477 
478         default:
479                 ret = ptrace_request(child, request, addr, data);
480                 break;
481         }
482 out_tsk:
483         put_task_struct(child);
484 out:
485         unlock_kernel();
486         return ret;
487 }
488 
489 asmlinkage void syscall_trace(struct pt_regs *regs)
490 {
491 
492 #if 0
493         printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
494                current->comm,
495                regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
496                current_thread_info()->flags, current->ptrace); 
497 #endif
498 
499         if (!test_thread_flag(TIF_SYSCALL_TRACE))
500                 return; 
501         if (!(current->ptrace & PT_PTRACED))
502                 return;
503         
504         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
505                                 ? 0x80 : 0));
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                 send_sig(current->exit_code, current, 1);
513                 current->exit_code = 0;
514         }
515 }
516 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp