Skip to content

Commit 326ce03

Browse files
Qian Caigregkh
Qian Cai
authored andcommitted
page_poison: play nicely with KASAN
[ Upstream commit 4117992 ] KASAN does not play well with the page poisoning (CONFIG_PAGE_POISONING). It triggers false positives in the allocation path: BUG: KASAN: use-after-free in memchr_inv+0x2ea/0x330 Read of size 8 at addr ffff88881f800000 by task swapper/0 CPU: 0 PID: 0 Comm: swapper Not tainted 5.0.0-rc1+ #54 Call Trace: dump_stack+0xe0/0x19a print_address_description.cold.2+0x9/0x28b kasan_report.cold.3+0x7a/0xb5 __asan_report_load8_noabort+0x19/0x20 memchr_inv+0x2ea/0x330 kernel_poison_pages+0x103/0x3d5 get_page_from_freelist+0x15e7/0x4d90 because KASAN has not yet unpoisoned the shadow page for allocation before it checks memchr_inv() but only found a stale poison pattern. Also, false positives in free path, BUG: KASAN: slab-out-of-bounds in kernel_poison_pages+0x29e/0x3d5 Write of size 4096 at addr ffff8888112cc000 by task swapper/0/1 CPU: 5 PID: 1 Comm: swapper/0 Not tainted 5.0.0-rc1+ #55 Call Trace: dump_stack+0xe0/0x19a print_address_description.cold.2+0x9/0x28b kasan_report.cold.3+0x7a/0xb5 check_memory_region+0x22d/0x250 memset+0x28/0x40 kernel_poison_pages+0x29e/0x3d5 __free_pages_ok+0x75f/0x13e0 due to KASAN adds poisoned redzones around slab objects, but the page poisoning needs to poison the whole page. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Qian Cai <[email protected]> Acked-by: Andrey Ryabinin <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 0326696 commit 326ce03

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

mm/page_alloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,8 +1945,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
19451945

19461946
arch_alloc_page(page, order);
19471947
kernel_map_pages(page, 1 << order, 1);
1948-
kernel_poison_pages(page, 1 << order, 1);
19491948
kasan_alloc_pages(page, order);
1949+
kernel_poison_pages(page, 1 << order, 1);
19501950
set_page_owner(page, order, gfp_flags);
19511951
}
19521952

mm/page_poison.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/page_ext.h>
77
#include <linux/poison.h>
88
#include <linux/ratelimit.h>
9+
#include <linux/kasan.h>
910

1011
static bool want_page_poisoning __read_mostly;
1112

@@ -40,7 +41,10 @@ static void poison_page(struct page *page)
4041
{
4142
void *addr = kmap_atomic(page);
4243

44+
/* KASAN still think the page is in-use, so skip it. */
45+
kasan_disable_current();
4346
memset(addr, PAGE_POISON, PAGE_SIZE);
47+
kasan_enable_current();
4448
kunmap_atomic(addr);
4549
}
4650

0 commit comments

Comments
 (0)