Skip to content

Commit 76bc99e

Browse files
xairytorvalds
authored andcommitted
kasan: fix HW_TAGS boot parameters
The initially proposed KASAN command line parameters are redundant. This change drops the complex "kasan.mode=off/prod/full" parameter and adds a simpler kill switch "kasan=off/on" instead. The new parameter together with the already existing ones provides a cleaner way to express the same set of features. The full set of parameters with this change: kasan=off/on - whether KASAN is enabled kasan.fault=report/panic - whether to only print a report or also panic kasan.stacktrace=off/on - whether to collect alloc/free stack traces Default values: kasan=on kasan.fault=report kasan.stacktrace=on (if CONFIG_DEBUG_KERNEL=y) kasan.stacktrace=off (otherwise) Link: https://linux-review.googlesource.com/id/Ib3694ed90b1e8ccac6cf77dfd301847af4aba7b8 Link: https://lkml.kernel.org/r/4e9c4a4bdcadc168317deb2419144582a9be6e61.1610736745.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Vincenzo Frascino <vincenzo.frascino@arm.com> Reviewed-by: Marco Elver <elver@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Alexander Potapenko <glider@google.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Peter Collingbourne <pcc@google.com> Cc: Evgenii Stepanov <eugenis@google.com> Cc: Branislav Rankov <Branislav.Rankov@arm.com> Cc: Kevin Brodsky <kevin.brodsky@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5dabd17 commit 76bc99e

2 files changed

Lines changed: 38 additions & 66 deletions

File tree

Documentation/dev-tools/kasan.rst

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,29 +160,14 @@ intended for use in production as a security mitigation. Therefore it supports
160160
boot parameters that allow to disable KASAN competely or otherwise control
161161
particular KASAN features.
162162

163-
The things that can be controlled are:
163+
- ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``).
164164

165-
1. Whether KASAN is enabled at all.
166-
2. Whether KASAN collects and saves alloc/free stacks.
167-
3. Whether KASAN panics on a detected bug or not.
165+
- ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack
166+
traces collection (default: ``on`` for ``CONFIG_DEBUG_KERNEL=y``, otherwise
167+
``off``).
168168

169-
The ``kasan.mode`` boot parameter allows to choose one of three main modes:
170-
171-
- ``kasan.mode=off`` - KASAN is disabled, no tag checks are performed
172-
- ``kasan.mode=prod`` - only essential production features are enabled
173-
- ``kasan.mode=full`` - all KASAN features are enabled
174-
175-
The chosen mode provides default control values for the features mentioned
176-
above. However it's also possible to override the default values by providing:
177-
178-
- ``kasan.stacktrace=off`` or ``=on`` - enable alloc/free stack collection
179-
(default: ``on`` for ``mode=full``,
180-
otherwise ``off``)
181-
- ``kasan.fault=report`` or ``=panic`` - only print KASAN report or also panic
182-
(default: ``report``)
183-
184-
If ``kasan.mode`` parameter is not provided, it defaults to ``full`` when
185-
``CONFIG_DEBUG_KERNEL`` is enabled, and to ``prod`` otherwise.
169+
- ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN
170+
report or also panic the kernel (default: ``report``).
186171

187172
For developers
188173
~~~~~~~~~~~~~~

mm/kasan/hw_tags.c

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919

2020
#include "kasan.h"
2121

22-
enum kasan_arg_mode {
23-
KASAN_ARG_MODE_DEFAULT,
24-
KASAN_ARG_MODE_OFF,
25-
KASAN_ARG_MODE_PROD,
26-
KASAN_ARG_MODE_FULL,
22+
enum kasan_arg {
23+
KASAN_ARG_DEFAULT,
24+
KASAN_ARG_OFF,
25+
KASAN_ARG_ON,
2726
};
2827

2928
enum kasan_arg_stacktrace {
@@ -38,7 +37,7 @@ enum kasan_arg_fault {
3837
KASAN_ARG_FAULT_PANIC,
3938
};
4039

41-
static enum kasan_arg_mode kasan_arg_mode __ro_after_init;
40+
static enum kasan_arg kasan_arg __ro_after_init;
4241
static enum kasan_arg_stacktrace kasan_arg_stacktrace __ro_after_init;
4342
static enum kasan_arg_fault kasan_arg_fault __ro_after_init;
4443

@@ -52,26 +51,24 @@ DEFINE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
5251
/* Whether panic or disable tag checking on fault. */
5352
bool kasan_flag_panic __ro_after_init;
5453

55-
/* kasan.mode=off/prod/full */
56-
static int __init early_kasan_mode(char *arg)
54+
/* kasan=off/on */
55+
static int __init early_kasan_flag(char *arg)
5756
{
5857
if (!arg)
5958
return -EINVAL;
6059

6160
if (!strcmp(arg, "off"))
62-
kasan_arg_mode = KASAN_ARG_MODE_OFF;
63-
else if (!strcmp(arg, "prod"))
64-
kasan_arg_mode = KASAN_ARG_MODE_PROD;
65-
else if (!strcmp(arg, "full"))
66-
kasan_arg_mode = KASAN_ARG_MODE_FULL;
61+
kasan_arg = KASAN_ARG_OFF;
62+
else if (!strcmp(arg, "on"))
63+
kasan_arg = KASAN_ARG_ON;
6764
else
6865
return -EINVAL;
6966

7067
return 0;
7168
}
72-
early_param("kasan.mode", early_kasan_mode);
69+
early_param("kasan", early_kasan_flag);
7370

74-
/* kasan.stack=off/on */
71+
/* kasan.stacktrace=off/on */
7572
static int __init early_kasan_flag_stacktrace(char *arg)
7673
{
7774
if (!arg)
@@ -113,8 +110,8 @@ void kasan_init_hw_tags_cpu(void)
113110
* as this function is only called for MTE-capable hardware.
114111
*/
115112

116-
/* If KASAN is disabled, do nothing. */
117-
if (kasan_arg_mode == KASAN_ARG_MODE_OFF)
113+
/* If KASAN is disabled via command line, don't initialize it. */
114+
if (kasan_arg == KASAN_ARG_OFF)
118115
return;
119116

120117
hw_init_tags(KASAN_TAG_MAX);
@@ -124,43 +121,28 @@ void kasan_init_hw_tags_cpu(void)
124121
/* kasan_init_hw_tags() is called once on boot CPU. */
125122
void __init kasan_init_hw_tags(void)
126123
{
127-
/* If hardware doesn't support MTE, do nothing. */
124+
/* If hardware doesn't support MTE, don't initialize KASAN. */
128125
if (!system_supports_mte())
129126
return;
130127

131-
/* Choose KASAN mode if kasan boot parameter is not provided. */
132-
if (kasan_arg_mode == KASAN_ARG_MODE_DEFAULT) {
133-
if (IS_ENABLED(CONFIG_DEBUG_KERNEL))
134-
kasan_arg_mode = KASAN_ARG_MODE_FULL;
135-
else
136-
kasan_arg_mode = KASAN_ARG_MODE_PROD;
137-
}
138-
139-
/* Preset parameter values based on the mode. */
140-
switch (kasan_arg_mode) {
141-
case KASAN_ARG_MODE_DEFAULT:
142-
/* Shouldn't happen as per the check above. */
143-
WARN_ON(1);
144-
return;
145-
case KASAN_ARG_MODE_OFF:
146-
/* If KASAN is disabled, do nothing. */
128+
/* If KASAN is disabled via command line, don't initialize it. */
129+
if (kasan_arg == KASAN_ARG_OFF)
147130
return;
148-
case KASAN_ARG_MODE_PROD:
149-
static_branch_enable(&kasan_flag_enabled);
150-
break;
151-
case KASAN_ARG_MODE_FULL:
152-
static_branch_enable(&kasan_flag_enabled);
153-
static_branch_enable(&kasan_flag_stacktrace);
154-
break;
155-
}
156131

157-
/* Now, optionally override the presets. */
132+
/* Enable KASAN. */
133+
static_branch_enable(&kasan_flag_enabled);
158134

159135
switch (kasan_arg_stacktrace) {
160136
case KASAN_ARG_STACKTRACE_DEFAULT:
137+
/*
138+
* Default to enabling stack trace collection for
139+
* debug kernels.
140+
*/
141+
if (IS_ENABLED(CONFIG_DEBUG_KERNEL))
142+
static_branch_enable(&kasan_flag_stacktrace);
161143
break;
162144
case KASAN_ARG_STACKTRACE_OFF:
163-
static_branch_disable(&kasan_flag_stacktrace);
145+
/* Do nothing, kasan_flag_stacktrace keeps its default value. */
164146
break;
165147
case KASAN_ARG_STACKTRACE_ON:
166148
static_branch_enable(&kasan_flag_stacktrace);
@@ -169,11 +151,16 @@ void __init kasan_init_hw_tags(void)
169151

170152
switch (kasan_arg_fault) {
171153
case KASAN_ARG_FAULT_DEFAULT:
154+
/*
155+
* Default to no panic on report.
156+
* Do nothing, kasan_flag_panic keeps its default value.
157+
*/
172158
break;
173159
case KASAN_ARG_FAULT_REPORT:
174-
kasan_flag_panic = false;
160+
/* Do nothing, kasan_flag_panic keeps its default value. */
175161
break;
176162
case KASAN_ARG_FAULT_PANIC:
163+
/* Enable panic on report. */
177164
kasan_flag_panic = true;
178165
break;
179166
}

0 commit comments

Comments
 (0)