LKML Archive on lore.kernel.org
 help / Atom feed
* [PATCH] x86_64: increase stack size for KASAN_EXTRA
@ 2018-12-28  2:06 Qian Cai
  2019-01-09 21:52 ` [RESEND PATCH] " Qian Cai
  0 siblings, 1 reply; 5+ messages in thread
From: Qian Cai @ 2018-12-28  2:06 UTC (permalink / raw)
  To: tglx, mingo, bp, hpa; +Cc: x86, linux-kernel, Qian Cai

If the kernel is configured with KASAN_EXTRA, the stack size is
increasted significantly due to enable this option will set
"-fstack-reuse" to "none" in GCC [1]. As the results, it could trigger
stack overrun quite often with 32k stack size compiled using GCC 8. For
example, this reproducer

https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/\
syscalls/madvise/madvise06.c

could trigger a "corrupted stack end detected inside scheduler" very
reliably with CONFIG_SCHED_STACK_END_CHECK enabled.

There are just too many functions that could have a large stack with
KASAN_EXTRA due to large local variables that have been called over and
over again without being able to reuse the stacks. Some noticiable ones
are,

size
7648 shrink_page_list
3584 xfs_rmap_convert
3312 migrate_page_move_mapping
3312 dev_ethtool
3200 migrate_misplaced_transhuge_page
3168 copy_process

There are other 49 functions are over 2k in size while compiling kernel
with "-Wframe-larger-than=" even with a related minimal config on this
machine. Hence, it is too much work to change Makefiles for each object
to compile without "-fsanitize-address-use-after-scope" individually.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715#c23

Although there is a patch in GCC 9 to help the situation, GCC 9 probably
won't be released in a few months and then it probably take another
6-month to 1-year for all major distros to include it as a default.
Hence, the stack usage with KASAN_EXTRA can be revisited again in 2020
when GCC 9 is everywhere. Until then, this patch will help users avoid
stack overrun.

This has already been fixed for arm64 for the same reason via
6e8830674ea (arm64: kasan: Increase stack size for KASAN_EXTRA).

Signed-off-by: Qian Cai <cai@lca.pw>
---
 arch/x86/include/asm/page_64_types.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/x86/include/asm/page_64_types.h b/arch/x86/include/asm/page_64_types.h
index 8f657286d599..0ce558a8150d 100644
--- a/arch/x86/include/asm/page_64_types.h
+++ b/arch/x86/include/asm/page_64_types.h
@@ -7,7 +7,11 @@
 #endif
 
 #ifdef CONFIG_KASAN
+#ifdef CONFIG_KASAN_EXTRA
+#define KASAN_STACK_ORDER 2
+#else
 #define KASAN_STACK_ORDER 1
+#endif
 #else
 #define KASAN_STACK_ORDER 0
 #endif
-- 
2.17.2 (Apple Git-113)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RESEND PATCH] x86_64: increase stack size for KASAN_EXTRA
  2018-12-28  2:06 [PATCH] x86_64: increase stack size for KASAN_EXTRA Qian Cai
@ 2019-01-09 21:52 ` " Qian Cai
  2019-01-09 22:02   ` Andrew Morton
  2019-01-11 18:55   ` Andrey Ryabinin
  0 siblings, 2 replies; 5+ messages in thread
From: Qian Cai @ 2019-01-09 21:52 UTC (permalink / raw)
  To: akpm; +Cc: tglx, mingo, bp, hpa, x86, linux-kernel, Qian Cai

If the kernel is configured with KASAN_EXTRA, the stack size is
increasted significantly due to enable this option will set
"-fstack-reuse" to "none" in GCC [1]. As the results, it could trigger
stack overrun quite often with 32k stack size compiled using GCC 8. For
example, this reproducer

https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/\
syscalls/madvise/madvise06.c

could trigger a "corrupted stack end detected inside scheduler" very
reliably with CONFIG_SCHED_STACK_END_CHECK enabled.

There are just too many functions that could have a large stack with
KASAN_EXTRA due to large local variables that have been called over and
over again without being able to reuse the stacks. Some noticiable ones
are,

size
7648 shrink_page_list
3584 xfs_rmap_convert
3312 migrate_page_move_mapping
3312 dev_ethtool
3200 migrate_misplaced_transhuge_page
3168 copy_process

There are other 49 functions are over 2k in size while compiling kernel
with "-Wframe-larger-than=" even with a related minimal config on this
machine. Hence, it is too much work to change Makefiles for each object
to compile without "-fsanitize-address-use-after-scope" individually.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715#c23

Although there is a patch in GCC 9 to help the situation, GCC 9 probably
won't be released in a few months and then it probably take another
6-month to 1-year for all major distros to include it as a default.
Hence, the stack usage with KASAN_EXTRA can be revisited again in 2020
when GCC 9 is everywhere. Until then, this patch will help users avoid
stack overrun.

This has already been fixed for arm64 for the same reason via
6e8830674ea (arm64: kasan: Increase stack size for KASAN_EXTRA).

Signed-off-by: Qian Cai <cai@lca.pw>
---
 arch/x86/include/asm/page_64_types.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/x86/include/asm/page_64_types.h b/arch/x86/include/asm/page_64_types.h
index 8f657286d599..0ce558a8150d 100644
--- a/arch/x86/include/asm/page_64_types.h
+++ b/arch/x86/include/asm/page_64_types.h
@@ -7,7 +7,11 @@
 #endif
 
 #ifdef CONFIG_KASAN
+#ifdef CONFIG_KASAN_EXTRA
+#define KASAN_STACK_ORDER 2
+#else
 #define KASAN_STACK_ORDER 1
+#endif
 #else
 #define KASAN_STACK_ORDER 0
 #endif
-- 
2.17.2 (Apple Git-113)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RESEND PATCH] x86_64: increase stack size for KASAN_EXTRA
  2019-01-09 21:52 ` [RESEND PATCH] " Qian Cai
@ 2019-01-09 22:02   ` Andrew Morton
  2019-01-10  4:20     ` Qian Cai
  2019-01-11 18:55   ` Andrey Ryabinin
  1 sibling, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2019-01-09 22:02 UTC (permalink / raw)
  To: Qian Cai; +Cc: tglx, mingo, bp, hpa, x86, linux-kernel

On Wed,  9 Jan 2019 16:52:09 -0500 Qian Cai <cai@lca.pw> wrote:

> If the kernel is configured with KASAN_EXTRA, the stack size is
> increasted significantly due to enable this option will set
> "-fstack-reuse" to "none" in GCC [1]. As the results, it could trigger
> stack overrun quite often with 32k stack size compiled using GCC 8. For
> example, this reproducer
> 
> https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/\
> syscalls/madvise/madvise06.c
> 
> could trigger a "corrupted stack end detected inside scheduler" very
> reliably with CONFIG_SCHED_STACK_END_CHECK enabled.
> 
> There are just too many functions that could have a large stack with
> KASAN_EXTRA due to large local variables that have been called over and
> over again without being able to reuse the stacks. Some noticiable ones
> are,
> 
> size
> 7648 shrink_page_list
> 3584 xfs_rmap_convert
> 3312 migrate_page_move_mapping
> 3312 dev_ethtool
> 3200 migrate_misplaced_transhuge_page
> 3168 copy_process
> 
> There are other 49 functions are over 2k in size while compiling kernel
> with "-Wframe-larger-than=" even with a related minimal config on this
> machine. Hence, it is too much work to change Makefiles for each object
> to compile without "-fsanitize-address-use-after-scope" individually.
> 
> [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715#c23
> 
> Although there is a patch in GCC 9 to help the situation, GCC 9 probably
> won't be released in a few months and then it probably take another
> 6-month to 1-year for all major distros to include it as a default.
> Hence, the stack usage with KASAN_EXTRA can be revisited again in 2020
> when GCC 9 is everywhere. Until then, this patch will help users avoid
> stack overrun.
> 
> This has already been fixed for arm64 for the same reason via
> 6e8830674ea (arm64: kasan: Increase stack size for KASAN_EXTRA).

Sounds good.

> --- a/arch/x86/include/asm/page_64_types.h
> +++ b/arch/x86/include/asm/page_64_types.h
> @@ -7,7 +7,11 @@
>  #endif
>  
>  #ifdef CONFIG_KASAN
> +#ifdef CONFIG_KASAN_EXTRA
> +#define KASAN_STACK_ORDER 2
> +#else
>  #define KASAN_STACK_ORDER 1
> +#endif
>  #else
>  #define KASAN_STACK_ORDER 0
>  #endif

I'm suspecting this logic could be performed in Kconfig, for all
architectures.  Add a new always-defined CONFIG_KASAN_STACK_ORDER?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RESEND PATCH] x86_64: increase stack size for KASAN_EXTRA
  2019-01-09 22:02   ` Andrew Morton
@ 2019-01-10  4:20     ` Qian Cai
  0 siblings, 0 replies; 5+ messages in thread
From: Qian Cai @ 2019-01-10  4:20 UTC (permalink / raw)
  To: Andrew Morton; +Cc: tglx, mingo, bp, hpa, x86, linux-kernel



On 1/9/19 5:02 PM, Andrew Morton wrote:
>> --- a/arch/x86/include/asm/page_64_types.h
>> +++ b/arch/x86/include/asm/page_64_types.h
>> @@ -7,7 +7,11 @@
>>  #endif
>>  
>>  #ifdef CONFIG_KASAN
>> +#ifdef CONFIG_KASAN_EXTRA
>> +#define KASAN_STACK_ORDER 2
>> +#else
>>  #define KASAN_STACK_ORDER 1
>> +#endif
>>  #else
>>  #define KASAN_STACK_ORDER 0
>>  #endif
> 
> I'm suspecting this logic could be performed in Kconfig, for all
> architectures.  Add a new always-defined CONFIG_KASAN_STACK_ORDER?
> 

Sounds doable, but KASAN only support x86_64, arm64, s390 and xtensa so far. I
am not sure I care about the later two nor I have any way to test them if they
suffer the same problem.

I'll keep looking if more arches start to implement KASAN, and then might be a
good time to reduce all those code duplication.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RESEND PATCH] x86_64: increase stack size for KASAN_EXTRA
  2019-01-09 21:52 ` [RESEND PATCH] " Qian Cai
  2019-01-09 22:02   ` Andrew Morton
@ 2019-01-11 18:55   ` Andrey Ryabinin
  1 sibling, 0 replies; 5+ messages in thread
From: Andrey Ryabinin @ 2019-01-11 18:55 UTC (permalink / raw)
  To: Qian Cai, akpm
  Cc: tglx, mingo, bp, hpa, x86, linux-kernel, Dmitry Vyukov,
	Alexander Potapenko, kasan-dev



On 1/10/19 12:52 AM, Qian Cai wrote:
> If the kernel is configured with KASAN_EXTRA, the stack size is
> increasted significantly due to enable this option will set
> "-fstack-reuse" to "none" in GCC [1]. As the results, it could trigger
> stack overrun quite often with 32k stack size compiled using GCC 8. For
> example, this reproducer
> 
> https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/\
> syscalls/madvise/madvise06.c
> 
> could trigger a "corrupted stack end detected inside scheduler" very
> reliably with CONFIG_SCHED_STACK_END_CHECK enabled.
> 
> There are just too many functions that could have a large stack with
> KASAN_EXTRA due to large local variables that have been called over and
> over again without being able to reuse the stacks. Some noticiable ones
> are,
> 
> size
> 7648 shrink_page_list
> 3584 xfs_rmap_convert
> 3312 migrate_page_move_mapping
> 3312 dev_ethtool
> 3200 migrate_misplaced_transhuge_page
> 3168 copy_process
> 
> There are other 49 functions are over 2k in size while compiling kernel
> with "-Wframe-larger-than=" even with a related minimal config on this
> machine. Hence, it is too much work to change Makefiles for each object
> to compile without "-fsanitize-address-use-after-scope" individually.
> 
> [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715#c23
> 
> Although there is a patch in GCC 9 to help the situation, GCC 9 probably
> won't be released in a few months and then it probably take another
> 6-month to 1-year for all major distros to include it as a default.
> Hence, the stack usage with KASAN_EXTRA can be revisited again in 2020
> when GCC 9 is everywhere. Until then, this patch will help users avoid
> stack overrun.
> 
> This has already been fixed for arm64 for the same reason via
> 6e8830674ea (arm64: kasan: Increase stack size for KASAN_EXTRA).
> 
> Signed-off-by: Qian Cai <cai@lca.pw>
> ---
>  arch/x86/include/asm/page_64_types.h | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/arch/x86/include/asm/page_64_types.h b/arch/x86/include/asm/page_64_types.h
> index 8f657286d599..0ce558a8150d 100644
> --- a/arch/x86/include/asm/page_64_types.h
> +++ b/arch/x86/include/asm/page_64_types.h
> @@ -7,7 +7,11 @@
>  #endif
>  
>  #ifdef CONFIG_KASAN
> +#ifdef CONFIG_KASAN_EXTRA
> +#define KASAN_STACK_ORDER 2

So the kernel stack becomes 4-order page. That's above PAGE_ALLOC_COSTLY_ORDER, so people
will start seeing fork() failures with -ENOMEM due to high memory fragmentation. I don't think
we can afford such change.

Give that use-after-scope has proven to be almost useless for the kernel, I think we should just
remove it entirely.

> +#else
>  #define KASAN_STACK_ORDER 1
> +#endif
>  #else
>  #define KASAN_STACK_ORDER 0
>  #endif
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-28  2:06 [PATCH] x86_64: increase stack size for KASAN_EXTRA Qian Cai
2019-01-09 21:52 ` [RESEND PATCH] " Qian Cai
2019-01-09 22:02   ` Andrew Morton
2019-01-10  4:20     ` Qian Cai
2019-01-11 18:55   ` Andrey Ryabinin

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org linux-kernel@archiver.kernel.org
	public-inbox-index lkml


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox