From: Kees Cook <keescook@chromium.org> To: linux-kernel@vger.kernel.org Cc: Kees Cook <keescook@chromium.org>, Rik van Riel <riel@redhat.com>, Casey Schaufler <casey@schaufler-ca.com>, PaX Team <pageexec@freemail.hu>, Brad Spengler <spender@grsecurity.net>, Russell King <linux@armlinux.org.uk>, Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will.deacon@arm.com>, Ard Biesheuvel <ard.biesheuvel@linaro.org>, Benjamin Herrenschmidt <benh@kernel.crashing.org>, Michael Ellerman <mpe@ellerman.id.au>, Tony Luck <tony.luck@intel.com>, Fenghua Yu <fenghua.yu@intel.com>, "David S. Miller" <davem@davemloft.net>, x86@kernel.org, Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Joonsoo Kim <iamjoonsoo.kim@lge.com>, Andrew Morton <akpm@linux-foundation.org>, Andy Lutomirski <luto@kernel.org>, Borislav Petkov <bp@suse.de>, Mathias Krause <minipli@googlemail.com>, Jan Kara <jack@suse.cz> Subject: [PATCH v2 01/11] mm: Implement stack frame object validation Date: Wed, 13 Jul 2016 14:55:54 -0700 [thread overview] Message-ID: <1468446964-22213-2-git-send-email-keescook@chromium.org> (raw) In-Reply-To: <1468446964-22213-1-git-send-email-keescook@chromium.org> This creates per-architecture function arch_within_stack_frames() that should validate if a given object is contained by a kernel stack frame. Initial implementation is on x86. This is based on code from PaX. Signed-off-by: Kees Cook <keescook@chromium.org> --- arch/Kconfig | 9 ++++++++ arch/x86/Kconfig | 1 + arch/x86/include/asm/thread_info.h | 44 ++++++++++++++++++++++++++++++++++++++ include/linux/thread_info.h | 9 ++++++++ 4 files changed, 63 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index d794384a0404..5e2776562035 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -424,6 +424,15 @@ config CC_STACKPROTECTOR_STRONG endchoice +config HAVE_ARCH_WITHIN_STACK_FRAMES + bool + help + An architecture should select this if it can walk the kernel stack + frames to determine if an object is part of either the arguments + or local variables (i.e. that it excludes saved return addresses, + and similar) by implementing an inline arch_within_stack_frames(), + which is used by CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING bool help diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 0a7b885964ba..4407f596b72c 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -91,6 +91,7 @@ config X86 select HAVE_ARCH_SOFT_DIRTY if X86_64 select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE + select HAVE_ARCH_WITHIN_STACK_FRAMES select HAVE_EBPF_JIT if X86_64 select HAVE_CC_STACKPROTECTOR select HAVE_CMPXCHG_DOUBLE diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h index 30c133ac05cd..ab386f1336f2 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -180,6 +180,50 @@ static inline unsigned long current_stack_pointer(void) return sp; } +/* + * Walks up the stack frames to make sure that the specified object is + * entirely contained by a single stack frame. + * + * Returns: + * 1 if within a frame + * -1 if placed across a frame boundary (or outside stack) + * 0 unable to determine (no frame pointers, etc) + */ +static inline int arch_within_stack_frames(const void * const stack, + const void * const stackend, + const void *obj, unsigned long len) +{ +#if defined(CONFIG_FRAME_POINTER) + const void *frame = NULL; + const void *oldframe; + + oldframe = __builtin_frame_address(1); + if (oldframe) + frame = __builtin_frame_address(2); + /* + * low ----------------------------------------------> high + * [saved bp][saved ip][args][local vars][saved bp][saved ip] + * ^----------------^ + * allow copies only within here + */ + while (stack <= frame && frame < stackend) { + /* + * If obj + len extends past the last frame, this + * check won't pass and the next frame will be 0, + * causing us to bail out and correctly report + * the copy as invalid. + */ + if (obj + len <= frame) + return obj >= oldframe + 2 * sizeof(void *) ? 1 : -1; + oldframe = frame; + frame = *(const void * const *)frame; + } + return -1; +#else + return 0; +#endif +} + #else /* !__ASSEMBLY__ */ #ifdef CONFIG_X86_64 diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h index b4c2a485b28a..3d5c80b4391d 100644 --- a/include/linux/thread_info.h +++ b/include/linux/thread_info.h @@ -146,6 +146,15 @@ static inline bool test_and_clear_restore_sigmask(void) #error "no set_restore_sigmask() provided and default one won't work" #endif +#ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES +static inline int arch_within_stack_frames(const void * const stack, + const void * const stackend, + const void *obj, unsigned long len) +{ + return 0; +} +#endif + #endif /* __KERNEL__ */ #endif /* _LINUX_THREAD_INFO_H */ -- 2.7.4
WARNING: multiple messages have this Message-ID (diff)
From: Kees Cook <keescook@chromium.org> To: linux-kernel@vger.kernel.org Cc: Kees Cook <keescook@chromium.org>, Rik van Riel <riel@redhat.com>, Casey Schaufler <casey@schaufler-ca.com>, PaX Team <pageexec@freemail.hu>, Brad Spengler <spender@grsecurity.net>, Russell King <linux@armlinux.org.uk>, Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will.deacon@arm.com>, Ard Biesheuvel <ard.biesheuvel@linaro.org>, Benjamin Herrenschmidt <benh@kernel.crashing.org>, Michael Ellerman <mpe@ellerman.id.au>, Tony Luck <tony.luck@intel.com>, Fenghua Yu <fenghua.yu@intel.com>, "David S. Miller" <davem@davemloft.net>, x86@kernel.org, Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Joonsoo Kim <iamjoonsoo.kim@lge.com>, Andrew Morton <akpm@linux-foundation.org>, Andy Lutomirski <luto@kernel.org>, Borislav Petkov <bp@suse.de>, Mathias Krause <minipli@googlemail.com>, Jan Kara <jack@suse.cz>, Vitaly Wool <vitalywool@gmail.com>, Andrea Arcangeli <aarcange@redhat.com>, Dmitry Vyukov <dvyukov@google.com>, Laura Abbott <labbott@fedoraproject.org>, linux-arm-kernel@lists.infradead.org, linux-ia64@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH v2 01/11] mm: Implement stack frame object validation Date: Wed, 13 Jul 2016 14:55:54 -0700 [thread overview] Message-ID: <1468446964-22213-2-git-send-email-keescook@chromium.org> (raw) Message-ID: <20160713215554.ubpx5Y7yWCBXh3QAqDSFKrdNTI4WUwVFq8T1oslb4Cc@z> (raw) In-Reply-To: <1468446964-22213-1-git-send-email-keescook@chromium.org> This creates per-architecture function arch_within_stack_frames() that should validate if a given object is contained by a kernel stack frame. Initial implementation is on x86. This is based on code from PaX. Signed-off-by: Kees Cook <keescook@chromium.org> --- arch/Kconfig | 9 ++++++++ arch/x86/Kconfig | 1 + arch/x86/include/asm/thread_info.h | 44 ++++++++++++++++++++++++++++++++++++++ include/linux/thread_info.h | 9 ++++++++ 4 files changed, 63 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index d794384a0404..5e2776562035 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -424,6 +424,15 @@ config CC_STACKPROTECTOR_STRONG endchoice +config HAVE_ARCH_WITHIN_STACK_FRAMES + bool + help + An architecture should select this if it can walk the kernel stack + frames to determine if an object is part of either the arguments + or local variables (i.e. that it excludes saved return addresses, + and similar) by implementing an inline arch_within_stack_frames(), + which is used by CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING bool help diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 0a7b885964ba..4407f596b72c 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -91,6 +91,7 @@ config X86 select HAVE_ARCH_SOFT_DIRTY if X86_64 select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE + select HAVE_ARCH_WITHIN_STACK_FRAMES select HAVE_EBPF_JIT if X86_64 select HAVE_CC_STACKPROTECTOR select HAVE_CMPXCHG_DOUBLE diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h index 30c133ac05cd..ab386f1336f2 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -180,6 +180,50 @@ static inline unsigned long current_stack_pointer(void) return sp; } +/* + * Walks up the stack frames to make sure that the specified object is + * entirely contained by a single stack frame. + * + * Returns: + * 1 if within a frame + * -1 if placed across a frame boundary (or outside stack) + * 0 unable to determine (no frame pointers, etc) + */ +static inline int arch_within_stack_frames(const void * const stack, + const void * const stackend, + const void *obj, unsigned long len) +{ +#if defined(CONFIG_FRAME_POINTER) + const void *frame = NULL; + const void *oldframe; + + oldframe = __builtin_frame_address(1); + if (oldframe) + frame = __builtin_frame_address(2); + /* + * low ----------------------------------------------> high + * [saved bp][saved ip][args][local vars][saved bp][saved ip] + * ^----------------^ + * allow copies only within here + */ + while (stack <= frame && frame < stackend) { + /* + * If obj + len extends past the last frame, this + * check won't pass and the next frame will be 0, + * causing us to bail out and correctly report + * the copy as invalid. + */ + if (obj + len <= frame) + return obj >= oldframe + 2 * sizeof(void *) ? 1 : -1; + oldframe = frame; + frame = *(const void * const *)frame; + } + return -1; +#else + return 0; +#endif +} + #else /* !__ASSEMBLY__ */ #ifdef CONFIG_X86_64 diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h index b4c2a485b28a..3d5c80b4391d 100644 --- a/include/linux/thread_info.h +++ b/include/linux/thread_info.h @@ -146,6 +146,15 @@ static inline bool test_and_clear_restore_sigmask(void) #error "no set_restore_sigmask() provided and default one won't work" #endif +#ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES +static inline int arch_within_stack_frames(const void * const stack, + const void * const stackend, + const void *obj, unsigned long len) +{ + return 0; +} +#endif + #endif /* __KERNEL__ */ #endif /* _LINUX_THREAD_INFO_H */ -- 2.7.4
next prev parent reply other threads:[~2016-07-13 21:55 UTC|newest] Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top 2016-07-13 21:55 [PATCH v2 0/11] mm: Hardened usercopy Kees Cook 2016-07-13 21:55 ` Kees Cook 2016-07-13 21:55 ` Kees Cook [this message] 2016-07-13 21:55 ` [PATCH v2 01/11] mm: Implement stack frame object validation Kees Cook 2016-07-13 22:01 ` Andy Lutomirski 2016-07-13 22:01 ` Andy Lutomirski 2016-07-13 22:04 ` Kees Cook 2016-07-13 22:04 ` Kees Cook 2016-07-14 5:48 ` Josh Poimboeuf 2016-07-14 5:48 ` Josh Poimboeuf 2016-07-14 18:10 ` Kees Cook 2016-07-14 18:10 ` Kees Cook 2016-07-14 19:23 ` Josh Poimboeuf 2016-07-14 19:23 ` Josh Poimboeuf 2016-07-14 21:38 ` Kees Cook 2016-07-14 21:38 ` Kees Cook 2016-07-13 21:55 ` [PATCH v2 02/11] mm: Hardened usercopy Kees Cook 2016-07-13 21:55 ` Kees Cook 2016-07-14 23:20 ` Balbir Singh 2016-07-14 23:20 ` Balbir Singh 2016-07-15 1:04 ` Rik van Riel 2016-07-15 1:04 ` Rik van Riel 2016-07-15 1:41 ` Balbir Singh 2016-07-15 1:41 ` Balbir Singh 2016-07-15 4:05 ` Kees Cook 2016-07-15 4:05 ` Kees Cook 2016-07-15 4:53 ` Kees Cook 2016-07-15 4:53 ` Kees Cook 2016-07-15 12:55 ` Balbir Singh 2016-07-15 12:55 ` Balbir Singh 2016-07-15 4:25 ` Kees Cook 2016-07-15 4:25 ` Kees Cook 2016-07-15 19:00 ` [kernel-hardening] " Daniel Micay 2016-07-15 19:00 ` Daniel Micay 2016-07-15 19:14 ` Kees Cook 2016-07-15 19:14 ` Kees Cook 2016-07-15 19:19 ` Daniel Micay 2016-07-15 19:19 ` Daniel Micay 2016-07-15 19:23 ` Kees Cook 2016-07-15 19:23 ` Kees Cook 2016-07-13 21:55 ` [PATCH v2 03/11] x86/uaccess: Enable hardened usercopy Kees Cook 2016-07-13 21:55 ` Kees Cook 2016-07-13 21:55 ` [PATCH v2 04/11] ARM: uaccess: " Kees Cook 2016-07-13 21:55 ` Kees Cook 2016-07-13 21:55 ` [PATCH v2 05/11] arm64/uaccess: " Kees Cook 2016-07-13 21:55 ` Kees Cook 2016-07-13 21:55 ` [PATCH v2 06/11] ia64/uaccess: " Kees Cook 2016-07-13 21:55 ` Kees Cook 2016-07-13 21:56 ` [PATCH v2 07/11] powerpc/uaccess: " Kees Cook 2016-07-13 21:56 ` Kees Cook 2016-07-13 21:56 ` [PATCH v2 08/11] sparc/uaccess: " Kees Cook 2016-07-13 21:56 ` Kees Cook 2016-07-13 21:56 ` [PATCH v2 09/11] s390/uaccess: " Kees Cook 2016-07-13 21:56 ` Kees Cook 2016-07-13 21:56 ` [PATCH v2 10/11] mm: SLAB hardened usercopy support Kees Cook 2016-07-13 21:56 ` Kees Cook 2016-07-13 21:56 ` [PATCH v2 11/11] mm: SLUB " Kees Cook 2016-07-13 21:56 ` Kees Cook 2016-07-14 10:07 ` [kernel-hardening] " Michael Ellerman 2016-07-14 10:07 ` Michael Ellerman 2016-07-14 10:07 ` [kernel-hardening] " Michael Ellerman 2016-07-14 10:07 ` Michael Ellerman 2016-07-15 2:05 ` Balbir Singh 2016-07-15 2:05 ` Balbir Singh 2016-07-15 4:29 ` Kees Cook 2016-07-15 4:29 ` Kees Cook
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1468446964-22213-2-git-send-email-keescook@chromium.org \ --to=keescook@chromium.org \ --cc=akpm@linux-foundation.org \ --cc=ard.biesheuvel@linaro.org \ --cc=benh@kernel.crashing.org \ --cc=bp@suse.de \ --cc=casey@schaufler-ca.com \ --cc=catalin.marinas@arm.com \ --cc=cl@linux.com \ --cc=davem@davemloft.net \ --cc=fenghua.yu@intel.com \ --cc=iamjoonsoo.kim@lge.com \ --cc=jack@suse.cz \ --cc=linux-kernel@vger.kernel.org \ --cc=linux@armlinux.org.uk \ --cc=luto@kernel.org \ --cc=minipli@googlemail.com \ --cc=mpe@ellerman.id.au \ --cc=pageexec@freemail.hu \ --cc=penberg@kernel.org \ --cc=riel@redhat.com \ --cc=rientjes@google.com \ --cc=spender@grsecurity.net \ --cc=tony.luck@intel.com \ --cc=will.deacon@arm.com \ --cc=x86@kernel.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).