usercopy: Check valid lifetime via stack depth
Subsystems (9)
PATCH v3 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/lkml/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]
v3:
- simplify bounds check (David)
- add missed Kconfig declaration
--- 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/Kconfig | 9 +++++++++ mm/usercopy.c | 23 +++++++++++++++++++++-- 8 files changed, 36 insertions(+), 2 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/Kconfig +9 -0
@@ -744,6 +744,15 @@ config IDLE_PAGE_TRACKING config ARCH_HAS_CACHE_LINE_SIZE bool +config ARCH_HAS_CURRENT_STACK_POINTER + bool + help + In support of HARDENED_USERCOPY performing stack variable lifetime + checking, an architecture-agnostic way to find the stack pointer + is needed. Once an architecture defines an unsigned long global + register alias named "current_stack_pointer", this config can be + selected. + config ARCH_HAS_PTE_DEVMAP bool
mm/usercopy.c +21 -2
@@ -29,7 +29,7 @@* Returns: * NOT_STACK: not at all on the stack * GOOD_FRAME: fully within a valid stack frame -* GOOD_STACK: fully on the stack (when can't do frame-checking) +* GOOD_STACK: within the current stack (when can't frame-check exactly) * BAD_STACK: error condition (invalid stack position or bad stack frame) */ static noinline int check_stack_object(const void *obj, unsigned long len)
@@ -55,6 +55,17 @@ static noinline int check_stack_object(const void *obj, unsigned long len)if (ret)return ret; +/* Finally, check stack depth if possible. */ +#ifdef CONFIG_ARCH_HAS_CURRENT_STACK_POINTER +if (IS_ENABLED (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 ; }
@@ -280,7 +291,15 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)*/ return ;default : -usercopy_abort ("process stack" ,NULL , to_user,0 , n); +usercopy_abort ("process stack" ,NULL , to_user, +#ifdef CONFIG_ARCH_HAS_CURRENT_STACK_POINTER +IS_ENABLED (CONFIG_STACK_GROWSUP ) ? + ptr - (void *)current_stack_pointer : + (void *)current_stack_pointer - ptr, +#else +0 , +#endif + n); }/* Check for bad heap object. */
Signature · 1 line
2.30.2
Andrew Morton
maintainerQuoted 10 lines · On Fri, 25 Feb 2022 09:33:45 -0800 Kees Cook <[email protected]> wrote:
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).Introduce
ARCH_HAS_CURRENT_STACK_POINTERto track which architectures have actually implemented the common global register alias.
Quoted 6 lines
Additionally report usercopy bounds checking failures with an offset from
current_stack_pointer, which may assist with diagnosing failures.The LKDTM
USERCOPY_STACK_FRAME_TOandUSERCOPY_STACK_FRAME_FROMtests (once slightly adjusted in a separate patch) will pass again with this fixed.
Again, what does this actually do?
Quoted 1 line
Reported-by Muhammad Usama Anjum <[email protected]>
A link to that report would shed some light. But actually describing the user-visible impact right there in the changelog is preferable.
It sounds like a selftest is newly failing, which makes it a userspace-visible regression, perhaps?
If so, do we have a Fixes: and is a cc:stable warranted?
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Fri, 25 Feb 2022 09:33:45 -0800 Kees Cook <[email protected]> wrote:
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).Introduce
ARCH_HAS_CURRENT_STACK_POINTERto track which architectures have actually implemented the common global register alias.
Quoted 8 lines
Quoted 6 lines
Additionally report usercopy bounds checking failures with an offset from
current_stack_pointer, which may assist with diagnosing failures.The LKDTM
USERCOPY_STACK_FRAME_TOandUSERCOPY_STACK_FRAME_FROMtests (once slightly adjusted in a separate patch) will pass again with this fixed.Again, what does this actually do?
One of the things that CONFIG_HARDENED_USERCOPY checks is whether an object is overlapping the stack at all. If it is, it performs a number of inexpensive bounds checks. One of the finer-grained checks is whether an object cross stack frame within the stack region. Doing this with CONFIG_FRAME_POINTER was cheap/easy. Doing it with ORC is too heavy, and was left out (a while ago), leaving the courser whole-stack check.
The LKDTM tests try to exercise the cross-frame cases to validate the defense. They have been failing every since (which was expected). More below...
Quoted 5 lines
Quoted 2 lines
Reported-by Muhammad Usama Anjum <[email protected]>A link to that report would shed some light. But actually describing the user-visible impact right there in the changelog is preferable.
Yes, good point. The bug[1] involves multiple LKDTM tests and their failure modes, so it wasn't a very clean pointer. But I will include it.
[1] https://github.com/kernelci/kernelci-project/issues/84
Quoted 2 lines
It sounds like a selftest is newly failing, which makes it a userspace-visible regression, perhaps?
No, it's been failing since ORC was introduced, but the regression in coverage (due to switching from FRAME_POINTER to ORC unwinder) was minimal. While discussing this with Muhammad, I realized we did, actually, have something that could be tested that was less than "the entire stack area" and "each specific frame", and that was current stack depth, so we gain back a little coverage.
Quoted 1 line
If so, do we have a Fixes: and is a cc:stable warranted?
I don't think it's warranted; it is technically a new feature.
-Kees
Signature · 1 line
Kees Cook
Andrew Morton
maintainerQuoted 16 lines · On Fri, 25 Feb 2022 17:35:49 -0800 Kees Cook <[email protected]> wrote:
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Fri, 25 Feb 2022 09:33:45 -0800 Kees Cook <[email protected]> wrote:
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).Introduce
ARCH_HAS_CURRENT_STACK_POINTERto 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.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
The LKDTM
USERCOPY_STACK_FRAME_TOandUSERCOPY_STACK_FRAME_FROMtests (once slightly adjusted in a separate patch) will pass again with this fixed.Again, what does this actually do?
[answers]
OK, thanks. I think a new changelog is warranted?
What's your preferred path for upstreaming this change?
Quoted 22 lines · Andrew Morton
Quoted 20 lines · On Fri, 25 Feb 2022 17:35:49 -0800 Kees Cook <[email protected]> wrote:
Quoted 19 lines · Andrew Morton
Quoted 16 lines · On Fri, 25 Feb 2022 09:33:45 -0800 Kees Cook <[email protected]> wrote:
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).Introduce
ARCH_HAS_CURRENT_STACK_POINTERto 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_TOandUSERCOPY_STACK_FRAME_FROMtests (once slightly adjusted in a separate patch) will pass again with this fixed.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Again, what does this actually do?
[answers]
OK, thanks. I think a new changelog is warranted?
Yup, I've cut/pasted most of that into the new changelog:
usercopy: Check valid lifetime via stack depth
One of the things that CONFIG_HARDENED_USERCOPY sanity-checks is whether an object that is about to be copied to/from userspace is overlapping the stack at all. If it is, it performs a number of inexpensive bounds checks. One of the finer-grained checks is whether an object crosses stack frames within the stack region. Doing this on x86 with CONFIG_FRAME_POINTER was cheap/easy. Doing it with ORC was deemed too heavy, and was left out (a while ago), leaving the courser whole-stack check.
The LKDTM tests USERCOPY_STACK_FRAME_TO and USERCOPY_STACK_FRAME_FROM try to exercise these cross-frame cases to validate the defense is working. They have been failing ever since ORC was added (which was expected). While Muhammad was investigating various LKDTM failures[1], he asked me for additional details on them, and I realized that when exact stack frame boundary checking is not available (i.e. everything except x86 with FRAME_POINTER), it could check if a stack object is 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) pass again with this fixed.
[1] https://github.com/kernelci/kernelci-project/issues/84
---
v1: https://lore.kernel.org/lkml/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/lkml/[email protected]
v4: - improve commit log (akpm)
Quoted 1 line
What's your preferred path for upstreaming this change?
I figured I would take it via my for-next/hardening tree; I have 2 arch changes ready to go (Acked by maintainers) there too (to add current_stack_pointer).
Thanks for the review!
Signature · 1 line
Kees Cook