RE: [GIT PULL] Stack randomization fix
From: David Laight
Date: Mon May 17 2021 - 05:15:26 EST
From: Linus Torvalds
> Sent: 15 May 2021 18:13
>
> On Sat, May 15, 2021 at 12:35 AM Ingo Molnar <mingo@xxxxxxxxxx> wrote:
> >
> > --- a/include/linux/randomize_kstack.h
> > +++ b/include/linux/randomize_kstack.h
> > @@ -38,7 +38,7 @@ void *__builtin_alloca(size_t size);
> > /* Keep allocation even after "ptr" loses scope. */ \
> > - asm volatile("" : "=o"(*ptr) :: "memory"); \
> > + asm volatile("" :: "r"(ptr) : "memory"); \
> > } \
>
> Side note: at some point, a compiler will (correctly) decide that
> regardless of the inline asm here, the scope of that allocation is
> only that one block.
>
> To be actually reliable, I suspect that add_random_kstack_offset()
> should return that pointer as a cookie, and then users should have a
>
> end_random_kstack_offset(cookie);
>
> at the end of the function that did the add_random_kstack_offset().
Hmmm... would that even help?
The scope of the alloca() block is still the code block in which it
is created - so you are still using a pointer to stale stack.
The alloca() would have to have function scope to remain valid
throughout the entire system call.
Then the simple asm statement that just saves the pointer ought
to be good enough since the compiler must assume that can be read
much later in the syscall code.
I've thought of an alternative approach.
Instead of using alloca() save the offset in a per-cpu location.
In the next system call use the saved offset to adjust the
stack pointer in the asm wrapper.
It has to be easier to adjust the stack pointer very early
in the syscall entry path?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)