Commit 2d27e585 authored by Vincenzo Frascino's avatar Vincenzo Frascino Committed by Will Deacon

kasan: Extend KASAN mode kernel parameter

Architectures supported by KASAN_HW_TAGS can provide an asymmetric mode
of execution. On an MTE enabled arm64 hw for example this can be
identified with the asymmetric tagging mode of execution. In particular,
when such a mode is present, the CPU triggers a fault on a tag mismatch
during a load operation and asynchronously updates a register when a tag
mismatch is detected during a store operation.

Extend the KASAN HW execution mode kernel command line parameter to
support asymmetric mode.

Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Signed-off-by: default avatarVincenzo Frascino <vincenzo.frascino@arm.com>
Reviewed-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Reviewed-by: default avatarAndrey Konovalov <andreyknvl@gmail.com>
Link: https://lore.kernel.org/r/20211006154751.4463-6-vincenzo.frascino@arm.comSigned-off-by: default avatarWill Deacon <will@kernel.org>
parent ec028836
...@@ -194,14 +194,17 @@ additional boot parameters that allow disabling KASAN or controlling features: ...@@ -194,14 +194,17 @@ additional boot parameters that allow disabling KASAN or controlling features:
- ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``). - ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``).
- ``kasan.mode=sync`` or ``=async`` controls whether KASAN is configured in - ``kasan.mode=sync``, ``=async`` or ``=asymm`` controls whether KASAN
synchronous or asynchronous mode of execution (default: ``sync``). is configured in synchronous, asynchronous or asymmetric mode of
execution (default: ``sync``).
Synchronous mode: a bad access is detected immediately when a tag Synchronous mode: a bad access is detected immediately when a tag
check fault occurs. check fault occurs.
Asynchronous mode: a bad access detection is delayed. When a tag check Asynchronous mode: a bad access detection is delayed. When a tag check
fault occurs, the information is stored in hardware (in the TFSR_EL1 fault occurs, the information is stored in hardware (in the TFSR_EL1
register for arm64). The kernel periodically checks the hardware and register for arm64). The kernel periodically checks the hardware and
only reports tag faults during these checks. only reports tag faults during these checks.
Asymmetric mode: a bad access is detected synchronously on reads and
asynchronously on writes.
- ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack - ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack
traces collection (default: ``on``). traces collection (default: ``on``).
......
...@@ -88,7 +88,7 @@ static void kasan_test_exit(struct kunit *test) ...@@ -88,7 +88,7 @@ static void kasan_test_exit(struct kunit *test)
*/ */
#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \ #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \ if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
!kasan_async_mode_enabled()) \ kasan_sync_fault_possible()) \
migrate_disable(); \ migrate_disable(); \
KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \ KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \
barrier(); \ barrier(); \
......
...@@ -29,6 +29,7 @@ enum kasan_arg_mode { ...@@ -29,6 +29,7 @@ enum kasan_arg_mode {
KASAN_ARG_MODE_DEFAULT, KASAN_ARG_MODE_DEFAULT,
KASAN_ARG_MODE_SYNC, KASAN_ARG_MODE_SYNC,
KASAN_ARG_MODE_ASYNC, KASAN_ARG_MODE_ASYNC,
KASAN_ARG_MODE_ASYMM,
}; };
enum kasan_arg_stacktrace { enum kasan_arg_stacktrace {
...@@ -45,9 +46,9 @@ static enum kasan_arg_stacktrace kasan_arg_stacktrace __ro_after_init; ...@@ -45,9 +46,9 @@ static enum kasan_arg_stacktrace kasan_arg_stacktrace __ro_after_init;
DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled);
EXPORT_SYMBOL(kasan_flag_enabled); EXPORT_SYMBOL(kasan_flag_enabled);
/* Whether the asynchronous mode is enabled. */ /* Whether the selected mode is synchronous/asynchronous/asymmetric.*/
bool kasan_flag_async __ro_after_init; enum kasan_mode kasan_mode __ro_after_init;
EXPORT_SYMBOL_GPL(kasan_flag_async); EXPORT_SYMBOL_GPL(kasan_mode);
/* Whether to collect alloc/free stack traces. */ /* Whether to collect alloc/free stack traces. */
DEFINE_STATIC_KEY_FALSE(kasan_flag_stacktrace); DEFINE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
...@@ -69,7 +70,7 @@ static int __init early_kasan_flag(char *arg) ...@@ -69,7 +70,7 @@ static int __init early_kasan_flag(char *arg)
} }
early_param("kasan", early_kasan_flag); early_param("kasan", early_kasan_flag);
/* kasan.mode=sync/async */ /* kasan.mode=sync/async/asymm */
static int __init early_kasan_mode(char *arg) static int __init early_kasan_mode(char *arg)
{ {
if (!arg) if (!arg)
...@@ -79,6 +80,8 @@ static int __init early_kasan_mode(char *arg) ...@@ -79,6 +80,8 @@ static int __init early_kasan_mode(char *arg)
kasan_arg_mode = KASAN_ARG_MODE_SYNC; kasan_arg_mode = KASAN_ARG_MODE_SYNC;
else if (!strcmp(arg, "async")) else if (!strcmp(arg, "async"))
kasan_arg_mode = KASAN_ARG_MODE_ASYNC; kasan_arg_mode = KASAN_ARG_MODE_ASYNC;
else if (!strcmp(arg, "asymm"))
kasan_arg_mode = KASAN_ARG_MODE_ASYMM;
else else
return -EINVAL; return -EINVAL;
...@@ -116,11 +119,13 @@ void kasan_init_hw_tags_cpu(void) ...@@ -116,11 +119,13 @@ void kasan_init_hw_tags_cpu(void)
return; return;
/* /*
* Enable async mode only when explicitly requested through * Enable async or asymm modes only when explicitly requested
* the command line. * through the command line.
*/ */
if (kasan_arg_mode == KASAN_ARG_MODE_ASYNC) if (kasan_arg_mode == KASAN_ARG_MODE_ASYNC)
hw_enable_tagging_async(); hw_enable_tagging_async();
else if (kasan_arg_mode == KASAN_ARG_MODE_ASYMM)
hw_enable_tagging_asymm();
else else
hw_enable_tagging_sync(); hw_enable_tagging_sync();
} }
...@@ -143,15 +148,19 @@ void __init kasan_init_hw_tags(void) ...@@ -143,15 +148,19 @@ void __init kasan_init_hw_tags(void)
case KASAN_ARG_MODE_DEFAULT: case KASAN_ARG_MODE_DEFAULT:
/* /*
* Default to sync mode. * Default to sync mode.
* Do nothing, kasan_flag_async keeps its default value.
*/ */
break; fallthrough;
case KASAN_ARG_MODE_SYNC: case KASAN_ARG_MODE_SYNC:
/* Do nothing, kasan_flag_async keeps its default value. */ /* Sync mode enabled. */
kasan_mode = KASAN_MODE_SYNC;
break; break;
case KASAN_ARG_MODE_ASYNC: case KASAN_ARG_MODE_ASYNC:
/* Async mode enabled. */ /* Async mode enabled. */
kasan_flag_async = true; kasan_mode = KASAN_MODE_ASYNC;
break;
case KASAN_ARG_MODE_ASYMM:
/* Asymm mode enabled. */
kasan_mode = KASAN_MODE_ASYMM;
break; break;
} }
......
...@@ -13,16 +13,28 @@ ...@@ -13,16 +13,28 @@
#include "../slab.h" #include "../slab.h"
DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace); DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
extern bool kasan_flag_async __ro_after_init;
enum kasan_mode {
KASAN_MODE_SYNC,
KASAN_MODE_ASYNC,
KASAN_MODE_ASYMM,
};
extern enum kasan_mode kasan_mode __ro_after_init;
static inline bool kasan_stack_collection_enabled(void) static inline bool kasan_stack_collection_enabled(void)
{ {
return static_branch_unlikely(&kasan_flag_stacktrace); return static_branch_unlikely(&kasan_flag_stacktrace);
} }
static inline bool kasan_async_mode_enabled(void) static inline bool kasan_async_fault_possible(void)
{
return kasan_mode == KASAN_MODE_ASYNC || kasan_mode == KASAN_MODE_ASYMM;
}
static inline bool kasan_sync_fault_possible(void)
{ {
return kasan_flag_async; return kasan_mode == KASAN_MODE_SYNC || kasan_mode == KASAN_MODE_ASYMM;
} }
#else #else
...@@ -31,11 +43,16 @@ static inline bool kasan_stack_collection_enabled(void) ...@@ -31,11 +43,16 @@ static inline bool kasan_stack_collection_enabled(void)
return true; return true;
} }
static inline bool kasan_async_mode_enabled(void) static inline bool kasan_async_fault_possible(void)
{ {
return false; return false;
} }
static inline bool kasan_sync_fault_possible(void)
{
return true;
}
#endif #endif
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
...@@ -287,6 +304,9 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag) ...@@ -287,6 +304,9 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
#ifndef arch_enable_tagging_async #ifndef arch_enable_tagging_async
#define arch_enable_tagging_async() #define arch_enable_tagging_async()
#endif #endif
#ifndef arch_enable_tagging_asymm
#define arch_enable_tagging_asymm()
#endif
#ifndef arch_force_async_tag_fault #ifndef arch_force_async_tag_fault
#define arch_force_async_tag_fault() #define arch_force_async_tag_fault()
#endif #endif
...@@ -302,6 +322,7 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag) ...@@ -302,6 +322,7 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
#define hw_enable_tagging_sync() arch_enable_tagging_sync() #define hw_enable_tagging_sync() arch_enable_tagging_sync()
#define hw_enable_tagging_async() arch_enable_tagging_async() #define hw_enable_tagging_async() arch_enable_tagging_async()
#define hw_enable_tagging_asymm() arch_enable_tagging_asymm()
#define hw_force_async_tag_fault() arch_force_async_tag_fault() #define hw_force_async_tag_fault() arch_force_async_tag_fault()
#define hw_get_random_tag() arch_get_random_tag() #define hw_get_random_tag() arch_get_random_tag()
#define hw_get_mem_tag(addr) arch_get_mem_tag(addr) #define hw_get_mem_tag(addr) arch_get_mem_tag(addr)
...@@ -312,6 +333,7 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag) ...@@ -312,6 +333,7 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
#define hw_enable_tagging_sync() #define hw_enable_tagging_sync()
#define hw_enable_tagging_async() #define hw_enable_tagging_async()
#define hw_enable_tagging_asymm()
#endif /* CONFIG_KASAN_HW_TAGS */ #endif /* CONFIG_KASAN_HW_TAGS */
......
...@@ -112,7 +112,7 @@ static void start_report(unsigned long *flags) ...@@ -112,7 +112,7 @@ static void start_report(unsigned long *flags)
static void end_report(unsigned long *flags, unsigned long addr) static void end_report(unsigned long *flags, unsigned long addr)
{ {
if (!kasan_async_mode_enabled()) if (!kasan_async_fault_possible())
trace_error_report_end(ERROR_DETECTOR_KASAN, addr); trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
pr_err("==================================================================\n"); pr_err("==================================================================\n");
add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment