Commit | Line | Data |
---|---|---|
f5509cc1 KC |
1 | /* |
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> | |
5b825c3a | 19 | #include <linux/sched.h> |
29930025 IM |
20 | #include <linux/sched/task.h> |
21 | #include <linux/sched/task_stack.h> | |
96dc4f9f | 22 | #include <linux/thread_info.h> |
f5509cc1 KC |
23 | #include <asm/sections.h> |
24 | ||
f5509cc1 KC |
25 | /* |
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 | */ | |
35 | static 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 | ||
b394d468 KC |
61 | /* |
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 | */ | |
69 | void __noreturn usercopy_abort(const char *name, const char *detail, | |
70 | bool to_user, unsigned long offset, | |
71 | unsigned long len) | |
f5509cc1 | 72 | { |
b394d468 KC |
73 | 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 | ||
f5509cc1 KC |
80 | /* |
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). */ | |
f4e6e289 KC |
89 | static bool overlaps(const unsigned long ptr, unsigned long n, |
90 | unsigned long low, unsigned long high) | |
f5509cc1 | 91 | { |
f4e6e289 | 92 | const unsigned long check_low = ptr; |
f5509cc1 KC |
93 | unsigned long check_high = check_low + n; |
94 | ||
95 | /* Does not overlap if entirely above or entirely below. */ | |
94cd97af | 96 | if (check_low >= high || check_high <= low) |
f5509cc1 KC |
97 | return false; |
98 | ||
99 | return true; | |
100 | } | |
101 | ||
102 | /* Is this address range in the kernel text area? */ | |
f4e6e289 KC |
103 | static inline void check_kernel_text_object(const unsigned long ptr, |
104 | unsigned long n, bool to_user) | |
f5509cc1 KC |
105 | { |
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)) | |
f4e6e289 | 111 | usercopy_abort("kernel text", NULL, to_user, ptr - textlow, n); |
f5509cc1 KC |
112 | |
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 | */ | |
46f6236a | 121 | textlow_linear = (unsigned long)lm_alias(textlow); |
f5509cc1 KC |
122 | /* No different mapping: we're done. */ |
123 | if (textlow_linear == textlow) | |
f4e6e289 | 124 | return; |
f5509cc1 KC |
125 | |
126 | /* Check the secondary mapping... */ | |
46f6236a | 127 | texthigh_linear = (unsigned long)lm_alias(texthigh); |
f5509cc1 | 128 | if (overlaps(ptr, n, textlow_linear, texthigh_linear)) |
f4e6e289 KC |
129 | usercopy_abort("linear kernel text", NULL, to_user, |
130 | ptr - textlow_linear, n); | |
f5509cc1 KC |
131 | } |
132 | ||
f4e6e289 KC |
133 | static inline void check_bogus_address(const unsigned long ptr, unsigned long n, |
134 | bool to_user) | |
f5509cc1 KC |
135 | { |
136 | /* Reject if object wraps past end of memory. */ | |
f4e6e289 KC |
137 | if (ptr + n < ptr) |
138 | usercopy_abort("wrapped address", NULL, to_user, 0, ptr + n); | |
f5509cc1 KC |
139 | |
140 | /* Reject if NULL or ZERO-allocation. */ | |
141 | if (ZERO_OR_NULL_PTR(ptr)) | |
f4e6e289 | 142 | usercopy_abort("null address", NULL, to_user, ptr, n); |
f5509cc1 KC |
143 | } |
144 | ||
8e1f74ea | 145 | /* Checks for allocs that are marked in some way as spanning multiple pages. */ |
f4e6e289 KC |
146 | static inline void check_page_span(const void *ptr, unsigned long n, |
147 | struct page *page, bool to_user) | |
f5509cc1 | 148 | { |
8e1f74ea | 149 | #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN |
f5509cc1 | 150 | const void *end = ptr + n - 1; |
8e1f74ea | 151 | struct page *endpage; |
f5509cc1 KC |
152 | bool is_reserved, is_cma; |
153 | ||
f5509cc1 KC |
154 | /* |
155 | * 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) | |
f4e6e289 KC |
164 | usercopy_abort("rodata", NULL, to_user, 0, n); |
165 | return; | |
f5509cc1 KC |
166 | } |
167 | ||
168 | /* Allow kernel data region (if not marked as Reserved). */ | |
169 | if (ptr >= (const void *)_sdata && end <= (const void *)_edata) | |
f4e6e289 | 170 | return; |
f5509cc1 KC |
171 | |
172 | /* Allow kernel bss region (if not marked as Reserved). */ | |
173 | if (ptr >= (const void *)__bss_start && | |
174 | end <= (const void *)__bss_stop) | |
f4e6e289 | 175 | return; |
f5509cc1 KC |
176 | |
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))) | |
f4e6e289 | 180 | return; |
f5509cc1 | 181 | |
8e1f74ea | 182 | /* Allow if fully inside the same compound (__GFP_COMP) page. */ |
f5509cc1 KC |
183 | endpage = virt_to_head_page(end); |
184 | if (likely(endpage == page)) | |
f4e6e289 | 185 | return; |
f5509cc1 KC |
186 | |
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) | |
f4e6e289 | 195 | usercopy_abort("spans multiple pages", NULL, to_user, 0, n); |
f5509cc1 KC |
196 | |
197 | for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) { | |
198 | page = virt_to_head_page(ptr); | |
199 | if (is_reserved && !PageReserved(page)) | |
f4e6e289 KC |
200 | usercopy_abort("spans Reserved and non-Reserved pages", |
201 | NULL, to_user, 0, n); | |
f5509cc1 | 202 | if (is_cma && !is_migrate_cma_page(page)) |
f4e6e289 KC |
203 | usercopy_abort("spans CMA and non-CMA pages", NULL, |
204 | to_user, 0, n); | |
f5509cc1 | 205 | } |
8e1f74ea | 206 | #endif |
8e1f74ea KC |
207 | } |
208 | ||
f4e6e289 KC |
209 | static inline void check_heap_object(const void *ptr, unsigned long n, |
210 | bool to_user) | |
8e1f74ea KC |
211 | { |
212 | struct page *page; | |
213 | ||
8e1f74ea | 214 | if (!virt_addr_valid(ptr)) |
f4e6e289 | 215 | return; |
8e1f74ea KC |
216 | |
217 | page = virt_to_head_page(ptr); | |
218 | ||
f4e6e289 KC |
219 | 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 | } | |
f5509cc1 KC |
226 | } |
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 | */ | |
234 | void __check_object_size(const void *ptr, unsigned long n, bool to_user) | |
235 | { | |
f5509cc1 KC |
236 | /* Skip all tests if size is zero. */ |
237 | if (!n) | |
238 | return; | |
239 | ||
240 | /* Check for invalid addresses. */ | |
f4e6e289 | 241 | check_bogus_address((const unsigned long)ptr, n, to_user); |
f5509cc1 KC |
242 | |
243 | /* Check for bad heap object. */ | |
f4e6e289 | 244 | check_heap_object(ptr, n, to_user); |
f5509cc1 KC |
245 | |
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: | |
f4e6e289 | 260 | usercopy_abort("process stack", NULL, to_user, 0, n); |
f5509cc1 KC |
261 | } |
262 | ||
263 | /* Check for object in kernel to avoid text exposure. */ | |
f4e6e289 | 264 | check_kernel_text_object((const unsigned long)ptr, n, to_user); |
f5509cc1 KC |
265 | } |
266 | EXPORT_SYMBOL(__check_object_size); |