ptrace_vm: let us simplify the code for ptrace and add useful features for VM
Subsystems (4)
PATCH 1/1 ptrace_vm: let us simplify the code for ptrace and add useful features for VM
This patch adds the new PTRACE_VM_SKIPCALL and PTRACE_VM_SKIPEXIT tags for ptrace's addr parameter.
In this way it is possible to (eventually) get rid of PTRACE_SYSEMU PTRACE_SYSEMU_SINGLESTEP, while providing not only the same features but a more general support for Virtual Machines.
renzo
Signed-off-by Renzo Davoli <[email protected]>
---
arch/powerpc/kernel/entry_32.S +3 -0
@@ -299,6 +299,7 @@ stw r0,_TRAP(r1) addi r3,r1,STACK_FRAME_OVERHEAD bl do_syscall_trace_enter + mr r10,r3 lwz r0,GPR0(r1) /* Restore original registers */ lwz r3,GPR3(r1) lwz r4,GPR4(r1)
@@ -307,6 +308,8 @@ lwz r7,GPR7(r1) lwz r8,GPR8(r1) REST_NVGPRS(r1) + cmpwi r10,0 + bne- ret_from_syscall b syscall_dotrace_cont syscall_exit_work:
arch/powerpc/kernel/entry_64.S +3 -0
@@ -213,6 +213,7 @@ bl .save_nvgprs addi r3,r1,STACK_FRAME_OVERHEAD bl .do_syscall_trace_enter + mr r11,r3 ld r0,GPR0(r1) /* Restore original registers */ ld r3,GPR3(r1) ld r4,GPR4(r1)
@@ -223,6 +224,8 @@ addi r9,r1,STACK_FRAME_OVERHEAD clrrdi r10,r1,THREAD_SHIFT ld r10,TI_FLAGS(r10) + cmpwi r11,0 + bne- syscall_exit b syscall_dotrace_cont syscall_enosys:
arch/powerpc/kernel/ptrace.c +6 -3
@@ -867,7 +867,7 @@ } } -void do_syscall_trace_enter (struct pt_regs *regs) +int do_syscall_trace_enter (struct pt_regs *regs) {secure_computing (regs->gpr[0 ]);
@@ -891,6 +891,7 @@ regs->gpr[5 ] &0xffffffff , regs->gpr[6] & 0xffffffff); } +return (current->ptrace &PT_SYSCALL_SKIPCALL ); }void do_syscall_trace_leave (struct pt_regs *regs)
@@ -900,7 +901,9 @@ regs->result);if ((test_thread_flag (TIF_SYSCALL_TRACE ) - ||test_thread_flag (TIF_SINGLESTEP )) - && (current->ptrace &PT_PTRACED )) + ||test_thread_flag (TIF_SINGLESTEP )) + && (current->ptrace &PT_PTRACED ) && + ((current->ptrace &PT_SYSCALL_SKIPEXIT )==0 )) +do_syscall_trace (); }
arch/ppc/kernel/entry.S +3 -0
@@ -292,6 +292,7 @@ stw r0,TRAP(r1) addi r3,r1,STACK_FRAME_OVERHEAD bl do_syscall_trace_enter + mr r10,r3 lwz r0,GPR0(r1) /* Restore original registers */ lwz r3,GPR3(r1) lwz r4,GPR4(r1)
@@ -300,6 +301,8 @@ lwz r7,GPR7(r1) lwz r8,GPR8(r1) REST_NVGPRS(r1) + cmpwi r10,0 + bne- ret_from_syscall b syscall_dotrace_cont syscall_exit_work:
arch/um/include/kern_util.h +1 -1
@@ -57,7 +57,7 @@extern unsigned long to_irq_stack (unsigned long *mask_out);extern unsigned long from_irq_stack (int nested); -extern void syscall_trace (struct uml_pt_regs *regs,int entryexit); +extern int syscall_trace (struct uml_pt_regs *regs,int entryexit);extern int singlestepping (void *t);extern void segv_handler (int sig,struct uml_pt_regs *regs);
arch/um/include/ptrace_user.h +11 -0
@@ -40,9 +40,20 @@#define PTRACE_OLDSETOPTIONS PTRACE_SETOPTIONS#endif +/* these constant should eventually enter in sys/ptrace.h */ +#ifndef PTRACE_SYSCALL_SKIPCALL +#define PTRACE_SYSCALL_SKIPCALL 0x6 +#endif +#ifndef PTRACE_SYSCALL_SKIPEXIT +#define PTRACE_SYSCALL_SKIPEXIT 0x2 +#endif +void set_using_sysemu (int value);int get_using_sysemu (void );extern int sysemu_supported; +void set_using_sysptvm (int value); +int get_using_sysptvm (void ); +extern int sysptvm_supported;#define SELECT_PTRACE_OPERATION (sysemu_mode, singlestep_mode) \ (((int[3][3] ) { \
arch/um/kernel/process.c +60 -13
100 lines not on this page · raw message
arch/um/kernel/ptrace.c +14 -3
@@ -76,6 +76,8 @@if (request ==PTRACE_SYSCALL )set_tsk_thread_flag (child,TIF_SYSCALL_TRACE );else clear_tsk_thread_flag (child,TIF_SYSCALL_TRACE ); + child->ptrace &= ~PT_SYSCALL_MASK ; + child->ptrace |= (addr &PTRACE_SYSCALL_MASK ) <<28 ; child->exit_code = data;wake_up_process (child); ret =0 ;
@@ -102,7 +104,9 @@ ret = -EIO ;if (!valid_signal (data))break ; + child->ptrace &= ~PT_SYSCALL_MASK ;clear_tsk_thread_flag (child,TIF_SYSCALL_TRACE ); + child->ptrace |= (addr &PTRACE_SYSCALL_MASK ) <<28 ;set_singlestepping (child,1 ); child->exit_code = data;/* give it a chance to run. */
@@ -245,7 +249,7 @@* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check */ -void syscall_trace (struct uml_pt_regs *regs,int entryexit) +int syscall_trace (struct uml_pt_regs *regs,int entryexit) {int is_singlestep = (current->ptrace &PT_DTRACE ) && entryexit;int tracesysgood;
@@ -267,10 +271,13 @@send_sigtrap (current, regs,0 );if (!test_thread_flag (TIF_SYSCALL_TRACE )) -return ; +return 0 ;if (!(current->ptrace &PT_PTRACED )) -return ; +return 0 ; + +if (entryexit && (current->ptrace &PT_SYSCALL_SKIPEXIT )) +return 0 ;/* * the 0x80 provides a way for the tracing parent to distinguish
@@ -291,4 +298,8 @@send_sig (current->exit_code, current,1 ); current->exit_code =0 ; } +if (!entryexit && (current->ptrace &PT_SYSCALL_SKIPCALL )) +return 1 ; +else +return 0 ; }
arch/um/kernel/skas/syscall.c +9 -6
@@ -17,8 +17,9 @@ struct pt_regs *regs = container_of(r, struct pt_regs, regs);long result;int syscall; +int skip_call; -syscall_trace (r,0 ); + skip_call=syscall_trace (r,0 );/* * This should go in the declaration of syscall, but when I do that,
@@ -29,12 +30,14 @@* gcc version 4.0.1 20050727 (Red Hat 4.0.1-5) * in case it's a compiler bug. */ - syscall =UPT_SYSCALL_NR (r); -if ((syscall >= NR_syscalls) || (syscall <0 )) - result = -ENOSYS ; -else result =EXECUTE_SYSCALL (syscall, regs); +if (skip_call ==0 ) { + syscall =UPT_SYSCALL_NR (r); +if ((syscall >= NR_syscalls) || (syscall <0 )) + result = -ENOSYS ; +else result =EXECUTE_SYSCALL (syscall, regs); -REGS_SET_SYSCALL_RETURN (r->gp, result); +REGS_SET_SYSCALL_RETURN (r->gp, result); + }syscall_trace (r,1 ); }
arch/um/os-Linux/skas/process.c +5 -3
@@ -157,7 +157,7 @@* (in local_using_sysemu */ static void handle_trap (int pid,struct uml_pt_regs *regs, -int local_using_sysemu) +int local_using_sysptvm_or_sysemu) {int err, status;
@@ -167,7 +167,7 @@/* Mark this as a syscall */ UPT_SYSCALL_NR (regs) =PT_SYSCALL_NR (regs->gp); -if (!local_using_sysemu) +if (!local_using_sysptvm_or_sysemu) { err =ptrace (PTRACE_POKEUSR , pid,PT_SYSCALL_NR_OFFSET , __NR_getpid);
@@ -354,6 +354,7 @@int err, status, op, pid = userspace_pid[0 ];/* To prevent races if using_sysemu changes under us.*/ int local_using_sysemu; +int local_using_sysptvm;if (getitimer (ITIMER_VIRTUAL , &timer))printk (UM_KERN_ERR "Failed to get itimer, errno = %d\n" , errno);
@@ -375,11 +376,12 @@/* Now we set local_using_sysemu to be used for one loop */ local_using_sysemu =get_using_sysemu (); + local_using_sysptvm =get_using_sysptvm (); op =SELECT_PTRACE_OPERATION (local_using_sysemu,singlestepping (NULL )); -if (ptrace (op, pid,0 ,0 )) { +if (ptrace (op, pid, local_using_sysptvm,0 )) {printk (UM_KERN_ERR "userspace - ptrace continue " "failed, op = %d, errno = %d\n" , op, errno);fatal_sigsegv ();
arch/um/os-Linux/start_up.c +125 -1
144 lines not on this page · raw message
arch/x86/ia32/ia32entry.S +11 -0
@@ -165,6 +165,8 @@ movq $-ENOSYS,RAX(%rsp)/* ptrace can change this for a bad syscall */ movq %rsp,%rdi /* &pt_regs -> arg1 */ call syscall_trace_enter + testq %rax,%rax + jnz ia32_skipcall LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ RESTORE_REST xchgl %ebp,%r9d
@@ -266,6 +268,8 @@ movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */ movq %rsp,%rdi /* &pt_regs -> arg1 */ call syscall_trace_enter + testq %rax,%rax + jnz ia32_skipcall LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ RESTORE_REST xchgl %ebp,%r9d
@@ -342,6 +346,8 @@ movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */ movq %rsp,%rdi /* &pt_regs -> arg1 */ call syscall_trace_enter + testq %rax,%rax + jnz ia32_skipcall LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ RESTORE_REST jmp ia32_do_syscall
@@ -352,6 +358,11 @@ movq $-ENOSYS,RAX-ARGOFFSET(%rsp) jmp int_ret_from_sys_call +ia32_skipcall: + LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ + RESTORE_REST + jmp int_ret_from_sys_call + quiet_ni_syscall: movq $-ENOSYS,%rax ret
arch/x86/kernel/entry_64.S +7 -0
@@ -316,6 +316,11 @@ movq $-ENOSYS,RAX-ARGOFFSET(%rsp) jmp ret_from_sys_call +skipcall: + LOAD_ARGS ARGOFFSET /* reload args from stack in case ptrace changed it */ + RESTORE_REST + jmp ret_from_sys_call + /* Do syscall tracing */ tracesys: SAVE_REST
@@ -323,6 +328,8 @@ FIXUP_TOP_OF_STACK %rdi movq %rsp,%rdi call syscall_trace_enter + testq %rax,%rax + jnz skipcall LOAD_ARGS ARGOFFSET /* reload args from stack in case ptrace changed it */ RESTORE_REST cmpq $__NR_syscall_max,%rax
arch/x86/kernel/ptrace.c +10 -3
@@ -1415,6 +1415,9 @@if (!(current->ptrace &PT_PTRACED )) goto out; +if (entryexit && (current->ptrace &PT_SYSCALL_SKIPEXIT )) +return 0 ; +/* If a process stops on the 1st tracepoint with SYSCALL_TRACE * and then is resumed with SYSEMU_SINGLESTEP, it will come in * here. We have to check this and return */
@@ -1442,7 +1445,8 @@send_sig (current->exit_code, current,1 ); current->exit_code =0 ; } - ret = is_sysemu; + ret = (is_sysemu || (!entryexit && (current->ptrace &PT_SYSCALL_SKIPCALL ))); + out:if (unlikely (current->audit_context) && !entryexit) audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
@@ -1481,7 +1485,7 @@ } } -asmlinkage void syscall_trace_enter(struct pt_regs *regs) +asmlinkage long syscall_trace_enter (struct pt_regs *regs) {/* do the secure computing check first */ secure_computing (regs->orig_ax);
@@ -1503,6 +1507,8 @@ regs->dx, regs->r10); } } + +return (current->ptrace &PT_SYSCALL_SKIPCALL ); } asmlinkage void syscall_trace_leave(struct pt_regs *regs)
@@ -1512,7 +1518,8 @@if ((test_thread_flag (TIF_SYSCALL_TRACE ) ||test_thread_flag (TIF_SINGLESTEP )) - && (current->ptrace &PT_PTRACED )) + && (current->ptrace &PT_PTRACED ) && + ((current->ptrace &PT_SYSCALL_SKIPEXIT )==0 ))syscall_trace (regs); }
include/linux/ptrace.h +9 -0
@@ -46,6 +46,11 @@#define PTRACE_EVENT_VFORK_DONE 5#define PTRACE_EVENT_EXIT 6 +/* options for new PTRACE_SYSCALL syntax*/ +#define PTRACE_SYSCALL_SKIPEXIT 0x2 +#define PTRACE_SYSCALL_SKIPCALL 0x6 +#define PTRACE_SYSCALL_MASK 0x00000006 +#include <asm/ptrace.h> #ifdef __KERNEL__
@@ -68,6 +73,10 @@#define PT_TRACE_VFORK_DONE 0x00000100#define PT_TRACE_EXIT 0x00000200 +#define PT_SYSCALL_SKIPEXIT 0x60000000 +#define PT_SYSCALL_SKIPCALL 0x40000000 +#define PT_SYSCALL_MASK 0x60000000 +#define PT_TRACE_MASK 0x000003f4/* single stepping state bits (used on ARM and PA-RISC) */
kernel/ptrace.c +6 -3
@@ -375,7 +375,7 @@#define is_sysemu_singlestep (request) 0#endif -static int ptrace_resume (struct task_struct *child,long request,long data) +static int ptrace_resume (struct task_struct *child,long request,long addr,long data) {if (!valid_signal (data))return -EIO ;
@@ -404,6 +404,9 @@else user_disable_single_step (child); + child->ptrace &= ~PT_SYSCALL_MASK ; + child->ptrace |= (addr &PTRACE_SYSCALL_MASK ) <<28 ; + child->exit_code = data;wake_up_process (child);
@@ -465,12 +468,12 @@#endif case PTRACE_SYSCALL :case PTRACE_CONT : -return ptrace_resume (child, request, data); +return ptrace_resume (child, request, addr, data);case PTRACE_KILL :if (child->exit_state)/* already dead */ return 0 ; -return ptrace_resume (child, request,SIGKILL ); +return ptrace_resume (child, request, addr,SIGKILL );default :break ;