linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Radu Rendec <radu.rendec@gmail.com>
To: Christophe Leroy <christophe.leroy@c-s.fr>,
	linuxppc-dev@lists.ozlabs.org
Cc: Oleg Nesterov <oleg@redhat.com>
Subject: Re: PowerPC arch_ptrace() writes beyond thread_struct/task_struct
Date: Thu, 06 Jun 2019 09:45:16 -0400	[thread overview]
Message-ID: <62eaddb44743811a8b2cf4da5aa85f5b61662539.camel@gmail.com> (raw)
In-Reply-To: <84012c6a-befa-5091-ee13-4da5d5eb7d75@c-s.fr>

On Thu, 2019-06-06 at 07:15 +0200, Christophe Leroy wrote:
> 
> Le 05/06/2019 à 23:45, Radu Rendec a écrit :
> > Hi Everyone,
> > 
> > I'm seeing some weird memory corruption that I have been able to isolate
> > to arch_ptrace() [arch/powerpc/kernel/ptrace.c] and PTRACE_POKEUSR. I am
> > on PowerPC 32 (MPC8378), kernel 4.9.179.
> > 
> > It's not very easy for me to test on the latest kernel, but I guess
> > little has changed since 4.9 in either the architecture specific ptrace
> > code or PowerPC register data structures.
> > 
> > What happens is that gdb calls ptrace(PTRACE_POKEUSER) with addr=0x158.
> > This goes down to arch_ptrace() [arch/powerpc/kernel/ptrace.c], inside
> > `case PTRACE_POKEUSR`, on the branch that does this:
> > 
> >      memcpy(&child->thread.TS_FPR(fpidx), &data,
> >              sizeof(long));
> > 
> > where:
> >      index = addr >> 2 = 0x56 = 86
> >      fpidx = index - PT_FPR0 = 86 - 48 = 38
> 
> In struct thread_fp_state, fpr field is u64, so I guess we should have 
> the following on PPC32:
> 
> fpidx = (index - PT_FPR0) >> 1;

I guess this would only apply to PPC32, since everything up to fpidx is
calculated in units of sizeof(long) - which is 4 on PPC32 and 8 on
PPC64. But fpr[0:31] is always u64.

It also looks odd that only sizeof(long) bytes are ever copied for any
given fpr[fpidx], which means one half of the u64 is never accessible on
PPC32.

Ont other thing I don't get is the "+1" in the definition of PT_FPSCR
for PPC32:

    #define PT_FPSCR (PT_FPR0 + 2*32 + 1)

Looking at struct thread_fp_state, fpscr follows immediately after
fpr[31]. Is the FPSCR register only 32-bit on PPC32? Is it stored in the
2nd half of (struct thread_fp_state).fpscr? This line:

    child->thread.fp_state.fpscr = data;

suggests so. And in that case, the "+1" in PT_FPSCR makes sense, but
only for big endian: assigning `data` (which is "long", 32-bit) to the
`fpscr` field (which is "u64") would go to the higher address, which is
indeed "+1" in units of 32-bit words.

Then there is also a problem is the condition that determines whether
memcpy() is used to access one of the fpr[0:31] or fpscr is assigned
directly:

    if (fpidx < (PT_FPSCR - PT_FPR0))

The case when the supplied addr points to the lower half of fpscr (which
is unused on PPC32?) erroneously indexes into fpr[0:31].

Is there any documentation of what "addr" is supposed to mean?


> >      &child->thread.TS_FPR(fpidx) = (void *)child + 1296
> > 
> >      offsetof(struct task_struct, thread) = 960
> >      sizeof(struct thread_struct) = 336
> >      sizeof(struct task_struct) = 1296
> > 
> > In other words, the memcpy() call writes just beyond thread_struct
> > (which is also beyond task_struct, for that matter).
> > 
> > This should never get past the bounds checks for `index`, so perhaps
> > there is a mismatch between ptrace macros and the actual register data
> > structures layout.
> > 
> > I will continue to investigate, but I'm not familiar with the PowerPC
> > registers so it will take a while before I make sense of all the data
> > structures and macros. Hopefully this rings a bell to someone who is
> > already familiar with those and could figure out quickly what the
> > problem is.



      reply	other threads:[~2019-06-06 13:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-05 21:45 PowerPC arch_ptrace() writes beyond thread_struct/task_struct Radu Rendec
2019-06-06  5:15 ` Christophe Leroy
2019-06-06 13:45   ` Radu Rendec [this message]

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=62eaddb44743811a8b2cf4da5aa85f5b61662539.camel@gmail.com \
    --to=radu.rendec@gmail.com \
    --cc=christophe.leroy@c-s.fr \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=oleg@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;
as well as URLs for NNTP newsgroup(s).