i386 - sys_clone from vsyscall
In-Reply-To: <[email protected]>
Quoted 6 lines · On Wed, 11 Jan 2006 at 17:53:10 +0000, erg0t wrote:
This patch fixes a little problem when
sys_cloneis called from a vsyscall and a new stack for the child is specifed.
Some regs, and return address are pushed in the stack before sysenter, so when the syscall returns, pop the regs and return, if we made a new stack for the thread we need to copy that to the new stack or the thread will segfault.
glibc works around this bug by hardcoding "int 0x80" at glibc-2.3.5/sysdeps/unix/sysv/linux/i386/clone.S line 99.
Quoted 2 lines
Sorry for use the
0xffffe410constant I didn't find another way to reference it.
It's SYSENTER_RETURN.
Quoted 1 line
Signed-off-by Daniel F <[email protected]>
Your patch almost works but it copies the stack into the parent's address space. Using access_process_vm() fixes it. However, that still leaves unfixed the case where vsyscall-int80 is used.
[patch] i386: fix sys_clone when using vsyscall-sysenter
Fix a problem when sys_clone is called from sysenter vsyscall and a new stack for the child is specified. Some data needs to be copied from the parent to the child stack or the child will segfault.
Bug report and initial patch from Daniel F <[email protected]>
arch/i386/kernel/process.c +16 -0
@@ -429,12 +429,15 @@ void prepare_to_copy(struct task_structunlazy_fpu (tsk); } +void SYSENTER_RETURN (void ); +int copy_thread (int nr,unsigned long clone_flags,unsigned long esp,unsigned long unused,struct task_struct * p,struct pt_regs * regs) {struct pt_regs * childregs;struct task_struct *tsk; +unsigned long vsyscall_stack[4 ];int err; childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long ) p->thread_info)) -1 ;
@@ -451,6 +454,19 @@ int copy_thread(int nr, unsigned long cl childregs = (struct pt_regs *) ((unsigned long ) childregs -8 ); *childregs = *regs; childregs->eax =0 ; +/* +* When we were called from a vsyscall some things are pushed on the stack; +* we need to copy the stuff to the new stack. +*/ +if (regs->esp != esp && (void *)regs->eip ==SYSENTER_RETURN ) { +int size =sizeof (vsyscall_stack); + +if (copy_from_user (vsyscall_stack, (void *)regs->esp, size)) +return -EFAULT ; +if (access_process_vm (p, esp - size, vsyscall_stack, size,1 ) != size) +return -EFAULT ; + esp -= size; + } childregs->esp = esp; p->thread.esp = (unsigned long ) childregs;
Signature · 1 line
Chuck
Thanks for your work on the patch
Quoted 3 lines
Your patch almost works but it copies the stack into the parent's address space. Using
access_process_vm()fixes it. However, that still leaves unfixed the case where vsyscall-int80 is used.
I copy the stack into the parent's address space becuase in this case the memory is shared, but access_process_vm() is more elegant :). About vsyscall-int80, I don't know how to test that case in my computer but I think a solution could be:
add a INT80H_RETURN symbol:
.LSTART_vsyscall:
int $0x80
.globl INT80H_RETURN
INT80H_RETURN:
ret
.LEND_vsyscall:
and then in process.c:
int size = 0 ; childregs->eax = 0;
if ((void *)regs->eip == SYSENTER_RETURN)
size = sizeof(vsyscall_stack) ;
if ((void *)regs->eip == INT80H_RETURN)
size = sizeof(unsigned long) ;
if (regs->esp != esp && size) {
if (copy_from_user(vsyscall_stack, (void *)regs->esp, size))
return -EFAULT;
if (access_process_vm(p, esp - size, vsyscall_stack,
size, 1) != size)
return -EFAULT;
esp -= size;
}
childregs->esp = esp;I hope somebody can test if it works that way.
Regards