Skip to contentSkip to conversation

arm64: usercopy: Implement stack frame object validation

PATCH In review James Morse 3 participants 13 messages Feb 16, 2017 – Apr 4, 2017

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v4 cover arm64: usercopy: Implement stack frame object validation

Hi all,

This version of Sahara's arch_within_stack_frames() series replaces the open-coded stack walker with a call to arm64's existing walker.

Patch 2 can be tested independently with this change[0].

lkdtm's use of unallocated stack regions is a separate problem, patch 3 tries to address this.

Sahara, it would be good to get your review of this!
I'm afraid I omitted your patch-3 as it stopped the lkdtm test from working, I suspect its not tricking the compiler, but I haven't investigated.

Thanks,

James

[0] Change to lkdtm to generate accesses that overlap stack frames. --------------%<--------------

drivers/misc/lkdtm_usercopy.c +2 -1
index 1dd611423d8b..fcbba3a14387 100644
@@ -57,7 +57,8 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
 
        /* This is a pointer to outside our current stack frame. */
        if (bad_frame) {
-               bad_stack = do_usercopy_stack_callee((uintptr_t)&bad_stack);
+               bad_stack = __builtin_frame_address(0);
+               bad_stack -= sizeof(good_stack)/2;
        } else {
                /* Put start address just inside stack. */
                bad_stack = task_stack_page(current) + THREAD_SIZE;

--------------%<--------------

James Morse (2):
arm64: Add arch_within_stack_frames() for hardened usercopy arm64/uaccess: Add hardened usercopy check for bad stack accesses

Sahara (1):
usercopy: create enum stack_type

 arch/arm64/Kconfig                   |  1 +
 arch/arm64/include/asm/thread_info.h |  7 ++++-
 arch/arm64/include/asm/uaccess.h     | 20 +++++++++++++
 arch/arm64/kernel/stacktrace.c       | 54 ++++++++++++++++++++++++++++++++++--
 arch/x86/include/asm/thread_info.h   | 19 +++++++------
 include/linux/thread_info.h          | 13 +++++++--
 mm/usercopy.c                        |  8 +-----
 7 files changed, 99 insertions(+), 23 deletions(-)
Signature · 1 line
2.10.1
#1 lore raw

PATCH v4 1/3 usercopy: create enum stack_type

From: Sahara <[email protected]>

This patch creates enum stack_type which is only used in usercopy.c for now. This enum type can be used for x86 and other architecture's thread_info.h, which may have arch_within_stack_frames().

---
 arch/x86/include/asm/thread_info.h | 19 ++++++++++---------
 include/linux/thread_info.h        | 13 ++++++++++---
 mm/usercopy.c                      |  8 +-------
 3 files changed, 21 insertions(+), 19 deletions(-)
arch/x86/include/asm/thread_info.h +10 -9
index ad6f5eb07a95..7af4b8b23b8b 100644
@@ -168,13 +168,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 +197,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/thread_info.h +10 -3
index 58373875e8ee..a38b3be347df 100644
@@ -22,6 +22,13 @@
 #endif
 
 #include <linux/bitops.h>
+
+enum stack_type {
+	BAD_STACK = -1,
+	NOT_STACK = 0,
+	GOOD_FRAME,
+	GOOD_STACK,
+};
 #include <asm/thread_info.h>
 
 #ifdef __KERNEL__
@@ -77,9 +84,9 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
 #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
 
 #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)
+static inline enum stack_type arch_within_stack_frames(const void * const stack,
+					const void * const stackend,
+					const void *obj, unsigned long len)
 {
 	return 0;
 }
mm/usercopy.c +1 -7
index 3c8da0af9695..3531ae735d87 100644
@@ -16,15 +16,9 @@
 
 #include <linux/mm.h>
 #include <linux/slab.h>
+#include <linux/thread_info.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.10.1
#2 lore raw

[kernel-hardening] Re: [PATCH v4 1/3] usercopy: create enum stack_type

Quoted 10 lines · James Morse

From: Sahara <[email protected]>

This patch creates enum stack_type which is only used in usercopy.c for now. This enum type can be used for x86 and other architecture's thread_info.h, which may have arch_within_stack_frames().

I've adjusted this slightly and added it to my usercopy tree for -next.

-Kees

Quoted 103 lines
---
 arch/x86/include/asm/thread_info.h | 19 ++++++++++---------
 include/linux/thread_info.h        | 13 ++++++++++---
 mm/usercopy.c                      |  8 +-------
 3 files changed, 21 insertions(+), 19 deletions(-)
arch/x86/include/asm/thread_info.h +10 -9
index ad6f5eb07a95..7af4b8b23b8b 100644
@@ -168,13 +168,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 +197,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/thread_info.h +10 -3
index 58373875e8ee..a38b3be347df 100644
@@ -22,6 +22,13 @@
 #endif
 
 #include <linux/bitops.h>
+
+enum stack_type {
+       BAD_STACK = -1,
+       NOT_STACK = 0,
+       GOOD_FRAME,
+       GOOD_STACK,
+};
 #include <asm/thread_info.h>
 
 #ifdef __KERNEL__
@@ -77,9 +84,9 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
 #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
 
 #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)
+static inline enum stack_type arch_within_stack_frames(const void * const stack,
+                                       const void * const stackend,
+                                       const void *obj, unsigned long len)
 {
        return 0;
 }
mm/usercopy.c +1 -8
index 3c8da0af9695..3531ae735d87 100644
@@ -16,15 +16,9 @@
 
 #include <linux/mm.h>
 #include <linux/slab.h>
+#include <linux/thread_info.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.10.1

Signature · 2 lines
Kees Cook
Pixel Security
#3 lore raw

PATCH v4 2/3 arm64: Add arch_within_stack_frames() for hardened usercopy

In reply to James Morse

Hardened usercopy tests that an object being copied to/from userspace doesn't overlap multiple stack frames.

Add arch_within_stack_frames() to do this using arm64's stackwalker. The callback looks for 'fp' appearing with the range occupied by the object.

(This isn't enough to trip the lkdtm tests on arm64)

CC: Sahara <[email protected]>
Based-on-a-patch-from: Sahara <[email protected]>

---
 arch/arm64/Kconfig                   |  1 +
 arch/arm64/include/asm/thread_info.h |  7 ++++-
 arch/arm64/kernel/stacktrace.c       | 54 ++++++++++++++++++++++++++++++++++--
 3 files changed, 58 insertions(+), 4 deletions(-)
arch/arm64/Kconfig +1 -0
index 111742126897..378caa9c0563 100644
@@ -67,6 +67,7 @@ config ARM64
 	select HAVE_ARCH_TRACEHOOK
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE
 	select HAVE_ARM_SMCCC
+	select HAVE_ARCH_WITHIN_STACK_FRAMES
 	select HAVE_EBPF_JIT
 	select HAVE_C_RECORDMCOUNT
 	select HAVE_CC_STACKPROTECTOR
arch/arm64/include/asm/thread_info.h +6 -1
index 46c3b93cf865..3540c46027fc 100644
@@ -68,7 +68,12 @@ struct thread_info {
 #define thread_saved_fp(tsk)	\
 	((unsigned long)(tsk->thread.cpu_context.fp))
 
-#endif
+
+extern enum stack_type arch_within_stack_frames(const void * const stack,
+						const void * const stackend,
+						const void *obj,
+						unsigned long len);
+#endif /* !__ASSEMBLY__ */
 
 /*
  * thread information flags:
arch/arm64/kernel/stacktrace.c +51 -3

69 lines not on this page · raw message

Signature · 1 line
2.10.1
#4 lore raw

[kernel-hardening] Re: [PATCH v4 2/3] arm64: Add arch_within_stack_frames() for hardened usercopy

Quoted 34 lines · James Morse

Hardened usercopy tests that an object being copied to/from userspace doesn't overlap multiple stack frames.

Add arch_within_stack_frames() to do this using arm64's stackwalker. The callback looks for 'fp' appearing with the range occupied by the object.

(This isn't enough to trip the lkdtm tests on arm64)

CC: Sahara <[email protected]>
Based-on-a-patch-from: Sahara <[email protected]>

---
 arch/arm64/Kconfig                   |  1 +
 arch/arm64/include/asm/thread_info.h |  7 ++++-
 arch/arm64/kernel/stacktrace.c       | 54 ++++++++++++++++++++++++++++++++++--
 3 files changed, 58 insertions(+), 4 deletions(-)
arch/arm64/Kconfig +1 -0
index 111742126897..378caa9c0563 100644
@@ -67,6 +67,7 @@ config ARM64
        select HAVE_ARCH_TRACEHOOK
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE
        select HAVE_ARM_SMCCC
+       select HAVE_ARCH_WITHIN_STACK_FRAMES
        select HAVE_EBPF_JIT
        select HAVE_C_RECORDMCOUNT
        select HAVE_CC_STACKPROTECTOR
arch/arm64/include/asm/thread_info.h +0 -0
index 46c3b93cf865..3540c46027fc 100644

arch/arm64/include/asm/thread_info.h struct thread_info { · line 75

Quoted 9 lines
arch/arm64/include/asm/thread_info.h +5 -1
@@ -68,7 +68,12 @@ struct thread_info {
 #define thread_saved_fp(tsk)   \
        ((unsigned long)(tsk->thread.cpu_context.fp))
 
-#endif
+
+extern enum stack_type arch_within_stack_frames(const void * const stack,
+                                               const void * const stackend,
+                                               const void *obj,
+                                               unsigned long len);

The caller of arch_within_stack_frames expects this to be inlined... could that be changed and then move the special stack check from the third patch into check_stack_object() directly?

Regardless, I'm fine with reusing the existing walker. I just want to avoid special cases in the uaccess code (so we can consolidate it in the future).

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw

PATCH v4 3/3 arm64/uaccess: Add hardened usercopy check for bad stack accesses

In reply to James Morse

lkdtm tests copy_{to,from}_user() by trying to copy an address range on the stack that isn't yet part of a stack frame.

By the time the stack walker is invoked to check that the object being copied doesn't overlap stack frame, the invalid range is part of a valid stack frame. Discarding a constant number of frames is fragile as different compiler versions may make different inline choices.

Instead, add a check that the object isn't between the current stack pointer and the end of the stack. Add this early enough that it should be inlined into the caller.

---
 arch/arm64/include/asm/uaccess.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm64/include/asm/uaccess.h +20 -0
index 46da3ea638bb..d3494840a61c 100644
@@ -356,6 +356,22 @@ do {									\
 		-EFAULT;						\
 })
 
+static inline void check_obj_in_unused_stack(const void *obj, unsigned long len)
+{
+	unsigned long stack = (unsigned long)task_stack_page(current);
+
+	if (__builtin_constant_p(len) || !IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len)
+		return;
+
+	/*
+	 * If current_stack_pointer is on the task stack, obj must not lie
+	 * between current_stack_pointer and the last stack address.
+	 */
+	if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack)
+		BUG_ON(stack <= (unsigned long)obj &&
+		       (unsigned long)obj < current_stack_pointer);
+}
+
 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
@@ -364,6 +380,7 @@ extern unsigned long __must_check __clear_user(void __user *addr, unsigned long
 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
 {
 	kasan_check_write(to, n);
+	check_obj_in_unused_stack(to, n);
 	check_object_size(to, n, false);
 	return __arch_copy_from_user(to, from, n);
 }
@@ -371,6 +388,7 @@ static inline unsigned long __must_check __copy_from_user(void *to, const void _
 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
 {
 	kasan_check_read(from, n);
+	check_obj_in_unused_stack(from, n);
 	check_object_size(from, n, true);
 	return __arch_copy_to_user(to, from, n);
 }
@@ -381,6 +399,7 @@ static inline unsigned long __must_check copy_from_user(void *to, const void __u
 	kasan_check_write(to, n);
 
 	if (access_ok(VERIFY_READ, from, n)) {
+		check_obj_in_unused_stack(to, n);
 		check_object_size(to, n, false);
 		res = __arch_copy_from_user(to, from, n);
 	}
@@ -394,6 +413,7 @@ static inline unsigned long __must_check copy_to_user(void __user *to, const voi
 	kasan_check_read(from, n);
 
 	if (access_ok(VERIFY_WRITE, to, n)) {
+		check_obj_in_unused_stack(from, n);
 		check_object_size(from, n, true);
 		n = __arch_copy_to_user(to, from, n);
 	}
Signature · 1 line
2.10.1
#6 lore raw

[kernel-hardening] Re: [PATCH v4 3/3] arm64/uaccess: Add hardened usercopy check for bad stack accesses

Quoted 31 lines · James Morse

lkdtm tests copy_{to,from}_user() by trying to copy an address range on the stack that isn't yet part of a stack frame.

By the time the stack walker is invoked to check that the object being copied doesn't overlap stack frame, the invalid range is part of a valid stack frame. Discarding a constant number of frames is fragile as different compiler versions may make different inline choices.

Instead, add a check that the object isn't between the current stack pointer and the end of the stack. Add this early enough that it should be inlined into the caller.

---
 arch/arm64/include/asm/uaccess.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm64/include/asm/uaccess.h +5 -0
index 46da3ea638bb..d3494840a61c 100644
@@ -356,6 +356,22 @@ do {                                                                       \
                -EFAULT;                                                \
 })
 
+static inline void check_obj_in_unused_stack(const void *obj, unsigned long len)
+{
+       unsigned long stack = (unsigned long)task_stack_page(current);
+
+       if (__builtin_constant_p(len) || !IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len)

arch/arm64/include/asm/uaccess.h do { \ · line 373

Quoted 10 lines
arch/arm64/include/asm/uaccess.h +10 -0
+               return;
+
+       /*
+        * If current_stack_pointer is on the task stack, obj must not lie
+        * between current_stack_pointer and the last stack address.
+        */
+       if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack)
+               BUG_ON(stack <= (unsigned long)obj &&
+                      (unsigned long)obj < current_stack_pointer);
+}

It seems like this would be a valid test on all architecture, yes? I wonder if this could be reworked so we don't have a special case for ARM here...

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#7 lore raw

Re: [kernel-hardening] [PATCH v4 3/3] arm64/uaccess: Add hardened usercopy check for bad stack accesses

In reply to James Morse

Hello James,

Quoted 14 lines
arch/arm64/include/asm/uaccess.h +6 -0
index 46da3ea638bb..d3494840a61c 100644
@@ -356,6 +356,22 @@ do {                                                                       \
                -EFAULT;                                                \
 })
 
+static inline void check_obj_in_unused_stack(const void *obj, unsigned long len)
+{
+       unsigned long stack = (unsigned long)task_stack_page(current);
+
+       if (__builtin_constant_p(len) || !IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len)
+               return;

arch/arm64/include/asm/uaccess.h do { \ · line 374

Quoted 10 lines
arch/arm64/include/asm/uaccess.h +10 -0
+
+       /*
+        * If current_stack_pointer is on the task stack, obj must not lie
+        * between current_stack_pointer and the last stack address.
+        */
+       if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack)
+               BUG_ON(stack <= (unsigned long)obj &&
+                      (unsigned long)obj < current_stack_pointer);
+}
+

It looks to me that this function is just doing the similar check that check_stack_object() may do.
Probably I guess you had a problem in checking the correct fp of caller's function while you tried to use walk_stackframe(). Anyway, your patch works fine for me.
But, I can not find any reason to create check_obj_in_unused_stack(). Instead of creating check_obj_in_unused_stack(), how about handing over current_stack_pointer to check_stack_object();
Even though Akashi's case happens, since we hand over correct stack pointer, it can check if the obj is within the correct stack boundary.

arch/arm64/include/asm/uaccess.h +0 -1
index d349484..0c21a0d 100644
@@ -356,22 +356,6 @@ do {
                         \
                -EFAULT;                                                \
 })
 
-static inline void check_obj_in_unused_stack(const void *obj,

unsigned long len)
-{
- unsigned long stack = (unsigned long)task_stack_page(current);
-

-       if (__builtin_constant_p(len) ||
!IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len)
-               return;
-
-       /*
-        * If current_stack_pointer is on the task stack, obj must not lie
-        * between current_stack_pointer and the last stack address.
-        */

- if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack)
- BUG_ON(stack <= (unsigned long)obj &&
- (unsigned long)obj < current_stack_pointer);
-}
-
extern unsigned long __must_check __arch_copy_from_user(void *to,
const void __user *from, unsigned long n);
extern unsigned long __must_check __arch_copy_to_user(void __user
*to, const void *from, unsigned long n);
extern unsigned long __must_check __copy_in_user(void __user *to,
const void __user *from, unsigned long n);
@@ -380,16 +364,14 @@ extern unsigned long __must_check __clear_user(void __user *addr, unsigned long
static inline unsigned long __must_check __copy_from_user(void *to,
const void __user *from, unsigned long n)

 {
        kasan_check_write(to, n);
-       check_obj_in_unused_stack(to, n);
-       check_object_size(to, n, false);
+       check_object_size(to, n, current_stack_pointer, false);
        return __arch_copy_from_user(to, from, n);

}

static inline unsigned long __must_check __copy_to_user(void __user
*to, const void *from, unsigned long n)

 {
        kasan_check_read(from, n);
-       check_obj_in_unused_stack(from, n);
-       check_object_size(from, n, true);
+       check_object_size(from, n, current_stack_pointer, true);
        return __arch_copy_to_user(to, from, n);

}

@@ -399,8 +381,7 @@ static inline unsigned long __must_check copy_from_user(void *to, const void __u

        kasan_check_write(to, n);

        if (access_ok(VERIFY_READ, from, n)) {
-               check_obj_in_unused_stack(to, n);
-               check_object_size(to, n, false);
+               check_object_size(to, n, current_stack_pointer, false);
                res = __arch_copy_from_user(to, from, n);
        }
        if (unlikely(res))
@@ -413,8 +394,7 @@ static inline unsigned long __must_check
copy_to_user(void __user *to, const voi
        kasan_check_read(from, n);

        if (access_ok(VERIFY_WRITE, to, n)) {
-               check_obj_in_unused_stack(from, n);
-               check_object_size(from, n, true);
+               check_object_size(from, n, current_stack_pointer, true);
                n = __arch_copy_to_user(to, from, n);
        }
        return n;
include/linux/thread_info.h +0 -0
index a38b3be..5c676bc 100644
@@ -94,17 +94,17 @@ static inline enum stack_type

arch_within_stack_frames(const void * const stack,

#ifdef CONFIG_HARDENED_USERCOPY
extern void __check_object_size(const void *ptr, unsigned long n,
- bool to_user);
+ unsigned long sp, bool to_user);

static __always_inline void check_object_size(const void *ptr, unsigned long n,
- bool to_user)
+ unsigned long sp, bool to_user)

 {
        if (!__builtin_constant_p(n))

- __check_object_size(ptr, n, to_user);
+ __check_object_size(ptr, n, sp, to_user);
}
#else
static inline void check_object_size(const void *ptr, unsigned long n,
- bool to_user)
+ unsigned long sp, bool to_user)
{ }
#endif /* CONFIG_HARDENED_USERCOPY */

lib/strncpy_from_user.c +0 -0
index 7e35fc4..4118da4 100644
@@ -112,7 +112,7 @@ long strncpy_from_user(char *dst, const char

__user *src, long count)

                long retval;

                kasan_check_write(dst, count);
-               check_object_size(dst, count, false);
+               check_object_size(dst, count, current_stack_pointer, false);
                user_access_begin();
                retval = do_strncpy_from_user(dst, src, count, max);
                user_access_end();
mm/usercopy.c +2 -1
index 3531ae7..3739022 100644
@@ -29,7 +29,8 @@
  *     GOOD_STACK: fully on the stack (when can't do frame-checking)
  *     BAD_STACK: error condition (invalid stack position or bad stack frame)
  */
-static noinline int check_stack_object(const void *obj, unsigned long len)
+static noinline int check_stack_object(const void *obj, unsigned long len,
+                                      unsigned long usercopy_sp)
 {
        const void * const stack = task_stack_page(current);
        const void * const stackend = stack + THREAD_SIZE;
@@ -44,7 +45,7 @@ static noinline int check_stack_object(const void

*obj, unsigned long len)
* the check above means at least one end is within the stack,
* so if this check fails, the other end is outside the stack). */
- if (obj < stack || stackend < obj + len)
+ if (obj < usercopy_sp || stackend < obj + len)

                return BAD_STACK;

        /* Check if object is safely within a valid frame. */

@@ -227,7 +229,8 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n,
* - known-safe heap or stack object
* - not in kernel text
*/
-void __check_object_size(const void *ptr, unsigned long n, bool to_user) +void __check_object_size(const void *ptr, unsigned long n,
+ unsigned long sp, bool to_user)

 {
        const char *err;

@@ -246,7 +249,7 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)

                goto report;

        /* Check for bad stack object. */
-       switch (check_stack_object(ptr, n)) {
+       switch (check_stack_object(ptr, n, sp)) {
        case NOT_STACK:
                /* Object is not touching the current process stack. */
                break;

Thanks so much for your patches.

BR
Sahara

#8 lore raw

Re: [kernel-hardening] [PATCH v4 3/3] arm64/uaccess: Add hardened usercopy check for bad stack accesses

Hi,

Quoted 19 lines · Keun-O Park
Quoted 19 lines
arch/arm64/include/asm/uaccess.h +13 -0
index 46da3ea638bb..d3494840a61c 100644
@@ -356,6 +356,22 @@ do {                                                                       \
 
+static inline void check_obj_in_unused_stack(const void *obj, unsigned long len)
+{
+       unsigned long stack = (unsigned long)task_stack_page(current);
+
+       if (__builtin_constant_p(len) || !IS_ENABLED(CONFIG_HARDENED_USERCOPY) || !len)
+               return;
+
+       /*
+        * If current_stack_pointer is on the task stack, obj must not lie
+        * between current_stack_pointer and the last stack address.
+        */
+       if ((current_stack_pointer & ~(THREAD_SIZE-1)) == stack)
+               BUG_ON(stack <= (unsigned long)obj &&
Quoted 8 lines

arch/arm64/include/asm/uaccess.h do { \ · line 372

Quoted 3 lines
arch/arm64/include/asm/uaccess.h +3 -0
+                      (unsigned long)obj < current_stack_pointer);
+}
+

It looks to me that this function is just doing the similar check that check_stack_object() may do.
Probably I guess you had a problem in checking the correct fp of caller's function while you tried to use walk_stackframe().

The value needs to be taken in a hopefully-inlined function if you want to catch LKDTMs 'just off the stack' writes. Doing it like this saved meddling with the generic code.

Quoted 2 lines

Instead of creating check_obj_in_unused_stack(), how about handing over current_stack_pointer to check_stack_object();

Sure, do you want to give this a go? (I have my hands full at the moment)

It might not be the right check on all architectures, x86 redzone comes to mind, but it doesn't look like they use this in the kernel.

There is also Al Viro's uaccess unification work that may affect whether this is worth doing now:
https://lkml.org/lkml/2015/12/3/494

Thanks,

James

#9 lore raw
In reply to James Morse
Quoted 26 lines · James Morse

Hi all,

This version of Sahara's arch_within_stack_frames() series replaces the open-coded stack walker with a call to arm64's existing walker.

Patch 2 can be tested independently with this change[0].

lkdtm's use of unallocated stack regions is a separate problem, patch 3 tries to address this.

Sahara, it would be good to get your review of this!
I'm afraid I omitted your patch-3 as it stopped the lkdtm test from working, I suspect its not tricking the compiler, but I haven't investigated.

Thanks,

James

[0] Change to lkdtm to generate accesses that overlap stack frames. --------------%<--------------

drivers/misc/lkdtm_usercopy.c +0 -0
index 1dd611423d8b..fcbba3a14387 100644

drivers/misc/lkdtm_usercopy.c static noinline void do_usercopy_stack(bool to_user, bool bad_frame) · line 61

Quoted 6 lines
drivers/misc/lkdtm_usercopy.c +2 -1
@@ -57,7 +57,8 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
 
        /* This is a pointer to outside our current stack frame. */
        if (bad_frame) {
-               bad_stack = do_usercopy_stack_callee((uintptr_t)&bad_stack);
+               bad_stack = __builtin_frame_address(0);
+               bad_stack -= sizeof(good_stack)/2;

Ah, sneaky, yeah, that'll work nicely.

(Though it should likely get wrapped in a CONFIG_STACK_GROWSUP/DOWN test...)

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#10 lore raw
Quoted 36 lines · Kees Cook
Quoted 26 lines · James Morse

Hi all,

This version of Sahara's arch_within_stack_frames() series replaces the open-coded stack walker with a call to arm64's existing walker.

Patch 2 can be tested independently with this change[0].

lkdtm's use of unallocated stack regions is a separate problem, patch 3 tries to address this.

Sahara, it would be good to get your review of this!
I'm afraid I omitted your patch-3 as it stopped the lkdtm test from working, I suspect its not tricking the compiler, but I haven't investigated.

Thanks,

James

[0] Change to lkdtm to generate accesses that overlap stack frames. --------------%<--------------

drivers/misc/lkdtm_usercopy.c +0 -0
index 1dd611423d8b..fcbba3a14387 100644

drivers/misc/lkdtm_usercopy.c static noinline void do_usercopy_stack(bool to_user, bool bad_frame) · line 61

Quoted 6 lines
drivers/misc/lkdtm_usercopy.c +2 -1
@@ -57,7 +57,8 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
 
        /* This is a pointer to outside our current stack frame. */
        if (bad_frame) {
-               bad_stack = do_usercopy_stack_callee((uintptr_t)&bad_stack);
+               bad_stack = __builtin_frame_address(0);
+               bad_stack -= sizeof(good_stack)/2;

Ah, sneaky, yeah, that'll work nicely.

Quoted 1 line

(Though it should likely get wrapped in a CONFIG_STACK_GROWSUP/DOWN test...)

Is this still in progress? Seemed like it was very close?

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#11 lore raw

Hi Kees,

Quoted 6 lines · Kees Cook
Quoted 3 lines · Kees Cook
Quoted 2 lines · James Morse

This version of Sahara's arch_within_stack_frames() series replaces the open-coded stack walker with a call to arm64's existing walker.

Is this still in progress? Seemed like it was very close?

Ah, sorry, I lost track of this when it jumped between mail folders... Sahara had comments on the last patch.

How does all this fit with Al Viro's uaccess unification tree?:
https://lkml.org/lkml/2017/3/29/61

Thanks,

James

#12 lore raw
Quoted 8 lines · James Morse

Hi Kees,

Quoted 4 lines · Kees Cook
Quoted 3 lines · Kees Cook
Quoted 2 lines · James Morse

This version of Sahara's arch_within_stack_frames() series replaces the open-coded stack walker with a call to arm64's existing walker.

Quoted 7 lines
Quoted 1 line

Is this still in progress? Seemed like it was very close?

Ah, sorry, I lost track of this when it jumped between mail folders... Sahara had comments on the last patch.

How does all this fit with Al Viro's uaccess unification tree?:
https://lkml.org/lkml/2017/3/29/61

It's orthogonal, though it results in bringing the hardened usercopy to more architectures... (but the stack walker is still needed on a per-arch basis).

-Kees

Signature · 2 lines
Kees Cook
Pixel Security