Skip to content

Commit 314eed3

Browse files
committed
usercopy: Avoid HIGHMEM pfn warning
When running on a system with >512MB RAM with a 32-bit kernel built with: CONFIG_DEBUG_VIRTUAL=y CONFIG_HIGHMEM=y CONFIG_HARDENED_USERCOPY=y all execve()s will fail due to argv copying into kmap()ed pages, and on usercopy checking the calls ultimately of virt_to_page() will be looking for "bad" kmap (highmem) pointers due to CONFIG_DEBUG_VIRTUAL=y: ------------[ cut here ]------------ kernel BUG at ../arch/x86/mm/physaddr.c:83! invalid opcode: 0000 [#1] PREEMPT SMP DEBUG_PAGEALLOC CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.3.0-rc8 #6 Hardware name: Dell Inc. Inspiron 1318/0C236D, BIOS A04 01/15/2009 EIP: __phys_addr+0xaf/0x100 ... Call Trace: __check_object_size+0xaf/0x3c0 ? __might_sleep+0x80/0xa0 copy_strings+0x1c2/0x370 copy_strings_kernel+0x2b/0x40 __do_execve_file+0x4ca/0x810 ? kmem_cache_alloc+0x1c7/0x370 do_execve+0x1b/0x20 ... The check is from arch/x86/mm/physaddr.c: VIRTUAL_BUG_ON((phys_addr >> PAGE_SHIFT) > max_low_pfn); Due to the kmap() in fs/exec.c: kaddr = kmap(kmapped_page); ... if (copy_from_user(kaddr+offset, str, bytes_to_copy)) ... Now we can fetch the correct page to avoid the pfn check. In both cases, hardened usercopy will need to walk the page-span checker (if enabled) to do sanity checking. Reported-by: Randy Dunlap <[email protected]> Tested-by: Randy Dunlap <[email protected]> Fixes: f5509cc ("mm: Hardened usercopy") Cc: Matthew Wilcox <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Link: https://lore.kernel.org/r/201909171056.7F2FFD17@keescook
1 parent 4d856f7 commit 314eed3

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

mm/usercopy.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1212

1313
#include <linux/mm.h>
14+
#include <linux/highmem.h>
1415
#include <linux/slab.h>
1516
#include <linux/sched.h>
1617
#include <linux/sched/task.h>
@@ -227,7 +228,12 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
227228
if (!virt_addr_valid(ptr))
228229
return;
229230

230-
page = virt_to_head_page(ptr);
231+
/*
232+
* When CONFIG_HIGHMEM=y, kmap_to_page() will give either the
233+
* highmem page or fallback to virt_to_page(). The following
234+
* is effectively a highmem-aware virt_to_head_page().
235+
*/
236+
page = compound_head(kmap_to_page((void *)ptr));
231237

232238
if (PageSlab(page)) {
233239
/* Check slab allocator for flags and size. */

0 commit comments

Comments
 (0)