blob: a562dd094ace2886f848281acd27034148061c22 [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>
18#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010019#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010020#include <linux/sched/task.h>
21#include <linux/sched/task_stack.h>
Sahara96dc4f92017-02-16 18:29:15 +000022#include <linux/thread_info.h>
Kees Cookf5509cc2016-06-07 11:05:33 -070023#include <asm/sections.h>
24
Kees Cookf5509cc2016-06-07 11:05:33 -070025/*
26 * Checks if a given pointer and length is contained by the current
27 * stack frame (if possible).
28 *
29 * Returns:
30 * NOT_STACK: not at all on the stack
31 * GOOD_FRAME: fully within a valid stack frame
32 * GOOD_STACK: fully on the stack (when can't do frame-checking)
33 * BAD_STACK: error condition (invalid stack position or bad stack frame)
34 */
35static noinline int check_stack_object(const void *obj, unsigned long len)
36{
37 const void * const stack = task_stack_page(current);
38 const void * const stackend = stack + THREAD_SIZE;
39 int ret;
40
41 /* Object is not on the stack at all. */
42 if (obj + len <= stack || stackend <= obj)
43 return NOT_STACK;
44
45 /*
46 * Reject: object partially overlaps the stack (passing the
47 * the check above means at least one end is within the stack,
48 * so if this check fails, the other end is outside the stack).
49 */
50 if (obj < stack || stackend < obj + len)
51 return BAD_STACK;
52
53 /* Check if object is safely within a valid frame. */
54 ret = arch_within_stack_frames(stack, stackend, obj, len);
55 if (ret)
56 return ret;
57
58 return GOOD_STACK;
59}
60
Kees Cookb394d462018-01-10 14:22:38 -080061/*
62 * If this function is reached, then CONFIG_HARDENED_USERCOPY has found an
63 * unexpected state during a copy_from_user() or copy_to_user() call.
64 * There are several checks being performed on the buffer by the
65 * __check_object_size() function. Normal stack buffer usage should never
66 * trip the checks, and kernel text addressing will always trip the check.
67 * For cache objects, copies must be within the object size.
68 */
69void __noreturn usercopy_abort(const char *name, const char *detail,
70 bool to_user, unsigned long offset,
71 unsigned long len)
Kees Cookf5509cc2016-06-07 11:05:33 -070072{
Kees Cookb394d462018-01-10 14:22:38 -080073 pr_emerg("Kernel memory %s attempt detected %s %s%s%s%s (offset %lu, size %lu)!\n",
74 to_user ? "exposure" : "overwrite",
75 to_user ? "from" : "to",
76 name ? : "unknown?!",
77 detail ? " '" : "", detail ? : "", detail ? "'" : "",
78 offset, len);
79
Kees Cookf5509cc2016-06-07 11:05:33 -070080 /*
81 * For greater effect, it would be nice to do do_group_exit(),
82 * but BUG() actually hooks all the lock-breaking and per-arch
83 * Oops code, so that is used here instead.
84 */
85 BUG();
86}
87
88/* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
Kees Cookf4e6e282018-01-10 14:48:22 -080089static bool overlaps(const unsigned long ptr, unsigned long n,
90 unsigned long low, unsigned long high)
Kees Cookf5509cc2016-06-07 11:05:33 -070091{
Kees Cookf4e6e282018-01-10 14:48:22 -080092 const unsigned long check_low = ptr;
Kees Cookf5509cc2016-06-07 11:05:33 -070093 unsigned long check_high = check_low + n;
94
95 /* Does not overlap if entirely above or entirely below. */
Josh Poimboeuf94cd97a2016-08-22 11:53:59 -050096 if (check_low >= high || check_high <= low)
Kees Cookf5509cc2016-06-07 11:05:33 -070097 return false;
98
99 return true;
100}
101
102/* Is this address range in the kernel text area? */
Kees Cookf4e6e282018-01-10 14:48:22 -0800103static inline void check_kernel_text_object(const unsigned long ptr,
104 unsigned long n, bool to_user)
Kees Cookf5509cc2016-06-07 11:05:33 -0700105{
106 unsigned long textlow = (unsigned long)_stext;
107 unsigned long texthigh = (unsigned long)_etext;
108 unsigned long textlow_linear, texthigh_linear;
109
110 if (overlaps(ptr, n, textlow, texthigh))
Kees Cookf4e6e282018-01-10 14:48:22 -0800111 usercopy_abort("kernel text", NULL, to_user, ptr - textlow, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700112
113 /*
114 * Some architectures have virtual memory mappings with a secondary
115 * mapping of the kernel text, i.e. there is more than one virtual
116 * kernel address that points to the kernel image. It is usually
117 * when there is a separate linear physical memory mapping, in that
118 * __pa() is not just the reverse of __va(). This can be detected
119 * and checked:
120 */
Laura Abbott46f62362017-01-10 13:35:45 -0800121 textlow_linear = (unsigned long)lm_alias(textlow);
Kees Cookf5509cc2016-06-07 11:05:33 -0700122 /* No different mapping: we're done. */
123 if (textlow_linear == textlow)
Kees Cookf4e6e282018-01-10 14:48:22 -0800124 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700125
126 /* Check the secondary mapping... */
Laura Abbott46f62362017-01-10 13:35:45 -0800127 texthigh_linear = (unsigned long)lm_alias(texthigh);
Kees Cookf5509cc2016-06-07 11:05:33 -0700128 if (overlaps(ptr, n, textlow_linear, texthigh_linear))
Kees Cookf4e6e282018-01-10 14:48:22 -0800129 usercopy_abort("linear kernel text", NULL, to_user,
130 ptr - textlow_linear, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700131}
132
Kees Cookf4e6e282018-01-10 14:48:22 -0800133static inline void check_bogus_address(const unsigned long ptr, unsigned long n,
134 bool to_user)
Kees Cookf5509cc2016-06-07 11:05:33 -0700135{
136 /* Reject if object wraps past end of memory. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800137 if (ptr + n < ptr)
138 usercopy_abort("wrapped address", NULL, to_user, 0, ptr + n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700139
140 /* Reject if NULL or ZERO-allocation. */
141 if (ZERO_OR_NULL_PTR(ptr))
Kees Cookf4e6e282018-01-10 14:48:22 -0800142 usercopy_abort("null address", NULL, to_user, ptr, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700143}
144
Kees Cook8e1f74e2016-09-07 09:54:34 -0700145/* Checks for allocs that are marked in some way as spanning multiple pages. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800146static inline void check_page_span(const void *ptr, unsigned long n,
147 struct page *page, bool to_user)
Kees Cookf5509cc2016-06-07 11:05:33 -0700148{
Kees Cook8e1f74e2016-09-07 09:54:34 -0700149#ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
Kees Cookf5509cc2016-06-07 11:05:33 -0700150 const void *end = ptr + n - 1;
Kees Cook8e1f74e2016-09-07 09:54:34 -0700151 struct page *endpage;
Kees Cookf5509cc2016-06-07 11:05:33 -0700152 bool is_reserved, is_cma;
153
154 /*
Kees Cookf5509cc2016-06-07 11:05:33 -0700155 * Sometimes the kernel data regions are not marked Reserved (see
156 * check below). And sometimes [_sdata,_edata) does not cover
157 * rodata and/or bss, so check each range explicitly.
158 */
159
160 /* Allow reads of kernel rodata region (if not marked as Reserved). */
161 if (ptr >= (const void *)__start_rodata &&
162 end <= (const void *)__end_rodata) {
163 if (!to_user)
Kees Cookf4e6e282018-01-10 14:48:22 -0800164 usercopy_abort("rodata", NULL, to_user, 0, n);
165 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700166 }
167
168 /* Allow kernel data region (if not marked as Reserved). */
169 if (ptr >= (const void *)_sdata && end <= (const void *)_edata)
Kees Cookf4e6e282018-01-10 14:48:22 -0800170 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700171
172 /* Allow kernel bss region (if not marked as Reserved). */
173 if (ptr >= (const void *)__bss_start &&
174 end <= (const void *)__bss_stop)
Kees Cookf4e6e282018-01-10 14:48:22 -0800175 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700176
177 /* Is the object wholly within one base page? */
178 if (likely(((unsigned long)ptr & (unsigned long)PAGE_MASK) ==
179 ((unsigned long)end & (unsigned long)PAGE_MASK)))
Kees Cookf4e6e282018-01-10 14:48:22 -0800180 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700181
Kees Cook8e1f74e2016-09-07 09:54:34 -0700182 /* Allow if fully inside the same compound (__GFP_COMP) page. */
Kees Cookf5509cc2016-06-07 11:05:33 -0700183 endpage = virt_to_head_page(end);
184 if (likely(endpage == page))
Kees Cookf4e6e282018-01-10 14:48:22 -0800185 return;
Kees Cookf5509cc2016-06-07 11:05:33 -0700186
187 /*
188 * Reject if range is entirely either Reserved (i.e. special or
189 * device memory), or CMA. Otherwise, reject since the object spans
190 * several independently allocated pages.
191 */
192 is_reserved = PageReserved(page);
193 is_cma = is_migrate_cma_page(page);
194 if (!is_reserved && !is_cma)
Kees Cookf4e6e282018-01-10 14:48:22 -0800195 usercopy_abort("spans multiple pages", NULL, to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700196
197 for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) {
198 page = virt_to_head_page(ptr);
199 if (is_reserved && !PageReserved(page))
Kees Cookf4e6e282018-01-10 14:48:22 -0800200 usercopy_abort("spans Reserved and non-Reserved pages",
201 NULL, to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700202 if (is_cma && !is_migrate_cma_page(page))
Kees Cookf4e6e282018-01-10 14:48:22 -0800203 usercopy_abort("spans CMA and non-CMA pages", NULL,
204 to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700205 }
Kees Cook8e1f74e2016-09-07 09:54:34 -0700206#endif
Kees Cook8e1f74e2016-09-07 09:54:34 -0700207}
Kees Cookf5509cc2016-06-07 11:05:33 -0700208
Kees Cookf4e6e282018-01-10 14:48:22 -0800209static inline void check_heap_object(const void *ptr, unsigned long n,
210 bool to_user)
Kees Cook8e1f74e2016-09-07 09:54:34 -0700211{
212 struct page *page;
213
Kees Cook8e1f74e2016-09-07 09:54:34 -0700214 if (!virt_addr_valid(ptr))
Kees Cookf4e6e282018-01-10 14:48:22 -0800215 return;
Kees Cook8e1f74e2016-09-07 09:54:34 -0700216
217 page = virt_to_head_page(ptr);
218
Kees Cookf4e6e282018-01-10 14:48:22 -0800219 if (PageSlab(page)) {
220 /* Check slab allocator for flags and size. */
221 __check_heap_object(ptr, n, page, to_user);
222 } else {
223 /* Verify object does not incorrectly span multiple pages. */
224 check_page_span(ptr, n, page, to_user);
225 }
Kees Cookf5509cc2016-06-07 11:05:33 -0700226}
227
228/*
229 * Validates that the given object is:
230 * - not bogus address
231 * - known-safe heap or stack object
232 * - not in kernel text
233 */
234void __check_object_size(const void *ptr, unsigned long n, bool to_user)
235{
Kees Cookf5509cc2016-06-07 11:05:33 -0700236 /* Skip all tests if size is zero. */
237 if (!n)
238 return;
239
240 /* Check for invalid addresses. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800241 check_bogus_address((const unsigned long)ptr, n, to_user);
Kees Cookf5509cc2016-06-07 11:05:33 -0700242
243 /* Check for bad heap object. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800244 check_heap_object(ptr, n, to_user);
Kees Cookf5509cc2016-06-07 11:05:33 -0700245
246 /* Check for bad stack object. */
247 switch (check_stack_object(ptr, n)) {
248 case NOT_STACK:
249 /* Object is not touching the current process stack. */
250 break;
251 case GOOD_FRAME:
252 case GOOD_STACK:
253 /*
254 * Object is either in the correct frame (when it
255 * is possible to check) or just generally on the
256 * process stack (when frame checking not available).
257 */
258 return;
259 default:
Kees Cookf4e6e282018-01-10 14:48:22 -0800260 usercopy_abort("process stack", NULL, to_user, 0, n);
Kees Cookf5509cc2016-06-07 11:05:33 -0700261 }
262
263 /* Check for object in kernel to avoid text exposure. */
Kees Cookf4e6e282018-01-10 14:48:22 -0800264 check_kernel_text_object((const unsigned long)ptr, n, to_user);
Kees Cookf5509cc2016-06-07 11:05:33 -0700265}
266EXPORT_SYMBOL(__check_object_size);