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:
7 * Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/highmem.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/sched/task.h>
22 #include <linux/sched/task_stack.h>
23 #include <linux/thread_info.h>
24 #include <linux/atomic.h>
25 #include <linux/jump_label.h>
26 #include <asm/sections.h>
29 * Checks if a given pointer and length is contained by the current
30 * stack frame (if possible).
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)
38 static noinline
int check_stack_object(const void *obj
, unsigned long len
)
40 const void * const stack
= task_stack_page(current
);
41 const void * const stackend
= stack
+ THREAD_SIZE
;
44 /* Object is not on the stack at all. */
45 if (obj
+ len
<= stack
|| stackend
<= obj
)
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).
53 if (obj
< stack
|| stackend
< obj
+ len
)
56 /* Check if object is safely within a valid frame. */
57 ret
= arch_within_stack_frames(stack
, stackend
, obj
, len
);
65 * 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.
67 * 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.
70 * 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).
76 void usercopy_warn(const char *name
, const char *detail
, bool to_user
,
77 unsigned long offset
, unsigned long len
)
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",
83 detail
? " '" : "", detail
? : "", detail
? "'" : "",
87 void __noreturn
usercopy_abort(const char *name
, const char *detail
,
88 bool to_user
, unsigned long offset
,
91 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",
95 detail
? " '" : "", detail
? : "", detail
? "'" : "",
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.
106 /* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
107 static bool overlaps(const unsigned long ptr
, unsigned long n
,
108 unsigned long low
, unsigned long high
)
110 const unsigned long check_low
= ptr
;
111 unsigned long check_high
= check_low
+ n
;
113 /* Does not overlap if entirely above or entirely below. */
114 if (check_low
>= high
|| check_high
<= low
)
120 /* Is this address range in the kernel text area? */
121 static inline void check_kernel_text_object(const unsigned long ptr
,
122 unsigned long n
, bool to_user
)
124 unsigned long textlow
= (unsigned long)_stext
;
125 unsigned long texthigh
= (unsigned long)_etext
;
126 unsigned long textlow_linear
, texthigh_linear
;
128 if (overlaps(ptr
, n
, textlow
, texthigh
))
129 usercopy_abort("kernel text", NULL
, to_user
, ptr
- textlow
, n
);
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
139 textlow_linear
= (unsigned long)lm_alias(textlow
);
140 /* No different mapping: we're done. */
141 if (textlow_linear
== textlow
)
144 /* Check the secondary mapping... */
145 texthigh_linear
= (unsigned long)lm_alias(texthigh
);
146 if (overlaps(ptr
, n
, textlow_linear
, texthigh_linear
))
147 usercopy_abort("linear kernel text", NULL
, to_user
,
148 ptr
- textlow_linear
, n
);
151 static inline void check_bogus_address(const unsigned long ptr
, unsigned long n
,
154 /* Reject if object wraps past end of memory. */
155 if (ptr
+ (n
- 1) < ptr
)
156 usercopy_abort("wrapped address", NULL
, to_user
, 0, ptr
+ n
);
158 /* Reject if NULL or ZERO-allocation. */
159 if (ZERO_OR_NULL_PTR(ptr
))
160 usercopy_abort("null address", NULL
, to_user
, ptr
, n
);
163 /* Checks for allocs that are marked in some way as spanning multiple pages. */
164 static inline void check_page_span(const void *ptr
, unsigned long n
,
165 struct page
*page
, bool to_user
)
167 #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
168 const void *end
= ptr
+ n
- 1;
169 struct page
*endpage
;
170 bool is_reserved
, is_cma
;
173 * 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.
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
) {
182 usercopy_abort("rodata", NULL
, to_user
, 0, n
);
186 /* Allow kernel data region (if not marked as Reserved). */
187 if (ptr
>= (const void *)_sdata
&& end
<= (const void *)_edata
)
190 /* Allow kernel bss region (if not marked as Reserved). */
191 if (ptr
>= (const void *)__bss_start
&&
192 end
<= (const void *)__bss_stop
)
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
)))
200 /* Allow if fully inside the same compound (__GFP_COMP) page. */
201 endpage
= virt_to_head_page(end
);
202 if (likely(endpage
== page
))
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.
210 is_reserved
= PageReserved(page
);
211 is_cma
= is_migrate_cma_page(page
);
212 if (!is_reserved
&& !is_cma
)
213 usercopy_abort("spans multiple pages", NULL
, to_user
, 0, n
);
215 for (ptr
+= PAGE_SIZE
; ptr
<= end
; ptr
+= PAGE_SIZE
) {
216 page
= virt_to_head_page(ptr
);
217 if (is_reserved
&& !PageReserved(page
))
218 usercopy_abort("spans Reserved and non-Reserved pages",
219 NULL
, to_user
, 0, n
);
220 if (is_cma
&& !is_migrate_cma_page(page
))
221 usercopy_abort("spans CMA and non-CMA pages", NULL
,
227 static inline void check_heap_object(const void *ptr
, unsigned long n
,
232 if (!virt_addr_valid(ptr
))
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().
240 page
= compound_head(kmap_to_page((void *)ptr
));
242 if (PageSlab(page
)) {
243 /* Check slab allocator for flags and size. */
244 __check_heap_object(ptr
, n
, page
, to_user
);
246 /* Verify object does not incorrectly span multiple pages. */
247 check_page_span(ptr
, n
, page
, to_user
);
251 static DEFINE_STATIC_KEY_FALSE_RO(bypass_usercopy_checks
);
254 * Validates that the given object is:
255 * - not bogus address
256 * - fully contained by stack (or stack frame, when available)
257 * - fully within SLAB object (or object whitelist area, when available)
258 * - not in kernel text
260 void __check_object_size(const void *ptr
, unsigned long n
, bool to_user
)
262 if (static_branch_unlikely(&bypass_usercopy_checks
))
265 /* Skip all tests if size is zero. */
269 /* Check for invalid addresses. */
270 check_bogus_address((const unsigned long)ptr
, n
, to_user
);
272 /* Check for bad stack object. */
273 switch (check_stack_object(ptr
, n
)) {
275 /* Object is not touching the current process stack. */
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).
286 usercopy_abort("process stack", NULL
, to_user
, 0, n
);
289 /* Check for bad heap object. */
290 check_heap_object(ptr
, n
, to_user
);
292 /* Check for object in kernel to avoid text exposure. */
293 check_kernel_text_object((const unsigned long)ptr
, n
, to_user
);
295 EXPORT_SYMBOL(__check_object_size
);
297 static bool enable_checks __initdata
= true;
299 static int __init
parse_hardened_usercopy(char *str
)
301 return strtobool(str
, &enable_checks
);
304 __setup("hardened_usercopy=", parse_hardened_usercopy
);
306 static int __init
set_hardened_usercopy(void)
308 if (enable_checks
== false)
309 static_branch_enable(&bypass_usercopy_checks
);
313 late_initcall(set_hardened_usercopy
);