From mboxrd@z Thu Jan 1 00:00:00 1970 Reply-To: kernel-hardening@lists.openwall.com MIME-Version: 1.0 Sender: keescook@google.com In-Reply-To: <87poq1jgtw.fsf@concordia.ellerman.id.au> References: <1468619065-3222-1-git-send-email-keescook@chromium.org> <1468619065-3222-3-git-send-email-keescook@chromium.org> <5790711f.2350420a.b4287.2cc0SMTPIN_ADDED_BROKEN@mx.google.com> <20160722174551.jddle6mf7zlq6xmb@treble> <87poq1jgtw.fsf@concordia.ellerman.id.au> From: Kees Cook Date: Mon, 25 Jul 2016 21:46:36 -0700 Message-ID: Content-Type: text/plain; charset=UTF-8 Subject: [kernel-hardening] Re: [PATCH v3 02/11] mm: Hardened usercopy To: Michael Ellerman Cc: Josh Poimboeuf , LKML , Balbir Singh , Daniel Micay , Rik van Riel , Casey Schaufler , PaX Team , Brad Spengler , Russell King , Catalin Marinas , Will Deacon , Ard Biesheuvel , Benjamin Herrenschmidt , Tony Luck , Fenghua Yu , "David S. Miller" , "x86@kernel.org" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Andy Lutomirski , Borislav Petkov , Mathias Krause , Jan Kara , Vitaly Wool , Dmitry Vyukov , Laura Abbott , "linux-arm-kernel@lists.infradead.org" , linux-ia64@vger.kernel.org, "linuxppc-dev@lists.ozlabs.org" , sparclinux , linux-arch , Linux-MM , "kernel-hardening@lists.openwall.com" List-ID: On Mon, Jul 25, 2016 at 7:03 PM, Michael Ellerman wrote: > Josh Poimboeuf writes: > >> On Thu, Jul 21, 2016 at 11:34:25AM -0700, Kees Cook wrote: >>> On Wed, Jul 20, 2016 at 11:52 PM, Michael Ellerman wrote: >>> > Kees Cook writes: >>> > >>> >> diff --git a/mm/usercopy.c b/mm/usercopy.c >>> >> new file mode 100644 >>> >> index 000000000000..e4bf4e7ccdf6 >>> >> --- /dev/null >>> >> +++ b/mm/usercopy.c >>> >> @@ -0,0 +1,234 @@ >>> > ... >>> >> + >>> >> +/* >>> >> + * Checks if a given pointer and length is contained by the current >>> >> + * stack frame (if possible). >>> >> + * >>> >> + * 0: not at all on the stack >>> >> + * 1: fully within a valid stack frame >>> >> + * 2: fully on the stack (when can't do frame-checking) >>> >> + * -1: error condition (invalid stack position or bad stack frame) >>> >> + */ >>> >> +static noinline int check_stack_object(const void *obj, unsigned long len) >>> >> +{ >>> >> + const void * const stack = task_stack_page(current); >>> >> + const void * const stackend = stack + THREAD_SIZE; >>> > >>> > That allows access to the entire stack, including the struct thread_info, >>> > is that what we want - it seems dangerous? Or did I miss a check >>> > somewhere else? >>> >>> That seems like a nice improvement to make, yeah. >>> >>> > We have end_of_stack() which computes the end of the stack taking >>> > thread_info into account (end being the opposite of your end above). >>> >>> Amusingly, the object_is_on_stack() check in sched.h doesn't take >>> thread_info into account either. :P Regardless, I think using >>> end_of_stack() may not be best. To tighten the check, I think we could >>> add this after checking that the object is on the stack: >>> >>> #ifdef CONFIG_STACK_GROWSUP >>> stackend -= sizeof(struct thread_info); >>> #else >>> stack += sizeof(struct thread_info); >>> #endif >>> >>> e.g. then if the pointer was in the thread_info, the second test would >>> fail, triggering the protection. >> >> FWIW, this won't work right on x86 after Andy's >> CONFIG_THREAD_INFO_IN_TASK patches get merged. > > Yeah. I wonder if it's better for the arch helper to just take the obj and len, > and work out it's own bounds for the stack using current and whatever makes > sense on that arch. > > It would avoid too much ifdefery in the generic code, and also avoid any > confusion about whether stackend is the high or low address. > > eg. on powerpc we could do: > > int noinline arch_within_stack_frames(const void *obj, unsigned long len) > { > void *stack_low = end_of_stack(current); > void *stack_high = task_stack_page(current) + THREAD_SIZE; > > > Whereas arches with STACK_GROWSUP=y could do roughly the reverse, and x86 can do > whatever it needs to depending on whether the thread_info is on or off stack. > > cheers Yeah, I agree: this should be in the arch code. If the arch can actually do frame checking, the thread_info (if it exists on the stack) would already be excluded. But it'd be a nice tightening of the check. -Kees -- Kees Cook Chrome OS & Brillo Security