kasan: report only the first error
Subsystems (4)
PATCH 1/1 kasan: report only the first error
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.
Given that multiple reports only do harm, it makes sense to disable kasan after the first one. Except for the tests in lib/test_kasan.c as we obviously want to see all reports from test.
--- lib/test_kasan.c | 9 +++++++++ mm/kasan/report.c | 7 +++++++ 2 files changed, 16 insertions(+)
lib/test_kasan.c +9 -0
@@ -11,6 +11,7 @@#define pr_fmt (fmt) "kasan test: %s " fmt, __func__ +#include <linux/atomic.h> #include <linux/delay.h> #include <linux/kernel.h> #include <linux/mman.h>
@@ -21,6 +22,8 @@#include <linux/uaccess.h> #include <linux/module.h> +extern atomic_t kasan_report_count; +/* * Note: test functions are marked noinline so that their names appear in * reports.
@@ -474,6 +477,9 @@ static noinline void __init use_after_scope_test(void)static int __init kmalloc_tests_init (void ) { +/* Rise reports limit high enough to see all the following bugs */ +atomic_set (&kasan_report_count,100 ); +kmalloc_oob_right ();kmalloc_oob_left ();kmalloc_node_oob_right ();
@@ -499,6 +505,9 @@ static int __init kmalloc_tests_init(void)ksize_unpoisons_memory ();copy_user_test ();use_after_scope_test (); + +/* kasan is unreliable now, disable reports */ +atomic_set (&kasan_report_count,0 );return -EAGAIN ; }
mm/kasan/report.c +7 -0
@@ -13,6 +13,7 @@* */ +#include <linux/atomic.h> #include <linux/ftrace.h> #include <linux/kernel.h> #include <linux/mm.h>
@@ -354,6 +355,9 @@ static void kasan_report_error(struct kasan_access_info *info)kasan_end_report (&flags); } +atomic_t kasan_report_count =ATOMIC_INIT (1 ); +EXPORT_SYMBOL_GPL (kasan_report_count); +void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip) {
@@ -362,6 +366,9 @@ void kasan_report(unsigned long addr, size_t size,if (likely (!kasan_report_enabled ()))return ; +if (atomic_dec_if_positive (&kasan_report_count) <0 ) +return ; +disable_trace_on_warning (); info.access_addr = (void *)addr;
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 11 lines · Andrey Ryabinin
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.Given that multiple reports only do harm, it makes sense to disable kasan after the first one. Except for the tests in
lib/test_kasan.cas we obviously want to see all reports from test.
Hi Andrey,
Could you make it configurable via CONFIG_KASAN_SOMETHING (which can default to showing only the first report)?
I sometimes use KASAN to see what bad accesses a particular bug causes, and seeing all of them (even knowing that they may be corrupt/induced) helps a lot.
Thanks!
Quoted 83 lines
Signed-off-by Andrey Ryabinin <[email protected]>--- lib/test_kasan.c | 9 +++++++++ mm/kasan/report.c | 7 +++++++ 2 files changed, 16 insertions(+)lib/test_kasan.c +9
-0@@ -11,6 +11,7 @@#define pr_fmt (fmt) "kasan test: %s " fmt, __func__ +#include <linux/atomic.h> #include <linux/delay.h> #include <linux/kernel.h> #include <linux/mman.h> @@ -21,6 +22,8 @@#include <linux/uaccess.h> #include <linux/module.h> +extern atomic_t kasan_report_count; +/* * Note: test functions are marked noinline so that their names appear in * reports. @@ -474,6 +477,9 @@ static noinline void __init use_after_scope_test(void)static int __init kmalloc_tests_init (void ) { +/* Rise reports limit high enough to see all the following bugs */ +atomic_set (&kasan_report_count,100 ); +kmalloc_oob_right ();kmalloc_oob_left ();kmalloc_node_oob_right ();@@ -499,6 +505,9 @@ static int __init kmalloc_tests_init(void)ksize_unpoisons_memory ();copy_user_test ();use_after_scope_test (); + +/* kasan is unreliable now, disable reports */ +atomic_set (&kasan_report_count,0 );return -EAGAIN ; }mm/kasan/report.c +7
-1@@ -13,6 +13,7 @@* */ +#include <linux/atomic.h> #include <linux/ftrace.h> #include <linux/kernel.h> #include <linux/mm.h> @@ -354,6 +355,9 @@ static void kasan_report_error(struct kasan_access_info *info)kasan_end_report (&flags); } +atomic_t kasan_report_count =ATOMIC_INIT (1 ); +EXPORT_SYMBOL_GPL (kasan_report_count); +void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip) {@@ -362,6 +366,9 @@ void kasan_report(unsigned long addr, size_t size,if (likely (!kasan_report_enabled ()))return ; +if (atomic_dec_if_positive (&kasan_report_count) <0 ) +return ; +disable_trace_on_warning (); info.access_addr = (void *)addr; --2.10.2
Quoted 6 lines
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20170322160647.32032-1-aryabinin%40virtuozzo.com.
For more options, visit https://groups.google.com/d/optout.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Andrey Konovalov
Quoted 8 lines · Andrey Ryabinin
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.
Quoted 8 lines
Quoted 3 lines
Given that multiple reports only do harm, it makes sense to disable kasan after the first one. Except for the tests in
lib/test_kasan.cas we obviously want to see all reports from test.Hi Andrey,
Could you make it configurable via
CONFIG_KASAN_SOMETHING(which can default to showing only the first report)?
I'd rather make this boot time configurable, but wouldn't want to without a good reason.
Quoted 3 lines
I sometimes use KASAN to see what bad accesses a particular bug causes, and seeing all of them (even knowing that they may be corrupt/induced) helps a lot.
I'm wondering why you need to see all reports?
Quoted 3 lines
Thanks!
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 15 lines · Andrey Ryabinin
Quoted 14 lines · Andrey Konovalov
Quoted 11 lines · Andrey Ryabinin
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.Given that multiple reports only do harm, it makes sense to disable kasan after the first one. Except for the tests in
lib/test_kasan.cas we obviously want to see all reports from test.
Quoted 7 lines
Quoted 4 lines
Hi Andrey,
Could you make it configurable via
CONFIG_KASAN_SOMETHING(which can default to showing only the first report)?I'd rather make this boot time configurable, but wouldn't want to without a good reason.
That would work for me.
Quoted 7 lines
Quoted 5 lines
I sometimes use KASAN to see what bad accesses a particular bug causes, and seeing all of them (even knowing that they may be corrupt/induced) helps a lot.
I'm wondering why you need to see all reports?
To get a better picture of what are the consequences of a bug. For example whether it leads to some bad or controllable memory corruption. Sometimes it's easier to let KASAN track the memory accesses then do that manually.
Quoted 4 lines
Quoted 4 lines
Thanks!
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Alexander Potapenko
reviewerQuoted 33 lines · Andrey Konovalov
Quoted 15 lines · Andrey Ryabinin
Quoted 14 lines · Andrey Konovalov
Quoted 11 lines · Andrey Ryabinin
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.Given that multiple reports only do harm, it makes sense to disable kasan after the first one. Except for the tests in
lib/test_kasan.cas we obviously want to see all reports from test.Quoted 7 lines
Quoted 4 lines
Hi Andrey,
Could you make it configurable via
CONFIG_KASAN_SOMETHING(which can default to showing only the first report)?I'd rather make this boot time configurable, but wouldn't want to without a good reason.
That would work for me.
Quoted 6 lines
Quoted 5 lines
I sometimes use KASAN to see what bad accesses a particular bug causes, and seeing all of them (even knowing that they may be corrupt/induced) helps a lot.
Quoted 6 lines
Quoted 1 line
I'm wondering why you need to see all reports?
To get a better picture of what are the consequences of a bug. For example whether it leads to some bad or controllable memory corruption. Sometimes it's easier to let KASAN track the memory accesses then do that manually.
Another case is when you're seeing an OOB read at boot time, which has limited impact, and you don't want to wait for the code owner to fix it to move forward.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Thanks!
Signature · 16 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Dmitry Vyukov
reviewerQuoted 20 lines · Alexander Potapenko
Quoted 19 lines · Andrey Konovalov
Quoted 17 lines · Andrey Ryabinin
Quoted 16 lines · Andrey Konovalov
Quoted 11 lines · Andrey Ryabinin
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.Given that multiple reports only do harm, it makes sense to disable kasan after the first one. Except for the tests in
lib/test_kasan.cas we obviously want to see all reports from test.Hi Andrey,
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Could you make it configurable via
CONFIG_KASAN_SOMETHING(which can default to showing only the first report)?I'd rather make this boot time configurable, but wouldn't want to without a good reason.
That would work for me.
Also note that KASAN now supports panic_on_warn=1, which achieves more or less the same. Of course, WARNINGs may be not that bad, but KASAN reports may be not tool bad as well (e.g. off-by-one reads).
Quoted 24 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
I sometimes use KASAN to see what bad accesses a particular bug causes, and seeing all of them (even knowing that they may be corrupt/induced) helps a lot.
I'm wondering why you need to see all reports?
To get a better picture of what are the consequences of a bug. For example whether it leads to some bad or controllable memory corruption. Sometimes it's easier to let KASAN track the memory accesses then do that manually.
Another case is when you're seeing an OOB read at boot time, which has limited impact, and you don't want to wait for the code owner to fix it to move forward.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Thanks!
--
Alexander Potapenko
Software Engineer
Quoted 7 lines
Google Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 1/1 kasan: report only the first error by default
In reply to Andrey RyabininDisable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.
Given that multiple reports usually only do harm, it makes sense to disable kasan after the first one. If user wants to see all the reports, the boot-time parameter kasan_multi_shot must be used.
---
Changes since v1:
- provide kasan_multi_shot boot parameter.
Documentation/admin-guide/kernel-parameters.txt | 6 ++++++ lib/test_kasan.c | 12 ++++++++++++ mm/kasan/kasan.h | 5 ----- mm/kasan/report.c | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -1726,6 +1726,12 @@ kernel and module base offset ASLR (Address Space Layout Randomization). + kasan_multi_shot + [KNL] Enforce KASAN (Kernel Address Sanitizer) to print + report on every invalid memory access. Without this + parameter KASAN will print report only for the first + invalid access. + keepinitrd [HW,ARM] kernelcore= [KNL,X86,IA-64,PPC]
lib/test_kasan.c +12 -0
@@ -11,6 +11,7 @@#define pr_fmt (fmt) "kasan test: %s " fmt, __func__ +#include <linux/atomic.h> #include <linux/delay.h> #include <linux/kernel.h> #include <linux/mman.h>
@@ -21,6 +22,8 @@#include <linux/uaccess.h> #include <linux/module.h> +extern atomic_t kasan_report_count; +/* * Note: test functions are marked noinline so that their names appear in * reports.
@@ -474,6 +477,9 @@ static noinline void __init use_after_scope_test(void)static int __init kmalloc_tests_init (void ) { +/* Rise reports limit high enough to see all the following bugs */ +atomic_add (100 , &kasan_report_count); +kmalloc_oob_right ();kmalloc_oob_left ();kmalloc_node_oob_right ();
@@ -499,6 +505,12 @@ static int __init kmalloc_tests_init(void)ksize_unpoisons_memory ();copy_user_test ();use_after_scope_test (); + +/* +* kasan is unreliable now, disable reports if +* we are in single shot mode +*/ +atomic_sub (100 , &kasan_report_count);return -EAGAIN ; }
mm/kasan/kasan.h +0 -5
@@ -96,11 +96,6 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr) << KASAN_SHADOW_SCALE_SHIFT); } -static inline bool kasan_report_enabled (void ) -{ -return !current->kasan_depth; -} -void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); void kasan_report_double_free(struct kmem_cache *cache, void *object,
mm/kasan/report.c +18 -0
@@ -13,7 +13,9 @@* */ +#include <linux/atomic.h> #include <linux/ftrace.h> +#include <linux/init.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/printk.h>
@@ -354,6 +356,22 @@ static void kasan_report_error(struct kasan_access_info *info)kasan_end_report (&flags); } +atomic_t kasan_report_count =ATOMIC_INIT (1 ); +EXPORT_SYMBOL_GPL (kasan_report_count); + +static int __init kasan_set_multi_shot (char *str) +{ +atomic_set (&kasan_report_count,1000000000 ); +return 1 ; +} +__setup ("kasan_multi_shot" , kasan_set_multi_shot); + +static inline bool kasan_report_enabled (void ) +{ +return !current->kasan_depth && + (atomic_dec_if_positive (&kasan_report_count) >=0 ); +} +void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip) {
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2] kasan: report only the first error by default
Review
Quoted 6 lines · Andrey Ryabinin
(quoted diff) +6
-0+ kasan_multi_shot + [KNL] Enforce KASAN (Kernel Address Sanitizer) to print + report on every invalid memory access. Without this + parameter KASAN will print report only for the first + invalid access. +
The option looks fine to me.
Review
Quoted 13 lines
static int
__initkmalloc_tests_init(void)
{(quoted diff) +8
-0+ /* Rise reports limit high enough to see all the following bugs */ + atomic_add(100, &kasan_report_count); + + /* + * kasan is unreliable now, disable reports if + * we are in single shot mode + */ + atomic_sub(100, &kasan_report_count); return -EAGAIN; }
... but these magic numbers look rather messy.
[...]
Review
Quoted 9 lines
(quoted diff) +9
-0+atomic_t kasan_report_count = ATOMIC_INIT(1); +EXPORT_SYMBOL_GPL(kasan_report_count); + +static int __init kasan_set_multi_shot(char *str) +{ + atomic_set(&kasan_report_count, 1000000000); + return 1; +} +__setup("kasan_multi_shot", kasan_set_multi_shot);
... likewise.
Rather than trying to pick an arbitrarily large number, how about we use separate flags to determine whether we're in multi-shot mode, and whether a (oneshot) report has been made.
How about the below?
Thanks,
Mark.
---->8----
include/linux/kasan.h +3 -0
@@ -74,6 +74,9 @@ struct kasan_cache {static inline void kasan_unpoison_slab (const void *ptr) {ksize (ptr); }size_t kasan_metadata_size (struct kmem_cache *cache); +bool kasan_save_enable_multi_shot (void ); +void kasan_restore_multi_shot (bool enabled); +#else /* CONFIG_KASAN */ static inline void kasan_unpoison_shadow (const void *address,size_t size) {}
lib/test_kasan.c +10 -0
@@ -12,6 +12,7 @@#define pr_fmt (fmt) "kasan test: %s " fmt, __func__#include <linux/delay.h> +#include <linux/kasan.h> #include <linux/kernel.h> #include <linux/mman.h> #include <linux/mm.h>
@@ -474,6 +475,12 @@ static noinline void __init use_after_scope_test(void)static int __init kmalloc_tests_init (void ) { +/* +* Temporarily enable multi-shot mode. Otherwise, we'd only get a +* report for the first case. +*/ +bool multishot =kasan_save_enable_multi_shot (); +kmalloc_oob_right ();kmalloc_oob_left ();kmalloc_node_oob_right ();
@@ -499,6 +506,9 @@ static int __init kmalloc_tests_init(void)ksize_unpoisons_memory ();copy_user_test ();use_after_scope_test (); + +kasan_restore_multi_shot (multishot); +return -EAGAIN ; }
mm/kasan/kasan.h +0 -5
@@ -96,11 +96,6 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr) << KASAN_SHADOW_SCALE_SHIFT); } -static inline bool kasan_report_enabled (void ) -{ -return !current->kasan_depth; -} -void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); void kasan_report_double_free(struct kmem_cache *cache, void *object,
mm/kasan/report.c +35 -0
@@ -13,6 +13,7 @@* */ +#include <linux/bitops.h> #include <linux/ftrace.h> #include <linux/kernel.h> #include <linux/mm.h>
@@ -293,6 +294,40 @@ static void kasan_report_error(struct kasan_access_info *info)kasan_end_report (&flags); } +static unsigned long kasan_flags; + +#define KASAN_BIT_REPORTED 0 +#define KASAN_BIT_MULTI_SHOT 1 + +bool kasan_save_enable_multi_shot (void ) +{ +return test_and_set_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags); +} +EXPORT_SYMBOL_GPL (kasan_save_enable_multi_shot); + +void kasan_restore_multi_shot (bool enabled) +{ +if (!enabled) +clear_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags); +} +EXPORT_SYMBOL_GPL (kasan_restore_multi_shot); + +static int __init kasan_set_multi_shot (char *str) +{ +set_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags); +return 1 ; +} +__setup ("kasan_multi_shot" , kasan_set_multi_shot); + +static inline bool kasan_report_enabled (void ) +{ +if (current->kasan_depth) +return false; +if (test_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags)) +return true; +return !test_and_set_bit (KASAN_BIT_REPORTED , &kasan_flags); +} +void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip) {
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2] kasan: report only the first error by default
Quoted 40 lines · Mark Rutland
Review
Quoted 6 lines · Andrey Ryabinin
(quoted diff) +6
-0+ kasan_multi_shot + [KNL] Enforce KASAN (Kernel Address Sanitizer) to print + report on every invalid memory access. Without this + parameter KASAN will print report only for the first + invalid access. +The option looks fine to me.
Review
Quoted 13 lines
static int
__initkmalloc_tests_init(void)
{(quoted diff) +8
-0+ /* Rise reports limit high enough to see all the following bugs */ + atomic_add(100, &kasan_report_count); + + /* + * kasan is unreliable now, disable reports if + * we are in single shot mode + */ + atomic_sub(100, &kasan_report_count); return -EAGAIN; }... but these magic numbers look rather messy.
[...]
mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info) · line 320
Quoted 9 lines
mm/kasan/report.c +9
-0+atomic_t kasan_report_count =ATOMIC_INIT (1 ); +EXPORT_SYMBOL_GPL (kasan_report_count); + +static int __init kasan_set_multi_shot (char *str) +{ +atomic_set (&kasan_report_count,1000000000 ); +return 1 ; +} +__setup ("kasan_multi_shot" , kasan_set_multi_shot);... likewise.
Quoted 5 lines
Rather than trying to pick an arbitrarily large number, how about we use separate flags to determine whether we're in multi-shot mode, and whether a (oneshot) report has been made.
How about the below?
Yes, it deferentially looks better.
Can you send a patch with a changelog, or do you want me to care of it?
Quoted 9 lines
Thanks,
Mark.mm/kasan/report.c +0
-0
mm/kasan/report.c line 17
Quoted 5 lines
mm/kasan/report.c +1
-0@@ -13,6 +13,7 @@* */ +#include <linux/bitops.h> #include <linux/ftrace.h>
We also need <linux/init.h> for __setup().
mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info)
Quoted 3 lines
mm/kasan/report.c +0
-0#include <linux/kernel.h> #include <linux/mm.h> @@ -293,6 +294,40 @@ static void kasan_report_error(struct kasan_access_info *info)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2] kasan: report only the first error by default
Quoted 10 lines · Andrey Ryabinin
Quoted 5 lines · Mark Rutland
Rather than trying to pick an arbitrarily large number, how about we use separate flags to determine whether we're in multi-shot mode, and whether a (oneshot) report has been made.
How about the below?
Yes, it deferentially looks better.
Can you send a patch with a changelog, or do you want me to care of it?
Would you be happy to take care of it, along with the fixup you suggested below, as v3?
You can add my:
Thanks,
Mark.
Quoted 9 lines
Quoted 3 lines
+#include <
linux/bitops.h>
#include <linux/ftrace.h>We also need <
linux/init.h> for__setup().Review static void kasan_report_error(struct kasan_access_info *info)
Quoted 3 lines
#include <
linux/kernel.h>
#include <linux/mm.h>(quoted diff) +0
-0@@ -293,6 +294,40 @@ static void kasan_report_error(struct kasan_access_info *info)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 1/1 kasan: report only the first error by default
In reply to Andrey RyabininFrom: Mark Rutland <[email protected]>
Disable kasan after the first report. There are several reasons for this:
* Single bug quite often has multiple invalid memory accesses causing storm in the dmesg.
* Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces.
* Reports after the first easily could be not bugs by itself but just side effects of the first one.
Given that multiple reports usually only do harm, it makes sense to disable kasan after the first one. If user wants to see all the reports, the boot-time parameter kasan_multi_shot must be used.
---
Changes since v2:
- Instead of using atomic report counter, use separate flags to determine whether we're in multi-shot mode, and whether a (oneshot) report has been made.
Changes since v1:
- provide kasan_multi_shot boot parameter.
Documentation/admin-guide/kernel-parameters.txt | 6 +++++ include/linux/kasan.h | 3 +++ lib/test_kasan.c | 10 +++++++ mm/kasan/kasan.h | 5 ---- mm/kasan/report.c | 36 +++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -1726,6 +1726,12 @@ kernel and module base offset ASLR (Address Space Layout Randomization). + kasan_multi_shot + [KNL] Enforce KASAN (Kernel Address Sanitizer) to print + report on every invalid memory access. Without this + parameter KASAN will print report only for the first + invalid access. + keepinitrd [HW,ARM] kernelcore= [KNL,X86,IA-64,PPC]
include/linux/kasan.h +3 -0
@@ -76,6 +76,9 @@ size_t ksize(const void *);static inline void kasan_unpoison_slab (const void *ptr) {ksize (ptr); }size_t kasan_metadata_size (struct kmem_cache *cache); +bool kasan_save_enable_multi_shot (void ); +void kasan_restore_multi_shot (bool enabled); +#else /* CONFIG_KASAN */ static inline void kasan_unpoison_shadow (const void *address,size_t size) {}
lib/test_kasan.c +10 -0
@@ -20,6 +20,7 @@#include <linux/string.h> #include <linux/uaccess.h> #include <linux/module.h> +#include <linux/kasan.h> /* * Note: test functions are marked noinline so that their names appear in
@@ -474,6 +475,12 @@ static noinline void __init use_after_scope_test(void)static int __init kmalloc_tests_init (void ) { +/* +* Temporarily enable multi-shot mode. Otherwise, we'd only get a +* report for the first case. +*/ +bool multishot =kasan_save_enable_multi_shot (); +kmalloc_oob_right ();kmalloc_oob_left ();kmalloc_node_oob_right ();
@@ -499,6 +506,9 @@ static int __init kmalloc_tests_init(void)ksize_unpoisons_memory ();copy_user_test ();use_after_scope_test (); + +kasan_restore_multi_shot (multishot); +return -EAGAIN ; }
mm/kasan/kasan.h +0 -5
@@ -96,11 +96,6 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr) << KASAN_SHADOW_SCALE_SHIFT); } -static inline bool kasan_report_enabled (void ) -{ -return !current->kasan_depth; -} -void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); void kasan_report_double_free(struct kmem_cache *cache, void *object,
mm/kasan/report.c +36 -0
@@ -13,7 +13,9 @@* */ +#include <linux/bitops.h> #include <linux/ftrace.h> +#include <linux/init.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/printk.h>
@@ -354,6 +356,40 @@ static void kasan_report_error(struct kasan_access_info *info)kasan_end_report (&flags); } +static unsigned long kasan_flags; + +#define KASAN_BIT_REPORTED 0 +#define KASAN_BIT_MULTI_SHOT 1 + +bool kasan_save_enable_multi_shot (void ) +{ +return test_and_set_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags); +} +EXPORT_SYMBOL_GPL (kasan_save_enable_multi_shot); + +void kasan_restore_multi_shot (bool enabled) +{ +if (!enabled) +clear_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags); +} +EXPORT_SYMBOL_GPL (kasan_restore_multi_shot); + +static int __init kasan_set_multi_shot (char *str) +{ +set_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags); +return 1 ; +} +__setup ("kasan_multi_shot" , kasan_set_multi_shot); + +static inline bool kasan_report_enabled (void ) +{ +if (current->kasan_depth) +return false; +if (test_bit (KASAN_BIT_MULTI_SHOT , &kasan_flags)) +return true; +return !test_and_set_bit (KASAN_BIT_REPORTED , &kasan_flags); +} +void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip) {
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>