1f5509cc1SKees Cook /* 2f5509cc1SKees Cook * This implements the various checks for CONFIG_HARDENED_USERCOPY*, 3f5509cc1SKees Cook * which are designed to protect kernel memory from needless exposure 4f5509cc1SKees Cook * and overwrite under many unintended conditions. This code is based 5f5509cc1SKees Cook * on PAX_USERCOPY, which is: 6f5509cc1SKees Cook * 7f5509cc1SKees Cook * Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source 8f5509cc1SKees Cook * Security Inc. 9f5509cc1SKees Cook * 10f5509cc1SKees Cook * This program is free software; you can redistribute it and/or modify 11f5509cc1SKees Cook * it under the terms of the GNU General Public License version 2 as 12f5509cc1SKees Cook * published by the Free Software Foundation. 13f5509cc1SKees Cook * 14f5509cc1SKees Cook */ 15f5509cc1SKees Cook #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16f5509cc1SKees Cook 17f5509cc1SKees Cook #include <linux/mm.h> 18f5509cc1SKees Cook #include <linux/slab.h> 195b825c3aSIngo Molnar #include <linux/sched.h> 20*29930025SIngo Molnar #include <linux/sched/task.h> 21*29930025SIngo Molnar #include <linux/sched/task_stack.h> 22f5509cc1SKees Cook #include <asm/sections.h> 23f5509cc1SKees Cook 24f5509cc1SKees Cook enum { 25f5509cc1SKees Cook BAD_STACK = -1, 26f5509cc1SKees Cook NOT_STACK = 0, 27f5509cc1SKees Cook GOOD_FRAME, 28f5509cc1SKees Cook GOOD_STACK, 29f5509cc1SKees Cook }; 30f5509cc1SKees Cook 31f5509cc1SKees Cook /* 32f5509cc1SKees Cook * Checks if a given pointer and length is contained by the current 33f5509cc1SKees Cook * stack frame (if possible). 34f5509cc1SKees Cook * 35f5509cc1SKees Cook * Returns: 36f5509cc1SKees Cook * NOT_STACK: not at all on the stack 37f5509cc1SKees Cook * GOOD_FRAME: fully within a valid stack frame 38f5509cc1SKees Cook * GOOD_STACK: fully on the stack (when can't do frame-checking) 39f5509cc1SKees Cook * BAD_STACK: error condition (invalid stack position or bad stack frame) 40f5509cc1SKees Cook */ 41f5509cc1SKees Cook static noinline int check_stack_object(const void *obj, unsigned long len) 42f5509cc1SKees Cook { 43f5509cc1SKees Cook const void * const stack = task_stack_page(current); 44f5509cc1SKees Cook const void * const stackend = stack + THREAD_SIZE; 45f5509cc1SKees Cook int ret; 46f5509cc1SKees Cook 47f5509cc1SKees Cook /* Object is not on the stack at all. */ 48f5509cc1SKees Cook if (obj + len <= stack || stackend <= obj) 49f5509cc1SKees Cook return NOT_STACK; 50f5509cc1SKees Cook 51f5509cc1SKees Cook /* 52f5509cc1SKees Cook * Reject: object partially overlaps the stack (passing the 53f5509cc1SKees Cook * the check above means at least one end is within the stack, 54f5509cc1SKees Cook * so if this check fails, the other end is outside the stack). 55f5509cc1SKees Cook */ 56f5509cc1SKees Cook if (obj < stack || stackend < obj + len) 57f5509cc1SKees Cook return BAD_STACK; 58f5509cc1SKees Cook 59f5509cc1SKees Cook /* Check if object is safely within a valid frame. */ 60f5509cc1SKees Cook ret = arch_within_stack_frames(stack, stackend, obj, len); 61f5509cc1SKees Cook if (ret) 62f5509cc1SKees Cook return ret; 63f5509cc1SKees Cook 64f5509cc1SKees Cook return GOOD_STACK; 65f5509cc1SKees Cook } 66f5509cc1SKees Cook 67f5509cc1SKees Cook static void report_usercopy(const void *ptr, unsigned long len, 68f5509cc1SKees Cook bool to_user, const char *type) 69f5509cc1SKees Cook { 70f5509cc1SKees Cook pr_emerg("kernel memory %s attempt detected %s %p (%s) (%lu bytes)\n", 71f5509cc1SKees Cook to_user ? "exposure" : "overwrite", 72f5509cc1SKees Cook to_user ? "from" : "to", ptr, type ? : "unknown", len); 73f5509cc1SKees Cook /* 74f5509cc1SKees Cook * For greater effect, it would be nice to do do_group_exit(), 75f5509cc1SKees Cook * but BUG() actually hooks all the lock-breaking and per-arch 76f5509cc1SKees Cook * Oops code, so that is used here instead. 77f5509cc1SKees Cook */ 78f5509cc1SKees Cook BUG(); 79f5509cc1SKees Cook } 80f5509cc1SKees Cook 81f5509cc1SKees Cook /* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */ 82f5509cc1SKees Cook static bool overlaps(const void *ptr, unsigned long n, unsigned long low, 83f5509cc1SKees Cook unsigned long high) 84f5509cc1SKees Cook { 85f5509cc1SKees Cook unsigned long check_low = (uintptr_t)ptr; 86f5509cc1SKees Cook unsigned long check_high = check_low + n; 87f5509cc1SKees Cook 88f5509cc1SKees Cook /* Does not overlap if entirely above or entirely below. */ 8994cd97afSJosh Poimboeuf if (check_low >= high || check_high <= low) 90f5509cc1SKees Cook return false; 91f5509cc1SKees Cook 92f5509cc1SKees Cook return true; 93f5509cc1SKees Cook } 94f5509cc1SKees Cook 95f5509cc1SKees Cook /* Is this address range in the kernel text area? */ 96f5509cc1SKees Cook static inline const char *check_kernel_text_object(const void *ptr, 97f5509cc1SKees Cook unsigned long n) 98f5509cc1SKees Cook { 99f5509cc1SKees Cook unsigned long textlow = (unsigned long)_stext; 100f5509cc1SKees Cook unsigned long texthigh = (unsigned long)_etext; 101f5509cc1SKees Cook unsigned long textlow_linear, texthigh_linear; 102f5509cc1SKees Cook 103f5509cc1SKees Cook if (overlaps(ptr, n, textlow, texthigh)) 104f5509cc1SKees Cook return "<kernel text>"; 105f5509cc1SKees Cook 106f5509cc1SKees Cook /* 107f5509cc1SKees Cook * Some architectures have virtual memory mappings with a secondary 108f5509cc1SKees Cook * mapping of the kernel text, i.e. there is more than one virtual 109f5509cc1SKees Cook * kernel address that points to the kernel image. It is usually 110f5509cc1SKees Cook * when there is a separate linear physical memory mapping, in that 111f5509cc1SKees Cook * __pa() is not just the reverse of __va(). This can be detected 112f5509cc1SKees Cook * and checked: 113f5509cc1SKees Cook */ 11446f6236aSLaura Abbott textlow_linear = (unsigned long)lm_alias(textlow); 115f5509cc1SKees Cook /* No different mapping: we're done. */ 116f5509cc1SKees Cook if (textlow_linear == textlow) 117f5509cc1SKees Cook return NULL; 118f5509cc1SKees Cook 119f5509cc1SKees Cook /* Check the secondary mapping... */ 12046f6236aSLaura Abbott texthigh_linear = (unsigned long)lm_alias(texthigh); 121f5509cc1SKees Cook if (overlaps(ptr, n, textlow_linear, texthigh_linear)) 122f5509cc1SKees Cook return "<linear kernel text>"; 123f5509cc1SKees Cook 124f5509cc1SKees Cook return NULL; 125f5509cc1SKees Cook } 126f5509cc1SKees Cook 127f5509cc1SKees Cook static inline const char *check_bogus_address(const void *ptr, unsigned long n) 128f5509cc1SKees Cook { 129f5509cc1SKees Cook /* Reject if object wraps past end of memory. */ 1307329a655SEric Biggers if ((unsigned long)ptr + n < (unsigned long)ptr) 131f5509cc1SKees Cook return "<wrapped address>"; 132f5509cc1SKees Cook 133f5509cc1SKees Cook /* Reject if NULL or ZERO-allocation. */ 134f5509cc1SKees Cook if (ZERO_OR_NULL_PTR(ptr)) 135f5509cc1SKees Cook return "<null>"; 136f5509cc1SKees Cook 137f5509cc1SKees Cook return NULL; 138f5509cc1SKees Cook } 139f5509cc1SKees Cook 1408e1f74eaSKees Cook /* Checks for allocs that are marked in some way as spanning multiple pages. */ 1418e1f74eaSKees Cook static inline const char *check_page_span(const void *ptr, unsigned long n, 1428e1f74eaSKees Cook struct page *page, bool to_user) 143f5509cc1SKees Cook { 1448e1f74eaSKees Cook #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN 145f5509cc1SKees Cook const void *end = ptr + n - 1; 1468e1f74eaSKees Cook struct page *endpage; 147f5509cc1SKees Cook bool is_reserved, is_cma; 148f5509cc1SKees Cook 149f5509cc1SKees Cook /* 150f5509cc1SKees Cook * Sometimes the kernel data regions are not marked Reserved (see 151f5509cc1SKees Cook * check below). And sometimes [_sdata,_edata) does not cover 152f5509cc1SKees Cook * rodata and/or bss, so check each range explicitly. 153f5509cc1SKees Cook */ 154f5509cc1SKees Cook 155f5509cc1SKees Cook /* Allow reads of kernel rodata region (if not marked as Reserved). */ 156f5509cc1SKees Cook if (ptr >= (const void *)__start_rodata && 157f5509cc1SKees Cook end <= (const void *)__end_rodata) { 158f5509cc1SKees Cook if (!to_user) 159f5509cc1SKees Cook return "<rodata>"; 160f5509cc1SKees Cook return NULL; 161f5509cc1SKees Cook } 162f5509cc1SKees Cook 163f5509cc1SKees Cook /* Allow kernel data region (if not marked as Reserved). */ 164f5509cc1SKees Cook if (ptr >= (const void *)_sdata && end <= (const void *)_edata) 165f5509cc1SKees Cook return NULL; 166f5509cc1SKees Cook 167f5509cc1SKees Cook /* Allow kernel bss region (if not marked as Reserved). */ 168f5509cc1SKees Cook if (ptr >= (const void *)__bss_start && 169f5509cc1SKees Cook end <= (const void *)__bss_stop) 170f5509cc1SKees Cook return NULL; 171f5509cc1SKees Cook 172f5509cc1SKees Cook /* Is the object wholly within one base page? */ 173f5509cc1SKees Cook if (likely(((unsigned long)ptr & (unsigned long)PAGE_MASK) == 174f5509cc1SKees Cook ((unsigned long)end & (unsigned long)PAGE_MASK))) 175f5509cc1SKees Cook return NULL; 176f5509cc1SKees Cook 1778e1f74eaSKees Cook /* Allow if fully inside the same compound (__GFP_COMP) page. */ 178f5509cc1SKees Cook endpage = virt_to_head_page(end); 179f5509cc1SKees Cook if (likely(endpage == page)) 180f5509cc1SKees Cook return NULL; 181f5509cc1SKees Cook 182f5509cc1SKees Cook /* 183f5509cc1SKees Cook * Reject if range is entirely either Reserved (i.e. special or 184f5509cc1SKees Cook * device memory), or CMA. Otherwise, reject since the object spans 185f5509cc1SKees Cook * several independently allocated pages. 186f5509cc1SKees Cook */ 187f5509cc1SKees Cook is_reserved = PageReserved(page); 188f5509cc1SKees Cook is_cma = is_migrate_cma_page(page); 189f5509cc1SKees Cook if (!is_reserved && !is_cma) 1908e1f74eaSKees Cook return "<spans multiple pages>"; 191f5509cc1SKees Cook 192f5509cc1SKees Cook for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) { 193f5509cc1SKees Cook page = virt_to_head_page(ptr); 194f5509cc1SKees Cook if (is_reserved && !PageReserved(page)) 1958e1f74eaSKees Cook return "<spans Reserved and non-Reserved pages>"; 196f5509cc1SKees Cook if (is_cma && !is_migrate_cma_page(page)) 1978e1f74eaSKees Cook return "<spans CMA and non-CMA pages>"; 198f5509cc1SKees Cook } 1998e1f74eaSKees Cook #endif 200f5509cc1SKees Cook 201f5509cc1SKees Cook return NULL; 2028e1f74eaSKees Cook } 203f5509cc1SKees Cook 2048e1f74eaSKees Cook static inline const char *check_heap_object(const void *ptr, unsigned long n, 2058e1f74eaSKees Cook bool to_user) 2068e1f74eaSKees Cook { 2078e1f74eaSKees Cook struct page *page; 2088e1f74eaSKees Cook 2098e1f74eaSKees Cook /* 2108e1f74eaSKees Cook * Some architectures (arm64) return true for virt_addr_valid() on 2118e1f74eaSKees Cook * vmalloced addresses. Work around this by checking for vmalloc 2128e1f74eaSKees Cook * first. 213aa4f0601SLaura Abbott * 214aa4f0601SLaura Abbott * We also need to check for module addresses explicitly since we 215aa4f0601SLaura Abbott * may copy static data from modules to userspace 2168e1f74eaSKees Cook */ 217aa4f0601SLaura Abbott if (is_vmalloc_or_module_addr(ptr)) 2188e1f74eaSKees Cook return NULL; 2198e1f74eaSKees Cook 2208e1f74eaSKees Cook if (!virt_addr_valid(ptr)) 2218e1f74eaSKees Cook return NULL; 2228e1f74eaSKees Cook 2238e1f74eaSKees Cook page = virt_to_head_page(ptr); 2248e1f74eaSKees Cook 2258e1f74eaSKees Cook /* Check slab allocator for flags and size. */ 2268e1f74eaSKees Cook if (PageSlab(page)) 2278e1f74eaSKees Cook return __check_heap_object(ptr, n, page); 2288e1f74eaSKees Cook 2298e1f74eaSKees Cook /* Verify object does not incorrectly span multiple pages. */ 2308e1f74eaSKees Cook return check_page_span(ptr, n, page, to_user); 231f5509cc1SKees Cook } 232f5509cc1SKees Cook 233f5509cc1SKees Cook /* 234f5509cc1SKees Cook * Validates that the given object is: 235f5509cc1SKees Cook * - not bogus address 236f5509cc1SKees Cook * - known-safe heap or stack object 237f5509cc1SKees Cook * - not in kernel text 238f5509cc1SKees Cook */ 239f5509cc1SKees Cook void __check_object_size(const void *ptr, unsigned long n, bool to_user) 240f5509cc1SKees Cook { 241f5509cc1SKees Cook const char *err; 242f5509cc1SKees Cook 243f5509cc1SKees Cook /* Skip all tests if size is zero. */ 244f5509cc1SKees Cook if (!n) 245f5509cc1SKees Cook return; 246f5509cc1SKees Cook 247f5509cc1SKees Cook /* Check for invalid addresses. */ 248f5509cc1SKees Cook err = check_bogus_address(ptr, n); 249f5509cc1SKees Cook if (err) 250f5509cc1SKees Cook goto report; 251f5509cc1SKees Cook 252f5509cc1SKees Cook /* Check for bad heap object. */ 253f5509cc1SKees Cook err = check_heap_object(ptr, n, to_user); 254f5509cc1SKees Cook if (err) 255f5509cc1SKees Cook goto report; 256f5509cc1SKees Cook 257f5509cc1SKees Cook /* Check for bad stack object. */ 258f5509cc1SKees Cook switch (check_stack_object(ptr, n)) { 259f5509cc1SKees Cook case NOT_STACK: 260f5509cc1SKees Cook /* Object is not touching the current process stack. */ 261f5509cc1SKees Cook break; 262f5509cc1SKees Cook case GOOD_FRAME: 263f5509cc1SKees Cook case GOOD_STACK: 264f5509cc1SKees Cook /* 265f5509cc1SKees Cook * Object is either in the correct frame (when it 266f5509cc1SKees Cook * is possible to check) or just generally on the 267f5509cc1SKees Cook * process stack (when frame checking not available). 268f5509cc1SKees Cook */ 269f5509cc1SKees Cook return; 270f5509cc1SKees Cook default: 271f5509cc1SKees Cook err = "<process stack>"; 272f5509cc1SKees Cook goto report; 273f5509cc1SKees Cook } 274f5509cc1SKees Cook 275f5509cc1SKees Cook /* Check for object in kernel to avoid text exposure. */ 276f5509cc1SKees Cook err = check_kernel_text_object(ptr, n); 277f5509cc1SKees Cook if (!err) 278f5509cc1SKees Cook return; 279f5509cc1SKees Cook 280f5509cc1SKees Cook report: 281f5509cc1SKees Cook report_usercopy(ptr, n, to_user, err); 282f5509cc1SKees Cook } 283f5509cc1SKees Cook EXPORT_SYMBOL(__check_object_size); 284