All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Andy Lutomirski <luto@kernel.org>
Cc: "Reshetova, Elena" <elena.reshetova@intel.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Jann Horn <jannh@google.com>,
	"Perla, Enrico" <enrico.perla@intel.com>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Thomas Gleixner <tglx@linutronix.de>,
	LKML <linux-kernel@vger.kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Greg KH <gregkh@linuxfoundation.org>
Subject: Re: [RFC PATCH] x86/entry/64: randomize kernel stack offset upon syscall
Date: Thu, 28 Mar 2019 08:45:57 -0700	[thread overview]
Message-ID: <CAGXu5j+r-NH5+QY=siE+6RTkUKoRF0Rso6KHM4NOMsCvpcjW5g@mail.gmail.com> (raw)
In-Reply-To: <CALCETrXnfk5zYWmpdLvYbcJ7hhFM13+LPq14MF7s99iqtS4-jg@mail.gmail.com>

On Tue, Mar 26, 2019 at 9:31 PM Andy Lutomirski <luto@kernel.org> wrote:
>
> On Tue, Mar 26, 2019 at 3:35 AM Reshetova, Elena
> <elena.reshetova@intel.com> wrote:
> >
> > > On Mon, Mar 18, 2019 at 1:16 PM Andy Lutomirski <luto@kernel.org> wrote:
> > > > On Mon, Mar 18, 2019 at 2:41 AM Elena Reshetova
> > > > <elena.reshetova@intel.com> wrote:
> > > > > Performance:
> > > > >
> > > > > 1) lmbench: ./lat_syscall -N 1000000 null
> > > > >     base:                     Simple syscall: 0.1774 microseconds
> > > > >     random_offset (rdtsc):     Simple syscall: 0.1803 microseconds
> > > > >     random_offset (rdrand): Simple syscall: 0.3702 microseconds
> > > > >
> > > > > 2)  Andy's tests, misc-tests: ./timing_test_64 10M sys_enosys
> > > > >     base:                     10000000 loops in 1.62224s = 162.22 nsec / loop
> > > > >     random_offset (rdtsc):     10000000 loops in 1.64660s = 164.66 nsec / loop
> > > > >     random_offset (rdrand): 10000000 loops in 3.51315s = 351.32 nsec / loop
> > > > >
> > > >
> > > > Egads!  RDTSC is nice and fast but probably fairly easy to defeat.
> > > > RDRAND is awful.  I had hoped for better.
> > >
> > > RDRAND can also fail.
> > >
> > > > So perhaps we need a little percpu buffer that collects 64 bits of
> > > > randomness at a time, shifts out the needed bits, and refills the
> > > > buffer when we run out.
> > >
> > > I'd like to avoid saving the _exact_ details of where the next offset
> > > will be, but if nothing else works, this should be okay. We can use 8
> > > bits at a time and call prandom_u32() every 4th call. Something like
> > > prandom_bytes(), but where it doesn't throw away the unused bytes.
> >
> > Actually I think this would make the end result even worse security-wise
> > than simply using rdtsc() on every syscall. Saving the randomness in percpu
> > buffer, which is probably easily accessible and can be probed if needed,
> > would supply attacker with much more knowledge about the next 3-4
> > random offsets that what he would get if we use "weak" rdtsc. Given
> > that for a successful exploit, an attacker would need to have stack aligned
> > once only, having a knowledge of 3-4 next offsets sounds like a present to an
> > exploit writer...  Additionally it creates complexity around the code that I
> > have issues justifying with "security" argument because of above...

That certainly solidifies my concern against saving randomness. :)

> > I have the patch now with alloca() and rdtsc() working, I can post it
> > (albeit it is very simple), but I am really hesitating on adding the percpu
> > buffer randomness storage to it...
> >
>
> Hmm.  I guess it depends on what types of attack you care about.  I
> bet that, if you do a bunch of iterations of mfence;rdtsc;syscall,
> you'll discover that the offset between the user rdtsc and the
> syscall's rdtsc has several values that occur with high probability.

How about rdtsc xor with the middle word of the stack canary? (to
avoid the 0-byte) Something like:

    rdtsc
    xorl [%gs:...canary....], %rax
    andq  $__MAX_STACK_RANDOM_OFFSET, %rax

I need to look at the right way to reference the canary during that
code. Andy might know off the top of his head. :)

-Kees

-- 
Kees Cook

  reply	other threads:[~2019-03-28 15:46 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-18  9:41 [RFC PATCH] x86/entry/64: randomize kernel stack offset upon syscall Elena Reshetova
2019-03-18 20:15 ` Andy Lutomirski
2019-03-18 21:07   ` Kees Cook
2019-03-26 10:35     ` Reshetova, Elena
2019-03-27  4:31       ` Andy Lutomirski
2019-03-28 15:45         ` Kees Cook [this message]
2019-03-28 16:29           ` Andy Lutomirski
2019-03-28 16:47             ` Kees Cook
2019-03-29  7:50               ` Reshetova, Elena
2019-03-18 23:31   ` Josh Poimboeuf
2019-03-20 12:10     ` Reshetova, Elena
2019-03-20 11:12   ` David Laight
2019-03-20 14:51     ` Andy Lutomirski
2019-03-20 12:04   ` Reshetova, Elena
2019-03-20  7:27 Elena Reshetova
2019-03-20  7:29 ` Reshetova, Elena
2019-03-29  8:13 Elena Reshetova
2019-04-03 21:17 ` Kees Cook
2019-04-04 11:41   ` Reshetova, Elena
2019-04-04 17:03     ` Kees Cook
2019-04-05 10:14       ` Reshetova, Elena
2019-04-05 13:14         ` Andy Lutomirski

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='CAGXu5j+r-NH5+QY=siE+6RTkUKoRF0Rso6KHM4NOMsCvpcjW5g@mail.gmail.com' \
    --to=keescook@chromium.org \
    --cc=bp@alien8.de \
    --cc=elena.reshetova@intel.com \
    --cc=enrico.perla@intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jannh@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.