Subject: Re: [PATCH v12 2/6] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls References: <1526488097-20611-1-git-send-email-alex.popov@linux.com> <1526488097-20611-3-git-send-email-alex.popov@linux.com> <20180518065349.GA10080@gmail.com> <23369d86-ad31-e7c5-e42f-28e0e51ec128@linux.com> From: Alexander Popov Message-ID: Date: Wed, 23 May 2018 00:00:13 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit To: Kees Cook Cc: Ingo Molnar , Kernel Hardening , PaX Team , Brad Spengler , Andy Lutomirski , Tycho Andersen , Laura Abbott , Mark Rutland , Ard Biesheuvel , Borislav Petkov , Richard Sandiford , Thomas Gleixner , "H . Peter Anvin" , Peter Zijlstra , "Dmitry V . Levin" , Emese Revfy , Jonathan Corbet , Andrey Ryabinin , "Kirill A . Shutemov" , Thomas Garnier , Andrew Morton , Alexei Starovoitov , Josef Bacik , Masami Hiramatsu , Nicholas Piggin , Al Viro , "David S . Miller" , Ding Tianhong , David Woodhouse , Josh Poimboeuf , Steven Rostedt , Dominik Brodowski , Juergen Gross , Linus Torvalds , Greg Kroah-Hartman , Dan Williams , Dave Hansen , Mathias Krause , Vikas Shivappa , Kyle Huey , Dmitry Safonov , Will Deacon , Arnd Bergmann , Florian Weimer , Boris Lukashev , Andrey Konovalov , X86 ML , LKML List-ID: On 22.05.2018 20:20, Kees Cook wrote: > On Tue, May 22, 2018 at 3:58 AM, Alexander Popov wrote: >> Hello Ingo and Kees, >> >> On 18.05.2018 09:53, Ingo Molnar wrote: >>> * Alexander Popov wrote: >>>> --- a/arch/x86/kernel/process_32.c >>>> +++ b/arch/x86/kernel/process_32.c >>>> @@ -136,6 +136,11 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, >>>> p->thread.sp0 = (unsigned long) (childregs+1); >>>> memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps)); >>>> >>>> +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK >>>> + p->thread.lowest_stack.val = (unsigned long)end_of_stack(p) + >>>> + sizeof(unsigned long); >>>> +#endif >>> >>> This should use an inline helper: >>> >>> stackleak_task_init(p); >> >> Right now I can't define such a helper because 'lowest_stack' is a part of >> 'thread_struct', but arch/x86/include/asm/processor.h and include/linux/sched.h >> are independent. >> >> I think the best solution is to move 'lowest_stack' variable to 'task_struct'. > > It might be possible work around this by using a macro in stackleak.h > (pardon any whitespace damage): > > #ifdef CONFIG_GCC_PLUGIN_STACKLEAK > # define stackleak_task_init(task) do { \ > task->thread.stack_leak.lowest_stack = \ > (unsigned long)end_of_stack(task) + \ > sizeof(unsigned long); \ > } while (0) > #else > # define stackleak_task_init(task) do { } while (0) > #endif > >> Is there any reason to fold this variable into a 'stackleak_info' structure? I >> see everybody happily uses #ifdef in 'task_struct'. > > I'd rather it stay in threadinfo, but I could be convinced otherwise. Currently 'lowest_stack' is a part of 'thread_struct', which is a "CPU-specific state of this task". But in fact 'lowest_stack' is not CPU-specific. If I move it to 'task_struct', Laura will avoid redefining it in 'thread_struct' in arch/arm64/include/asm/processor.h. And that will also allow me to create a stackleak_task_init() inline helper in stackleak.h. Best regards, Alexander