Re: [GIT PULL] Stack randomization fix

From: Linus Torvalds
Date: Sat May 15 2021 - 13:22:30 EST


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().

And that end_random_kstack_offset() should have *another* "asm
volatile" that uses the pointer.

Note that you need both of the asm volatile barriers: without the
first one, the compiler might decide to move the allocation down, and
without the second one, the compiler might decide the allocation is
out of scope and can be undone before the actual system call or
whatever is done.

Honestly, I don't personally much care (I think this is all stupid
overhead, and think clearing the stack is much better), and I suspect
that current compilers always end up cleaning up alloca at function
exit, so this probably all _works_ as-is. But it's technically fragile
and wrong.

Linus