Re: [PATCH v2 1/3] x86/entry: Clear extra registers beyond syscall arguments for 64bit kernels

From: Josh Poimboeuf
Date: Mon Feb 05 2018 - 12:48:42 EST


On Mon, Feb 05, 2018 at 05:31:39PM +0000, Andy Lutomirski wrote:
> On Mon, Feb 5, 2018 at 5:26 PM, Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:
> > On Mon, Feb 05, 2018 at 04:37:26PM +0000, Andy Lutomirski wrote:
> >> On Mon, Feb 5, 2018 at 4:30 PM, Ingo Molnar <mingo@xxxxxxxxxx> wrote:
> >> >
> >> > * Andy Lutomirski <luto@xxxxxxxxxxxxxx> wrote:
> >> >
> >> >>
> >> >>
> >> >> > On Feb 5, 2018, at 3:42 AM, Ingo Molnar <mingo@xxxxxxxxxx> wrote:
> >> >> >
> >> >> >
> >> >> > * Dan Williams <dan.j.williams@xxxxxxxxx> wrote:
> >> >> >
> >> >> >> + /*
> >> >> >> + * Sanitize extra registers of values that a speculation attack
> >> >> >> + * might want to exploit. In the CONFIG_FRAME_POINTER=y case,
> >> >> >> + * the expectation is that %ebp will be clobbered before it
> >> >> >> + * could be used.
> >> >> >> + */
> >> >> >> + .macro CLEAR_EXTRA_REGS_NOSPEC
> >> >> >> + xorq %r15, %r15
> >> >> >> + xorq %r14, %r14
> >> >> >> + xorq %r13, %r13
> >> >> >> + xorq %r12, %r12
> >> >> >> + xorl %ebx, %ebx
> >> >> >> +#ifndef CONFIG_FRAME_POINTER
> >> >> >> + xorl %ebp, %ebp
> >> >> >> +#endif
> >> >> >> + .endm
> >> >> >
> >> >> > Yeah, so this series look pretty good to me, but there's one small detail: I think
> >> >> > RBP should be cleared unconditionally here, even in the CONFIG_FRAME_POINTERS=y
> >> >> > case, because:
> >> >>
> >> >> ENCODE_FRAME_POINTER should take care of rbp, though.
> >> >
> >> > AFAICS there's various entry paths where it's not used I think: for example the
> >> > compat system calls in entry_64_compat.S don't seem to encode RBP in such a
> >> > fashion (unless I missed some macro side effect).
> >>
> >> Then that's a separate bug that should be fixed. Josh?
> >
> > We don't encode the frame pointer on syscalls, because "fast path"
> > (though that's obviously no longer a consideration).
>
> Should we start encoding the frame pointer?

Perhaps, but I should clarify it's not a bug. For syscalls, we instead
just standardized the location of the last stack frame. But that's a
bit fragile and I was never too happy with it.

Encoding the frame pointer would be a lot more straightforward, though
we'd still need to figure out a way to detect the "end" of the stack for
kthreads. We could probably encode that information as well: "here's
the end, but there are no pt_regs". Could just be an "encoded" NULL.

--
Josh