x86 - 32-bit ptrace emulation mishandles 6th arg

[ jdike - Pushing Chuck's patch - see
http://lkml.org/lkml/2005/9/16/261 for some history and a test
program. UML is also broken without this patch - its processes get
SIGBUS from the corrupt 6th argument to mmap being interpretted as a
file offset ]

When the 32-bit vDSO is used to make a system call, the %ebp register for
the 6th syscall arg has to be loaded from the user stack (where it's pushed
by the vDSO user code). The native i386 kernel always does this before
stopping for syscall tracing, so %ebp can be seen and modified via ptrace
to access the 6th syscall argument. The x86-64 kernel fails to do this,
presenting the stack address to ptrace instead. This makes the %rbp value
seen by 64-bit ptrace of a 32-bit process, and the %ebp value seen by a
32-bit caller of ptrace, both differ from the native i386 behavior.

This patch fixes the problem by putting the word loaded from the user stack
into %rbp before calling syscall_trace_enter, and reloading the 6th syscall
argument from there afterwards (so ptrace can change it). This makes the
behavior match that of i386 kernels.

Original-Patch-By: Roland McGrath <roland@redhat.com>

Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

authored by Chuck Ebbert and committed by Thomas Gleixner ecd744ee fd181c72

+6 -13
+6 -13
arch/x86/ia32/ia32entry.S
··· 159 159 160 160 sysenter_tracesys: 161 161 CFI_RESTORE_STATE 162 + xchgl %r9d,%ebp 162 163 SAVE_REST 163 164 CLEAR_RREGS 165 + movq %r9,R9(%rsp) 164 166 movq $-ENOSYS,RAX(%rsp) /* really needed? */ 165 167 movq %rsp,%rdi /* &pt_regs -> arg1 */ 166 168 call syscall_trace_enter 167 169 LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ 168 170 RESTORE_REST 169 - movl %ebp, %ebp 170 - /* no need to do an access_ok check here because rbp has been 171 - 32bit zero extended */ 172 - 1: movl (%rbp),%r9d 173 - .section __ex_table,"a" 174 - .quad 1b,ia32_badarg 175 - .previous 171 + xchgl %ebp,%r9d 176 172 jmp sysenter_do_call 177 173 CFI_ENDPROC 178 174 ENDPROC(ia32_sysenter_target) ··· 258 262 259 263 cstar_tracesys: 260 264 CFI_RESTORE_STATE 265 + xchgl %r9d,%ebp 261 266 SAVE_REST 262 267 CLEAR_RREGS 268 + movq %r9,R9(%rsp) 263 269 movq $-ENOSYS,RAX(%rsp) /* really needed? */ 264 270 movq %rsp,%rdi /* &pt_regs -> arg1 */ 265 271 call syscall_trace_enter 266 272 LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ 267 273 RESTORE_REST 274 + xchgl %ebp,%r9d 268 275 movl RSP-ARGOFFSET(%rsp), %r8d 269 - /* no need to do an access_ok check here because r8 has been 270 - 32bit zero extended */ 271 - 1: movl (%r8),%r9d 272 - .section __ex_table,"a" 273 - .quad 1b,ia32_badarg 274 - .previous 275 276 jmp cstar_do_call 276 277 END(ia32_cstar_target) 277 278