UserMode Linux needs deferred KASAN initialization as it has a custom
kasan_arch_is_ready() implementation that tracks shadow memory readiness
via the kasan_um_is_ready flag.
As it's explained in commit 5b301409e8bc("UML: add support for KASAN
under x86_64"), if CONFIG_STATIC_LINK=y, then it works only with
CONFIG_KASAN_OUTLINE instrumentation.
Calling kasan_init_generic() in the end of kasan_init() like in other
arch does not work for UML as kasan_init() is called way before
main()->linux_main(). It produces the SEGFAULT in:
kasan_init()
kasan_init_generic
kasan_enable
static_key_enable
STATIC_KEY_CHECK_USE
...
<kasan_init+173> movabs r9, kasan_flag_enabled
<kasan_init+183> movabs r8, __func__.2
<kasan_init+193> movabs rcx, 0x60a04540
<kasan_init+203> movabs rdi, 0x60a045a0
<kasan_init+213> movabs r10, warn_slowpath_fmt
WARN_ON_ONCE("static key '%pS' used before call to jump_label_init()")
<kasan_init+226> movabs r12, kasan_flag_enabled
That's why we need to call kasan_init_generic() which enables the
static flag after jump_label_init(). The earliest available place
is arch_mm_preinit().
kasan_init()
main()
start_kernel
setup_arch
jump_label_init
...
mm_core_init
arch_mm_preinit
kasan_init_generic()
PowerPC, for example, has kasan_late_init() in arch_mm_preinit().
Though there is no static key enabling there, but it should be the best
place to enable KASAN "fully".
Verified with defconfig, enabling KASAN.
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217049
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
Changes in v4:
- Addressed the issue in UML arch, where kasan_init_generic() is
called before jump_label_init() (Andrey Ryabinin)
---
arch/um/Kconfig | 1 +
arch/um/include/asm/kasan.h | 5 -----
arch/um/kernel/mem.c | 12 +++++++++---
3 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/arch/um/Kconfig b/arch/um/Kconfig
index 9083bfdb773..8d14c8fc2cd 100644
--- a/arch/um/Kconfig
+++ b/arch/um/Kconfig
@@ -5,6 +5,7 @@ menu "UML-specific options"
config UML
bool
default y
+ select ARCH_DEFER_KASAN
select ARCH_WANTS_DYNAMIC_TASK_STRUCT
select ARCH_HAS_CACHE_LINE_SIZE
select ARCH_HAS_CPU_FINALIZE_INIT
diff --git a/arch/um/include/asm/kasan.h b/arch/um/include/asm/kasan.h
index f97bb1f7b85..81bcdc0f962 100644
--- a/arch/um/include/asm/kasan.h
+++ b/arch/um/include/asm/kasan.h
@@ -24,11 +24,6 @@
#ifdef CONFIG_KASAN
void kasan_init(void);
-extern int kasan_um_is_ready;
-
-#ifdef CONFIG_STATIC_LINK
-#define kasan_arch_is_ready() (kasan_um_is_ready)
-#endif
#else
static inline void kasan_init(void) { }
#endif /* CONFIG_KASAN */
diff --git a/arch/um/kernel/mem.c b/arch/um/kernel/mem.c
index 76bec7de81b..704a26211ed 100644
--- a/arch/um/kernel/mem.c
+++ b/arch/um/kernel/mem.c
@@ -21,10 +21,10 @@
#include <os.h>
#include <um_malloc.h>
#include <linux/sched/task.h>
+#include <linux/kasan.h>
#ifdef CONFIG_KASAN
-int kasan_um_is_ready;
-void kasan_init(void)
+void __init kasan_init(void)
{
/*
* kasan_map_memory will map all of the required address space and
@@ -32,7 +32,10 @@ void kasan_init(void)
*/
kasan_map_memory((void *)KASAN_SHADOW_START, KASAN_SHADOW_SIZE);
init_task.kasan_depth = 0;
- kasan_um_is_ready = true;
+ /* Since kasan_init() is called before main(),
+ * KASAN is initialized but the enablement is deferred after
+ * jump_label_init(). See arch_mm_preinit().
+ */
}
static void (*kasan_init_ptr)(void)
@@ -58,6 +61,9 @@ static unsigned long brk_end;
void __init arch_mm_preinit(void)
{
+ /* Safe to call after jump_label_init(). Enables KASAN. */
+ kasan_init_generic();
+
/* clear the zero-page */
memset(empty_zero_page, 0, PAGE_SIZE);
--
2.34.1
On 8/5/25 4:26 PM, Sabyrzhan Tasbolatov wrote: > > diff --git a/arch/um/Kconfig b/arch/um/Kconfig > index 9083bfdb773..8d14c8fc2cd 100644 > --- a/arch/um/Kconfig > +++ b/arch/um/Kconfig > @@ -5,6 +5,7 @@ menu "UML-specific options" > config UML > bool > default y > + select ARCH_DEFER_KASAN select ARCH_DEFER_KASAN if STATIC_LINK > select ARCH_WANTS_DYNAMIC_TASK_STRUCT > select ARCH_HAS_CACHE_LINE_SIZE > select ARCH_HAS_CPU_FINALIZE_INIT > diff --git a/arch/um/include/asm/kasan.h b/arch/um/include/asm/kasan.h > index f97bb1f7b85..81bcdc0f962 100644 > --- a/arch/um/include/asm/kasan.h > +++ b/arch/um/include/asm/kasan.h > @@ -24,11 +24,6 @@ > > #ifdef CONFIG_KASAN > void kasan_init(void); > -extern int kasan_um_is_ready; > - > -#ifdef CONFIG_STATIC_LINK > -#define kasan_arch_is_ready() (kasan_um_is_ready) > -#endif > #else > static inline void kasan_init(void) { } > #endif /* CONFIG_KASAN */
On Tue, Aug 5, 2025 at 10:19 PM Andrey Ryabinin <ryabinin.a.a@gmail.com> wrote: > > > > On 8/5/25 4:26 PM, Sabyrzhan Tasbolatov wrote: > > > > diff --git a/arch/um/Kconfig b/arch/um/Kconfig > > index 9083bfdb773..8d14c8fc2cd 100644 > > --- a/arch/um/Kconfig > > +++ b/arch/um/Kconfig > > @@ -5,6 +5,7 @@ menu "UML-specific options" > > config UML > > bool > > default y > > + select ARCH_DEFER_KASAN > > select ARCH_DEFER_KASAN if STATIC_LINK As pointed out in commit 5b301409e8bc("UML: add support for KASAN under x86_64"), : Also note that, while UML supports both KASAN in inline mode (CONFIG_KASAN_INLINE) : and static linking (CONFIG_STATIC_LINK), it does not support both at the same time. I've tested that for UML, ARCH_DEFER_KASAN works if STATIC_LINK && KASAN_OUTLINE ARCH_DEFER_KASAN works if KASAN_INLINE && !STATIC_LINK ARCH_DEFER_KASAN if STATIC_LINK, and KASAN_INLINE=y by default from defconfig crashes with SEGFAULT here (I didn't understand what it is, I think the main() constructors is not prepared in UML): ► 0 0x609d6f87 strlen+43 1 0x60a20db0 _dl_new_object+48 2 0x60a24627 _dl_non_dynamic_init+103 3 0x60a25f9a __libc_init_first+42 4 0x609eb6b2 __libc_start_main_impl+2434 5 0x6004a025 _start+37 Since this is the case only for UML, AFAIU, I don't think we want to change conditions in lib/Kconfig.kasan. Shall I leave UML Kconfig as it is? e.g. select ARCH_DEFER_KASAN > > > select ARCH_WANTS_DYNAMIC_TASK_STRUCT > > select ARCH_HAS_CACHE_LINE_SIZE > > select ARCH_HAS_CPU_FINALIZE_INIT > > diff --git a/arch/um/include/asm/kasan.h b/arch/um/include/asm/kasan.h > > index f97bb1f7b85..81bcdc0f962 100644 > > --- a/arch/um/include/asm/kasan.h > > +++ b/arch/um/include/asm/kasan.h > > @@ -24,11 +24,6 @@ > > > > #ifdef CONFIG_KASAN > > void kasan_init(void); > > -extern int kasan_um_is_ready; > > - > > -#ifdef CONFIG_STATIC_LINK > > -#define kasan_arch_is_ready() (kasan_um_is_ready) > > -#endif > > #else > > static inline void kasan_init(void) { } > > #endif /* CONFIG_KASAN */
On 8/6/25 6:35 AM, Sabyrzhan Tasbolatov wrote: > On Tue, Aug 5, 2025 at 10:19 PM Andrey Ryabinin <ryabinin.a.a@gmail.com> wrote: >> >> >> >> On 8/5/25 4:26 PM, Sabyrzhan Tasbolatov wrote: >>> >>> diff --git a/arch/um/Kconfig b/arch/um/Kconfig >>> index 9083bfdb773..8d14c8fc2cd 100644 >>> --- a/arch/um/Kconfig >>> +++ b/arch/um/Kconfig >>> @@ -5,6 +5,7 @@ menu "UML-specific options" >>> config UML >>> bool >>> default y >>> + select ARCH_DEFER_KASAN >> >> select ARCH_DEFER_KASAN if STATIC_LINK > > As pointed out in commit 5b301409e8bc("UML: add support for KASAN > under x86_64"), > > : Also note that, while UML supports both KASAN in inline mode > (CONFIG_KASAN_INLINE) > : and static linking (CONFIG_STATIC_LINK), it does not support both at > the same time. > > I've tested that for UML, > ARCH_DEFER_KASAN works if STATIC_LINK && KASAN_OUTLINE > ARCH_DEFER_KASAN works if KASAN_INLINE && !STATIC_LINK > > ARCH_DEFER_KASAN if STATIC_LINK, and KASAN_INLINE=y by default from defconfig > crashes with SEGFAULT here (I didn't understand what it is, I think > the main() constructors > is not prepared in UML): > > ► 0 0x609d6f87 strlen+43 > 1 0x60a20db0 _dl_new_object+48 > 2 0x60a24627 _dl_non_dynamic_init+103 > 3 0x60a25f9a __libc_init_first+42 > 4 0x609eb6b2 __libc_start_main_impl+2434 > 5 0x6004a025 _start+37 > No surprise here, kasan_arch_is_ready() or ARCH_DEFER_KASAN doesn't work with KASAN_INLINE=y This configuration combination (STATIC_LINK + KASAN_INLINE) wasn't possible before: #ifndef kasan_arch_is_ready static inline bool kasan_arch_is_ready(void) { return true; } #elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE) #error kasan_arch_is_ready only works in KASAN generic outline mode! #endif > Since this is the case only for UML, AFAIU, I don't think we want to change > conditions in lib/Kconfig.kasan. Shall I leave UML Kconfig as it is? e.g. > > select ARCH_DEFER_KASAN > No, this should have if STATIC_LINK
© 2016 - 2025 Red Hat, Inc.