mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chuck Ebbert <76306.1226@compuserve.com>
To: Parag Warudkar <kernel-stuff@comcast.net>
Cc: Andrew Morton <akpm@osdl.org>, Roland McGrath <roland@redhat.com>,
	Andi Kleen <ak@suse.de>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: 2.6.13-mm2
Date: Fri, 16 Sep 2005 20:36:14 -0400	[thread overview]
Message-ID: <200509162038_MC3-1-AA6D-60D9@compuserve.com> (raw)

On Thu, 08 Sep 2005 at 20:26:51 -0400, Parag Warudkar wrote:

> Andrew Morton wrote:
> 
> > Parag, perhaps you could confirm that reverting that patch fixes 
> > things up?
> 
> Sure - reverting the x86-64-ptrace-ia32-bp-fix patch fixes it.

 It looks to me like that patch corrupts ebp.

 This one works for me, though ebp still appears wrong to a 32-bit
debugger on syscall exit trace.  Maybe that doesn't matter so much.

 Test program follows (it will create a file named "__ptfile__"), then patch.

================================================================================

/* ptrace test program 
 *
 * Tests if ptrace can modify syscall arg6 (ebp) in ia32 mode.
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <asm/user.h>

int parent, child, status, traces;
struct user_regs_struct regs;
char zero = '0', one = '1';

void dump_regs(struct user_regs_struct *regs)
{
	printf("eax = %08X (%d), orig_eax = %08X (%d)\n",
		(unsigned int)regs->eax,
		(unsigned int)regs->eax,
		(unsigned int)regs->orig_eax,
		(unsigned int)regs->orig_eax);
	printf("ebx = %08X, ecx = %08X, edx = %08X\n",
		(unsigned int)regs->ebx,
		(unsigned int)regs->ecx,
		(unsigned int)regs->edx);
	printf("esi = %08X, edi = %08X, ebp = %08X\n",
		(unsigned int)regs->esi,
		(unsigned int)regs->edi,
		(unsigned int)regs->ebp);
}

void do_parent()
{
again:
	waitpid(child, &status, 0);
	if (WIFSTOPPED(status)) {
		if (traces && traces <= 2 ) { /* skip first, then do two */
			puts("child stopped");
			ptrace(PTRACE_GETREGS, child, 0, &regs);
			dump_regs(&regs);
			if (regs.orig_eax == 192 && traces == 1) { /* mmap2 */
				if (regs.ebp != 0)
					puts("tracer: arg6 was not 0: "
					     "ptrace is broken!");
				else {
					regs.ebp = 1; /* change arg 6 */
					ptrace(PTRACE_SETREGS, child, 0, &regs);
				}
			} else if (traces == 1) /* first syscall wasn't mmap2 */
				puts("unexpected problem, ignore the result!");
		}
		ptrace(PTRACE_SYSCALL, child, 0, 0);
		traces++;
	}
	if (!WIFEXITED(status))
		goto again;
}

void do_child()
{
	int f, i;
	void *a;
	int works = 1;

	f = open("__ptfile__", O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
	for (i = 0; i < 4096; i++) /* a page of '0' */
		write(f, &zero, 1);
	for (i = 0; i < 4096; i++) /* a page of '1' */
		write(f, &one, 1);
	close(f);
	
	f = open("__ptfile__", O_RDONLY);
	ptrace(PTRACE_TRACEME, 0, 0, 0);
	kill(getpid(), SIGUSR1);
	a = mmap(0, 4096, PROT_READ, MAP_SHARED, f, 0); /* map first page */
	if (*(char *)a != one)
		works = 0; /* got first page: arg6 was unchanged */
	munmap(a, 4096);
	close(f);
	remove("__ptfile__");
	
	if (works)
		puts("ptrace works");
	else
		puts("mmap args didn't change: ptrace is broken!");
}

int main(int argc, char * const argv[])
{
	parent = getpid();
	child = fork();

	if (child)
		do_parent();
	else
		do_child();

	return 0;
}
================================================================================

Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Original-Patch-By: Roland McGrath <roland@redhat.com>

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.

 arch/x86_64/ia32/ia32entry.S |   19 ++++++-------------
 1 files changed, 6 insertions(+), 13 deletions(-)

--- 2.6.13-64.orig/arch/x86_64/ia32/ia32entry.S
+++ 2.6.13-64/arch/x86_64/ia32/ia32entry.S
@@ -102,20 +102,16 @@ sysenter_do_call:	
 	.byte	0xf, 0x35
 
 sysenter_tracesys:
+	xchgl	%r9d,%ebp
 	SAVE_REST
 	CLEAR_RREGS
+	movq	%r9,R9(%rsp)
 	movq	$-ENOSYS,RAX(%rsp)	/* really needed? */
 	movq	%rsp,%rdi        /* &pt_regs -> arg1 */
 	call	syscall_trace_enter
 	LOAD_ARGS ARGOFFSET  /* reload args from stack in case ptrace changed it */
 	RESTORE_REST
-	movl	%ebp, %ebp
-	/* no need to do an access_ok check here because rbp has been
-	   32bit zero extended */ 
-1:	movl	(%rbp),%r9d
-	.section __ex_table,"a"
-	.quad 1b,ia32_badarg
-	.previous
+	xchgl	%ebp,%r9d
 	jmp	sysenter_do_call
 	CFI_ENDPROC
 
@@ -183,20 +179,17 @@ cstar_do_call:	
 	sysretl
 	
 cstar_tracesys:	
+	xchgl %r9d,%ebp
 	SAVE_REST
 	CLEAR_RREGS
+	movq %r9,R9(%rsp)
 	movq $-ENOSYS,RAX(%rsp)	/* really needed? */
 	movq %rsp,%rdi        /* &pt_regs -> arg1 */
 	call syscall_trace_enter
 	LOAD_ARGS ARGOFFSET  /* reload args from stack in case ptrace changed it */
 	RESTORE_REST
+	xchgl %ebp,%r9d
 	movl RSP-ARGOFFSET(%rsp), %r8d
-	/* no need to do an access_ok check here because r8 has been
-	   32bit zero extended */ 
-1:	movl	(%r8),%r9d
-	.section __ex_table,"a"
-	.quad 1b,ia32_badarg
-	.previous
 	jmp cstar_do_call
 				
 ia32_badarg:
__
Chuck

             reply	other threads:[~2005-09-17  0:40 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-17  0:36 Chuck Ebbert [this message]
2005-09-17  4:17 ` 2.6.13-mm2 Parag Warudkar
2005-09-17  4:30   ` 2.6.13-mm2 Parag Warudkar
     [not found] <4KtRD-7Nt-13@gated-at.bofh.it>
2005-09-08 23:23 ` 2.6.13-mm2 Ronny V. Vindenes
2005-09-08 23:34   ` 2.6.13-mm2 Andrew Morton
2005-09-09  0:26     ` 2.6.13-mm2 Parag Warudkar
2005-09-09  0:55       ` 2.6.13-mm2 Roland McGrath
  -- strict thread matches above, loose matches on Subject: below --
2005-09-08 12:30 2.6.13-mm2 Andrew Morton
2005-09-08 13:12 ` 2.6.13-mm2 Benoit Boissinot
2005-09-08 13:48 ` 2.6.13-mm2 Christoph Hellwig
2005-09-08 14:30 ` 2.6.13-mm2 Martin J. Bligh
2005-09-09  0:39   ` 2.6.13-mm2 Andi Kleen
2005-09-09 10:41     ` 2.6.13-mm2 Andrew Morton
2005-09-09 10:46       ` 2.6.13-mm2 Andi Kleen
2005-09-08 17:20 ` 2.6.13-mm2 Michael Thonke
2005-09-08 19:39   ` 2.6.13-mm2 Andrew Morton
2005-09-10  7:02     ` 2.6.13-mm2 Michael Thonke
2005-09-09  1:47 ` 2.6.13-mm2 Grant Coady
2005-09-09  9:43   ` 2.6.13-mm2 Andrew Morton
2005-09-09 13:45     ` 2.6.13-mm2 Grant Coady
2005-09-10  6:33       ` 2.6.13-mm2 Marko Kohtala
2005-09-10 11:45 ` 2.6.13-mm2 Manuel Lauss
2005-09-10 12:42   ` 2.6.13-mm2 Antonino A. Daplas
2005-09-10 13:46     ` 2.6.13-mm2 Manuel Lauss
2005-09-10 20:21       ` 2.6.13-mm2 Antonino A. Daplas
2005-09-10 21:26       ` 2.6.13-mm2 Antonino A. Daplas
2005-09-10 18:43 ` 2.6.13-mm2 Dominik Karall
2005-09-10 22:12   ` 2.6.13-mm2 Andrew Morton
2005-09-10 23:46 ` 2.6.13-mm2 J.A. Magallon
2005-09-10 23:56   ` 2.6.13-mm2 Andrew Morton
2005-09-11  0:07     ` 2.6.13-mm2 Patrick McHardy
2005-09-11  0:49       ` 2.6.13-mm2 J.A. Magallon
2005-09-11  0:58         ` 2.6.13-mm2 J.A. Magallon
2005-09-11  1:03           ` 2.6.13-mm2 Patrick McHardy
2005-09-11  1:22             ` 2.6.13-mm2 J.A. Magallon
2005-09-11  1:25               ` 2.6.13-mm2 Patrick McHardy
2005-09-11 17:03 ` 2.6.13-mm2 Rafael J. Wysocki
2005-09-11 19:36   ` 2.6.13-mm2 Andrew Morton
2005-09-11 20:03     ` 2.6.13-mm2 Hugh Dickins
2005-09-12 19:19       ` 2.6.13-mm2 Rafael J. Wysocki
2005-09-11 20:08     ` 2.6.13-mm2 Daniel Ritz
2005-09-12 10:04       ` 2.6.13-mm2 Rafael J. Wysocki
2005-09-12 10:06       ` 2.6.13-mm2 Rafael J. Wysocki
2005-09-12 10:09         ` 2.6.13-mm2 Rafael J. Wysocki
2005-09-18 21:49           ` 2.6.13-mm2 Daniel Ritz
2005-09-19  3:07             ` 2.6.13-mm2 Hugh Dickins
2005-09-19 15:56               ` 2.6.13-mm2 Daniel Ritz
2005-09-23 16:52             ` 2.6.13-mm2 Rafael J. Wysocki
2005-09-28 20:05               ` 2.6.13-mm2 Daniel Ritz
2005-09-29 15:22                 ` 2.6.13-mm2 Linus Torvalds
2005-09-12  3:07 ` 2.6.13-mm2 Martin J. Bligh
2005-09-12  5:01   ` 2.6.13-mm2 Andi Kleen
2005-09-12  6:09     ` 2.6.13-mm2 Martin J. Bligh
2005-09-12  7:16       ` 2.6.13-mm2 Andi Kleen
2005-09-12 18:06     ` 2.6.13-mm2 Martin J. Bligh
2005-09-12 18:19       ` 2.6.13-mm2 Dave Hansen
2005-09-12 18:51       ` 2.6.13-mm2 Andi Kleen
2005-09-12 22:46         ` 2.6.13-mm2 Martin J. Bligh
2005-09-13  0:08           ` 2.6.13-mm2 Andrew Morton
2005-09-13  4:00             ` 2.6.13-mm2 Martin J. Bligh
2005-09-12  3:10 ` 2.6.13-mm2 Martin J. Bligh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200509162038_MC3-1-AA6D-60D9@compuserve.com \
    --to=76306.1226@compuserve.com \
    --cc=ak@suse.de \
    --cc=akpm@osdl.org \
    --cc=kernel-stuff@comcast.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=roland@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®