]> asedeno.scripts.mit.edu Git - linux.git/blob - mm/usercopy.c
usercopy: Enhance and rename report_usercopy()
[linux.git] / mm / usercopy.c
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>
19 #include <linux/sched.h>
20 #include <linux/sched/task.h>
21 #include <linux/sched/task_stack.h>
22 #include <linux/thread_info.h>
23 #include <asm/sections.h>
24
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
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)
72 {
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
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). */
89 static bool overlaps(const void *ptr, unsigned long n, unsigned long low,
90                      unsigned long high)
91 {
92         unsigned long check_low = (uintptr_t)ptr;
93         unsigned long check_high = check_low + n;
94
95         /* Does not overlap if entirely above or entirely below. */
96         if (check_low >= high || check_high <= low)
97                 return false;
98
99         return true;
100 }
101
102 /* Is this address range in the kernel text area? */
103 static inline const char *check_kernel_text_object(const void *ptr,
104                                                    unsigned long n)
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))
111                 return "<kernel text>";
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          */
121         textlow_linear = (unsigned long)lm_alias(textlow);
122         /* No different mapping: we're done. */
123         if (textlow_linear == textlow)
124                 return NULL;
125
126         /* Check the secondary mapping... */
127         texthigh_linear = (unsigned long)lm_alias(texthigh);
128         if (overlaps(ptr, n, textlow_linear, texthigh_linear))
129                 return "<linear kernel text>";
130
131         return NULL;
132 }
133
134 static inline const char *check_bogus_address(const void *ptr, unsigned long n)
135 {
136         /* Reject if object wraps past end of memory. */
137         if ((unsigned long)ptr + n < (unsigned long)ptr)
138                 return "<wrapped address>";
139
140         /* Reject if NULL or ZERO-allocation. */
141         if (ZERO_OR_NULL_PTR(ptr))
142                 return "<null>";
143
144         return NULL;
145 }
146
147 /* Checks for allocs that are marked in some way as spanning multiple pages. */
148 static inline const char *check_page_span(const void *ptr, unsigned long n,
149                                           struct page *page, bool to_user)
150 {
151 #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
152         const void *end = ptr + n - 1;
153         struct page *endpage;
154         bool is_reserved, is_cma;
155
156         /*
157          * Sometimes the kernel data regions are not marked Reserved (see
158          * check below). And sometimes [_sdata,_edata) does not cover
159          * rodata and/or bss, so check each range explicitly.
160          */
161
162         /* Allow reads of kernel rodata region (if not marked as Reserved). */
163         if (ptr >= (const void *)__start_rodata &&
164             end <= (const void *)__end_rodata) {
165                 if (!to_user)
166                         return "<rodata>";
167                 return NULL;
168         }
169
170         /* Allow kernel data region (if not marked as Reserved). */
171         if (ptr >= (const void *)_sdata && end <= (const void *)_edata)
172                 return NULL;
173
174         /* Allow kernel bss region (if not marked as Reserved). */
175         if (ptr >= (const void *)__bss_start &&
176             end <= (const void *)__bss_stop)
177                 return NULL;
178
179         /* Is the object wholly within one base page? */
180         if (likely(((unsigned long)ptr & (unsigned long)PAGE_MASK) ==
181                    ((unsigned long)end & (unsigned long)PAGE_MASK)))
182                 return NULL;
183
184         /* Allow if fully inside the same compound (__GFP_COMP) page. */
185         endpage = virt_to_head_page(end);
186         if (likely(endpage == page))
187                 return NULL;
188
189         /*
190          * Reject if range is entirely either Reserved (i.e. special or
191          * device memory), or CMA. Otherwise, reject since the object spans
192          * several independently allocated pages.
193          */
194         is_reserved = PageReserved(page);
195         is_cma = is_migrate_cma_page(page);
196         if (!is_reserved && !is_cma)
197                 return "<spans multiple pages>";
198
199         for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) {
200                 page = virt_to_head_page(ptr);
201                 if (is_reserved && !PageReserved(page))
202                         return "<spans Reserved and non-Reserved pages>";
203                 if (is_cma && !is_migrate_cma_page(page))
204                         return "<spans CMA and non-CMA pages>";
205         }
206 #endif
207
208         return NULL;
209 }
210
211 static inline const char *check_heap_object(const void *ptr, unsigned long n,
212                                             bool to_user)
213 {
214         struct page *page;
215
216         if (!virt_addr_valid(ptr))
217                 return NULL;
218
219         page = virt_to_head_page(ptr);
220
221         /* Check slab allocator for flags and size. */
222         if (PageSlab(page))
223                 return __check_heap_object(ptr, n, page);
224
225         /* Verify object does not incorrectly span multiple pages. */
226         return check_page_span(ptr, n, page, to_user);
227 }
228
229 /*
230  * Validates that the given object is:
231  * - not bogus address
232  * - known-safe heap or stack object
233  * - not in kernel text
234  */
235 void __check_object_size(const void *ptr, unsigned long n, bool to_user)
236 {
237         const char *err;
238
239         /* Skip all tests if size is zero. */
240         if (!n)
241                 return;
242
243         /* Check for invalid addresses. */
244         err = check_bogus_address(ptr, n);
245         if (err)
246                 goto report;
247
248         /* Check for bad heap object. */
249         err = check_heap_object(ptr, n, to_user);
250         if (err)
251                 goto report;
252
253         /* Check for bad stack object. */
254         switch (check_stack_object(ptr, n)) {
255         case NOT_STACK:
256                 /* Object is not touching the current process stack. */
257                 break;
258         case GOOD_FRAME:
259         case GOOD_STACK:
260                 /*
261                  * Object is either in the correct frame (when it
262                  * is possible to check) or just generally on the
263                  * process stack (when frame checking not available).
264                  */
265                 return;
266         default:
267                 err = "<process stack>";
268                 goto report;
269         }
270
271         /* Check for object in kernel to avoid text exposure. */
272         err = check_kernel_text_object(ptr, n);
273         if (!err)
274                 return;
275
276 report:
277         usercopy_abort(err, NULL, to_user, 0, n);
278 }
279 EXPORT_SYMBOL(__check_object_size);