usercopy: create usercopy.h and move enum stack_type
Subsystems (5)
PATCH v2 1/3 usercopy: create usercopy.h and move enum stack_type
From: Sahara <[email protected]>
This patch creates linux/usercopy.h and moves the enum which is only used in usercopy.c so as to make it usable to x86 and other architecture's thread_info.h, which may have arch_within_stack_frames().
--- arch/x86/include/asm/thread_info.h | 20 +++++++++++--------- include/linux/usercopy.h | 11 +++++++++++ mm/usercopy.c | 8 +------- 3 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 include/linux/usercopy.h
arch/x86/include/asm/thread_info.h +11 -9
@@ -51,6 +51,7 @@struct task_struct ;#include <asm/cpufeature.h> #include <linux/atomic.h> +#include <linux/usercopy.h> struct thread_info {unsigned long flags;/* low level flags */
@@ -168,13 +169,13 @@ static inline unsigned long current_stack_pointer(void)* 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) +* GOOD_FRAME if within a frame +* BAD_STACK if placed across a frame boundary (or outside stack) +* NOT_STACK 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) +static inline enum stack_type 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 ;
@@ -197,13 +198,14 @@ static inline int arch_within_stack_frames(const void * const stack,* the copy as invalid. */ if (obj + len <= frame) -return obj >= oldframe +2 *sizeof (void *) ?1 :-1 ; +return obj >= oldframe +2 *sizeof (void *) ? +GOOD_FRAME :BAD_STACK ; oldframe = frame; frame = *(const void * const *)frame; } -return -1 ; +return BAD_STACK ;#else -return 0 ; +return NOT_STACK ;#endif }
include/linux/usercopy.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef __LINUX_USERCOPY_H +#define __LINUX_USERCOPY_H + +enum stack_type { +BAD_STACK =-1 , +NOT_STACK =0 , +GOOD_FRAME , +GOOD_STACK , +}; + +#endif
mm/usercopy.c +1 -7
@@ -16,15 +16,9 @@#include <linux/mm.h> #include <linux/slab.h> +#include <linux/usercopy.h> #include <asm/sections.h> -enum { -BAD_STACK =-1 , -NOT_STACK =0 , -GOOD_FRAME , -GOOD_STACK , -}; -/* * Checks if a given pointer and length is contained by the current * stack frame (if possible).
Signature · 1 line
2.7.4
PATCH v2 2/3 arm64: usercopy: Implement stack frame object validation
From: Sahara <[email protected]>
This implements arch_within_stack_frames() for arm64 that should validate if a given object is contained by a kernel stack frame.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/thread_info.h | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -97,6 +97,7 @@ config ARM64 select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES if HAVE_KPROBES + select HAVE_ARCH_WITHIN_STACK_FRAMES select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING
arch/arm64/include/asm/thread_info.h +65 -0
78 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 3/3 lkdtm: add tests for dynamic array in local stack
From: Sahara <[email protected]>
This seems an unusual case in kernel. But, when an array is dynamically created, this variable can be placed ahead of current frame pointer. This patch tests this dynamic array case.
--- drivers/misc/lkdtm_usercopy.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/misc/lkdtm_usercopy.c +27 -1
@@ -7,6 +7,7 @@#include <linux/vmalloc.h> #include <linux/mman.h> #include <linux/uaccess.h> +#include <linux/random.h> #include <asm/cacheflush.h> /*
@@ -33,7 +34,14 @@ static noinline unsigned char *trick_compiler(unsigned char *stack) static noinline unsigned char *do_usercopy_stack_callee(int value) { -unsigned char buf[32 ]; +/* +* buf[128] is big enough to put it in the position ahead of +* check_stack_object()'s frame pointer. Because dynamically allocated +* array can be placed between check_stack_object()'s frame pointer and +* do_usercopy_stack()'s frame pointer, we need an object which exists +* ahead of check_stack_object()'s frame pointer. +*/ +unsigned char buf[128 ];int i;/* Exercise stack to avoid everything living in registers. */
@@ -49,6 +57,7 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)unsigned long user_addr;unsigned char good_stack[32 ];unsigned char *bad_stack; +unsigned int array_size;int i;/* Exercise stack to avoid everything living in registers. */
@@ -72,7 +81,9 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)return ; } + array_size =get_random_int () &0x0f ;if (to_user) { +unsigned char array[array_size];pr_info ("attempting good copy_to_user of local stack\n" );if (copy_to_user ((void __user *)user_addr, good_stack, unconst +sizeof (good_stack))) {
@@ -80,6 +91,13 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) goto free_user; } +pr_info ("attempting good copy_to_user of callee stack\n" ); +if (copy_to_user ((void __user *)user_addr, array, + unconst +sizeof (array))) { +pr_warn ("copy_to_user failed unexpectedly?!\n" ); + goto free_user; + } +pr_info ("attempting bad copy_to_user of distant stack\n" );if (copy_to_user ((void __user *)user_addr, bad_stack, unconst +sizeof (good_stack))) {
@@ -87,6 +105,7 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) goto free_user; } }else { +unsigned char array[array_size];/* * There isn't a safe way to not be protected by usercopy * if we're going to write to another thread's stack.
@@ -101,6 +120,13 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) goto free_user; } +pr_info ("attempting good copy_from_user of callee stack\n" ); +if (copy_from_user (array, (void __user *)user_addr, + unconst +sizeof (array))) { +pr_warn ("copy_from_user failed unexpectedly?!\n" ); + goto free_user; + } +pr_info ("attempting bad copy_from_user of distant stack\n" );if (copy_from_user (bad_stack, (void __user *)user_addr, unconst +sizeof (good_stack))) {
Signature · 1 line
2.7.4
Quoted 5 lines · On Thu, Feb 2, 2017 at 4:50 AM, <[email protected]> wrote:
From: Sahara <[email protected]>
This patch creates
linux/usercopy.hand moves the enum which is only used inusercopy.cso as to make it usable to x86 and other architecture'sthread_info.h, which may havearch_within_stack_frames().
Yeah, I had meant to do this; thanks! See my nit below...
Quoted 66 lines
Signed-off-by Sahara <[email protected]>Suggested-by James Morse <[email protected]>--- arch/x86/include/asm/thread_info.h | 20 +++++++++++--------- include/linux/usercopy.h | 11 +++++++++++ mm/usercopy.c | 8 +------- 3 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 include/linux/usercopy.harch/x86/include/asm/thread_info.h +11
-9@@ -51,6 +51,7 @@struct task_struct ;#include <asm/cpufeature.h> #include <linux/atomic.h> +#include <linux/usercopy.h> struct thread_info {unsigned long flags;/* low level flags */ @@ -168,13 +169,13 @@ static inline unsigned long current_stack_pointer(void)* 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) +* GOOD_FRAME if within a frame +* BAD_STACK if placed across a frame boundary (or outside stack) +* NOT_STACK 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) +static inline enum stack_type 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 ;@@ -197,13 +198,14 @@ static inline int arch_within_stack_frames(const void * const stack,* the copy as invalid. */ if (obj + len <= frame) -return obj >= oldframe +2 *sizeof (void *) ?1 :-1 ; +return obj >= oldframe +2 *sizeof (void *) ? +GOOD_FRAME :BAD_STACK ; oldframe = frame; frame = *(const void * const *)frame; } -return -1 ; +return BAD_STACK ;#else -return 0 ; +return NOT_STACK ;#endif }include/linux/usercopy.h new file +1
-0@@ -0,0 +1,11 @@ +#ifndef __LINUX_USERCOPY_H
include/linux/usercopy.h line 11
Quoted 10 lines
include/linux/usercopy.h new file +10
-0+#define __LINUX_USERCOPY_H + +enum stack_type { +BAD_STACK =-1 , +NOT_STACK =0 , +GOOD_FRAME , +GOOD_STACK , +}; + +#endif
I think this should live with the declaration of arch_within_stack_frames in include/linux/thread_info.h instead of creating a new usercopy.h.
-Kees
Quoted 24 lines
mm/usercopy.c +1
-8@@ -16,15 +16,9 @@#include <linux/mm.h> #include <linux/slab.h> +#include <linux/usercopy.h> #include <asm/sections.h> -enum { -BAD_STACK =-1 , -NOT_STACK =0 , -GOOD_FRAME , -GOOD_STACK , -}; -/* * Checks if a given pointer and length is contained by the current * stack frame (if possible). -- 2.7.4
Signature · 2 lines
Kees Cook Pixel Security