usercopy: Check valid lifetime via stack depth
Subsystems (8)
PATCH v2 1/1 usercopy: Check valid lifetime via stack depth
Under CONFIG_HARDENED_USERCOPY=y, when exact stack frame boundary checking is not available (i.e. everything except x86 with FRAME_POINTER), check a stack object as being at least "current depth valid", in the sense that any object within the stack region but not between start-of-stack and current_stack_pointer should be considered unavailable (i.e. its lifetime is from a call no longer present on the stack).
Introduce ARCH_HAS_CURRENT_STACK_POINTER to track which architectures have actually implemented the common global register alias.
Additionally report usercopy bounds checking failures with an offset from current_stack_pointer, which may assist with diagnosing failures.
The LKDTM USERCOPY_STACK_FRAME_TO and USERCOPY_STACK_FRAME_FROM tests (once slightly adjusted in a separate patch) will pass again with this fixed.
---
v1: https://lore.kernel.org/all/[email protected]/
v2: adjust for only some archs having current_stack_pointer
--- arch/arm/Kconfig | 1 + arch/arm64/Kconfig | 1 + arch/powerpc/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/sh/Kconfig | 1 + arch/x86/Kconfig | 1 + mm/usercopy.c | 41 ++++++++++++++++++++++++++++++++++++++--- 7 files changed, 44 insertions(+), 3 deletions(-)
arch/arm/Kconfig +1 -0
@@ -5,6 +5,7 @@ config ARM select ARCH_32BIT_OFF_T select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE if HAVE_KRETPROBES && FRAME_POINTER && !ARM_UNWIND select ARCH_HAS_BINFMT_FLAT + select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE select ARCH_HAS_ELF_RANDOMIZE
arch/arm64/Kconfig +1 -0
@@ -18,6 +18,7 @@ config ARM64 select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2 select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE select ARCH_HAS_CACHE_LINE_SIZE + select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DMA_PREP_COHERENT
arch/powerpc/Kconfig +1 -0
@@ -108,6 +108,7 @@ config PPC select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVE select ARCH_HAS_COPY_MC if PPC64 + select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DEBUG_WX if STRICT_KERNEL_RWX
arch/s390/Kconfig +1 -0
@@ -60,6 +60,7 @@ config S390 select ARCH_ENABLE_MEMORY_HOTPLUG if SPARSEMEM select ARCH_ENABLE_MEMORY_HOTREMOVE select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2 + select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DEBUG_WX select ARCH_HAS_DEVMEM_IS_ALLOWED
arch/sh/Kconfig +1 -0
@@ -7,6 +7,7 @@ config SUPERH select ARCH_HAVE_CUSTOM_GPIO_H select ARCH_HAVE_NMI_SAFE_CMPXCHG if (GUSA_RB || CPU_SH4A) select ARCH_HAS_BINFMT_FLAT if !MMU + select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_GIGANTIC_PAGE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_PTE_SPECIAL
arch/x86/Kconfig +1 -0
@@ -69,6 +69,7 @@ config X86 select ARCH_ENABLE_THP_MIGRATION if X86_64 && TRANSPARENT_HUGEPAGE select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_CACHE_LINE_SIZE + select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL select ARCH_HAS_DEBUG_VM_PGTABLE if !X86_PAE select ARCH_HAS_DEVMEM_IS_ALLOWED
mm/usercopy.c +38 -3
65 lines not on this page · raw message
Signature · 1 line
2.30.2
From: Kees Cook
Quoted 9 lines
Sent: 24 February 2022 06:04
Under
CONFIG_HARDENED_USERCOPY=y, when exact stack frame boundary checking is not available (i.e. everything except x86 withFRAME_POINTER), check a stack object as being at least "current depth valid", in the sense that any object within the stack region but not between start-of-stack andcurrent_stack_pointershould be considered unavailable (i.e. its lifetime is from a call no longer present on the stack).
...
Quoted 7 lines
mm/usercopy.c +0
-0@@ -22,6 +22,30 @@#include <asm/sections.h> #include "slab.h"
mm/usercopy.c line 33
Quoted 10 lines
mm/usercopy.c +9
-0+/* +* Only called if obj is within stack/stackend bounds. Determine if within +* current stack depth. +*/ +static inline int check_stack_object_depth (const void *obj, +unsigned long len) +{ +#ifdef CONFIG_ARCH_HAS_CURRENT_STACK_POINTER +#ifndef CONFIG_STACK_GROWSUP
Pointless negation
Quoted 4 lines
mm/usercopy.c +4
-0+const void *const high = stackend; +const void *const low = (void *)current_stack_pointer; +#else +const void *const high = (void *)current_stack_pointer;
mm/usercopy.c line 47
Quoted 10 lines
mm/usercopy.c +10
-0+const void *const low = stack; +#endif + +/* Reject: object not within current stack depth. */ +if (obj < low || high < obj + len) +return BAD_STACK ; + +#endif +return GOOD_STACK ; +}
If the comment at the top of the function is correct then only a single test for the correct end of the buffer against the current stack pointer is needed.
Something like:
#ifdef CONFIG_STACK_GROWSUP if ((void *)current_stack_pointer < obj + len) return BAD_STACK; #else if (obj < (void *)current_stack_pointer) return BAD_STACK; #endif return GOOD_STACK;
Although it may depend on exactly where the stack pointer points to - especially for GROWSUP.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 50 lines · David Laight
From: Kees Cook
Quoted 9 lines
Sent: 24 February 2022 06:04
Under
CONFIG_HARDENED_USERCOPY=y, when exact stack frame boundary checking is not available (i.e. everything except x86 withFRAME_POINTER), check a stack object as being at least "current depth valid", in the sense that any object within the stack region but not between start-of-stack andcurrent_stack_pointershould be considered unavailable (i.e. its lifetime is from a call no longer present on the stack)....
Quoted 7 lines
mm/usercopy.c +0
-0@@ -22,6 +22,30 @@#include <asm/sections.h> #include "slab.h" mm/usercopy.c line 33
Quoted 10 lines
mm/usercopy.c +9
-0+/* +* Only called if obj is within stack/stackend bounds. Determine if within +* current stack depth. +*/ +static inline int check_stack_object_depth (const void *obj, +unsigned long len) +{ +#ifdef CONFIG_ARCH_HAS_CURRENT_STACK_POINTER +#ifndef CONFIG_STACK_GROWSUP Pointless negation
Quoted 4 lines
mm/usercopy.c +4
-0+const void *const high = stackend; +const void *const low = (void *)current_stack_pointer; +#else +const void *const high = (void *)current_stack_pointer;mm/usercopy.c line 47
Quoted 10 lines
mm/usercopy.c +10
-0+const void *const low = stack; +#endif + +/* Reject: object not within current stack depth. */ +if (obj < low || high < obj + len) +return BAD_STACK ; + +#endif +return GOOD_STACK ; +}If the comment at the top of the function is correct then only a single test for the correct end of the buffer against the current stack pointer is needed.
Something like:
Quoted 8 lines
#ifdef CONFIG_STACK_GROWSUP if ((void *)current_stack_pointer < obj + len) return BAD_STACK; #else if (obj < (void *)current_stack_pointer) return BAD_STACK; #endif return GOOD_STACK;
Oh, yeah, excellent point. I suspect the compiler would probably optimize it all away, but yes, this is, in fact, easier to read, and short enough I should probably just not bother with a separate function.
Thanks!
-Kees
Quoted 10 lines
Although it may depend on exactly where the stack pointer points to - especially for GROWSUP.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Signature · 1 line
Kees Cook