blob: e81d11715d95b34d1e99e57dc0fd398e9a9e0cbe [file] [log] [blame]
Kees Cookf5509cc2016-06-07 11:05:33 -07001/*
2 * This implements the various checks for CONFIG_HARDENED_USERCOPY*,
3 * which are designed to protect kernel memory from needless exposure
4 * and overwrite under many unintended conditions. This code is based
5 * on PAX_USERCOPY, which is:
6 *
7 * Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source
8 * Security Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/mm.h>
Kees Cook12c6c4a2019-09-17 11:00:25 -070018#include <linux/highmem.h>
Kees Cookf5509cc2016-06-07 11:05:33 -070019#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010020#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010021#include <linux/sched/task.h>
22#include <linux/sched/task_stack.h>
Sahara96dc4f92017-02-16 18:29:15 +000023#include <linux/thread_info.h>
Chris von Recklinghausenb5cb15d2018-07-03 15:43:08 -040024#include <linux/atomic.h>
25#include <linux/jump_label.h>
Kees Cookf5509cc2016-06-07 11:05:33 -070026#include <asm/sections.h>
27
Kees Cookf5509cc2016-06-07 11:05:33 -070028/*
29 * Checks if a given pointer and length is contained by the current
30 * stack frame (if possible).
31 *
32 * Returns:
33 * NOT_STACK: not at all on the stack
34 * GOOD_FRAME: fully within a valid stack frame
35 * GOOD_STACK: fully on the stack (when can't do frame-checking)
36 * BAD_STACK: error condition (invalid stack position or bad stack frame)
37 */
38static noinline int check_stack_object(const void *obj, unsigned long len)
39{
40 const void * const stack = task_stack_page(current);
41 const void * const stackend = stack + THREAD_SIZE;
42 int ret;
43
44 /* Object is not on the stack at all. */
45 if (obj + len <= stack || stackend <= obj)
46 return NOT_STACK;
47
48 /*
49 * Reject: object partially overlaps the stack (passing the
50 * the check above means at least one end is within the stack,
51 * so if this check fails, the other end is outside the stack).
52 */
53 if (obj < stack || stackend < obj + len)
54 return BAD_STACK;
55
56 /* Check if object is safely within a valid frame. */
57 ret = arch_within_stack_frames(stack, stackend, obj, len);
58 if (ret)
59 return ret;
60
61 return GOOD_STACK;
62}
63
Kees Cookb394d462018-01-10 14:22:38 -080064/*
Kees Cookafcc90f82018-01-10 15:17:01 -080065 * If these functions are reached, then CONFIG_HARDENED_USERCOPY has found
66 * an unexpected state during a copy_from_user() or copy_to_user() call.
Kees Cookb394d462018-01-10 14:22:38 -080067 * There are several checks being performed on the buffer by the
68 * __check_object_size() function. Normal stack buffer usage should never
69 * trip the checks, and kernel text addressing will always trip the check.
Kees Cookafcc90f82018-01-10 15:17:01 -080070 * For cache objects, it is checking that only the whitelisted range of
71 * bytes for a given cache is being accessed (via the cache's usersize and
72 * useroffset fields). To adjust a cache whitelist, use the usercopy-aware
73 * kmem_cache_create_usercopy() function to create the cache (and
74 * carefully audit the whitelist range).
Kees Cookb394d462018-01-10 14:22:38 -080075 */
Kees Cookafcc90f82018-01-10 15:17:01 -080076void usercopy_warn(const char *name, const char *detail, bool to_user,
77 unsigned long offset, unsigned long len)
78{
79 WARN_ONCE(1, "Bad or missing usercopy whitelist? Kernel memory %s attempt detected %s %s%s%s%s (offset %lu, size %lu)!\n",
80 to_user ? "exposure" : "overwrite",
81 to_user ? "from" : "to",
82 name ? : "unknown?!",
83 detail ? " '" : "", detail ? : "", detail ? "'" : "",
84 offset, len);
85}
86
Kees Cookb394d462018-01-10 14:22:38 -080087void __noreturn usercopy_abort(const char *name, const char *detail,
88 bool to_user, unsigned long offset,
89 unsigned long len)
Kees Cookf5509cc2016-06-07 11:05:33 -070090{
Kees Cookb394d462018-01-10 14:22:38 -080091 pr_emerg("Kernel memory %s attempt detected %s %s%s%s%s (offset %lu, size %lu)!\n",
92 to_user ? "exposure" : "overwrite",
93 to_user ? "from" : "to",
94 name ? : "unknown?!",
95 detail ? " '" : "", detail ? : "", detail ? "'" : "",
96 offset, len);
97
Kees Cookf5509cc2016-06-07 11:05:33 -070098 /*
99 * For greater effect, it would be nice to do do_group_exit(),
100 * but BUG() actually hooks all the lock-breaking and per-arch
101 * Oops code, so that is used here instead.
102 */
103 BUG();
104}
105
106/* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
Kees Cookf4e6e282018-01-10 14:48:22 -0800107static bool overlaps(const unsigned long ptr, unsigned long n,
108 unsigned long low, unsigned long high)
Kees Cookf5509cc2016-06-07 11:05:33 -0700109{
Kees Cookf4e6e282018-01-10 14:48:22 -0800110 const unsigned long check_low = ptr;
Kees Cookf5509cc2016-06-07 11:05:33 -0700111 unsigned long check_high = check_low + n;
112
113 /* Does not overlap if entirely above or entirely below. */
Josh Poimboeuf94cd97a2016-08-22 11:53:59 -0500114 if (check_low >= high || check_high <= low)
Kees Cookf5509cc2016-06-07 11:05:33 -0700115 return false;
116
117 return true;
118}
119
120/* Is this address range in the kernel text area? */
Kees Cookf4e6e282018-01-10 14:48:22 -0800121static inline void check_kernel_text_object(const unsigned long ptr,
122 unsigned long n, bool to_user)
Kees Cookf5509cc2016-06-07 11:05:33 -0700123{
124 unsigned long textlow = (unsigned long)_stext;
125 unsigned long texthigh = (unsigned long)_etext;
126 unsigned long textlow_linear, texthigh_linear;
127
128 if (overlaps(ptr, n, textlow, texthigh))
Kees Cookf4e6e282018-01-10 14:48:22 -0800129 usercopy_abort("kernel text", NULL, to_user, ptr - textlow, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700130
131 /*
132 * Some architectures have virtual memory mappings with a secondary
133 * mapping of the kernel text, i.e. there is more than one virtual
134 * kernel address that points to the kernel image. It is usually
135 * when there is a separate linear physical memory mapping, in that
136 * __pa() is not just the reverse of __va(). This can be detected
137 * and checked:
138 */
Laura Abbott46f62362017-01-10 13:35:45 -0800139 textlow_linear = (unsigned long)lm_alias(textlow);
Kees Cookf5509cc2016-06-07 11:05:33 -0700140 /* No different mapping: we're done. */
141 if (textlow_linear == textlow)
Kees Cookf4e6e282018-01-10 14:48:22 -0800142 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700143
144 /* Check the secondary mapping... */
Laura Abbott46f62362017-01-10 13:35:45 -0800145 texthigh_linear = (unsigned long)lm_alias(texthigh);
Kees Cookf5509cc2016-06-07 11:05:33 -0700146 if (overlaps(ptr, n, textlow_linear, texthigh_linear))
Kees Cookf4e6e282018-01-10 14:48:22 -0800147 usercopy_abort("linear kernel text", NULL, to_user,
148 ptr - textlow_linear, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700149}
150
Kees Cookf4e6e282018-01-10 14:48:22 -0800151static inline void check_bogus_address(const unsigned long ptr, unsigned long n,
152 bool to_user)
Kees Cookf5509cc2016-06-07 11:05:33 -0700153{
154 /* Reject if object wraps past end of memory. */
Isaac J. Manjarres056368f2019-08-13 15:37:37 -0700155 if (ptr + (n - 1) < ptr)
Kees Cookf4e6e282018-01-10 14:48:22 -0800156 usercopy_abort("wrapped address", NULL, to_user, 0, ptr + n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700157
158 /* Reject if NULL or ZERO-allocation. */
159 if (ZERO_OR_NULL_PTR(ptr))
Kees Cookf4e6e282018-01-10 14:48:22 -0800160 usercopy_abort("null address", NULL, to_user, ptr, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700161}
162
Kees Cook8e1f74e2016-09-07 09:54:34 -0700163/* Checks for allocs that are marked in some way as spanning multiple pages. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800164static inline void check_page_span(const void *ptr, unsigned long n,
165 struct page *page, bool to_user)
Kees Cookf5509cc2016-06-07 11:05:33 -0700166{
Kees Cook8e1f74e2016-09-07 09:54:34 -0700167#ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
Kees Cookf5509cc2016-06-07 11:05:33 -0700168 const void *end = ptr + n - 1;
Kees Cook8e1f74e2016-09-07 09:54:34 -0700169 struct page *endpage;
Kees Cookf5509cc2016-06-07 11:05:33 -0700170 bool is_reserved, is_cma;
171
172 /*
Kees Cookf5509cc2016-06-07 11:05:33 -0700173 * Sometimes the kernel data regions are not marked Reserved (see
174 * check below). And sometimes [_sdata,_edata) does not cover
175 * rodata and/or bss, so check each range explicitly.
176 */
177
178 /* Allow reads of kernel rodata region (if not marked as Reserved). */
179 if (ptr >= (const void *)__start_rodata &&
180 end <= (const void *)__end_rodata) {
181 if (!to_user)
Kees Cookf4e6e282018-01-10 14:48:22 -0800182 usercopy_abort("rodata", NULL, to_user, 0, n);
183 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700184 }
185
186 /* Allow kernel data region (if not marked as Reserved). */
187 if (ptr >= (const void *)_sdata && end <= (const void *)_edata)
Kees Cookf4e6e282018-01-10 14:48:22 -0800188 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700189
190 /* Allow kernel bss region (if not marked as Reserved). */
191 if (ptr >= (const void *)__bss_start &&
192 end <= (const void *)__bss_stop)
Kees Cookf4e6e282018-01-10 14:48:22 -0800193 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700194
195 /* Is the object wholly within one base page? */
196 if (likely(((unsigned long)ptr & (unsigned long)PAGE_MASK) ==
197 ((unsigned long)end & (unsigned long)PAGE_MASK)))
Kees Cookf4e6e282018-01-10 14:48:22 -0800198 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700199
Kees Cook8e1f74e2016-09-07 09:54:34 -0700200 /* Allow if fully inside the same compound (__GFP_COMP) page. */
Kees Cookf5509cc2016-06-07 11:05:33 -0700201 endpage = virt_to_head_page(end);
202 if (likely(endpage == page))
Kees Cookf4e6e282018-01-10 14:48:22 -0800203 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700204
205 /*
206 * Reject if range is entirely either Reserved (i.e. special or
207 * device memory), or CMA. Otherwise, reject since the object spans
208 * several independently allocated pages.
209 */
210 is_reserved = PageReserved(page);
211 is_cma = is_migrate_cma_page(page);
212 if (!is_reserved && !is_cma)
Kees Cookf4e6e282018-01-10 14:48:22 -0800213 usercopy_abort("spans multiple pages", NULL, to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700214
215 for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) {
216 page = virt_to_head_page(ptr);
217 if (is_reserved && !PageReserved(page))
Kees Cookf4e6e282018-01-10 14:48:22 -0800218 usercopy_abort("spans Reserved and non-Reserved pages",
219 NULL, to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700220 if (is_cma && !is_migrate_cma_page(page))
Kees Cookf4e6e282018-01-10 14:48:22 -0800221 usercopy_abort("spans CMA and non-CMA pages", NULL,
222 to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700223 }
Kees Cook8e1f74e2016-09-07 09:54:34 -0700224#endif
Kees Cook8e1f74e2016-09-07 09:54:34 -0700225}
Kees Cookf5509cc2016-06-07 11:05:33 -0700226
Kees Cookf4e6e282018-01-10 14:48:22 -0800227static inline void check_heap_object(const void *ptr, unsigned long n,
228 bool to_user)
Kees Cook8e1f74e2016-09-07 09:54:34 -0700229{
230 struct page *page;
231
Kees Cook8e1f74e2016-09-07 09:54:34 -0700232 if (!virt_addr_valid(ptr))
Kees Cookf4e6e282018-01-10 14:48:22 -0800233 return;
Kees Cook8e1f74e2016-09-07 09:54:34 -0700234
Kees Cook12c6c4a2019-09-17 11:00:25 -0700235 /*
236 * When CONFIG_HIGHMEM=y, kmap_to_page() will give either the
237 * highmem page or fallback to virt_to_page(). The following
238 * is effectively a highmem-aware virt_to_head_page().
239 */
240 page = compound_head(kmap_to_page((void *)ptr));
Kees Cook8e1f74e2016-09-07 09:54:34 -0700241
Kees Cookf4e6e282018-01-10 14:48:22 -0800242 if (PageSlab(page)) {
243 /* Check slab allocator for flags and size. */
244 __check_heap_object(ptr, n, page, to_user);
245 } else {
246 /* Verify object does not incorrectly span multiple pages. */
247 check_page_span(ptr, n, page, to_user);
248 }
Kees Cookf5509cc2016-06-07 11:05:33 -0700249}
250
Chris von Recklinghausenb5cb15d2018-07-03 15:43:08 -0400251static DEFINE_STATIC_KEY_FALSE_RO(bypass_usercopy_checks);
252
Kees Cookf5509cc2016-06-07 11:05:33 -0700253/*
254 * Validates that the given object is:
255 * - not bogus address
Qian Cai8a4b6e82019-01-08 15:23:04 -0800256 * - fully contained by stack (or stack frame, when available)
257 * - fully within SLAB object (or object whitelist area, when available)
Kees Cookf5509cc2016-06-07 11:05:33 -0700258 * - not in kernel text
259 */
260void __check_object_size(const void *ptr, unsigned long n, bool to_user)
261{
Chris von Recklinghausenb5cb15d2018-07-03 15:43:08 -0400262 if (static_branch_unlikely(&bypass_usercopy_checks))
263 return;
264
Kees Cookf5509cc2016-06-07 11:05:33 -0700265 /* Skip all tests if size is zero. */
266 if (!n)
267 return;
268
269 /* Check for invalid addresses. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800270 check_bogus_address((const unsigned long)ptr, n, to_user);
Kees Cookf5509cc2016-06-07 11:05:33 -0700271
Kees Cookf5509cc2016-06-07 11:05:33 -0700272 /* Check for bad stack object. */
273 switch (check_stack_object(ptr, n)) {
274 case NOT_STACK:
275 /* Object is not touching the current process stack. */
276 break;
277 case GOOD_FRAME:
278 case GOOD_STACK:
279 /*
280 * Object is either in the correct frame (when it
281 * is possible to check) or just generally on the
282 * process stack (when frame checking not available).
283 */
284 return;
285 default:
Kees Cookf4e6e282018-01-10 14:48:22 -0800286 usercopy_abort("process stack", NULL, to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700287 }
288
Qian Cai8a4b6e82019-01-08 15:23:04 -0800289 /* Check for bad heap object. */
290 check_heap_object(ptr, n, to_user);
291
Kees Cookf5509cc2016-06-07 11:05:33 -0700292 /* Check for object in kernel to avoid text exposure. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800293 check_kernel_text_object((const unsigned long)ptr, n, to_user);
Kees Cookf5509cc2016-06-07 11:05:33 -0700294}
295EXPORT_SYMBOL(__check_object_size);
Chris von Recklinghausenb5cb15d2018-07-03 15:43:08 -0400296
297static bool enable_checks __initdata = true;
298
299static int __init parse_hardened_usercopy(char *str)
300{
301 return strtobool(str, &enable_checks);
302}
303
304__setup("hardened_usercopy=", parse_hardened_usercopy);
305
306static int __init set_hardened_usercopy(void)
307{
308 if (enable_checks == false)
309 static_branch_enable(&bypass_usercopy_checks);
310 return 1;
311}
312
313late_initcall(set_hardened_usercopy);