mm/kasan: dump alloc and free stack for page allocator
Subsystems (8)
PATCH v3 1/1 mm/kasan: dump alloc and free stack for page allocator
This patch is KASAN's report adds the alloc/free stack for page allocator in order to help programmer to see memory corruption caused by the page.
By default, KASAN doesn't record alloc or free stack for page allocator. It is difficult to fix up the page use-after-free or double-free issue.
We add the following changing:
1) KASAN enable PAGE_OWNER by default to get the alloc stack of the page.
2) Add new feature option to get the free stack of the page.
The new feature KASAN_DUMP_PAGE depends on DEBUG_PAGEALLOC, it will help to record free stack of the page, it is very helpful for solving the page use-after-free or double-free issue.
When KASAN_DUMP_PAGE is enabled then KASAN's report will show the last alloc and free stack of the page, it should be:
BUG: KASAN: use-after-free in kmalloc_pagealloc_uaf+0x70/0x80 Write of size 1 at addr ffffffc0d60e4000 by task cat/115 ... prep_new_page+0x1c8/0x218 get_page_from_freelist+0x1ba0/0x28d0 __alloc_pages_nodemask+0x1d4/0x1978 kmalloc_order+0x28/0x58 kmalloc_order_trace+0x28/0xe0 kmalloc_pagealloc_uaf+0x2c/0x80 page last free stack trace: __free_pages_ok+0x116c/0x1630 __free_pages+0x50/0x78 kfree+0x1c4/0x250 kmalloc_pagealloc_uaf+0x38/0x80
Changes since v1:
- slim page_owner and move it into kasan
- enable the feature by default
Changes since v2:
- enable PAGE_OWNER by default
- use DEBUG_PAGEALLOC to get page information
--- lib/Kconfig.kasan | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
lib/Kconfig.kasan +15 -0
@@ -41,6 +41,7 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGER_OWNER help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2
@@ -63,6 +64,7 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGER_OWNER help Enables software tag-based KASAN mode. This mode requires Top Byte Ignore support by the CPU and therefore
@@ -135,6 +137,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_DUMP_PAGE + bool "Dump the last allocation and freeing stack of the page" + depends on KASAN + select DEBUG_PAGEALLOC + help + By default, KASAN enable PAGE_OWNER only to record alloc stack + for page allocator. It is difficult to fix up page use-after-free + or double-free issue. + This feature depends on DEBUG_PAGEALLOC, it will extra record + free stack of page. It is very helpful for solving the page + use-after-free or double-free issue. + This option will have a small memory overhead. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASAN
Signature · 1 line
2.18.0
Quoted 90 lines · Walter Wu
This patch is KASAN's report adds the alloc/free stack for page allocator in order to help programmer to see memory corruption caused by the page.
By default, KASAN doesn't record alloc or free stack for page allocator. It is difficult to fix up the page use-after-free or double-free issue.
We add the following changing:
1) KASAN enablePAGE_OWNERby default to get the alloc stack of the page.
2) Add new feature option to get the free stack of the page.The new feature
KASAN_DUMP_PAGEdepends onDEBUG_PAGEALLOC, it will help to record free stack of the page, it is very helpful for solving the page use-after-free or double-free issue.When
KASAN_DUMP_PAGEis enabled then KASAN's report will show the last alloc and free stack of the page, it should be:BUG: KASAN: use-after-free in kmalloc_pagealloc_uaf+0x70/0x80 Write of size 1 at addr ffffffc0d60e4000 by task cat/115 ... prep_new_page+0x1c8/0x218 get_page_from_freelist+0x1ba0/0x28d0 __alloc_pages_nodemask+0x1d4/0x1978 kmalloc_order+0x28/0x58 kmalloc_order_trace+0x28/0xe0 kmalloc_pagealloc_uaf+0x2c/0x80 page last free stack trace: __free_pages_ok+0x116c/0x1630 __free_pages+0x50/0x78 kfree+0x1c4/0x250 kmalloc_pagealloc_uaf+0x38/0x80Changes since v1:
- slimpage_ownerand move it into kasan
- enable the feature by defaultChanges since v2:
- enablePAGE_OWNERby default
- useDEBUG_PAGEALLOCto get page informationcc Andrey Ryabinin <[email protected]>cc Vlastimil Babka <[email protected]>cc Andrey Konovalov <[email protected]>Signed-off-by Walter Wu <[email protected]>---
lib/Kconfig.kasan| 15 +++++++++++++++1 file changed, 15 insertions(+)lib/Kconfig.kasan +15
-0@@ -41,6 +41,7 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGER_OWNER help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2@@ -63,6 +64,7 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGER_OWNER help Enables software tag-based KASAN mode. This mode requires Top Byte Ignore support by the CPU and therefore@@ -135,6 +137,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_DUMP_PAGE + bool "Dump the last allocation and freeing stack of the page" + depends on KASAN + select DEBUG_PAGEALLOC + help + By default, KASAN enable PAGE_OWNER only to record alloc stack + for page allocator. It is difficult to fix up page use-after-free + or double-free issue. + This feature depends on DEBUG_PAGEALLOC, it will extra record + free stack of page. It is very helpful for solving the page + use-after-free or double-free issue. + This option will have a small memory overhead. +config
TEST_KASAN
Quoted 3 lines
tristate "Module for testing KASAN for bug detection" depends on m && KASAN—
The new config looks redundant and confusing. It looks to me more of a document update in Documentation/dev-tools/kasan.txt to educate developers to select PAGE_OWNER and DEBUG_PAGEALLOC if needed.
Quoted 4 lines · Qian Cai
The new config looks redundant and confusing. It looks to me more of a document update in
Documentation/dev-tools/kasan.txtto educate developers to selectPAGE_OWNERandDEBUG_PAGEALLOCif needed.
Agreed. But if you want it fully automatic, how about something like this (on top of mmotm/next)? If you agree I'll add changelog and send properly.
----8<----
Quoted 1 line
From
a528d14c71d7fdf5872ca8ab3bd1b5bad26670c9Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Sep 2019 15:51:23 +0200
Subject: [PATCH] make KASAN enable page_owner with free stack capture
--- include/linux/page_owner.h | 1 + lib/Kconfig.kasan | 4 ++++ mm/Kconfig.debug | 5 +++++ mm/page_alloc.c | 6 +++++- mm/page_owner.c | 37 ++++++++++++++++++++++++------------- 5 files changed, 39 insertions(+), 14 deletions(-)
include/linux/page_owner.h +1 -0
@@ -6,6 +6,7 @@#ifdef CONFIG_PAGE_OWNER extern struct static_key_false page_owner_inited; +extern bool page_owner_free_stack_disabled;extern struct page_ext_operations page_owner_ops;extern void __reset_page_owner (struct page *page,unsigned int order);
lib/Kconfig.kasan +4 -0
@@ -41,6 +41,8 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2
@@ -63,6 +65,8 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables software tag-based KASAN mode. This mode requires Top Byte Ignore support by the CPU and therefore
mm/Kconfig.debug +5 -0
@@ -13,6 +13,7 @@ config DEBUG_PAGEALLOC depends on DEBUG_KERNEL depends on !HIBERNATION || ARCH_SUPPORTS_DEBUG_PAGEALLOC && !PPC && !SPARC select PAGE_POISONING if !ARCH_SUPPORTS_DEBUG_PAGEALLOC + select PAGE_OWNER_FREE_STACK if PAGE_OWNER ---help--- Unmap pages from the kernel linear mapping after free_pages(). Depending on runtime enablement, this results in a small or large
@@ -62,6 +63,10 @@ config PAGE_OWNER If unsure, say N. +config PAGE_OWNER_FREE_STACK + def_bool n + depends on PAGE_OWNER + config PAGE_POISONING bool "Poison pages after freeing" select PAGE_POISONING_NO_SANITY if HIBERNATION
mm/page_alloc.c +5 -1
@@ -710,8 +710,12 @@ static int __init early_debug_pagealloc(char *buf)if (kstrtobool (buf, &enable))return -EINVAL ; -if (enable) +if (enable) {static_branch_enable (&_debug_pagealloc_enabled); +#ifdef CONFIG_PAGE_OWNER + page_owner_free_stack_disabled = false; +#endif + }return 0 ; }
mm/page_owner.c +24 -13
@@ -24,13 +24,15 @@ struct page_owner {short last_migrate_reason;gfp_t gfp_mask;depot_stack_handle_t handle; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t free_handle;#endif };static bool page_owner_disabled = true; +bool page_owner_free_stack_disabled = true;DEFINE_STATIC_KEY_FALSE (page_owner_inited); +static DEFINE_STATIC_KEY_FALSE (page_owner_free_stack);static depot_stack_handle_t dummy_handle;static depot_stack_handle_t failure_handle;
@@ -46,6 +48,11 @@ static int __init early_page_owner_param(char *buf)if (strcmp (buf,"on" ) ==0 ) page_owner_disabled = false; +if (IS_ENABLED (CONFIG_KASAN )) { + page_owner_disabled = false; + page_owner_free_stack_disabled = false; + } +return 0 ; }early_param ("page_owner" , early_page_owner_param);
@@ -91,6 +98,8 @@ static void init_page_owner(void)register_failure_stack ();register_early_stack ();static_branch_enable (&page_owner_inited); +if (!page_owner_free_stack_disabled) +static_branch_enable (&page_owner_free_stack);init_early_allocated_pages (); }
@@ -148,11 +157,11 @@ void __reset_page_owner(struct page *page, unsigned int order) {int i;struct page_ext *page_ext; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t handle =0 ;struct page_owner *page_owner; -if (debug_pagealloc_enabled ()) +if (static_branch_unlikely (&page_owner_free_stack)) handle =save_stack (GFP_NOWAIT | __GFP_NOWARN);#endif
@@ -161,8 +170,8 @@ void __reset_page_owner(struct page *page, unsigned int order)if (unlikely (!page_ext))continue ;__clear_bit (PAGE_EXT_OWNER_ACTIVE , &page_ext->flags); -#ifdef CONFIG_DEBUG_PAGEALLOC -if (debug_pagealloc_enabled ()) { +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { page_owner =get_page_owner (page_ext); page_owner->free_handle = handle; }
@@ -451,14 +460,16 @@ void __dump_page_owner(struct page *page)stack_trace_print (entries, nr_entries,0 ); } -#ifdef CONFIG_DEBUG_PAGEALLOC - handle =READ_ONCE (page_owner->free_handle); -if (!handle) { -pr_alert ("page_owner free stack trace missing\n" ); - }else { - nr_entries =stack_depot_fetch (handle, &entries); -pr_alert ("page last free stack trace:\n" ); -stack_trace_print (entries, nr_entries,0 ); +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { + handle =READ_ONCE (page_owner->free_handle); +if (!handle) { +pr_alert ("page_owner free stack trace missing\n" ); + }else { + nr_entries =stack_depot_fetch (handle, &entries); +pr_alert ("page last free stack trace:\n" ); +stack_trace_print (entries, nr_entries,0 ); + } }#endif
Signature · 1 line
2.23.0
Quoted 17 lines · Vlastimil Babka
Quoted 4 lines · Qian Cai
The new config looks redundant and confusing. It looks to me more of a document update in
Documentation/dev-tools/kasan.txtto educate developers to selectPAGE_OWNERandDEBUG_PAGEALLOCif needed.Agreed. But if you want it fully automatic, how about something like this (on top of mmotm/next)? If you agree I'll add changelog and send properly.
----8<----
From
a528d14c71d7fdf5872ca8ab3bd1b5bad26670c9Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Sep 2019 15:51:23 +0200
Subject: [PATCH] make KASAN enablepage_ownerwith free stack capture
Quoted 8 lines
--- include/linux/page_owner.h | 1 + lib/Kconfig.kasan | 4 ++++ mm/Kconfig.debug | 5 +++++ mm/page_alloc.c | 6 +++++- mm/page_owner.c | 37 ++++++++++++++++++++++++------------- 5 files changed, 39 insertions(+), 14 deletions(-)
Looks ok to me. This certainly better than full dependency on the DEBUG_PAGEALLOC which we don't need.
Quoted 16 lines
extern void
__reset_page_owner(struct page *page, unsigned int order);lib/Kconfig.kasan +2
-0@@ -41,6 +41,8 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2
lib/Kconfig.kasan config KASAN_SW_TAGS · line 70
Quoted 6 lines
lib/Kconfig.kasan +2
-0@@ -63,6 +65,8 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help
What is the difference between PAGE_OWNER+PAGE_OWNER_FREE_STACK and DEBUG_PAGEALLOC?
If you directly enable PAGE_OWNER+PAGE_OWNER_FREE_STACK PAGE_OWNER_FREE_STACK,don't you think low-memory device to want to use KASAN?
Quoted 17 lines · Walter Wu
Quoted 17 lines
extern void
__reset_page_owner(struct page *page, unsigned int order);lib/Kconfig.kasan +2
-0@@ -41,6 +41,8 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2@@ -63,6 +65,8 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB
Quoted 8 lines
lib/Kconfig.kasan config KASAN_GENERIC · line 46
Quoted 5 lines
lib/Kconfig.kasan +2
-0select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK helpWhat is the difference between PAGE_OWNER+PAGE_OWNER_FREE_STACK and
DEBUG_PAGEALLOC?
Same memory usage, but debug_pagealloc means also extra checks and restricting memory access to freed pages to catch UAF.
Quoted 3 lines
If you directly enable PAGE_OWNER+PAGE_OWNER_FREE_STACK PAGE_OWNER_FREE_STACK,don't you think low-memory device to want to use KASAN?
OK, so it should be optional? But I think it's enough to distinguish no PAGE_OWNER at all, and PAGE_OWNER+PAGE_OWNER_FREE_STACK together - I don't see much point in PAGE_OWNER only for this kind of debugging.
So how about this? KASAN wouldn't select PAGE_OWNER* but it would be recommended in the help+docs. When PAGE_OWNER and KASAN are selected by user, PAGE_OWNER_FREE_STACK gets also selected, and both will be also runtime enabled without explicit page_owner=on. I mostly want to avoid another boot-time option for enabling PAGE_OWNER_FREE_STACK.
Would that be enough flexibility for low-memory devices vs full-fledged debugging?
Quoted 30 lines · Vlastimil Babka
Quoted 17 lines · Walter Wu
Quoted 17 lines
extern void
__reset_page_owner(struct page *page, unsigned int order);lib/Kconfig.kasan +2
-0@@ -41,6 +41,8 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2@@ -63,6 +65,8 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUBQuoted 8 lines
lib/Kconfig.kasan config KASAN_GENERIC · line 46
Quoted 5 lines
lib/Kconfig.kasan +2
-0select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK helpWhat is the difference between PAGE_OWNER+PAGE_OWNER_FREE_STACK and
DEBUG_PAGEALLOC?Same memory usage, but
debug_pageallocmeans also extra checks and restricting memory access to freed pages to catch UAF.
Quoted 8 lines
Quoted 3 lines
If you directly enable PAGE_OWNER+PAGE_OWNER_FREE_STACK PAGE_OWNER_FREE_STACK,don't you think low-memory device to want to use KASAN?
OK, so it should be optional? But I think it's enough to distinguish no
PAGE_OWNERat all, and PAGE_OWNER+PAGE_OWNER_FREE_STACK together - I don't see much point inPAGE_OWNERonly for this kind of debugging.
If it's possible, it should be optional.
My experience is that PAGE_OWNER usually debug memory leakage.
Quoted 8 lines
So how about this? KASAN wouldn't select PAGE_OWNER* but it would be recommended in the help+docs. When
PAGE_OWNERand KASAN are selected by user,PAGE_OWNER_FREE_STACKgets also selected, and both will be also runtime enabled without explicit page_owner=on.
I mostly want to avoid another boot-time option for enablingPAGE_OWNER_FREE_STACK.
Would that be enough flexibility for low-memory devices vs full-fledged debugging?
We usually see feature option to decide whether it meet the platform. The boot-time option isn't troubled to us, because enable the feature owner should know what he should add to do.
Quoted 33 lines · Vlastimil Babka
Quoted 17 lines · Walter Wu
Quoted 17 lines
extern void
__reset_page_owner(struct page *page, unsigned int order);lib/Kconfig.kasan +0
-0@@ -41,6 +41,8 @@ config KASAN_GENERICselect
SLUB_DEBUGif SLUB
select CONSTRUCTORS
select STACKDEPOT
+ selectPAGE_OWNER
+ selectPAGE_OWNER_FREE_STACK
help
Enables generic KASAN mode.
Supported in both GCC and Clang. With GCC it requires version 4.9.2lib/Kconfig.kasan +0
-0@@ -63,6 +65,8 @@ config KASAN_SW_TAGSselect
SLUB_DEBUGif SLUBQuoted 8 lines
Quoted 5 lines
select CONSTRUCTORS
select STACKDEPOT
+ selectPAGE_OWNER
+ selectPAGE_OWNER_FREE_STACK
helpWhat is the difference between PAGE_OWNER+PAGE_OWNER_FREE_STACK and
DEBUG_PAGEALLOC?Same memory usage, but
debug_pageallocmeans also extra checks and restricting memory access to freed pages to catch UAF.Quoted 3 lines
If you directly enable PAGE_OWNER+PAGE_OWNER_FREE_STACK PAGE_OWNER_FREE_STACK,don't you think low-memory device to want to use KASAN?
Quoted 5 lines
OK, so it should be optional? But I think it's enough to distinguish no
PAGE_OWNERat all, and PAGE_OWNER+PAGE_OWNER_FREE_STACK together - I don't see much point inPAGE_OWNERonly for this kind of debugging.So how about this? KASAN wouldn't select PAGE_OWNER* but it would be recommended in the help+docs. When
PAGE_OWNERand KASAN are selected by user,PAGE_OWNER_FREE_STACKgets also selected, and both will be also runtime enabled without explicit page_owner=on. I mostly want to avoid another boot-time option for enablingPAGE_OWNER_FREE_STACK.
Would that be enough flexibility for low-memory devices vs full-fledged debugging?
Originally I thought that with you patch users still can disable page_owner via "page_owner=off" boot param. But now I realized that this won't work. I think it should work, we should allow users to disable it.
Or another alternative option (and actually easier one to implement), leave PAGE_OWNER as is (no "select"s in Kconfigs) Make PAGE_OWNER_FREE_STACK like this:
+config PAGE_OWNER_FREE_STACK
+ def_bool KASAN || DEBUG_PAGEALLOC
+ depends on PAGE_OWNER
+
So, users that want alloc/free stack will have to enable CONFIG_PAGE_OWNER=y and add page_owner=on to boot cmdline.
Basically the difference between these alternative is whether we enable page_owner by default or not. But there is always a possibility to disable it.
Quoted 9 lines · Andrey Ryabinin
Or another alternative option (and actually easier one to implement), leave
PAGE_OWNERas is (no "select"s in Kconfigs) MakePAGE_OWNER_FREE_STACKlike this:(quoted diff) +4
-0+config PAGE_OWNER_FREE_STACK + def_bool KASAN || DEBUG_PAGEALLOC + depends on PAGE_OWNER +
Quoted 4 lines
So, users that want alloc/free stack will have to enable
CONFIG_PAGE_OWNER=yand add page_owner=on to boot cmdline.Basically the difference between these alternative is whether we enable
page_ownerby default or not. But there is always a possibility to disable it.
OK, how about this?
BTW, the bugzilla [1] also mentions that on overflow we might be dumping the wrong page (including stacks). I'll leave that to somebody familiar with KASAN internals though.
[1] https://bugzilla.kernel.org/show_bug.cgi?id=203967
----8<----
Quoted 1 line
From
887e3c092c073d996098ac2b101b0feaef110b54Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 16 Sep 2019 11:28:19 +0200
Subject: [PATCH] mm, debug, kasan: save and dump freeing stack trace for kasan
The commit "mm, page_owner, debug_pagealloc: save and dump freeing stack trace" enhanced page_owner to also store freeing stack trace, when debug_pagealloc is also enabled. KASAN would also like to do this [1] to improve error reports to debug e.g. UAF issues. This patch therefore introduces a helper config option PAGE_OWNER_FREE_STACK, which is enabled when PAGE_OWNER and either of DEBUG_PAGEALLOC or KASAN is enabled. Boot-time, the free stack saving is enabled when booting a KASAN kernel with page_owner=on, or non-KASAN kernel with debug_pagealloc=on and page_owner=on.
[1] https://bugzilla.kernel.org/show_bug.cgi?id=203967
--- Documentation/dev-tools/kasan.rst | 4 ++++ include/linux/page_owner.h | 1 + mm/Kconfig.debug | 4 ++++ mm/page_alloc.c | 6 +++++- mm/page_owner.c | 35 +++++++++++++++++++------------ 5 files changed, 36 insertions(+), 14 deletions(-)
Documentation/dev-tools/kasan.rst +4 -0
@@ -41,6 +41,10 @@ smaller binary while the latter is 1.1 - 2 times faster. Both KASAN modes work with both SLUB and SLAB memory allocators. For better bug detection and nicer reporting, enable CONFIG_STACKTRACE. +To augment reports with last allocation and freeing stack of the physical +page, it is recommended to configure kernel also with CONFIG_PAGE_OWNER = y +and boot with page_owner=on. + To disable instrumentation for specific files or directories, add a line similar to the following to the respective kernel Makefile:
include/linux/page_owner.h +1 -0
@@ -6,6 +6,7 @@#ifdef CONFIG_PAGE_OWNER extern struct static_key_false page_owner_inited; +extern bool page_owner_free_stack_disabled;extern struct page_ext_operations page_owner_ops;extern void __reset_page_owner (struct page *page,unsigned int order);
mm/Kconfig.debug +4 -0
@@ -62,6 +62,10 @@ config PAGE_OWNER If unsure, say N. +config PAGE_OWNER_FREE_STACK + def_bool KASAN || DEBUG_PAGEALLOC + depends on PAGE_OWNER + config PAGE_POISONING bool "Poison pages after freeing" select PAGE_POISONING_NO_SANITY if HIBERNATION
mm/page_alloc.c +5 -1
@@ -710,8 +710,12 @@ static int __init early_debug_pagealloc(char *buf)if (kstrtobool (buf, &enable))return -EINVAL ; -if (enable) +if (enable) {static_branch_enable (&_debug_pagealloc_enabled); +#ifdef CONFIG_PAGE_OWNER + page_owner_free_stack_disabled = false; +#endif + }return 0 ; }
mm/page_owner.c +22 -13
@@ -24,13 +24,15 @@ struct page_owner {short last_migrate_reason;gfp_t gfp_mask;depot_stack_handle_t handle; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t free_handle;#endif };static bool page_owner_disabled = true; +bool page_owner_free_stack_disabled = true;DEFINE_STATIC_KEY_FALSE (page_owner_inited); +static DEFINE_STATIC_KEY_FALSE (page_owner_free_stack);static depot_stack_handle_t dummy_handle;static depot_stack_handle_t failure_handle;
@@ -46,6 +48,9 @@ static int __init early_page_owner_param(char *buf)if (strcmp (buf,"on" ) ==0 ) page_owner_disabled = false; +if (!page_owner_disabled &&IS_ENABLED (CONFIG_KASAN )) + page_owner_free_stack_disabled = false; +return 0 ; }early_param ("page_owner" , early_page_owner_param);
@@ -91,6 +96,8 @@ static void init_page_owner(void)register_failure_stack ();register_early_stack ();static_branch_enable (&page_owner_inited); +if (!page_owner_free_stack_disabled) +static_branch_enable (&page_owner_free_stack);init_early_allocated_pages (); }
@@ -148,11 +155,11 @@ void __reset_page_owner(struct page *page, unsigned int order) {int i;struct page_ext *page_ext; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t handle =0 ;struct page_owner *page_owner; -if (debug_pagealloc_enabled ()) +if (static_branch_unlikely (&page_owner_free_stack)) handle =save_stack (GFP_NOWAIT | __GFP_NOWARN);#endif
@@ -161,8 +168,8 @@ void __reset_page_owner(struct page *page, unsigned int order)if (unlikely (!page_ext))continue ;__clear_bit (PAGE_EXT_OWNER_ACTIVE , &page_ext->flags); -#ifdef CONFIG_DEBUG_PAGEALLOC -if (debug_pagealloc_enabled ()) { +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { page_owner =get_page_owner (page_ext); page_owner->free_handle = handle; }
@@ -451,14 +458,16 @@ void __dump_page_owner(struct page *page)stack_trace_print (entries, nr_entries,0 ); } -#ifdef CONFIG_DEBUG_PAGEALLOC - handle =READ_ONCE (page_owner->free_handle); -if (!handle) { -pr_alert ("page_owner free stack trace missing\n" ); - }else { - nr_entries =stack_depot_fetch (handle, &entries); -pr_alert ("page last free stack trace:\n" ); -stack_trace_print (entries, nr_entries,0 ); +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { + handle =READ_ONCE (page_owner->free_handle); +if (!handle) { +pr_alert ("page_owner free stack trace missing\n" ); + }else { + nr_entries =stack_depot_fetch (handle, &entries); +pr_alert ("page last free stack trace:\n" ); +stack_trace_print (entries, nr_entries,0 ); + } }#endif
Signature · 1 line
2.23.0
Quoted 17 lines · Vlastimil Babka
Quoted 9 lines · Andrey Ryabinin
Or another alternative option (and actually easier one to implement), leave
PAGE_OWNERas is (no "select"s in Kconfigs) MakePAGE_OWNER_FREE_STACKlike this:mm/Kconfig.debug +4
-0+config PAGE_OWNER_FREE_STACK + def_bool KASAN || DEBUG_PAGEALLOC + depends on PAGE_OWNER +Quoted 4 lines
So, users that want alloc/free stack will have to enable
CONFIG_PAGE_OWNER=yand add page_owner=on to boot cmdline.Basically the difference between these alternative is whether we enable
page_ownerby default or not. But there is always a possibility to disable it.OK, how about this?
...
mm/page_alloc.c static int __init early_debug_pagealloc(char *buf) · line 716
Quoted 13 lines
mm/page_alloc.c +3
-1@@ -710,8 +710,12 @@ static int __init early_debug_pagealloc(char *buf)if (kstrtobool (buf, &enable))return -EINVAL ; -if (enable) +if (enable) {static_branch_enable (&_debug_pagealloc_enabled); +#ifdef CONFIG_PAGE_OWNER + page_owner_free_stack_disabled = false;
I think this won't work with CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=y
Quoted 27 lines
mm/page_alloc.c +2
-0+#endif + }return 0 ; }mm/page_owner.c +3
-1@@ -24,13 +24,15 @@ struct page_owner {short last_migrate_reason;gfp_t gfp_mask;depot_stack_handle_t handle; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t free_handle;#endif };static bool page_owner_disabled = true; +bool page_owner_free_stack_disabled = true;DEFINE_STATIC_KEY_FALSE (page_owner_inited); +static DEFINE_STATIC_KEY_FALSE (page_owner_free_stack);static depot_stack_handle_t dummy_handle;static depot_stack_handle_t failure_handle;
mm/page_owner.c static int __init early_page_owner_param(char *buf) · line 51
Quoted 4 lines
mm/page_owner.c +1
-0@@ -46,6 +48,9 @@ static int __init early_page_owner_param(char *buf)if (strcmp (buf,"on" ) ==0 ) page_owner_disabled = false; +if (!page_owner_disabled &&IS_ENABLED (CONFIG_KASAN ))
I'd rather keep all logic in one place, i.e. "if (!page_owner_disabled && (IS_ENABLED(CONFIG_KASAN) || debug_pagealloc_enabled())" With this no changes in early_debug_pagealloc() required and CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=y should also work correctly.
Quoted 5 lines
mm/page_owner.c +2
-0+ page_owner_free_stack_disabled = false; +return 0 ; }early_param ("page_owner" , early_page_owner_param);
Quoted 6 lines · Andrey Ryabinin
Quoted 6 lines
mm/page_alloc.c +0
-0@@ -710,8 +710,12 @@ static int __init early_debug_pagealloc(char *buf)if (kstrtobool (buf, &enable))return -EINVAL ;
Quoted 7 lines
mm/page_alloc.c static int __init early_debug_pagealloc(char *buf) · line 716
Quoted 5 lines
mm/page_alloc.c +3
-1-if (enable) +if (enable) {static_branch_enable (&_debug_pagealloc_enabled); +#ifdef CONFIG_PAGE_OWNER + page_owner_free_stack_disabled = false;I think this won't work with
CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=y
Good point, thanks.
Quoted 30 lines
Quoted 30 lines
mm/page_alloc.c +2
-0+#endif + }return 0 ; }mm/page_owner.c +3
-1@@ -24,13 +24,15 @@ struct page_owner {short last_migrate_reason;gfp_t gfp_mask;depot_stack_handle_t handle; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t free_handle;#endif };static bool page_owner_disabled = true; +bool page_owner_free_stack_disabled = true;DEFINE_STATIC_KEY_FALSE (page_owner_inited); +static DEFINE_STATIC_KEY_FALSE (page_owner_free_stack);static depot_stack_handle_t dummy_handle;static depot_stack_handle_t failure_handle;@@ -46,6 +48,9 @@ static int __init early_page_owner_param(char *buf)if (strcmp (buf,"on" ) ==0 ) page_owner_disabled = false;
Quoted 4 lines
mm/page_owner.c static int __init early_page_owner_param(char *buf) · line 51
Quoted 1 line
mm/page_owner.c +1
-0+if (!page_owner_disabled &&IS_ENABLED (CONFIG_KASAN ))I'd rather keep all logic in one place, i.e. "if (!page_owner_disabled && (
IS_ENABLED(CONFIG_KASAN)||debug_pagealloc_enabled())" With this no changes inearly_debug_pagealloc()required andCONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=yshould also work correctly.
In this function it would not work if the debug_pagealloc param gets processed later than page_owner, but should be doable in init_page_owner(), I'll try, thanks.
Quoted 8 lines
Quoted 6 lines
mm/page_owner.c +2
-0+ page_owner_free_stack_disabled = false; +return 0 ; }early_param ("page_owner" , early_page_owner_param);
PATCH 1/1 mm, debug, kasan: save and dump freeing stack trace for kasan
In reply to Andrey RyabininQuoted 2 lines · Andrey Ryabinin
I'd rather keep all logic in one place, i.e. "if (!page_owner_disabled && (
IS_ENABLED(CONFIG_KASAN)||debug_pagealloc_enabled())" With this no changes inearly_debug_pagealloc()required andCONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=yshould also work correctly.
OK.
----8<----
Quoted 1 line
From
7437c43f02682fdde5680fa83e87029f7529e222Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 16 Sep 2019 11:28:19 +0200
Subject: [PATCH] mm, debug, kasan: save and dump freeing stack trace for kasan
The commit "mm, page_owner, debug_pagealloc: save and dump freeing stack trace" enhanced page_owner to also store freeing stack trace, when debug_pagealloc is also enabled. KASAN would also like to do this [1] to improve error reports to debug e.g. UAF issues. This patch therefore introduces a helper config option PAGE_OWNER_FREE_STACK, which is enabled when PAGE_OWNER and either of DEBUG_PAGEALLOC or KASAN is enabled. Boot-time, the free stack saving is enabled when booting a KASAN kernel with page_owner=on, or non-KASAN kernel with debug_pagealloc=on and page_owner=on.
[1] https://bugzilla.kernel.org/show_bug.cgi?id=203967
--- Documentation/dev-tools/kasan.rst | 4 ++++ mm/Kconfig.debug | 4 ++++ mm/page_owner.c | 31 ++++++++++++++++++------------- 3 files changed, 26 insertions(+), 13 deletions(-)
Documentation/dev-tools/kasan.rst +4 -0
@@ -41,6 +41,10 @@ smaller binary while the latter is 1.1 - 2 times faster. Both KASAN modes work with both SLUB and SLAB memory allocators. For better bug detection and nicer reporting, enable CONFIG_STACKTRACE. +To augment reports with last allocation and freeing stack of the physical +page, it is recommended to configure kernel also with CONFIG_PAGE_OWNER = y +and boot with page_owner=on. + To disable instrumentation for specific files or directories, add a line similar to the following to the respective kernel Makefile:
mm/Kconfig.debug +4 -0
@@ -62,6 +62,10 @@ config PAGE_OWNER If unsure, say N. +config PAGE_OWNER_FREE_STACK + def_bool KASAN || DEBUG_PAGEALLOC + depends on PAGE_OWNER + config PAGE_POISONING bool "Poison pages after freeing" select PAGE_POISONING_NO_SANITY if HIBERNATION
mm/page_owner.c +18 -13
@@ -24,13 +24,14 @@ struct page_owner {short last_migrate_reason;gfp_t gfp_mask;depot_stack_handle_t handle; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t free_handle;#endif };static bool page_owner_disabled = true;DEFINE_STATIC_KEY_FALSE (page_owner_inited); +static DEFINE_STATIC_KEY_FALSE (page_owner_free_stack);static depot_stack_handle_t dummy_handle;static depot_stack_handle_t failure_handle;
@@ -91,6 +92,8 @@ static void init_page_owner(void)register_failure_stack ();register_early_stack ();static_branch_enable (&page_owner_inited); +if (IS_ENABLED (CONFIG_KASAN ) ||debug_pagealloc_enabled ()) +static_branch_enable (&page_owner_free_stack);init_early_allocated_pages (); }
@@ -148,11 +151,11 @@ void __reset_page_owner(struct page *page, unsigned int order) {int i;struct page_ext *page_ext; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t handle =0 ;struct page_owner *page_owner; -if (debug_pagealloc_enabled ()) +if (static_branch_unlikely (&page_owner_free_stack)) handle =save_stack (GFP_NOWAIT | __GFP_NOWARN);#endif
@@ -161,8 +164,8 @@ void __reset_page_owner(struct page *page, unsigned int order)if (unlikely (!page_ext))continue ;__clear_bit (PAGE_EXT_OWNER_ACTIVE , &page_ext->flags); -#ifdef CONFIG_DEBUG_PAGEALLOC -if (debug_pagealloc_enabled ()) { +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { page_owner =get_page_owner (page_ext); page_owner->free_handle = handle; }
@@ -451,14 +454,16 @@ void __dump_page_owner(struct page *page)stack_trace_print (entries, nr_entries,0 ); } -#ifdef CONFIG_DEBUG_PAGEALLOC - handle =READ_ONCE (page_owner->free_handle); -if (!handle) { -pr_alert ("page_owner free stack trace missing\n" ); - }else { - nr_entries =stack_depot_fetch (handle, &entries); -pr_alert ("page last free stack trace:\n" ); -stack_trace_print (entries, nr_entries,0 ); +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { + handle =READ_ONCE (page_owner->free_handle); +if (!handle) { +pr_alert ("page_owner free stack trace missing\n" ); + }else { + nr_entries =stack_depot_fetch (handle, &entries); +pr_alert ("page last free stack trace:\n" ); +stack_trace_print (entries, nr_entries,0 ); + } }#endif
Signature · 1 line
2.23.0
Re: [PATCH] mm, debug, kasan: save and dump freeing stack trace for kasan
Quoted 22 lines · Vlastimil Babka
Quoted 2 lines · Andrey Ryabinin
I'd rather keep all logic in one place, i.e. "if (!page_owner_disabled && (
IS_ENABLED(CONFIG_KASAN)||debug_pagealloc_enabled())" With this no changes inearly_debug_pagealloc()required andCONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=yshould also work correctly.OK.
----8<----
From
7437c43f02682fdde5680fa83e87029f7529e222Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 16 Sep 2019 11:28:19 +0200
Subject: [PATCH] mm, debug, kasan: save and dump freeing stack trace for kasanThe commit "mm,
page_owner,debug_pagealloc: save and dump freeing stack trace" enhancedpage_ownerto also store freeing stack trace, whendebug_pageallocis also enabled. KASAN would also like to do this [1] to improve error reports to debug e.g. UAF issues. This patch therefore introduces a helper config optionPAGE_OWNER_FREE_STACK, which is enabled whenPAGE_OWNERand either ofDEBUG_PAGEALLOCor KASAN is enabled. Boot-time, the free stack saving is enabled when booting a KASAN kernel with page_owner=on, or non-KASAN kernel with debug_pagealloc=on and page_owner=on.
Quoted 7 lines
[1] https://bugzilla.kernel.org/show_bug.cgi?id=203967
Suggested-by Dmitry Vyukov <[email protected]>Suggested-by Walter Wu <[email protected]>Suggested-by Andrey Ryabinin <[email protected]>Signed-off-by Vlastimil Babka <[email protected]>---
Quoted 52 lines · Vlastimil Babka
Quoted 4 lines · Qian Cai
The new config looks redundant and confusing. It looks to me more of a document update in
Documentation/dev-tools/kasan.txtto educate developers to selectPAGE_OWNERandDEBUG_PAGEALLOCif needed.Agreed. But if you want it fully automatic, how about something like this (on top of mmotm/next)? If you agree I'll add changelog and send properly.
----8<----
From
a528d14c71d7fdf5872ca8ab3bd1b5bad26670c9Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Thu, 12 Sep 2019 15:51:23 +0200
Subject: [PATCH] make KASAN enablepage_ownerwith free stack capture--- include/linux/page_owner.h | 1 + lib/Kconfig.kasan | 4 ++++ mm/Kconfig.debug | 5 +++++ mm/page_alloc.c | 6 +++++- mm/page_owner.c | 37 ++++++++++++++++++++++++------------- 5 files changed, 39 insertions(+), 14 deletions(-)include/linux/page_owner.h +1
-0@@ -6,6 +6,7 @@#ifdef CONFIG_PAGE_OWNER extern struct static_key_false page_owner_inited; +extern bool page_owner_free_stack_disabled;extern struct page_ext_operations page_owner_ops;extern void __reset_page_owner (struct page *page,unsigned int order);lib/Kconfig.kasan +2
-0@@ -41,6 +41,8 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2
lib/Kconfig.kasan config KASAN_SW_TAGS · line 72
Quoted 8 lines
lib/Kconfig.kasan +2
-0@@ -63,6 +65,8 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER + select PAGE_OWNER_FREE_STACK help Enables software tag-based KASAN mode. This mode requires Top Byte Ignore support by the CPU and therefore
I don't know how KASAN people will feel about this. Especially, KASAN_SW_TAGS was designed for people who complain about memory footprint of KASAN_GENERIC is too high as far as I can tell.
I guess it depends on them to test the new memory footprint of KASAN to see if they are happy with it.
Quoted 133 lines
mm/Kconfig.debug +5
-0@@ -13,6 +13,7 @@ config DEBUG_PAGEALLOC depends on DEBUG_KERNEL depends on !HIBERNATION || ARCH_SUPPORTS_DEBUG_PAGEALLOC && !PPC && !SPARC select PAGE_POISONING if !ARCH_SUPPORTS_DEBUG_PAGEALLOC + select PAGE_OWNER_FREE_STACK if PAGE_OWNER ---help--- Unmap pages from the kernel linear mapping after free_pages(). Depending on runtime enablement, this results in a small or large@@ -62,6 +63,10 @@ config PAGE_OWNER If unsure, say N. +config PAGE_OWNER_FREE_STACK + def_bool n + depends on PAGE_OWNER + config PAGE_POISONING bool "Poison pages after freeing" select PAGE_POISONING_NO_SANITY if HIBERNATIONmm/page_alloc.c +5
-1@@ -710,8 +710,12 @@ static int __init early_debug_pagealloc(char *buf)if (kstrtobool (buf, &enable))return -EINVAL ; -if (enable) +if (enable) {static_branch_enable (&_debug_pagealloc_enabled); +#ifdef CONFIG_PAGE_OWNER + page_owner_free_stack_disabled = false; +#endif + }return 0 ; }mm/page_owner.c +24
-13@@ -24,13 +24,15 @@ struct page_owner {short last_migrate_reason;gfp_t gfp_mask;depot_stack_handle_t handle; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t free_handle;#endif };static bool page_owner_disabled = true; +bool page_owner_free_stack_disabled = true;DEFINE_STATIC_KEY_FALSE (page_owner_inited); +static DEFINE_STATIC_KEY_FALSE (page_owner_free_stack);static depot_stack_handle_t dummy_handle;static depot_stack_handle_t failure_handle;@@ -46,6 +48,11 @@ static int __init early_page_owner_param(char *buf)if (strcmp (buf,"on" ) ==0 ) page_owner_disabled = false; +if (IS_ENABLED (CONFIG_KASAN )) { + page_owner_disabled = false; + page_owner_free_stack_disabled = false; + } +return 0 ; }early_param ("page_owner" , early_page_owner_param);@@ -91,6 +98,8 @@ static void init_page_owner(void)register_failure_stack ();register_early_stack ();static_branch_enable (&page_owner_inited); +if (!page_owner_free_stack_disabled) +static_branch_enable (&page_owner_free_stack);init_early_allocated_pages (); }@@ -148,11 +157,11 @@ void __reset_page_owner(struct page *page, unsigned int order) {int i;struct page_ext *page_ext; -#ifdef CONFIG_DEBUG_PAGEALLOC +#ifdef CONFIG_PAGE_OWNER_FREE_STACK depot_stack_handle_t handle =0 ;struct page_owner *page_owner; -if (debug_pagealloc_enabled ()) +if (static_branch_unlikely (&page_owner_free_stack)) handle =save_stack (GFP_NOWAIT | __GFP_NOWARN);#endif @@ -161,8 +170,8 @@ void __reset_page_owner(struct page *page, unsigned int order)if (unlikely (!page_ext))continue ;__clear_bit (PAGE_EXT_OWNER_ACTIVE , &page_ext->flags); -#ifdef CONFIG_DEBUG_PAGEALLOC -if (debug_pagealloc_enabled ()) { +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { page_owner =get_page_owner (page_ext); page_owner->free_handle = handle; }@@ -451,14 +460,16 @@ void __dump_page_owner(struct page *page)stack_trace_print (entries, nr_entries,0 ); } -#ifdef CONFIG_DEBUG_PAGEALLOC - handle =READ_ONCE (page_owner->free_handle); -if (!handle) { -pr_alert ("page_owner free stack trace missing\n" ); - }else { - nr_entries =stack_depot_fetch (handle, &entries); -pr_alert ("page last free stack trace:\n" ); -stack_trace_print (entries, nr_entries,0 ); +#ifdef CONFIG_PAGE_OWNER_FREE_STACK +if (static_branch_unlikely (&page_owner_free_stack)) { + handle =READ_ONCE (page_owner->free_handle); +if (!handle) { +pr_alert ("page_owner free stack trace missing\n" ); + }else { + nr_entries =stack_depot_fetch (handle, &entries); +pr_alert ("page last free stack trace:\n" ); +stack_trace_print (entries, nr_entries,0 ); + } }#endif