mm: Hardened usercopy
Subsystems (13)
v3 cover mm: Hardened usercopy
Hi,
[I'm going to carry this series in my kspp -next tree now, though I'd really love to have some explicit Acked-bys or Reviewed-bys. If you've looked through it or tested it, please consider it. :) (I added Valdis and mpe's Tested-bys where they seemed correct, thank you!)]
This is a start of the mainline port of PAX_USERCOPY[1]. After I started writing tests (now in lkdtm in -next) for Casey's earlier port[2], I kept tweaking things further and further until I ended up with a whole new patch series. To that end, I took Rik and other people's feedback along with other changes and clean-ups.
Based on my understanding, PAX_USERCOPY was designed to catch a few classes of flaws (mainly bad bounds checking) around the use of copy_to_user()/copy_from_user(). These changes don't touch get_user() and put_user(), since these operate on constant sized lengths, and tend to be much less vulnerable. There are effectively three distinct protections in the whole series, each of which I've given a separate CONFIG, though this patch set is only the first of the three intended protections. (Generally speaking, PAX_USERCOPY covers what I'm calling CONFIG_HARDENED_USERCOPY (this) and CONFIG_HARDENED_USERCOPY_WHITELIST (future), and PAX_USERCOPY_SLABS covers CONFIG_HARDENED_USERCOPY_SPLIT_KMALLOC (future).)
This series, which adds CONFIG_HARDENED_USERCOPY, checks that objects being copied to/from userspace meet certain criteria:
- if address is a heap object, the size must not exceed the object's allocated size. (This will catch all kinds of heap overflow flaws.)
- if address range is in the current process stack, it must be within the current stack frame (if such checking is possible) or at least entirely within the current process's stack. (This could catch large lengths that would have extended beyond the current process stack, or overflows if their length extends back into the original stack.)
- if the address range is part of kernel data, rodata, or bss, allow it.
- if address range is page-allocated, that it doesn't span multiple allocations.
- if address is within the kernel text, reject it.
- everything else is accepted
The patches in the series are:
- Support for arch-specific stack frame checking (which will likely be replaced in the future by Josh's more comprehensive unwinder):
1- mm: Implement stack frame object validation
- The core copy_to/from_user() checks, without the slab object checks:
2- mm: Hardened usercopy
- Per-arch enablement of the protection:
3- x86/uaccess: Enable hardened usercopy
4- ARM: uaccess: Enable hardened usercopy
5- arm64/uaccess: Enable hardened usercopy
6- ia64/uaccess: Enable hardened usercopy
7- powerpc/uaccess: Enable hardened usercopy
8- sparc/uaccess: Enable hardened usercopy
9- s390/uaccess: Enable hardened usercopy
- The heap allocator implementation of object size checking:
10- mm: SLAB hardened usercopy support
11- mm: SLUB hardened usercopy support
Some notes:
- This is expected to apply on top of -next which contains fixes for the position of _etext on both arm and arm64, though it has minor conflicts with KASAN that are trivial to fix up. Living in -next are also tests for this protection in lkdtm, prefixed with USERCOPY_.
- I couldn't detect a measurable performance change with these features enabled. Kernel build times were unchanged, hackbench was unchanged, etc. I think we could flip this to "on by default" at some point, but for now, I'm leaving it off until I can get some more definitive measurements. I would love if someone with greater familiarity with perf could give this a spin and report results.
- The SLOB support extracted from grsecurity seems entirely broken. I have no idea what's going on there, I spent my time testing SLAB and SLUB. Having someone else look at SLOB would be nice, but this series doesn't depend on it.
Additional features that would be nice, but aren't blocking this series:
- Needs more architecture support for stack frame checking (only x86 now, but it seems Josh will have a good solution for this soon).
Thanks!
-Kees
[1] https://grsecurity.net/download.php "grsecurity - test kernel patch"
[2] http://www.openwall.com/lists/kernel-hardening/2016/05/19/5
v3:
- switch to using BUG for better Oops integration
- when checking page allocations, check each for Reserved
- use enums for the stack check return for readability
v2:
- added s390 support
- handle slub red zone
- disallow writes to rodata area
- stack frame walker now CONFIG-controlled arch-specific helper
PATCH v3 1/11 mm: Implement stack frame object validation
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.
--- 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(+)
arch/Kconfig +9 -0
@@ -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
arch/x86/Kconfig +1 -0
@@ -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
arch/x86/include/asm/thread_info.h +44 -0
50 lines not on this page · raw message
include/linux/thread_info.h +9 -0
@@ -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 */
Signature · 1 line
2.7.4
PATCH v3 2/11 mm: Hardened usercopy
In reply to Kees CookThis is the start of porting PAX_USERCOPY into the mainline kernel. This is the first set of features, controlled by CONFIG_HARDENED_USERCOPY. The work is based on code by PaX Team and Brad Spengler, and an earlier port from Casey Schaufler. Additional non-slab page tests are from Rik van Riel.
This patch contains the logic for validating several conditions when performing copy_to_user() and copy_from_user() on the kernel object being copied to/from:
- address range doesn't wrap around
- address range isn't NULL or zero-allocated (with a non-zero copy size)
- if on the slab allocator:
- object size must be less than or equal to copy size (when check is implemented in the allocator, which appear in subsequent patches)
- otherwise, object must not span page allocations
- if on the stack
- object must not extend before/after the current process task
- object must be contained by the current stack frame (when there is arch/build support for identifying stack frames)
- object must not overlap with kernel text
--- arch/Kconfig | 7 ++ include/linux/slab.h | 12 +++ include/linux/thread_info.h | 15 +++ mm/Makefile | 4 + mm/usercopy.c | 234 ++++++++++++++++++++++++++++++++++++++++++++ security/Kconfig | 28 ++++++ 6 files changed, 300 insertions(+) create mode 100644 mm/usercopy.c
arch/Kconfig +7 -0
@@ -433,6 +433,13 @@ config HAVE_ARCH_WITHIN_STACK_FRAMES and similar) by implementing an inline arch_within_stack_frames(), which is used by CONFIG_HARDENED_USERCOPY. +config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING bool help
include/linux/slab.h +12 -0
@@ -155,6 +155,18 @@ void kfree(const void *);void kzfree (const void *);size_t ksize (const void *); +#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page); +#else +static inline const char *__check_heap_object (const void *ptr, +unsigned long n, +struct page *page) +{ +return NULL ; +} +#endif +/* * Some archs want to perform DMA into kmalloc caches and need a guaranteed * alignment larger than the alignment of a 64-bit integer.
include/linux/thread_info.h +15 -0
@@ -155,6 +155,21 @@ static inline int arch_within_stack_frames(const void * const stack, }#endif +#ifdef CONFIG_HARDENED_USERCOPY +extern void __check_object_size (const void *ptr,unsigned long n, +bool to_user); + +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ +__check_object_size (ptr, n, to_user); +} +#else +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ } +#endif /* CONFIG_HARDENED_USERCOPY */ +#endif /* __KERNEL__ */ #endif /* _LINUX_THREAD_INFO_H */
mm/Makefile +4 -0
@@ -21,6 +21,9 @@ KCOV_INSTRUMENT_memcontrol.o := n KCOV_INSTRUMENT_mmzone.o := n KCOV_INSTRUMENT_vmstat.o := n +# Since __builtin_frame_address does work as used, disable the warning. +CFLAGS_usercopy.o += $(call cc-disable-warning, frame-address) + mmu-y := nommu.o mmu-$(CONFIG_MMU) := gup.o highmem.o memory.o mincore.o \ mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \
@@ -99,3 +102,4 @@ obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o obj-$(CONFIG_FRAME_VECTOR) += frame_vector.o obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o +obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o
mm/usercopy.c new file +234 -0
234 lines not on this page · raw message
security/Kconfig +28 -0
@@ -118,6 +118,34 @@ config LSM_MMAP_MIN_ADDR this low address space will need the permission specific to the systems running LSM. +config HAVE_HARDENED_USERCOPY_ALLOCATOR + bool + help + The heap allocator implements __check_heap_object() for + validating memory ranges against heap object sizes in + support of CONFIG_HARDENED_USERCOPY. + +config HAVE_ARCH_HARDENED_USERCOPY + bool + help + The architecture supports CONFIG_HARDENED_USERCOPY by + calling check_object_size() just before performing the + userspace copies in the low level implementation of + copy_to_user() and copy_from_user(). + +config HARDENED_USERCOPY + bool "Harden memory copies between kernel and userspace" + depends on HAVE_ARCH_HARDENED_USERCOPY + select BUG + help + This option checks for obviously wrong memory regions when + copying memory to/from the kernel (via copy_to_user() and + copy_from_user() functions) by rejecting memory ranges that + are larger than the specified heap object, span multiple + separately allocates pages, are not on the process stack, + or are part of the kernel text. This kills entire classes + of heap overflow exploits and similar kernel memory exposures. + source security/selinux/Kconfig source security/smack/Kconfig source security/tomoyo/Kconfig
Signature · 1 line
2.7.4
Quoted 151 lines · Kees Cook
This is the start of porting
PAX_USERCOPYinto the mainline kernel. This is the first set of features, controlled byCONFIG_HARDENED_USERCOPY. The work is based on code by PaX Team and Brad Spengler, and an earlier port from Casey Schaufler. Additional non-slab page tests are from Rik van Riel.This patch contains the logic for validating several conditions when performing
copy_to_user()andcopy_from_user()on the kernel object being copied to/from:
- address range doesn't wrap around
- address range isn't NULL or zero-allocated (with a non-zero copy size)
- if on the slab allocator:
- object size must be less than or equal to copy size (when check is implemented in the allocator, which appear in subsequent patches)
- otherwise, object must not span page allocations
- if on the stack
- object must not extend before/after the current process task
- object must be contained by the current stack frame (when there isarch/buildsupport for identifying stack frames)
- object must not overlap with kernel textSigned-off-by Kees Cook <[email protected]>Tested-by Valdis Kletnieks <[email protected]>Tested-by Michael Ellerman <[email protected]>--- arch/Kconfig | 7 ++ include/linux/slab.h | 12 +++ include/linux/thread_info.h | 15 +++ mm/Makefile | 4 + mm/usercopy.c | 234 ++++++++++++++++++++++++++++++++++++++++++++ security/Kconfig | 28 ++++++ 6 files changed, 300 insertions(+) create mode 100644 mm/usercopy.carch/Kconfig +7
-0@@ -433,6 +433,13 @@ config HAVE_ARCH_WITHIN_STACK_FRAMES and similar) by implementing an inline arch_within_stack_frames(), which is used by CONFIG_HARDENED_USERCOPY. +config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING bool helpinclude/linux/slab.h +12
-0@@ -155,6 +155,18 @@ void kfree(const void *);void kzfree (const void *);size_t ksize (const void *); +#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page); +#else +static inline const char *__check_heap_object (const void *ptr, +unsigned long n, +struct page *page) +{ +return NULL ; +} +#endif +/* * Some archs want to perform DMA into kmalloc caches and need a guaranteed * alignment larger than the alignment of a 64-bit integer. include/linux/thread_info.h +15
-0@@ -155,6 +155,21 @@ static inline int arch_within_stack_frames(const void * const stack, }#endif +#ifdef CONFIG_HARDENED_USERCOPY +extern void __check_object_size (const void *ptr,unsigned long n, +bool to_user); + +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ +__check_object_size (ptr, n, to_user); +} +#else +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ } +#endif /* CONFIG_HARDENED_USERCOPY */ +#endif /* __KERNEL__ */ #endif /* _LINUX_THREAD_INFO_H */ mm/Makefile +4
-0@@ -21,6 +21,9 @@ KCOV_INSTRUMENT_memcontrol.o := n KCOV_INSTRUMENT_mmzone.o := n KCOV_INSTRUMENT_vmstat.o := n +# Since __builtin_frame_address does work as used, disable the warning. +CFLAGS_usercopy.o += $(call cc-disable-warning, frame-address) + mmu-y := nommu.o mmu-$(CONFIG_MMU) := gup.o highmem.o memory.o mincore.o \ mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \@@ -99,3 +102,4 @@ obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o obj-$(CONFIG_FRAME_VECTOR) += frame_vector.o obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o +obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.omm/usercopy.c new file +26
-0@@ -0,0 +1,234 @@ +/* +* This implements the various checks for CONFIG_HARDENED_USERCOPY*, +* which are designed to protect kernel memory from needless exposure +* and overwrite under many unintended conditions. This code is based +* on PAX_USERCOPY, which is: +* +* Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source +* Security Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/mm.h> +#include <linux/slab.h> +#include <asm/sections.h> + +enum { +BAD_STACK =-1 , +NOT_STACK =0 , +GOOD_FRAME , +GOOD_STACK , +};
mm/usercopy.c line 36
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/
Nit: update comments to match enum (BAD_STACK instead of -1 etc.)
Quoted 134 lines
mm/usercopy.c new file +134
-0+static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ; +int ret; + +/* Object is not on the stack at all. */ +if (obj + len <= stack || stackend <= obj) +return NOT_STACK ; + +/* +* Reject: object partially overlaps the stack (passing the +* 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) +return BAD_STACK ; + +/* Check if object is safely within a valid frame. */ + ret =arch_within_stack_frames (stack, stackend, obj, len); +if (ret) +return ret; + +return GOOD_STACK ; +} + +static void report_usercopy (const void *ptr,unsigned long len, +bool to_user,const char *type) +{ +pr_emerg ("kernel memory %s attempt detected %s %p (%s) (%lu bytes)\n" , + to_user ?"exposure" :"overwrite" , + to_user ?"from" :"to" , ptr, type ? :"unknown" , len); +/* +* For greater effect, it would be nice to do do_group_exit(), +* but BUG() actually hooks all the lock-breaking and per-arch +* Oops code, so that is used here instead. +*/ +BUG (); +} + +/* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */ +static bool overlaps (const void *ptr,unsigned long n,unsigned long low, +unsigned long high) +{ +unsigned long check_low = (uintptr_t )ptr; +unsigned long check_high = check_low + n; + +/* Does not overlap if entirely above or entirely below. */ +if (check_low >= high || check_high < low) +return false; + +return true; +} + +/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif + +return NULL ; +} + +static inline const char *check_bogus_address (const void *ptr,unsigned long n) +{ +/* Reject if object wraps past end of memory. */ +if (ptr + n < ptr) +return "<wrapped address>" ; + +/* Reject if NULL or ZERO-allocation. */ +if (ZERO_OR_NULL_PTR (ptr)) +return "<null>" ; + +return NULL ; +} + +static inline const char *check_heap_object (const void *ptr,unsigned long n, +bool to_user) +{ +struct page *page, *endpage; +const void *end = ptr + n -1 ; + +if (!virt_addr_valid (ptr)) +return NULL ; + + page =virt_to_head_page (ptr); + +/* Check slab allocator for flags and size. */ +if (PageSlab (page)) +return __check_heap_object (ptr, n, page); + +/* +* Sometimes the kernel data regions are not marked Reserved (see +* check below). And sometimes [_sdata,_edata) does not cover +* rodata and/or bss, so check each range explicitly. +*/ + +/* Allow reads of kernel rodata region (if not marked as Reserved). */ +if (ptr >= (const void *)__start_rodata && + end <= (const void *)__end_rodata) { +if (!to_user) +return "<rodata>" ; +return NULL ; + } + +/* Allow kernel data region (if not marked as Reserved). */ +if (ptr >= (const void *)_sdata && end <= (const void *)_edata) +return NULL ; + +/* Allow kernel bss region (if not marked as Reserved). */ +if (ptr >= (const void *)__bss_start && + end <= (const void *)__bss_stop) +return NULL ; + +/* Is the object wholly within one base page? */ +if (likely (((unsigned long )ptr & (unsigned long )PAGE_MASK ) == + ((unsigned long )end & (unsigned long )PAGE_MASK ))) +return NULL ; + +/* Allow if start and end are inside the same compound page. */ + endpage =virt_to_head_page (end); +if (likely (endpage == page)) +return NULL ;
mm/usercopy.c line 180
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +/* +* Reject if range is not Reserved (i.e. special or device memory), +* since then the object spans several independently allocated pages. +*/ +for (; ptr <= end ; ptr +=PAGE_SIZE , page =virt_to_head_page (ptr)) { +if (!PageReserved (page)) +return "<spans multiple pages>" ; + } +
This doesn't work when copying CMA allocated memory since CMA purposely allocates larger than a page block size without setting head pages. Given CMA may be used with drivers doing zero copy buffers, I think it should be permitted.
Something like the following lets it pass (I can clean up and submit the is_migrate_cma_page APIs as a separate patch for review)
include/linux/mmzone.h +13 -0
@@ -18,6 +18,7 @@#include <linux/page-flags-layout.h> #include <linux/atomic.h> #include <asm/page.h> +#include <asm/pgtable.h> /* Free memory management - zoned buddy allocator. */ #ifndef CONFIG_FORCE_MAX_ZONEORDER
@@ -85,6 +86,18 @@ extern int page_group_by_mobility_disabled;get_pfnblock_flags_mask (page,page_to_pfn (page), \ PB_migrate_end, MIGRATETYPE_MASK) +#ifdef CONFIG_CMA +static inline bool is_migrate_cma_page (struct page *page) +{ +return get_pageblock_migratetype (page) ==MIGRATE_CMA ; +} +#else +static inline bool is_migrate_cma_page (struct page *page) +{ +return false; +} +#endif +struct free_area {struct list_head free_list[MIGRATE_TYPES ];unsigned long nr_free;
mm/usercopy.c +2 -1
@@ -16,6 +16,7 @@#include <linux/mm.h> #include <linux/slab.h> +#include <linux/mmzone.h> #include <asm/sections.h> enum {
@@ -174,7 +175,7 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n,* since then the object spans several independently allocated pages. */ for (; ptr <= end ; ptr +=PAGE_SIZE , page =virt_to_head_page (ptr)) { -if (!PageReserved (page)) +if (!PageReserved (page) && !is_migrate_cma_page (page))return "<spans multiple pages>" ; }
Thanks,
Laura
Quoted 164 lines · Laura Abbott
Quoted 163 lines · Kees Cook
This is the start of porting
PAX_USERCOPYinto the mainline kernel. This is the first set of features, controlled byCONFIG_HARDENED_USERCOPY. The work is based on code by PaX Team and Brad Spengler, and an earlier port from Casey Schaufler. Additional non-slab page tests are from Rik van Riel.This patch contains the logic for validating several conditions when performing
copy_to_user()andcopy_from_user()on the kernel object being copied to/from:
- address range doesn't wrap around
- address range isn't NULL or zero-allocated (with a non-zero copy size)
- if on the slab allocator:
- object size must be less than or equal to copy size (when check is implemented in the allocator, which appear in subsequent patches)
- otherwise, object must not span page allocations
- if on the stack
- object must not extend before/after the current process task
- object must be contained by the current stack frame (when there isarch/buildsupport for identifying stack frames)
- object must not overlap with kernel textSigned-off-by Kees Cook <[email protected]>Tested-by Valdis Kletnieks <[email protected]>Tested-by Michael Ellerman <[email protected]>--- arch/Kconfig | 7 ++ include/linux/slab.h | 12 +++ include/linux/thread_info.h | 15 +++ mm/Makefile | 4 + mm/usercopy.c | 234 ++++++++++++++++++++++++++++++++++++++++++++ security/Kconfig | 28 ++++++ 6 files changed, 300 insertions(+) create mode 100644 mm/usercopy.carch/Kconfig +7
-0@@ -433,6 +433,13 @@ config HAVE_ARCH_WITHIN_STACK_FRAMES and similar) by implementing an inline arch_within_stack_frames(), which is used by CONFIG_HARDENED_USERCOPY. +config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING boolhelp
include/linux/slab.h +12
-0@@ -155,6 +155,18 @@ void kfree(const void *);void kzfree (const void *);size_t ksize (const void *); +#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page); +#else +static inline const char *__check_heap_object (const void *ptr, +unsigned long n, +struct page *page) +{ +return NULL ; +} +#endif +/* * Some archs want to perform DMA into kmalloc caches and need a guaranteed
* alignment larger than the alignment of a 64-bit integer.include/linux/thread_info.h +15
-0@@ -155,6 +155,21 @@ static inline int arch_within_stack_frames(const void * const stack, }#endif +#ifdef CONFIG_HARDENED_USERCOPY +extern void __check_object_size (const void *ptr,unsigned long n, +bool to_user); + +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ +__check_object_size (ptr, n, to_user); +} +#else +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ } +#endif /* CONFIG_HARDENED_USERCOPY */ +#endif /* __KERNEL__ */ #endif /*
_LINUX_THREAD_INFO_H*/mm/Makefile +3
-0@@ -21,6 +21,9 @@ KCOV_INSTRUMENT_memcontrol.o := n KCOV_INSTRUMENT_mmzone.o := n KCOV_INSTRUMENT_vmstat.o := n +# Since __builtin_frame_address does work as used, disable the warning. +CFLAGS_usercopy.o += $(call cc-disable-warning, frame-address) + mmu-y := nommu.o mmu-$(CONFIG_MMU) := gup.o highmem.o memory.o mincore.o \ mlock.o mmap.o mprotect.o mremap.o msync.ormap.o \
mm/Makefile +1
-0@@ -99,3 +102,4 @@ obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o obj-$(CONFIG_FRAME_VECTOR) += frame_vector.o obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o +obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.omm/usercopy.c new file +31
-0@@ -0,0 +1,234 @@ +/* +* This implements the various checks for CONFIG_HARDENED_USERCOPY*, +* which are designed to protect kernel memory from needless exposure +* and overwrite under many unintended conditions. This code is based +* on PAX_USERCOPY, which is: +* +* Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source +* Security Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/mm.h> +#include <linux/slab.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). +*
Quoted 8 lines
mm/usercopy.c line 36
Quoted 5 lines
mm/usercopy.c new file +5
-0+* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ Nit: update comments to match enum (
BAD_STACKinstead of -1 etc.)
Ah, yes, thanks. I will fix this.
Quoted 160 lines
Quoted 149 lines
mm/usercopy.c new file +1
-0+static noinline int check_stack_object(const void *obj, unsigned longlen)
mm/usercopy.c new file +29
-0+{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ; +int ret; + +/* Object is not on the stack at all. */ +if (obj + len <= stack || stackend <= obj) +return NOT_STACK ; + +/* +* Reject: object partially overlaps the stack (passing the +* 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) +return BAD_STACK ; + +/* Check if object is safely within a valid frame. */ + ret =arch_within_stack_frames (stack, stackend, obj, len); +if (ret) +return ret; + +return GOOD_STACK ; +} + +static void report_usercopy (const void *ptr,unsigned long len, +bool to_user,const char *type) +{ + pr_emerg("kernel memory %s attempt detected %s %p (%s) (%lubytes)\n",
mm/usercopy.c new file +11
-0+ to_user ?"exposure" :"overwrite" , + to_user ? "from" : "to", ptr, type ? : "unknown", len); +/* +* For greater effect, it would be nice to do do_group_exit(), +* but BUG() actually hooks all the lock-breaking and per-arch +* Oops code, so that is used here instead. +*/ +BUG (); +} + +/* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
mm/usercopy.c new file +34
-0+static bool overlaps (const void *ptr,unsigned long n,unsigned long low, +unsigned long high) +{ +unsigned long check_low = (uintptr_t )ptr; +unsigned long check_high = check_low + n; + +/* Does not overlap if entirely above or entirely below. */ +if (check_low >= high || check_high < low) +return false; + +return true; +} + +/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif + +return NULL ; +} + +static inline const char *check_bogus_address(const void *ptr, unsignedlong n)
mm/usercopy.c new file +13
-0+{ +/* Reject if object wraps past end of memory. */ +if (ptr + n < ptr) +return "<wrapped address>" ; + +/* Reject if NULL or ZERO-allocation. */ +if (ZERO_OR_NULL_PTR (ptr)) +return "<null>" ; + +return NULL ; +} + +static inline const char *check_heap_object(const void *ptr, unsignedlong n,
mm/usercopy.c new file +21
-0+ bool to_user) +{ +struct page *page, *endpage; +const void *end = ptr + n -1 ; + +if (!virt_addr_valid (ptr)) +return NULL ; + + page =virt_to_head_page (ptr); + +/* Check slab allocator for flags and size. */ +if (PageSlab (page)) +return __check_heap_object (ptr, n, page); + +/* +* Sometimes the kernel data regions are not marked Reserved (see +* check below). And sometimes [_sdata,_edata) does not cover +* rodata and/or bss, so check each range explicitly. +*/ + +/* Allow reads of kernel rodata region (if not marked as Reserved). */
mm/usercopy.c new file +28
-0+if (ptr >= (const void *)__start_rodata && + end <= (const void *)__end_rodata) { +if (!to_user) +return "<rodata>" ; +return NULL ; + } + +/* Allow kernel data region (if not marked as Reserved). */ +if (ptr >= (const void *)_sdata && end <= (const void *)_edata) +return NULL ; + +/* Allow kernel bss region (if not marked as Reserved). */ +if (ptr >= (const void *)__bss_start && + end <= (const void *)__bss_stop) +return NULL ; + +/* Is the object wholly within one base page? */ +if (likely (((unsigned long )ptr & (unsigned long )PAGE_MASK ) == + ((unsigned long )end & (unsigned long )PAGE_MASK ))) +return NULL ; + +/* Allow if start and end are inside the same compound page. */ + endpage =virt_to_head_page (end); +if (likely (endpage == page)) +return NULL ; + +/* +* Reject if range is not Reserved (i.e. special or device memory),
+ * since then the object spans several independently allocated
pages.mm/usercopy.c new file +2
-0+*/ + for (; ptr <= end ; ptr += PAGE_SIZE, page =
virt_to_head_page(ptr)) {mm/usercopy.c
Quoted 4 lines
mm/usercopy.c new file +4
-0+if (!PageReserved (page)) +return "<spans multiple pages>" ; + } +This doesn't work when copying CMA allocated memory since CMA purposely allocates larger than a page block size without setting head pages. Given CMA may be used with drivers doing zero copy buffers, I think it should be permitted.
Quoted 2 lines
Something like the following lets it pass (I can clean up and submit the
is_migrate_cma_pageAPIs as a separate patch for review)
Yeah, this would be great. I'd rather use an accessor to check this than a direct check for MIGRATE_CMA.
Quoted 47 lines
include/linux/mmzone.h +12
-0@@ -18,6 +18,7 @@#include <linux/page-flags-layout.h> #include <linux/atomic.h> #include <asm/page.h> +#include <asm/pgtable.h> /* Free memory management - zoned buddy allocator. */ #ifndef CONFIG_FORCE_MAX_ZONEORDER @@ -85,6 +86,18 @@ extern int page_group_by_mobility_disabled; get_pfnblock_flags_mask(page, page_to_pfn(page), \ PB_migrate_end, MIGRATETYPE_MASK) +#ifdef CONFIG_CMA +static inline bool is_migrate_cma_page(struct page *page) +{ + return get_pageblock_migratetype(page) == MIGRATE_CMA; +} +#else +static inline bool is_migrate_cma_page(struct page *page) +{ + return false; +} +#endif + struct free_area { struct list_head free_list[MIGRATE_TYPES]; unsigned long nr_free;mm/usercopy.c +1
-0@@ -16,6 +16,7 @@#include <linux/mm.h> #include <linux/slab.h> +#include <linux/mmzone.h> #include <asm/sections.h> enum {@@ -174,7 +175,7 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n, * since then the object spans several independently allocatedpages.
*/ for (; ptr <= end ; ptr += PAGE_SIZE, page = virt_to_head_page(ptr)){
mm/usercopy.c static inline const char *check_heap_object(const void *ptr, unsigned long n, · line 180
Quoted 4 lines
mm/usercopy.c +1
-1-if (!PageReserved (page)) +if (!PageReserved (page) && !is_migrate_cma_page (page))return "<spans multiple pages>" ; }
Yeah, I'll modify this a bit so that which type it starts as is maintained for all pages (rather than allowing to flip back and forth -- even though that is likely impossible).
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH 1/1 mm: Add is_migrate_cma_page
Code such as hardened user copy[1] needs a way to tell if a page is CMA or not. Add is_migrate_cma_page in a similar way to is_migrate_isolate_page.
[1]http://article.gmane.org/gmane.linux.kernel.mm/155238
---
Here's an explicit patch, slightly different than what I posted before. It can be kept separate or folded in as needed.
--- include/linux/mmzone.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/mmzone.h +2 -0
@@ -68,8 +68,10 @@ extern char * const migratetype_names[MIGRATE_TYPES];#ifdef CONFIG_CMA # define is_migrate_cma (migratetype) unlikely((migratetype) == MIGRATE_CMA) +# define is_migrate_cma_page (_page) (get_pageblock_migratetype(_page) == MIGRATE_CMA)#else # define is_migrate_cma (migratetype) false +# define is_migrate_cma_page (_page) false#endif #define for_each_migratetype_order (order, type) \
Signature · 1 line
2.7.4
Re: [PATCH] mm: Add is_migrate_cma_page
Quoted 7 lines · Laura Abbott
Code such as hardened user copy[1] needs a way to tell if a page is CMA or not. Add
is_migrate_cma_pagein a similar way tois_migrate_isolate_page.[1]http://article.gmane.org/gmane.linux.kernel.mm/155238
Signed-off-by Laura Abbott <[email protected]>
Great, thanks!
Quoted 3 lines
---
Here's an explicit patch, slightly different than what I posted before. It can be kept separate or folded in as needed.
Assuming there's no objection, I'll add it to my tree and use the new macro.
-Kees
Quoted 22 lines
--- include/linux/mmzone.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/mmzone.h +2
-1@@ -68,8 +68,10 @@ extern char * const migratetype_names[MIGRATE_TYPES];#ifdef CONFIG_CMA # define is_migrate_cma (migratetype) unlikely((migratetype) == MIGRATE_CMA) +# define is_migrate_cma_page (_page) (get_pageblock_migratetype(_page) == MIGRATE_CMA)#else # define is_migrate_cma (migratetype) false +# define is_migrate_cma_page (_page) false#endif #define for_each_migratetype_order (order, type) \ --2.7.4
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 21 lines · Kees Cook
Quoted 10 lines · Laura Abbott
On 07/15/2016 02:44 PM, Kees Cook wrote:
This doesn't work when copying CMA allocated memory since CMA purposely allocates larger than a page block size without setting head pages. Given CMA may be used with drivers doing zero copy buffers, I think it should be permitted.
Something like the following lets it pass (I can clean up and submit theis_migrate_cma_pageAPIs as a separate patch for review)Yeah, this would be great. I'd rather use an accessor to check this than a direct check for
MIGRATE_CMA.Quoted 7 lines
*/
for (; ptr <= end ; ptr +=PAGE_SIZE, page =virt_to_head_page(ptr)) {
- if (!PageReserved(page))
+ if (!PageReserved(page) && !is_migrate_cma_page(page)) return "<spans multiple pages>";
}
Quoted 4 lines
Yeah, I'll modify this a bit so that which type it starts as is maintained for all pages (rather than allowing to flip back and forth -- even though that is likely impossible).
Sorry, I completely missed the MIGRATE_CMA bits. Could you clarify if you caught this in testing/review?
Balbir Singh.
Quoted 22 lines · Balbir Singh
Quoted 21 lines · Kees Cook
Quoted 10 lines · Laura Abbott
On 07/15/2016 02:44 PM, Kees Cook wrote:
This doesn't work when copying CMA allocated memory since CMA purposely allocates larger than a page block size without setting head pages. Given CMA may be used with drivers doing zero copy buffers, I think it should be permitted.
Something like the following lets it pass (I can clean up and submit the
is_migrate_cma_pageAPIs as a separate patch for review)Yeah, this would be great. I'd rather use an accessor to check this than a direct check for
MIGRATE_CMA.Quoted 7 lines
*/ for (; ptr <= end ; ptr += PAGE_SIZE, page = virt_to_head_page(ptr)){
mm/usercopy.c +1
-1-if (!PageReserved (page)) +if (!PageReserved (page) && !is_migrate_cma_page (page))return "<spans multiple pages>" ; }
Quoted 9 lines
Quoted 4 lines
Yeah, I'll modify this a bit so that which type it starts as is maintained for all pages (rather than allowing to flip back and forth -- even though that is likely impossible).
Sorry, I completely missed the
MIGRATE_CMAbits. Could you clarify if you caught this in testing/review?Balbir Singh.
I caught it while looking at the code and then wrote a test case to confirm I was correct because I wasn't sure how to easily find an in tree user.
Thanks,
Laura
Quoted 247 lines · Kees Cook
This is the start of porting
PAX_USERCOPYinto the mainline kernel. This is the first set of features, controlled byCONFIG_HARDENED_USERCOPY. The work is based on code by PaX Team and Brad Spengler, and an earlier port from Casey Schaufler. Additional non-slab page tests are from Rik van Riel.This patch contains the logic for validating several conditions when performing
copy_to_user()andcopy_from_user()on the kernel object being copied to/from:
- address range doesn't wrap around
- address range isn't NULL or zero-allocated (with a non-zero copy size)
- if on the slab allocator:
- object size must be less than or equal to copy size (when check is implemented in the allocator, which appear in subsequent patches)
- otherwise, object must not span page allocations
- if on the stack
- object must not extend before/after the current process task
- object must be contained by the current stack frame (when there isarch/buildsupport for identifying stack frames)
- object must not overlap with kernel textSigned-off-by Kees Cook <[email protected]>Tested-by Valdis Kletnieks <[email protected]>Tested-by Michael Ellerman <[email protected]>--- arch/Kconfig | 7 ++ include/linux/slab.h | 12 +++ include/linux/thread_info.h | 15 +++ mm/Makefile | 4 + mm/usercopy.c | 234 ++++++++++++++++++++++++++++++++++++++++++++ security/Kconfig | 28 ++++++ 6 files changed, 300 insertions(+) create mode 100644 mm/usercopy.carch/Kconfig +7
-0@@ -433,6 +433,13 @@ config HAVE_ARCH_WITHIN_STACK_FRAMES and similar) by implementing an inline arch_within_stack_frames(), which is used by CONFIG_HARDENED_USERCOPY. +config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING bool helpinclude/linux/slab.h +12
-0@@ -155,6 +155,18 @@ void kfree(const void *);void kzfree (const void *);size_t ksize (const void *); +#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page); +#else +static inline const char *__check_heap_object (const void *ptr, +unsigned long n, +struct page *page) +{ +return NULL ; +} +#endif +/* * Some archs want to perform DMA into kmalloc caches and need a guaranteed * alignment larger than the alignment of a 64-bit integer. include/linux/thread_info.h +15
-0@@ -155,6 +155,21 @@ static inline int arch_within_stack_frames(const void * const stack, }#endif +#ifdef CONFIG_HARDENED_USERCOPY +extern void __check_object_size (const void *ptr,unsigned long n, +bool to_user); + +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ +__check_object_size (ptr, n, to_user); +} +#else +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ } +#endif /* CONFIG_HARDENED_USERCOPY */ +#endif /* __KERNEL__ */ #endif /* _LINUX_THREAD_INFO_H */ mm/Makefile +4
-0@@ -21,6 +21,9 @@ KCOV_INSTRUMENT_memcontrol.o := n KCOV_INSTRUMENT_mmzone.o := n KCOV_INSTRUMENT_vmstat.o := n +# Since __builtin_frame_address does work as used, disable the warning. +CFLAGS_usercopy.o += $(call cc-disable-warning, frame-address) + mmu-y := nommu.o mmu-$(CONFIG_MMU) := gup.o highmem.o memory.o mincore.o \ mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \@@ -99,3 +102,4 @@ obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o obj-$(CONFIG_FRAME_VECTOR) += frame_vector.o obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o +obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.omm/usercopy.c new file +122
-0@@ -0,0 +1,234 @@ +/* +* This implements the various checks for CONFIG_HARDENED_USERCOPY*, +* which are designed to protect kernel memory from needless exposure +* and overwrite under many unintended conditions. This code is based +* on PAX_USERCOPY, which is: +* +* Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source +* Security Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/mm.h> +#include <linux/slab.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). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ; +int ret; + +/* Object is not on the stack at all. */ +if (obj + len <= stack || stackend <= obj) +return NOT_STACK ; + +/* +* Reject: object partially overlaps the stack (passing the +* 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) +return BAD_STACK ; + +/* Check if object is safely within a valid frame. */ + ret =arch_within_stack_frames (stack, stackend, obj, len); +if (ret) +return ret; + +return GOOD_STACK ; +} + +static void report_usercopy (const void *ptr,unsigned long len, +bool to_user,const char *type) +{ +pr_emerg ("kernel memory %s attempt detected %s %p (%s) (%lu bytes)\n" , + to_user ?"exposure" :"overwrite" , + to_user ?"from" :"to" , ptr, type ? :"unknown" , len); +/* +* For greater effect, it would be nice to do do_group_exit(), +* but BUG() actually hooks all the lock-breaking and per-arch +* Oops code, so that is used here instead. +*/ +BUG (); +} + +/* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */ +static bool overlaps (const void *ptr,unsigned long n,unsigned long low, +unsigned long high) +{ +unsigned long check_low = (uintptr_t )ptr; +unsigned long check_high = check_low + n; + +/* Does not overlap if entirely above or entirely below. */ +if (check_low >= high || check_high < low) +return false; + +return true; +} + +/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif + +return NULL ; +} + +static inline const char *check_bogus_address (const void *ptr,unsigned long n) +{ +/* Reject if object wraps past end of memory. */ +if (ptr + n < ptr) +return "<wrapped address>" ; + +/* Reject if NULL or ZERO-allocation. */ +if (ZERO_OR_NULL_PTR (ptr)) +return "<null>" ; + +return NULL ; +}
mm/usercopy.c line 132
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +static inline const char *check_heap_object (const void *ptr,unsigned long n, +bool to_user) +{ +struct page *page, *endpage; +const void *end = ptr + n -1 ; + +if (!virt_addr_valid (ptr)) +return NULL ; +
virt_addr_valid returns true on vmalloc addresses on arm64 which causes some intermittent false positives (tab completion in a qemu buildroot environment was showing it fairly reliably). I think this is an arm64 bug because virt_addr_valid should return true if and only if virt_to_page returns the corresponding page. We can work around this for now by explicitly checking against is_vmalloc_addr.
Thanks,
Laura
Quoted 142 lines
mm/usercopy.c new file +102
-0+ page =virt_to_head_page (ptr); + +/* Check slab allocator for flags and size. */ +if (PageSlab (page)) +return __check_heap_object (ptr, n, page); + +/* +* Sometimes the kernel data regions are not marked Reserved (see +* check below). And sometimes [_sdata,_edata) does not cover +* rodata and/or bss, so check each range explicitly. +*/ + +/* Allow reads of kernel rodata region (if not marked as Reserved). */ +if (ptr >= (const void *)__start_rodata && + end <= (const void *)__end_rodata) { +if (!to_user) +return "<rodata>" ; +return NULL ; + } + +/* Allow kernel data region (if not marked as Reserved). */ +if (ptr >= (const void *)_sdata && end <= (const void *)_edata) +return NULL ; + +/* Allow kernel bss region (if not marked as Reserved). */ +if (ptr >= (const void *)__bss_start && + end <= (const void *)__bss_stop) +return NULL ; + +/* Is the object wholly within one base page? */ +if (likely (((unsigned long )ptr & (unsigned long )PAGE_MASK ) == + ((unsigned long )end & (unsigned long )PAGE_MASK ))) +return NULL ; + +/* Allow if start and end are inside the same compound page. */ + endpage =virt_to_head_page (end); +if (likely (endpage == page)) +return NULL ; + +/* +* Reject if range is not Reserved (i.e. special or device memory), +* since then the object spans several independently allocated pages. +*/ +for (; ptr <= end ; ptr +=PAGE_SIZE , page =virt_to_head_page (ptr)) { +if (!PageReserved (page)) +return "<spans multiple pages>" ; + } + +return NULL ; +} + +/* +* Validates that the given object is one of: +* - known safe heap object +* - known safe stack object +* - not in kernel text +*/ +void __check_object_size (const void *ptr,unsigned long n,bool to_user) +{ +const char *err; + +/* Skip all tests if size is zero. */ +if (!n) +return ; + +/* Check for invalid addresses. */ + err =check_bogus_address (ptr, n); +if (err) + goto report; + +/* Check for bad heap object. */ + err =check_heap_object (ptr, n, to_user); +if (err) + goto report; + +/* Check for bad stack object. */ +switch (check_stack_object (ptr, n)) { +case NOT_STACK : +/* Object is not touching the current process stack. */ +break ; +case GOOD_FRAME : +case GOOD_STACK : +/* +* Object is either in the correct frame (when it +* is possible to check) or just generally on the +* process stack (when frame checking not available). +*/ +return ; +default : + err ="<process stack>" ; + goto report; + } + +/* Check for object in kernel to avoid text exposure. */ + err =check_kernel_text_object (ptr, n); +if (!err) +return ; + +report: +report_usercopy (ptr, n, to_user, err); +} +EXPORT_SYMBOL (__check_object_size);security/Kconfig +28
-0@@ -118,6 +118,34 @@ config LSM_MMAP_MIN_ADDR this low address space will need the permission specific to the systems running LSM. +config HAVE_HARDENED_USERCOPY_ALLOCATOR + bool + help + The heap allocator implements __check_heap_object() for + validating memory ranges against heap object sizes in + support of CONFIG_HARDENED_USERCOPY. + +config HAVE_ARCH_HARDENED_USERCOPY + bool + help + The architecture supports CONFIG_HARDENED_USERCOPY by + calling check_object_size() just before performing the + userspace copies in the low level implementation of + copy_to_user() and copy_from_user(). + +config HARDENED_USERCOPY + bool "Harden memory copies between kernel and userspace" + depends on HAVE_ARCH_HARDENED_USERCOPY + select BUG + help + This option checks for obviously wrong memory regions when + copying memory to/from the kernel (via copy_to_user() and + copy_from_user() functions) by rejecting memory ranges that + are larger than the specified heap object, span multiple + separately allocates pages, are not on the process stack, + or are part of the kernel text. This kills entire classes + of heap overflow exploits and similar kernel memory exposures. + source security/selinux/Kconfig source security/smack/Kconfig source security/tomoyo/Kconfig
Quoted 272 lines · Laura Abbott
Quoted 261 lines · Kees Cook
This is the start of porting
PAX_USERCOPYinto the mainline kernel. This is the first set of features, controlled byCONFIG_HARDENED_USERCOPY. The work is based on code by PaX Team and Brad Spengler, and an earlier port from Casey Schaufler. Additional non-slab page tests are from Rik van Riel.This patch contains the logic for validating several conditions when performing
copy_to_user()andcopy_from_user()on the kernel object being copied to/from:
- address range doesn't wrap around
- address range isn't NULL or zero-allocated (with a non-zero copy size)
- if on the slab allocator:
- object size must be less than or equal to copy size (when check is implemented in the allocator, which appear in subsequent patches)
- otherwise, object must not span page allocations
- if on the stack
- object must not extend before/after the current process task
- object must be contained by the current stack frame (when there isarch/buildsupport for identifying stack frames)
- object must not overlap with kernel textSigned-off-by Kees Cook <[email protected]>Tested-by Valdis Kletnieks <[email protected]>Tested-by Michael Ellerman <[email protected]>--- arch/Kconfig | 7 ++ include/linux/slab.h | 12 +++ include/linux/thread_info.h | 15 +++ mm/Makefile | 4 + mm/usercopy.c | 234 ++++++++++++++++++++++++++++++++++++++++++++ security/Kconfig | 28 ++++++ 6 files changed, 300 insertions(+) create mode 100644 mm/usercopy.carch/Kconfig +7
-0@@ -433,6 +433,13 @@ config HAVE_ARCH_WITHIN_STACK_FRAMES and similar) by implementing an inline arch_within_stack_frames(), which is used by CONFIG_HARDENED_USERCOPY. +config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY. + config HAVE_CONTEXT_TRACKING boolhelp
include/linux/slab.h +12
-0@@ -155,6 +155,18 @@ void kfree(const void *);void kzfree (const void *);size_t ksize (const void *); +#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page); +#else +static inline const char *__check_heap_object (const void *ptr, +unsigned long n, +struct page *page) +{ +return NULL ; +} +#endif +/* * Some archs want to perform DMA into kmalloc caches and need a guaranteed
* alignment larger than the alignment of a 64-bit integer.include/linux/thread_info.h +15
-0@@ -155,6 +155,21 @@ static inline int arch_within_stack_frames(const void * const stack, }#endif +#ifdef CONFIG_HARDENED_USERCOPY +extern void __check_object_size (const void *ptr,unsigned long n, +bool to_user); + +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ +__check_object_size (ptr, n, to_user); +} +#else +static inline void check_object_size (const void *ptr,unsigned long n, +bool to_user) +{ } +#endif /* CONFIG_HARDENED_USERCOPY */ +#endif /* __KERNEL__ */ #endif /*
_LINUX_THREAD_INFO_H*/mm/Makefile +3
-0@@ -21,6 +21,9 @@ KCOV_INSTRUMENT_memcontrol.o := n KCOV_INSTRUMENT_mmzone.o := n KCOV_INSTRUMENT_vmstat.o := n +# Since __builtin_frame_address does work as used, disable the warning. +CFLAGS_usercopy.o += $(call cc-disable-warning, frame-address) + mmu-y := nommu.o mmu-$(CONFIG_MMU) := gup.o highmem.o memory.o mincore.o \ mlock.o mmap.o mprotect.o mremap.o msync.ormap.o \
mm/Makefile +1
-0@@ -99,3 +102,4 @@ obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o obj-$(CONFIG_FRAME_VECTOR) += frame_vector.o obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o +obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.omm/usercopy.c new file +37
-0@@ -0,0 +1,234 @@ +/* +* This implements the various checks for CONFIG_HARDENED_USERCOPY*, +* which are designed to protect kernel memory from needless exposure +* and overwrite under many unintended conditions. This code is based +* on PAX_USERCOPY, which is: +* +* Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source +* Security Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/mm.h> +#include <linux/slab.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). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned longlen)
mm/usercopy.c new file +29
-0+{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ; +int ret; + +/* Object is not on the stack at all. */ +if (obj + len <= stack || stackend <= obj) +return NOT_STACK ; + +/* +* Reject: object partially overlaps the stack (passing the +* 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) +return BAD_STACK ; + +/* Check if object is safely within a valid frame. */ + ret =arch_within_stack_frames (stack, stackend, obj, len); +if (ret) +return ret; + +return GOOD_STACK ; +} + +static void report_usercopy (const void *ptr,unsigned long len, +bool to_user,const char *type) +{ + pr_emerg("kernel memory %s attempt detected %s %p (%s) (%lubytes)\n",
mm/usercopy.c new file +11
-0+ to_user ?"exposure" :"overwrite" , + to_user ? "from" : "to", ptr, type ? : "unknown", len); +/* +* For greater effect, it would be nice to do do_group_exit(), +* but BUG() actually hooks all the lock-breaking and per-arch +* Oops code, so that is used here instead. +*/ +BUG (); +} + +/* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
mm/usercopy.c new file +34
-0+static bool overlaps (const void *ptr,unsigned long n,unsigned long low, +unsigned long high) +{ +unsigned long check_low = (uintptr_t )ptr; +unsigned long check_high = check_low + n; + +/* Does not overlap if entirely above or entirely below. */ +if (check_low >= high || check_high < low) +return false; + +return true; +} + +/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif + +return NULL ; +} + +static inline const char *check_bogus_address(const void *ptr, unsignedlong n)
mm/usercopy.c new file +13
-0+{ +/* Reject if object wraps past end of memory. */ +if (ptr + n < ptr) +return "<wrapped address>" ; + +/* Reject if NULL or ZERO-allocation. */ +if (ZERO_OR_NULL_PTR (ptr)) +return "<null>" ; + +return NULL ; +} + +static inline const char *check_heap_object(const void *ptr, unsignedlong n,
mm/usercopy.c line 132
Quoted 8 lines
mm/usercopy.c new file +8
-0+ bool to_user) +{ +struct page *page, *endpage; +const void *end = ptr + n -1 ; + +if (!virt_addr_valid (ptr)) +return NULL ; +
Quoted 6 lines
virt_addr_validreturns true on vmalloc addresses on arm64 which causes some intermittent false positives (tab completion in a qemu buildroot environment was showing it fairly reliably). I think this is an arm64 bug becausevirt_addr_validshould return true if and only ifvirt_to_pagereturns the corresponding page. We can work around this for now by explicitly checking againstis_vmalloc_addr.
Hrm, that's weird. Sounds like a bug too, but I'll add a check for is_vmalloc_addr() to catch it for now.
-Kees
Quoted 152 lines
Thanks,
LauraQuoted 146 lines
mm/usercopy.c new file +13
-0+ page =virt_to_head_page (ptr); + +/* Check slab allocator for flags and size. */ +if (PageSlab (page)) +return __check_heap_object (ptr, n, page); + +/* +* Sometimes the kernel data regions are not marked Reserved (see +* check below). And sometimes [_sdata,_edata) does not cover +* rodata and/or bss, so check each range explicitly. +*/ + +/* Allow reads of kernel rodata region (if not marked as Reserved). */
mm/usercopy.c new file +28
-0+if (ptr >= (const void *)__start_rodata && + end <= (const void *)__end_rodata) { +if (!to_user) +return "<rodata>" ; +return NULL ; + } + +/* Allow kernel data region (if not marked as Reserved). */ +if (ptr >= (const void *)_sdata && end <= (const void *)_edata) +return NULL ; + +/* Allow kernel bss region (if not marked as Reserved). */ +if (ptr >= (const void *)__bss_start && + end <= (const void *)__bss_stop) +return NULL ; + +/* Is the object wholly within one base page? */ +if (likely (((unsigned long )ptr & (unsigned long )PAGE_MASK ) == + ((unsigned long )end & (unsigned long )PAGE_MASK ))) +return NULL ; + +/* Allow if start and end are inside the same compound page. */ + endpage =virt_to_head_page (end); +if (likely (endpage == page)) +return NULL ; + +/* +* Reject if range is not Reserved (i.e. special or device memory),
+ * since then the object spans several independently allocated
pages.mm/usercopy.c new file +2
-0+*/ + for (; ptr <= end ; ptr += PAGE_SIZE, page =
virt_to_head_page(ptr)) {mm/usercopy.c new file +58
-0+if (!PageReserved (page)) +return "<spans multiple pages>" ; + } + +return NULL ; +} + +/* +* Validates that the given object is one of: +* - known safe heap object +* - known safe stack object +* - not in kernel text +*/ +void __check_object_size (const void *ptr,unsigned long n,bool to_user) +{ +const char *err; + +/* Skip all tests if size is zero. */ +if (!n) +return ; + +/* Check for invalid addresses. */ + err =check_bogus_address (ptr, n); +if (err) + goto report; + +/* Check for bad heap object. */ + err =check_heap_object (ptr, n, to_user); +if (err) + goto report; + +/* Check for bad stack object. */ +switch (check_stack_object (ptr, n)) { +case NOT_STACK : +/* Object is not touching the current process stack. */ +break ; +case GOOD_FRAME : +case GOOD_STACK : +/* +* Object is either in the correct frame (when it +* is possible to check) or just generally on the +* process stack (when frame checking not available). +*/ +return ; +default : + err ="<process stack>" ; + goto report; + } + +/* Check for object in kernel to avoid text exposure. */ + err =check_kernel_text_object (ptr, n); +if (!err) +return ; + +report: +report_usercopy (ptr, n, to_user, err); +} +EXPORT_SYMBOL (__check_object_size);security/Kconfig +28
-0@@ -118,6 +118,34 @@ config LSM_MMAP_MIN_ADDR this low address space will need the permission specific to the systems running LSM. +config HAVE_HARDENED_USERCOPY_ALLOCATOR + bool + help + The heap allocator implements __check_heap_object() for + validating memory ranges against heap object sizes in + support of CONFIG_HARDENED_USERCOPY. + +config HAVE_ARCH_HARDENED_USERCOPY + bool + help + The architecture supports CONFIG_HARDENED_USERCOPY by + calling check_object_size() just before performing the + userspace copies in the low level implementation of + copy_to_user() and copy_from_user(). + +config HARDENED_USERCOPY + bool "Harden memory copies between kernel and userspace" + depends on HAVE_ARCH_HARDENED_USERCOPY + select BUG + help + This option checks for obviously wrong memory regions when + copying memory to/from the kernel (via copy_to_user() and + copy_from_user() functions) by rejecting memory ranges that + are larger than the specified heap object, span multiple + separately allocates pages, are not on the process stack, + or are part of the kernel text. This kills entire classes + of heap overflow exploits and similar kernel memory exposures. + source security/selinux/Kconfig source security/smack/Kconfig source security/tomoyo/Kconfig
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 21 lines · Kees Cook
Quoted 13 lines · Laura Abbott
mm/usercopy.c line 132
Quoted 10 lines · Kees Cook
+static inline const char *check_heap_object(const void *ptr, unsigned long n,
mm/usercopy.c new file +8
-0+ bool to_user) +{ +struct page *page, *endpage; +const void *end = ptr + n -1 ; + +if (!virt_addr_valid (ptr)) +return NULL ; +Quoted 6 lines
virt_addr_validreturns true on vmalloc addresses on arm64 which causes some intermittent false positives (tab completion in a qemu buildroot environment was showing it fairly reliably). I think this is an arm64 bug becausevirt_addr_validshould return true if and only ifvirt_to_pagereturns the corresponding page. We can work around this for now by explicitly checking againstis_vmalloc_addr.
Quoted 2 lines
Hrm, that's weird. Sounds like a bug too, but I'll add a check for
is_vmalloc_addr()to catch it for now.
BTW, if you were testing against -next, KASAN moved things around in copy_*_user() in a way I wasn't expecting (__copy* and copy* now both call __arch_copy* instead of copy* calling __copy*). I'll have this fixed in the next version.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
arch/Kconfig config HAVE_ARCH_WITHIN_STACK_FRAMES · line 441
Quoted 6 lines · Kees Cook
arch/Kconfig +6
-0+config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY.
I have trouble parsing this. (What does secondary linear mapping mean?) So let me give an example below
Quoted 1 line
+
[...]
Quoted 9 lines
mm/usercopy.c new file +9
-0+/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ;
mm/usercopy.c line 109
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif + +return NULL ; +}
s390 has an address space for user (primary address space from 0..4TB/8PB) and a separate address space (home space from 0..4TB/8PB) for the kernel. In this home space the kernel mapping is virtual containing the physical memory as well as vmalloc memory (creating aliases into the physical one). The kernel text is mapped from _stext to _etext in this mapping. So I assume this would qualify for HAVE_ARCH_LINEAR_KERNEL_MAPPING ?
Quoted 9 lines · Christian Borntraeger
arch/Kconfig config HAVE_ARCH_WITHIN_STACK_FRAMES · line 441
Quoted 6 lines · Kees Cook
arch/Kconfig +6
-0+config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY.I have trouble parsing this. (What does secondary linear mapping mean?)
I likely need help clarifying this language...
Quoted 21 lines
So let me give an example below
Quoted 1 line
+
[...]
Quoted 17 lines
mm/usercopy.c new file +17
-0+/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif +
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return NULL; +}s390 has an address space for user (primary address space from 0..4TB/8PB) and a separate address space (home space from 0..4TB/8PB) for the kernel. In this home space the kernel mapping is virtual containing the physical memory as well as vmalloc memory (creating aliases into the physical one). The kernel text is mapped from
_stextto_etextin this mapping. So I assume this would qualify forHAVE_ARCH_LINEAR_KERNEL_MAPPING?
If I understand your example, yes. In the home space you have two addresses that reference the kernel image? The intent is that if __va(__pa(_stext)) != _stext, there's a linear mapping of physical memory in the virtual memory range. On x86_64, the kernel is visible in two locations in virtual memory. The kernel start in physical memory address 0x01000000 maps to virtual address 0xffff880001000000, and the "regular" virtual memory kernel address is at 0xffffffff81000000:
# grep Kernel /proc/iomem 01000000-01a59767 : Kernel code 01a59768-0213d77f : Kernel data 02280000-02fdefff : Kernel bss
# grep startup_64 /proc/kallsyms ffffffff81000000 T startup_64
# less /sys/kernel/debug/kernel_page_tables ... ---[ Low Kernel Mapping ]--- ... 0xffff880001000000-0xffff880001a00000 10M ro PSE GLB NX pmd 0xffff880001a00000-0xffff880001a5c000 368K ro GLB NX pte 0xffff880001a5c000-0xffff880001c00000 1680K RW GLB NX pte ... ---[ High Kernel Mapping ]--- ... 0xffffffff81000000-0xffffffff81a00000 10M ro PSE GLB x pmd 0xffffffff81a00000-0xffffffff81a5c000 368K ro GLB x pte 0xffffffff81a5c000-0xffffffff81c00000 1680K RW GLB NX pte ...
I wonder if I can avoid the CONFIG entirely if I just did a __va(__pa(_stext)) != _stext test... would that break anyone?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 44 lines · Kees Cook
Quoted 9 lines · Christian Borntraeger
arch/Kconfig config HAVE_ARCH_WITHIN_STACK_FRAMES · line 441
Quoted 6 lines · Kees Cook
arch/Kconfig +6
-0+config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY.I have trouble parsing this. (What does secondary linear mapping mean?)
I likely need help clarifying this language...
Quoted 21 lines
So let me give an example below
Quoted 1 line
+
[...]
Quoted 17 lines
mm/usercopy.c new file +17
-0+/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif +Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return NULL; +}s390 has an address space for user (primary address space from 0..4TB/8PB) and a separate address space (home space from 0..4TB/8PB) for the kernel. In this home space the kernel mapping is virtual containing the physical memory as well as vmalloc memory (creating aliases into the physical one). The kernel text is mapped from
_stextto_etextin this mapping. So I assume this would qualify forHAVE_ARCH_LINEAR_KERNEL_MAPPING?
Quoted 2 lines
If I understand your example, yes. In the home space you have two addresses that reference the kernel image?
No, there is only one address that points to the kernel. As we have no kernel ASLR yet, and the kernel mapping is a 1:1 mapping from 0 to memory end and the kernel is only from _stext to _etext. The vmalloc area contains modules and vmalloc but not a 2nd kernel mapping.
But thanks for your example, now I understood. If we have only one address
Quoted 2 lines
Quoted 2 lines
mm/usercopy.c line 99
Quoted 2 lines
mm/usercopy.c new file +2
-0+if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ;
This is just enough.
So what about for the CONFIG text:
An architecture should select this if the kernel mapping has a secondary linear mapping of the kernel text - in other words more than one virtual kernel address that points to the kernel image. This is used to verify that kernel text exposures are not visible under CONFIG_HARDENED_USERCOPY.
Quoted 2 lines
I wonder if I can avoid the CONFIG entirely if I just did a __va(__pa(_stext)) !=
_stexttest... would that break anyone?
Can this be resolved on all platforms at compile time?
Quoted 59 lines · Christian Borntraeger
Quoted 44 lines · Kees Cook
Quoted 9 lines · Christian Borntraeger
arch/Kconfig config HAVE_ARCH_WITHIN_STACK_FRAMES · line 441
Quoted 6 lines · Kees Cook
arch/Kconfig +6
-0+config HAVE_ARCH_LINEAR_KERNEL_MAPPING + bool + help + An architecture should select this if it has a secondary linear + mapping of the kernel text. This is used to verify that kernel + text exposures are not visible under CONFIG_HARDENED_USERCOPY.I have trouble parsing this. (What does secondary linear mapping mean?)
I likely need help clarifying this language...
Quoted 21 lines
So let me give an example below
Quoted 1 line
+
[...]
Quoted 17 lines
mm/usercopy.c new file +17
-0+/* Is this address range in the kernel text area? */ +static inline const char *check_kernel_text_object (const void *ptr, +unsigned long n) +{ +unsigned long textlow = (unsigned long )_stext; +unsigned long texthigh = (unsigned long )_etext; + +if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ; + +#ifdef HAVE_ARCH_LINEAR_KERNEL_MAPPING +/* Check against linear mapping as well. */ +if (overlaps (ptr, n, (unsigned long )__va (__pa (textlow)), + (unsigned long )__va (__pa (texthigh)))) +return "<linear kernel text>" ; +#endif +Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return NULL; +}s390 has an address space for user (primary address space from 0..4TB/8PB) and a separate address space (home space from 0..4TB/8PB) for the kernel. In this home space the kernel mapping is virtual containing the physical memory as well as vmalloc memory (creating aliases into the physical one). The kernel text is mapped from
_stextto_etextin this mapping. So I assume this would qualify forHAVE_ARCH_LINEAR_KERNEL_MAPPING?Quoted 2 lines
If I understand your example, yes. In the home space you have two addresses that reference the kernel image?
No, there is only one address that points to the kernel. As we have no kernel ASLR yet, and the kernel mapping is a 1:1 mapping from 0 to memory end and the kernel is only from
_stextto_etext. The vmalloc area contains modules and vmalloc but not a 2nd kernel mapping.But thanks for your example, now I understood. If we have only one address
Quoted 2 lines
Quoted 2 lines
mm/usercopy.c line 99
Quoted 2 lines
mm/usercopy.c new file +2
-0+if (overlaps (ptr, n, textlow, texthigh)) +return "<kernel text>" ;
Quoted 8 lines
This is just enough.
So what about for the CONFIG text:
An architecture should select this if the kernel mapping has a secondary linear mapping of the kernel text - in other words more than one virtual kernel address that points to the kernel image. This is used to verify that kernel text exposures are not visible under
CONFIG_HARDENED_USERCOPY.
Sounds good, I've adjusted it for now.
Quoted 4 lines
Quoted 2 lines
I wonder if I can avoid the CONFIG entirely if I just did a __va(__pa(_stext)) !=
_stexttest... would that break anyone?Can this be resolved on all platforms at compile time?
Well, I think it still needs a runtime check (compile-time may not be able to tell about kaslr, or who knows what else). I would really like to avoid the CONFIG if possible, though. Would this do the right thing on s390? This appears to work where I'm able to test it (32/64 x86, 32/64 arm):
unsigned long textlow = (unsigned long)_stext;
unsigned long texthigh = (unsigned long)_etext;
unsigned long textlow_linear = (unsigned long)__va(__pa(textlow);
unsigned long texthigh_linear = (unsigned long)__va(__pa(texthigh);
if (overlaps(ptr, n, textlow, texthigh))
return "<kernel text>";
/* Check against possible secondary linear mapping as well. */
if (textlow != textlow_linear &&
overlaps(ptr, n, textlow_linear, texthigh_linear))
return "<linear kernel text>";
return NULL;-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
On 07/19/2016 10:34 PM, Kees Cook wrote:
[...]
Quoted 21 lines
Quoted 7 lines
So what about for the CONFIG text:
An architecture should select this if the kernel mapping has a secondary linear mapping of the kernel text - in other words more than one virtual kernel address that points to the kernel image. This is used to verify that kernel text exposures are not visible under
CONFIG_HARDENED_USERCOPY.Sounds good, I've adjusted it for now.
Quoted 4 lines
Quoted 2 lines
I wonder if I can avoid the CONFIG entirely if I just did a __va(__pa(_stext)) !=
_stexttest... would that break anyone?Can this be resolved on all platforms at compile time?
Well, I think it still needs a runtime check (compile-time may not be able to tell about kaslr, or who knows what else). I would really like to avoid the CONFIG if possible, though. Would this do the right thing on s390? This appears to work where I'm able to test it (32/64 x86, 32/64 arm):
Quoted 5 lines
unsigned long textlow = (unsigned long)_stext;
unsigned long texthigh = (unsigned long)_etext;
unsigned long textlow_linear = (unsigned long)__va(__pa(textlow);
unsigned long texthigh_linear = (unsigned long)__va(__pa(texthigh);as we have
#define PAGE_OFFSET 0x0UL #define __pa(x) (unsigned long)(x) #define __va(x) (void *)(unsigned long)(x)
both should be identical on s390 as of today, so it should work fine and only do the check once
Quoted 11 lines
if (overlaps(ptr, n, textlow, texthigh))
return "<kernel text>";
/* Check against possible secondary linear mapping as well. */
if (textlow != textlow_linear &&
overlaps(ptr, n, textlow_linear, texthigh_linear))
return "<linear kernel text>";
return NULL;Quoted 2 lines
-Kees
PS: Not sure how useful and flexible this offers is but you can get some temporary free access to an s390 on https://developer.ibm.com/linuxone/
Michael Ellerman
reviewerQuoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@
...
Quoted 4 lines
mm/usercopy.c new file +4
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible).
mm/usercopy.c line 40
Quoted 10 lines
mm/usercopy.c new file +10
-0+* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;
That allows access to the entire stack, including the struct thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
We have end_of_stack() which computes the end of the stack taking thread_info into account (end being the opposite of your end above).
cheers
Michael Ellerman
reviewerQuoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@
...
Quoted 4 lines
mm/usercopy.c new file +4
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible).
mm/usercopy.c line 40
Quoted 10 lines
mm/usercopy.c new file +10
-0+* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;
That allows access to the entire stack, including the struct thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
We have end_of_stack() which computes the end of the stack taking thread_info into account (end being the opposite of your end above).
cheers
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Michael Ellerman
reviewerQuoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@
...
Quoted 4 lines
mm/usercopy.c new file +4
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible).
mm/usercopy.c line 40
Quoted 10 lines
mm/usercopy.c new file +10
-0+* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;
That allows access to the entire stack, including the struct thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
We have end_of_stack() which computes the end of the stack taking thread_info into account (end being the opposite of your end above).
cheers
Michael Ellerman
reviewerQuoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@
...
Quoted 4 lines
mm/usercopy.c new file +4
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible).
mm/usercopy.c line 40
Quoted 10 lines
mm/usercopy.c new file +10
-0+* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;
That allows access to the entire stack, including the struct thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
We have end_of_stack() which computes the end of the stack taking thread_info into account (end being the opposite of your end above).
cheers
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Michael Ellerman
reviewerQuoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@
...
Quoted 4 lines
mm/usercopy.c new file +4
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible).
mm/usercopy.c line 40
Quoted 10 lines
mm/usercopy.c new file +10
-0+* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;
That allows access to the entire stack, including the struct thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
We have end_of_stack() which computes the end of the stack taking thread_info into account (end being the opposite of your end above).
cheers
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Michael Ellerman
reviewerQuoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@
...
Quoted 4 lines
mm/usercopy.c new file +4
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible).
mm/usercopy.c line 40
Quoted 10 lines
mm/usercopy.c new file +10
-0+* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ +static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;
That allows access to the entire stack, including the struct thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
We have end_of_stack() which computes the end of the stack taking thread_info into account (end being the opposite of your end above).
cheers
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 19 lines · Michael Ellerman
Quoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@...
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/
Quoted 8 lines
mm/usercopy.c line 40
Quoted 4 lines
mm/usercopy.c new file +4
-0+static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;That allows access to the entire stack, including the struct
thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?
That seems like a nice improvement to make, yeah.
Quoted 2 lines
We have
end_of_stack()which computes the end of the stack takingthread_infointo account (end being the opposite of your end above).
Amusingly, the object_is_on_stack() check in sched.h doesn't take thread_info into account either. :P Regardless, I think using end_of_stack() may not be best. To tighten the check, I think we could add this after checking that the object is on the stack:
#ifdef CONFIG_STACK_GROWSUP
stackend -= sizeof(struct thread_info);
#else
stack += sizeof(struct thread_info);#endif
e.g. then if the pointer was in the thread_info, the second test would fail, triggering the protection.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 39 lines · Kees Cook
Quoted 19 lines · Michael Ellerman
Quoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@...
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ Quoted 8 lines
mm/usercopy.c line 40
Quoted 4 lines
mm/usercopy.c new file +4
-0+static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;That allows access to the entire stack, including the struct
thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?That seems like a nice improvement to make, yeah.
Quoted 2 lines
We have
end_of_stack()which computes the end of the stack takingthread_infointo account (end being the opposite of your end above).Amusingly, the
object_is_on_stack()check insched.hdoesn't takethread_infointo account either. :P Regardless, I think usingend_of_stack()may not be best. To tighten the check, I think we could add this after checking that the object is on the stack:
Quoted 8 lines
#ifdef CONFIG_STACK_GROWSUP stackend -= sizeof(struct thread_info); #else stack += sizeof(struct thread_info);#endif
e.g. then if the pointer was in the
thread_info, the second test would fail, triggering the protection.
FWIW, this won't work right on x86 after Andy'sCONFIG_THREAD_INFO_IN_TASK patches get merged.
Signature · 1 line
Josh
From: Josh Poimboeuf
Quoted 1 line
Sent: 22 July 2016 18:46
..
Quoted 34 lines
Quoted 34 lines
Quoted 9 lines
Quoted 9 lines
+/*
+ * Checks if a given pointer and length is contained by the current
+ * stack frame (if possible).
+ *+ * 0: not at all on the stack + * 1: fully within a valid stack frame + * 2: fully on the stack (when can't do frame-checking) + * -1: error condition (invalid stack position or bad stack frame)+ */
Quoted 8 lines
mm/usercopy.c line 40
Quoted 4 lines
mm/usercopy.c new file +4
-0+static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;That allows access to the entire stack, including the struct
thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?That seems like a nice improvement to make, yeah.
Quoted 2 lines
We have
end_of_stack()which computes the end of the stack takingthread_infointo account (end being the opposite of your end above).Amusingly, the
object_is_on_stack()check insched.hdoesn't takethread_infointo account either. :P Regardless, I think usingend_of_stack()may not be best. To tighten the check, I think we could add this after checking that the object is on the stack:#ifdef CONFIG_STACK_GROWSUP stackend -= sizeof(struct thread_info); #else stack += sizeof(struct thread_info);#endif
Quoted 5 lines
Quoted 2 lines
e.g. then if the pointer was in the
thread_info, the second test would fail, triggering the protection.FWIW, this won't work right on x86 after Andy's
CONFIG_THREAD_INFO_IN_TASKpatches get merged.
What ends up in the 'thread_info' area?
If it contains the fp save area then programs like gdb may end up requesting copy_in/out directly from that area.
Interestingly the avx registers don't need saving on a normal system call entry (they are all caller-saved) so the kernel stack can safely overwrite that area.
Syscall entry probably ought to execute the 'zero all avx registers' instruction. They do need saving on interrupt entry - but the stack used will be less.
David
Michael Ellerman
reviewerQuoted 10 lines · David Laight
From: Josh Poimboeuf
Quoted 7 lines
Sent: 22 July 2016 18:46
Quoted 3 lines
e.g. then if the pointer was in the
thread_info, the second test would fail, triggering the protection.FWIW, this won't work right on x86 after Andy's
CONFIG_THREAD_INFO_IN_TASKpatches get merged.What ends up in the '
thread_info' area?
It depends on the arch.
Quoted 2 lines
If it contains the fp save area then programs like gdb may end up requesting copy_in/out directly from that area.
On the arches I've seen thread_info doesn't usually contain register save areas, but if it did then it would be up to the arch helper to allow that copy to go through.
However given thread_info generally contains lots of low level flags that would be a good target for an attacker, the best way to cope with ptrace wanting to copy to/from it would be to use a temporary, and prohibit copying directly to/from thread_info - IMHO.
cheers
Michael Ellerman
reviewerQuoted 46 lines · Josh Poimboeuf
Quoted 45 lines · Kees Cook
Quoted 19 lines · Michael Ellerman
Quoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@...
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ Quoted 8 lines
mm/usercopy.c line 40
Quoted 4 lines
mm/usercopy.c new file +4
-0+static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;That allows access to the entire stack, including the struct
thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?That seems like a nice improvement to make, yeah.
Quoted 2 lines
We have
end_of_stack()which computes the end of the stack takingthread_infointo account (end being the opposite of your end above).Amusingly, the
object_is_on_stack()check insched.hdoesn't takethread_infointo account either. :P Regardless, I think usingend_of_stack()may not be best. To tighten the check, I think we could add this after checking that the object is on the stack:#ifdef CONFIG_STACK_GROWSUP stackend -= sizeof(struct thread_info); #else stack += sizeof(struct thread_info);#endif
Quoted 5 lines
Quoted 2 lines
e.g. then if the pointer was in the
thread_info, the second test would fail, triggering the protection.FWIW, this won't work right on x86 after Andy's
CONFIG_THREAD_INFO_IN_TASKpatches get merged.
Yeah. I wonder if it's better for the arch helper to just take the obj and len, and work out it's own bounds for the stack using current and whatever makes sense on that arch.
It would avoid too much ifdefery in the generic code, and also avoid any confusion about whether stackend is the high or low address.
eg. on powerpc we could do:
int noinline arch_within_stack_frames(const void *obj, unsigned long len)
{
void *stack_low = end_of_stack(current);
void *stack_high = task_stack_page(current) + THREAD_SIZE;Whereas arches with STACK_GROWSUP=y could do roughly the reverse, and x86 can do whatever it needs to depending on whether the thread_info is on or off stack.
cheers
Quoted 69 lines · Michael Ellerman
Quoted 46 lines · Josh Poimboeuf
Quoted 45 lines · Kees Cook
Quoted 19 lines · Michael Ellerman
Quoted 6 lines · Kees Cook
mm/usercopy.c new file +0
-0@@ -0,0 +1,234 @@...
Quoted 10 lines
mm/usercopy.c new file +10
-0+ +/* +* Checks if a given pointer and length is contained by the current +* stack frame (if possible). +* +* 0: not at all on the stack +* 1: fully within a valid stack frame +* 2: fully on the stack (when can't do frame-checking) +* -1: error condition (invalid stack position or bad stack frame) +*/ Quoted 8 lines
mm/usercopy.c line 40
Quoted 4 lines
mm/usercopy.c new file +4
-0+static noinline int check_stack_object(const void *obj, unsigned long len) +{ +const void *const stack =task_stack_page (current); +const void *const stackend = stack +THREAD_SIZE ;That allows access to the entire stack, including the struct
thread_info, is that what we want - it seems dangerous? Or did I miss a check somewhere else?That seems like a nice improvement to make, yeah.
Quoted 2 lines
We have
end_of_stack()which computes the end of the stack takingthread_infointo account (end being the opposite of your end above).Amusingly, the
object_is_on_stack()check insched.hdoesn't takethread_infointo account either. :P Regardless, I think usingend_of_stack()may not be best. To tighten the check, I think we could add this after checking that the object is on the stack:#ifdef CONFIG_STACK_GROWSUP stackend -= sizeof(struct thread_info); #else stack += sizeof(struct thread_info);#endif
Quoted 5 lines
Quoted 2 lines
e.g. then if the pointer was in the
thread_info, the second test would fail, triggering the protection.FWIW, this won't work right on x86 after Andy's
CONFIG_THREAD_INFO_IN_TASKpatches get merged.Yeah. I wonder if it's better for the arch helper to just take the obj and len, and work out it's own bounds for the stack using current and whatever makes sense on that arch.
It would avoid too much ifdefery in the generic code, and also avoid any confusion about whether stackend is the high or low address.
eg. on powerpc we could do:
int noinline
arch_within_stack_frames(const void *obj, unsigned long len){ void *stack_low = end_of_stack(current); void *stack_high = task_stack_page(current) + THREAD_SIZE;
Quoted 4 lines
Whereas arches with STACK_GROWSUP=y could do roughly the reverse, and x86 can do whatever it needs to depending on whether the
thread_infois on or off stack.cheers
Yeah, I agree: this should be in the arch code. If the arch can actually do frame checking, the thread_info (if it exists on the stack) would already be excluded. But it'd be a nice tightening of the check.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH v3 3/11 x86/uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on x86. This is done both in copy_*_user() and __copy_*_user() because copy_*_user() actually calls down to _copy_*_user() and not __copy_*_user().
Based on code from PaX and grsecurity.
--- arch/x86/Kconfig | 2 ++ arch/x86/include/asm/uaccess.h | 10 ++++++---- arch/x86/include/asm/uaccess_32.h | 2 ++ arch/x86/include/asm/uaccess_64.h | 2 ++ 4 files changed, 12 insertions(+), 4 deletions(-)
arch/x86/Kconfig +2 -0
@@ -80,11 +80,13 @@ config X86 select HAVE_ALIGNED_STRUCT_PAGE if SLUB select HAVE_AOUT if X86_32 select HAVE_ARCH_AUDITSYSCALL + select HAVE_ARCH_HARDENED_USERCOPY select HAVE_ARCH_HUGE_VMAP if X86_64 || X86_PAE select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_KASAN if X86_64 && SPARSEMEM_VMEMMAP select HAVE_ARCH_KGDB select HAVE_ARCH_KMEMCHECK + select HAVE_ARCH_LINEAR_KERNEL_MAPPING if X86_64 select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT select HAVE_ARCH_SECCOMP_FILTER
arch/x86/include/asm/uaccess.h +6 -4
@@ -742,9 +742,10 @@ copy_from_user(void *to, const void __user *from, unsigned long n)* case, and do only runtime checking for non-constant sizes. */ -if (likely (sz <0 || sz >= n)) +if (likely (sz <0 || sz >= n)) { +check_object_size (to, n, false); n =_copy_from_user (to, from, n); -else if (__builtin_constant_p (n)) + }else if (__builtin_constant_p (n))copy_from_user_overflow ();else __copy_from_user_overflow (sz, n);
@@ -762,9 +763,10 @@ copy_to_user(void __user *to, const void *from, unsigned long n)might_fault ();/* See the comment in copy_from_user() above. */ -if (likely (sz <0 || sz >= n)) +if (likely (sz <0 || sz >= n)) { +check_object_size (from, n, true); n =_copy_to_user (to, from, n); -else if (__builtin_constant_p (n)) + }else if (__builtin_constant_p (n))copy_to_user_overflow ();else __copy_to_user_overflow (sz, n);
arch/x86/include/asm/uaccess_32.h +2 -0
@@ -37,6 +37,7 @@ unsigned long __must_check __copy_from_user_ll_nocache_nozerostatic __always_inline unsigned long __must_check __copy_to_user_inatomic (void __user *to,const void *from,unsigned long n) { +check_object_size (from, n, true);return __copy_to_user_ll (to, from, n); }
@@ -95,6 +96,7 @@ static __always_inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n) {might_fault (); +check_object_size (to, n, false);if (__builtin_constant_p (n)) {unsigned long ret;
arch/x86/include/asm/uaccess_64.h +2 -0
@@ -54,6 +54,7 @@ int __copy_from_user_nocheck(void *dst, const void __user *src, unsigned size) {int ret =0 ; +check_object_size (dst, size, false);if (!__builtin_constant_p (size))return copy_user_generic (dst, (__force void *)src, size);switch (size) {
@@ -119,6 +120,7 @@ int __copy_to_user_nocheck(void __user *dst, const void *src, unsigned size) {int ret =0 ; +check_object_size (src, size, true);if (!__builtin_constant_p (size))return copy_user_generic ((__force void *)dst, src, size);switch (size) {
Signature · 1 line
2.7.4
PATCH v3 4/11 ARM: uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on arm.
Based on code from PaX and grsecurity.
--- arch/arm/Kconfig | 1 + arch/arm/include/asm/uaccess.h | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/Kconfig +1 -0
@@ -35,6 +35,7 @@ config ARM select HARDIRQS_SW_RESEND select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT) select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6 + select HAVE_ARCH_HARDENED_USERCOPY select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU select HAVE_ARCH_MMAP_RND_BITS if MMU
arch/arm/include/asm/uaccess.h +9 -2
@@ -496,7 +496,10 @@ arm_copy_from_user(void *to, const void __user *from, unsigned long n);static inline unsigned long __must_check __copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned int __ua_flags =uaccess_save_and_enable (); +unsigned int __ua_flags; + +check_object_size (to, n, false); + __ua_flags =uaccess_save_and_enable (); n =arm_copy_from_user (to, from, n);uaccess_restore (__ua_flags);return n;
@@ -511,11 +514,15 @@ static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n) {#ifndef CONFIG_UACCESS_WITH_MEMCPY -unsigned int __ua_flags =uaccess_save_and_enable (); +unsigned int __ua_flags; + +check_object_size (from, n, true); + __ua_flags =uaccess_save_and_enable (); n =arm_copy_to_user (to, from, n);uaccess_restore (__ua_flags);return n;#else +check_object_size (from, n, true);return arm_copy_to_user (to, from, n);#endif }
Signature · 1 line
2.7.4
PATCH v3 5/11 arm64/uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on arm64. As done by KASAN in -next, renames the low-level functions to __arch_copy_*_user() so a static inline can do additional work before the copy.
--- arch/arm64/Kconfig | 2 ++ arch/arm64/include/asm/uaccess.h | 16 ++++++++++++++-- arch/arm64/kernel/arm64ksyms.c | 4 ++-- arch/arm64/lib/copy_from_user.S | 4 ++-- arch/arm64/lib/copy_to_user.S | 4 ++-- 5 files changed, 22 insertions(+), 8 deletions(-)
arch/arm64/Kconfig +2 -0
@@ -51,10 +51,12 @@ config ARM64 select HAVE_ALIGNED_STRUCT_PAGE if SLUB select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_BITREVERSE + select HAVE_ARCH_HARDENED_USERCOPY select HAVE_ARCH_HUGE_VMAP select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_KASAN if SPARSEMEM_VMEMMAP && !(ARM64_16K_PAGES && ARM64_VA_BITS_48) select HAVE_ARCH_KGDB + select HAVE_ARCH_LINEAR_KERNEL_MAPPING select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT select HAVE_ARCH_SECCOMP_FILTER
arch/arm64/include/asm/uaccess.h +14 -2
@@ -256,11 +256,23 @@ do { \ -EFAULT ; \ }) -extern unsigned long __must_check __copy_from_user (void *to,const void __user *from,unsigned long n); -extern unsigned long __must_check __copy_to_user (void __user *to,const void *from,unsigned long n); +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);extern unsigned long __must_check __clear_user (void __user *addr,unsigned long n); +static inline unsigned long __must_check __copy_from_user (void *to,const void __user *from,unsigned long n) +{ +check_object_size (to, n, 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) +{ +check_object_size (from, n, true); +return __arch_copy_to_user (to, from, n); +} +static inline unsigned long __must_check copy_from_user (void *to,const void __user *from,unsigned long n) {if (access_ok (VERIFY_READ , from, n))
arch/arm64/kernel/arm64ksyms.c +2 -2
@@ -34,8 +34,8 @@ EXPORT_SYMBOL(copy_page);EXPORT_SYMBOL (clear_page);/* user mem (segment) */ -EXPORT_SYMBOL (__copy_from_user); -EXPORT_SYMBOL (__copy_to_user); +EXPORT_SYMBOL (__arch_copy_from_user); +EXPORT_SYMBOL (__arch_copy_to_user);EXPORT_SYMBOL (__clear_user);EXPORT_SYMBOL (__copy_in_user);
arch/arm64/lib/copy_from_user.S +2 -2
@@ -66,7 +66,7 @@ .endm end .req x5 -ENTRY(__copy_from_user) +ENTRY(__arch_copy_from_user) ALTERNATIVE("nop", __stringify(SET_PSTATE_PAN(0)), ARM64_ALT_PAN_NOT_UAO, \ CONFIG_ARM64_PAN) add end, x0, x2
@@ -75,7 +75,7 @@ ALTERNATIVE("nop", __stringify(SET_PSTATE_PAN(1)), ARM64_ALT_PAN_NOT_UAO, \ CONFIG_ARM64_PAN) mov x0, #0 // Nothing to copy ret -ENDPROC(__copy_from_user) +ENDPROC(__arch_copy_from_user) .section .fixup,"ax" .align 2
arch/arm64/lib/copy_to_user.S +2 -2
@@ -65,7 +65,7 @@ .endm end .req x5 -ENTRY(__copy_to_user) +ENTRY(__arch_copy_to_user) ALTERNATIVE("nop", __stringify(SET_PSTATE_PAN(0)), ARM64_ALT_PAN_NOT_UAO, \ CONFIG_ARM64_PAN) add end, x0, x2
@@ -74,7 +74,7 @@ ALTERNATIVE("nop", __stringify(SET_PSTATE_PAN(1)), ARM64_ALT_PAN_NOT_UAO, \ CONFIG_ARM64_PAN) mov x0, #0 ret -ENDPROC(__copy_to_user) +ENDPROC(__arch_copy_to_user) .section .fixup,"ax" .align 2
Signature · 1 line
2.7.4
PATCH v3 6/11 ia64/uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on ia64.
Based on code from PaX and grsecurity.
--- arch/ia64/Kconfig | 1 + arch/ia64/include/asm/uaccess.h | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-)
arch/ia64/Kconfig +1 -0
@@ -53,6 +53,7 @@ config IA64 select MODULES_USE_ELF_RELA select ARCH_USE_CMPXCHG_LOCKREF select HAVE_ARCH_AUDITSYSCALL + select HAVE_ARCH_HARDENED_USERCOPY default y help The Itanium Processor Family is Intel's 64-bit successor to
arch/ia64/include/asm/uaccess.h +15 -3
@@ -241,12 +241,18 @@ extern unsigned long __must_check __copy_user (void __user *to, const void __usestatic inline unsigned long __copy_to_user (void __user *to,const void *from,unsigned long count) { +if (!__builtin_constant_p (count)) +check_object_size (from, count, true); +return __copy_user (to, (__force void __user *) from, count); }static inline unsigned long __copy_from_user (void *to,const void __user *from,unsigned long count) { +if (!__builtin_constant_p (count)) +check_object_size (to, count, false); +return __copy_user ((__force void __user *) to, from, count); }
@@ -258,8 +264,11 @@ __copy_from_user (void *to, const void __user *from, unsigned long count)const void *__cu_from = (from); \long __cu_len = (n); \ \ -if (__access_ok (__cu_to, __cu_len,get_fs ())) \ - __cu_len = __copy_user(__cu_to, (__force void __user *) __cu_from, __cu_len); \ +if (__access_ok (__cu_to, __cu_len,get_fs ())) { \ +if (!__builtin_constant_p (n)) \ +check_object_size (__cu_from, __cu_len, true); \ + __cu_len = __copy_user(__cu_to, (__force void __user *) __cu_from, __cu_len); \ + } \ __cu_len; \ })
@@ -270,8 +279,11 @@ __copy_from_user (void *to, const void __user *from, unsigned long count)long __cu_len = (n); \ \__chk_user_ptr (__cu_from); \ -if (__access_ok (__cu_from, __cu_len,get_fs ())) \ +if (__access_ok (__cu_from, __cu_len,get_fs ())) { \ +if (!__builtin_constant_p (n)) \ +check_object_size (__cu_to, __cu_len, false); \ __cu_len = __copy_user((__force void __user *) __cu_to, __cu_from, __cu_len); \ + } \ __cu_len; \ })
Signature · 1 line
2.7.4
PATCH v3 7/11 powerpc/uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on powerpc.
Based on code from PaX and grsecurity.
--- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/uaccess.h | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-)
arch/powerpc/Kconfig +1 -0
@@ -164,6 +164,7 @@ config PPC select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_SUPPORTS_DEFERRED_STRUCT_PAGE_INIT select HAVE_LIVEPATCH if HAVE_DYNAMIC_FTRACE_WITH_REGS + select HAVE_ARCH_HARDENED_USERCOPY config GENERIC_CSUM def_bool CPU_LITTLE_ENDIAN
arch/powerpc/include/asm/uaccess.h +19 -2
@@ -310,10 +310,15 @@ static inline unsigned long copy_from_user(void *to, {unsigned long over; -if (access_ok (VERIFY_READ , from, n)) +if (access_ok (VERIFY_READ , from, n)) { +if (!__builtin_constant_p (n)) +check_object_size (to, n, false);return __copy_tofrom_user ((__force void __user *)to, from, n); + }if ((unsigned long )from <TASK_SIZE ) { over = (unsigned long )from + n -TASK_SIZE ; +if (!__builtin_constant_p (n - over)) +check_object_size (to, n - over, false);return __copy_tofrom_user ((__force void __user *)to, from, n - over) + over; }
@@ -325,10 +330,15 @@ static inline unsigned long copy_to_user(void __user *to, {unsigned long over; -if (access_ok (VERIFY_WRITE , to, n)) +if (access_ok (VERIFY_WRITE , to, n)) { +if (!__builtin_constant_p (n)) +check_object_size (from, n, true);return __copy_tofrom_user (to, (__force void __user *)from, n); + }if ((unsigned long )to <TASK_SIZE ) { over = (unsigned long )to + n -TASK_SIZE ; +if (!__builtin_constant_p (n)) +check_object_size (from, n - over, true);return __copy_tofrom_user (to, (__force void __user *)from, n - over) + over; }
@@ -372,6 +382,10 @@ static inline unsigned long __copy_from_user_inatomic(void *to,if (ret ==0 )return 0 ; } + +if (!__builtin_constant_p (n)) +check_object_size (to, n, false); +return __copy_tofrom_user ((__force void __user *)to, from, n); }
@@ -398,6 +412,9 @@ static inline unsigned long __copy_to_user_inatomic(void __user *to,if (ret ==0 )return 0 ; } +if (!__builtin_constant_p (n)) +check_object_size (from, n, true); +return __copy_tofrom_user (to, (__force const void __user *)from, n); }
Signature · 1 line
2.7.4
PATCH v3 8/11 sparc/uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on sparc.
Based on code from PaX and grsecurity.
--- arch/sparc/Kconfig | 1 + arch/sparc/include/asm/uaccess_32.h | 14 ++++++++++---- arch/sparc/include/asm/uaccess_64.h | 11 +++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-)
arch/sparc/Kconfig +1 -0
@@ -43,6 +43,7 @@ config SPARC select OLD_SIGSUSPEND select ARCH_HAS_SG_CHAIN select CPU_NO_EFFICIENT_FFS + select HAVE_ARCH_HARDENED_USERCOPY config SPARC32 def_bool !64BIT
arch/sparc/include/asm/uaccess_32.h +10 -4
@@ -248,22 +248,28 @@ unsigned long __copy_user(void __user *to, const void __user *from, unsigned lonstatic inline unsigned long copy_to_user (void __user *to,const void *from,unsigned long n) { -if (n &&__access_ok ((unsigned long ) to, n)) +if (n &&__access_ok ((unsigned long ) to, n)) { +if (!__builtin_constant_p (n)) +check_object_size (from, n, true);return __copy_user (to, (__force void __user *) from, n); -else + }else return n; }static inline unsigned long __copy_to_user (void __user *to,const void *from,unsigned long n) { +if (!__builtin_constant_p (n)) +check_object_size (from, n, true);return __copy_user (to, (__force void __user *) from, n); }static inline unsigned long copy_from_user (void *to,const void __user *from,unsigned long n) { -if (n &&__access_ok ((unsigned long ) from, n)) +if (n &&__access_ok ((unsigned long ) from, n)) { +if (!__builtin_constant_p (n)) +check_object_size (to, n, false);return __copy_user ((__force void __user *) to, from, n); -else + }else return n; }
arch/sparc/include/asm/uaccess_64.h +9 -2
@@ -210,8 +210,12 @@ unsigned long copy_from_user_fixup(void *to, const void __user *from,static inline unsigned long __must_check copy_from_user (void *to,const void __user *from,unsigned long size) { -unsigned long ret =___copy_from_user (to, from, size); +unsigned long ret; +if (!__builtin_constant_p (size)) +check_object_size (to, size, false); + + ret =___copy_from_user (to, from, size);if (unlikely (ret)) ret =copy_from_user_fixup (to, from, size);
@@ -227,8 +231,11 @@ unsigned long copy_to_user_fixup(void __user *to, const void *from,static inline unsigned long __must_check copy_to_user (void __user *to,const void *from,unsigned long size) { -unsigned long ret =___copy_to_user (to, from, size); +unsigned long ret; +if (!__builtin_constant_p (size)) +check_object_size (from, size, true); + ret =___copy_to_user (to, from, size);if (unlikely (ret)) ret =copy_to_user_fixup (to, from, size);return ret;
Signature · 1 line
2.7.4
PATCH v3 9/11 s390/uaccess: Enable hardened usercopy
In reply to Kees CookEnables CONFIG_HARDENED_USERCOPY checks on s390.
--- arch/s390/Kconfig | 1 + arch/s390/lib/uaccess.c | 2 ++ 2 files changed, 3 insertions(+)
arch/s390/Kconfig +1 -0
@@ -122,6 +122,7 @@ config S390 select HAVE_ALIGNED_STRUCT_PAGE if SLUB select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_EARLY_PFN_TO_NID + select HAVE_ARCH_HARDENED_USERCOPY select HAVE_ARCH_JUMP_LABEL select CPU_NO_EFFICIENT_FFS if !HAVE_MARCH_Z9_109_FEATURES select HAVE_ARCH_SECCOMP_FILTER
arch/s390/lib/uaccess.c +2 -0
@@ -104,6 +104,7 @@ static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,unsigned long __copy_from_user (void *to,const void __user *from,unsigned long n) { +check_object_size (to, n, false);if (static_branch_likely (&have_mvcos))return copy_from_user_mvcos (to, from, n);return copy_from_user_mvcp (to, from, n);
@@ -177,6 +178,7 @@ static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,unsigned long __copy_to_user (void __user *to,const void *from,unsigned long n) { +check_object_size (from, n, true);if (static_branch_likely (&have_mvcos))return copy_to_user_mvcos (to, from, n);return copy_to_user_mvcs (to, from, n);
Signature · 1 line
2.7.4
PATCH v3 10/11 mm: SLAB hardened usercopy support
In reply to Kees CookUnder CONFIG_HARDENED_USERCOPY, this adds object size checking to the SLAB allocator to catch any copies that may span objects.
Based on code from PaX and grsecurity.
--- init/Kconfig | 1 + mm/slab.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
init/Kconfig +1 -0
@@ -1757,6 +1757,7 @@ choice config SLAB bool "SLAB" + select HAVE_HARDENED_USERCOPY_ALLOCATOR help The regular slab allocator that is established and known to work well in all environments. It organizes cache hot objects in
mm/slab.c +30 -0
@@ -4477,6 +4477,36 @@ static int __init slab_proc_init(void)module_init (slab_proc_init);#endif +#ifdef CONFIG_HARDENED_USERCOPY +/* +* Rejects objects that are incorrectly sized. +* +* Returns NULL if check passes, otherwise const char * to name of cache +* to indicate an error. +*/ +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page) +{ +struct kmem_cache *cachep; +unsigned int objnr; +unsigned long offset; + +/* Find and validate object. */ + cachep = page->slab_cache; + objnr =obj_to_index (cachep, page, (void *)ptr); +BUG_ON (objnr >= cachep->num); + +/* Find offset within object. */ + offset = ptr -index_to_obj (cachep, page, objnr) -obj_offset (cachep); + +/* Allow address range falling entirely within object size. */ +if (offset <= cachep->object_size && n <= cachep->object_size - offset) +return NULL ; + +return cachep->name; +} +#endif /* CONFIG_HARDENED_USERCOPY */ +/** * ksize - get the actual amount of memory allocated for a given object * @objp: Pointer to the object
Signature · 1 line
2.7.4
PATCH v3 11/11 mm: SLUB hardened usercopy support
In reply to Kees CookUnder CONFIG_HARDENED_USERCOPY, this adds object size checking to the SLUB allocator to catch any copies that may span objects. Includes a redzone handling fix discovered by Michael Ellerman.
Based on code from PaX and grsecurity.
--- init/Kconfig | 1 + mm/slub.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
init/Kconfig +1 -0
@@ -1765,6 +1765,7 @@ config SLAB config SLUB bool "SLUB (Unqueued Allocator)" + select HAVE_HARDENED_USERCOPY_ALLOCATOR help SLUB is a slab allocator that minimizes cache line usage instead of managing queues of cached objects (SLAB approach).
mm/slub.c +36 -0
@@ -3614,6 +3614,42 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)EXPORT_SYMBOL (__kmalloc_node);#endif +#ifdef CONFIG_HARDENED_USERCOPY +/* +* Rejects objects that are incorrectly sized. +* +* Returns NULL if check passes, otherwise const char * to name of cache +* to indicate an error. +*/ +const char *__check_heap_object (const void *ptr,unsigned long n, +struct page *page) +{ +struct kmem_cache *s; +unsigned long offset; +size_t object_size; + +/* Find object and usable object size. */ + s = page->slab_cache; + object_size =slab_ksize (s); + +/* Find offset within object. */ + offset = (ptr -page_address (page)) % s->size; + +/* Adjust for redzone and reject if within the redzone. */ +if (kmem_cache_debug (s) && s->flags &SLAB_RED_ZONE ) { +if (offset < s->red_left_pad) +return s->name; + offset -= s->red_left_pad; + } + +/* Allow address range falling entirely within object size. */ +if (offset <= object_size && n <= object_size - offset) +return NULL ; + +return s->name; +} +#endif /* CONFIG_HARDENED_USERCOPY */ +static size_t __ksize (const void *object) {struct page *page;
Signature · 1 line
2.7.4
Quoted 86 lines · Kees Cook
Hi,
[I'm going to carry this series in my kspp -next tree now, though I'd really love to have some explicit Acked-bys or Reviewed-bys. If you've looked through it or tested it, please consider it. :) (I added Valdis and mpe's Tested-bys where they seemed correct, thank you!)]
This is a start of the mainline port of PAX_USERCOPY[1]. After I started writing tests (now in lkdtm in -next) for Casey's earlier port[2], I kept tweaking things further and further until I ended up with a whole new patch series. To that end, I took Rik and other people's feedback along with other changes and clean-ups.
Based on my understanding,PAX_USERCOPYwas designed to catch a few classes of flaws (mainly bad bounds checking) around the use of copy_to_user()/copy_from_user(). These changes don't touchget_user()andput_user(), since these operate on constant sized lengths, and tend to be much less vulnerable. There are effectively three distinct protections in the whole series, each of which I've given a separate CONFIG, though this patch set is only the first of the three intended protections. (Generally speaking,PAX_USERCOPYcovers what I'm callingCONFIG_HARDENED_USERCOPY(this) andCONFIG_HARDENED_USERCOPY_WHITELIST(future), andPAX_USERCOPY_SLABScoversCONFIG_HARDENED_USERCOPY_SPLIT_KMALLOC(future).)
This series, which addsCONFIG_HARDENED_USERCOPY, checks that objects being copied to/from userspace meet certain criteria:
- if address is a heap object, the size must not exceed the object's allocated size. (This will catch all kinds of heap overflow flaws.)
- if address range is in the current process stack, it must be within the current stack frame (if such checking is possible) or at least entirely within the current process's stack. (This could catch large lengths that would have extended beyond the current process stack, or overflows if their length extends back into the original stack.)
- if the address range is part of kernel data, rodata, or bss, allow it.
- if address range is page-allocated, that it doesn't span multiple allocations.
- if address is within the kernel text, reject it.
- everything else is accepted
The patches in the series are:
- Support for arch-specific stack frame checking (which will likely be replaced in the future by Josh's more comprehensive unwinder):
1- mm: Implement stack frame object validation
- The core copy_to/from_user() checks, without the slab object checks:
2- mm: Hardened usercopy
- Per-arch enablement of the protection:
3- x86/uaccess: Enable hardened usercopy
4- ARM: uaccess: Enable hardened usercopy
5- arm64/uaccess: Enable hardened usercopy
6- ia64/uaccess: Enable hardened usercopy
7- powerpc/uaccess: Enable hardened usercopy
8- sparc/uaccess: Enable hardened usercopy
9- s390/uaccess: Enable hardened usercopy
- The heap allocator implementation of object size checking:
10- mm: SLAB hardened usercopy support
11- mm: SLUB hardened usercopy support
Some notes:
- This is expected to apply on top of -next which contains fixes for the position of_etexton both arm and arm64, though it has minor conflicts with KASAN that are trivial to fix up. Living in -next are also tests for this protection in lkdtm, prefixed withUSERCOPY_.
- I couldn't detect a measurable performance change with these features enabled. Kernel build times were unchanged, hackbench was unchanged, etc. I think we could flip this to "on by default" at some point, but for now, I'm leaving it off until I can get some more definitive measurements. I would love if someone with greater familiarity with perf could give this a spin and report results.
- The SLOB support extracted from grsecurity seems entirely broken. I have no idea what's going on there, I spent my time testing SLAB and SLUB. Having someone else look at SLOB would be nice, but this series doesn't depend on it.
Additional features that would be nice, but aren't blocking this series:
- Needs more architecture support for stack frame checking (only x86 now, but it seems Josh will have a good solution for this soon).
Thanks!
-Kees
Quoted 9 lines
[1] https://grsecurity.net/download.php "grsecurity - test kernel patch"
[2] http://www.openwall.com/lists/kernel-hardening/2016/05/19/5
v3:
- switch to using BUG for better Oops integration
- when checking page allocations, check each for Reserved
- use enums for the stack check return for readability
Thanks looks good so far! I'll try and test it and report back
Balbir
From: Kees Cook
Quoted 2 lines
Sent: 15 July 2016 22:44
This is a start of the mainline port of PAX_USERCOPY[1].
...
Quoted 3 lines
- if address range is in the current process stack, it must be within the current stack frame (if such checking is possible) or at least entirely within the current process's stack.
...
That description doesn't seem quite right to me.
I presume the check is:
Within the current process's stack and not crossing the ends of the current stack frame.
The 'current' stack frame is likely to be that of copy_to/from_user(). Even if you use the stack of the caller, any problematic buffers are likely to have been passed in from a calling function. So unless you are going to walk the stack (good luck on that) I'm not sure checking the stack frames is worth it.
I'd also guess that a lot of copies are from the middle of structures so cannot fail the tests you are adding.
David
Quoted 7 lines · David Laight
From: Kees Cook
Quoted 2 lines
Sent: 15 July 2016 22:44
This is a start of the mainline port of PAX_USERCOPY[1]....
Quoted 3 lines
- if address range is in the current process stack, it must be within the current stack frame (if such checking is possible) or at least entirely within the current process's stack.
Quoted 6 lines
...
That description doesn't seem quite right to me.
I presume the check is:
Within the current process's stack and not crossing the ends of the current stack frame.
Actually, it's a bad description all around. :) The check is that the range is within a valid stack frame (current or any prior caller's frame). i.e. it does not cross a frame or touch the saved frame pointer nor instruction pointer.
Quoted 5 lines
The 'current' stack frame is likely to be that of copy_to/from_user(). Even if you use the stack of the caller, any problematic buffers are likely to have been passed in from a calling function. So unless you are going to walk the stack (good luck on that) I'm not sure checking the stack frames is worth it.
Yup: that's exactly what it's doing: walking up the stack. :)
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
From: Kees Cook
Quoted 1 line
Sent: 20 July 2016 16:32
...
Quoted 1 line
Yup: that's exactly what it's doing: walking up the stack. :)
Remind me to make sure all our customers run kernels with it disabled.
David
Quoted 7 lines · David Laight
From: Kees Cook
Quoted 1 line
Sent: 20 July 2016 16:32
...
Quoted 1 line
Yup: that's exactly what it's doing: walking up the stack. :)
Remind me to make sure all our customers run kernels with it disabled.
You want a single copy_from_user to write to data in multiple stack frames?
Signature · 1 line
All Rights Reversed.
Quoted 6 lines · David Laight
From: Kees Cook
Quoted 1 line
Sent: 20 July 2016 16:32
...
Quoted 1 line
Yup: that's exactly what it's doing: walking up the stack. :)
Remind me to make sure all our customers run kernels with it disabled.
What's your concern with stack walking?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security