mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-27 19:29:37 +00:00
![Andrey Konovalov](/assets/img/avatar_default.png)
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>
191 lines
4.3 KiB
C
191 lines
4.3 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* This file contains core hardware tag-based KASAN code.
|
|
*
|
|
* Copyright (c) 2020 Google, Inc.
|
|
* Author: Andrey Konovalov <andreyknvl@google.com>
|
|
*/
|
|
|
|
#define pr_fmt(fmt) "kasan: " fmt
|
|
|
|
#include <linux/init.h>
|
|
#include <linux/kasan.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/memory.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/static_key.h>
|
|
#include <linux/string.h>
|
|
#include <linux/types.h>
|
|
|
|
#include "kasan.h"
|
|
|
|
enum kasan_arg {
|
|
KASAN_ARG_DEFAULT,
|
|
KASAN_ARG_OFF,
|
|
KASAN_ARG_ON,
|
|
};
|
|
|
|
enum kasan_arg_stacktrace {
|
|
KASAN_ARG_STACKTRACE_DEFAULT,
|
|
KASAN_ARG_STACKTRACE_OFF,
|
|
KASAN_ARG_STACKTRACE_ON,
|
|
};
|
|
|
|
enum kasan_arg_fault {
|
|
KASAN_ARG_FAULT_DEFAULT,
|
|
KASAN_ARG_FAULT_REPORT,
|
|
KASAN_ARG_FAULT_PANIC,
|
|
};
|
|
|
|
static enum kasan_arg kasan_arg __ro_after_init;
|
|
static enum kasan_arg_stacktrace kasan_arg_stacktrace __ro_after_init;
|
|
static enum kasan_arg_fault kasan_arg_fault __ro_after_init;
|
|
|
|
/* Whether KASAN is enabled at all. */
|
|
DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled);
|
|
EXPORT_SYMBOL(kasan_flag_enabled);
|
|
|
|
/* Whether to collect alloc/free stack traces. */
|
|
DEFINE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
|
|
|
|
/* Whether panic or disable tag checking on fault. */
|
|
bool kasan_flag_panic __ro_after_init;
|
|
|
|
/* kasan=off/on */
|
|
static int __init early_kasan_flag(char *arg)
|
|
{
|
|
if (!arg)
|
|
return -EINVAL;
|
|
|
|
if (!strcmp(arg, "off"))
|
|
kasan_arg = KASAN_ARG_OFF;
|
|
else if (!strcmp(arg, "on"))
|
|
kasan_arg = KASAN_ARG_ON;
|
|
else
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
}
|
|
early_param("kasan", early_kasan_flag);
|
|
|
|
/* kasan.stacktrace=off/on */
|
|
static int __init early_kasan_flag_stacktrace(char *arg)
|
|
{
|
|
if (!arg)
|
|
return -EINVAL;
|
|
|
|
if (!strcmp(arg, "off"))
|
|
kasan_arg_stacktrace = KASAN_ARG_STACKTRACE_OFF;
|
|
else if (!strcmp(arg, "on"))
|
|
kasan_arg_stacktrace = KASAN_ARG_STACKTRACE_ON;
|
|
else
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
}
|
|
early_param("kasan.stacktrace", early_kasan_flag_stacktrace);
|
|
|
|
/* kasan.fault=report/panic */
|
|
static int __init early_kasan_fault(char *arg)
|
|
{
|
|
if (!arg)
|
|
return -EINVAL;
|
|
|
|
if (!strcmp(arg, "report"))
|
|
kasan_arg_fault = KASAN_ARG_FAULT_REPORT;
|
|
else if (!strcmp(arg, "panic"))
|
|
kasan_arg_fault = KASAN_ARG_FAULT_PANIC;
|
|
else
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
}
|
|
early_param("kasan.fault", early_kasan_fault);
|
|
|
|
/* kasan_init_hw_tags_cpu() is called for each CPU. */
|
|
void kasan_init_hw_tags_cpu(void)
|
|
{
|
|
/*
|
|
* There's no need to check that the hardware is MTE-capable here,
|
|
* as this function is only called for MTE-capable hardware.
|
|
*/
|
|
|
|
/* If KASAN is disabled via command line, don't initialize it. */
|
|
if (kasan_arg == KASAN_ARG_OFF)
|
|
return;
|
|
|
|
hw_init_tags(KASAN_TAG_MAX);
|
|
hw_enable_tagging();
|
|
}
|
|
|
|
/* kasan_init_hw_tags() is called once on boot CPU. */
|
|
void __init kasan_init_hw_tags(void)
|
|
{
|
|
/* If hardware doesn't support MTE, don't initialize KASAN. */
|
|
if (!system_supports_mte())
|
|
return;
|
|
|
|
/* If KASAN is disabled via command line, don't initialize it. */
|
|
if (kasan_arg == KASAN_ARG_OFF)
|
|
return;
|
|
|
|
/* Enable KASAN. */
|
|
static_branch_enable(&kasan_flag_enabled);
|
|
|
|
switch (kasan_arg_stacktrace) {
|
|
case KASAN_ARG_STACKTRACE_DEFAULT:
|
|
/*
|
|
* Default to enabling stack trace collection for
|
|
* debug kernels.
|
|
*/
|
|
if (IS_ENABLED(CONFIG_DEBUG_KERNEL))
|
|
static_branch_enable(&kasan_flag_stacktrace);
|
|
break;
|
|
case KASAN_ARG_STACKTRACE_OFF:
|
|
/* Do nothing, kasan_flag_stacktrace keeps its default value. */
|
|
break;
|
|
case KASAN_ARG_STACKTRACE_ON:
|
|
static_branch_enable(&kasan_flag_stacktrace);
|
|
break;
|
|
}
|
|
|
|
switch (kasan_arg_fault) {
|
|
case KASAN_ARG_FAULT_DEFAULT:
|
|
/*
|
|
* Default to no panic on report.
|
|
* Do nothing, kasan_flag_panic keeps its default value.
|
|
*/
|
|
break;
|
|
case KASAN_ARG_FAULT_REPORT:
|
|
/* Do nothing, kasan_flag_panic keeps its default value. */
|
|
break;
|
|
case KASAN_ARG_FAULT_PANIC:
|
|
/* Enable panic on report. */
|
|
kasan_flag_panic = true;
|
|
break;
|
|
}
|
|
|
|
pr_info("KernelAddressSanitizer initialized\n");
|
|
}
|
|
|
|
void kasan_set_free_info(struct kmem_cache *cache,
|
|
void *object, u8 tag)
|
|
{
|
|
struct kasan_alloc_meta *alloc_meta;
|
|
|
|
alloc_meta = kasan_get_alloc_meta(cache, object);
|
|
if (alloc_meta)
|
|
kasan_set_track(&alloc_meta->free_track[0], GFP_NOWAIT);
|
|
}
|
|
|
|
struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
|
|
void *object, u8 tag)
|
|
{
|
|
struct kasan_alloc_meta *alloc_meta;
|
|
|
|
alloc_meta = kasan_get_alloc_meta(cache, object);
|
|
if (!alloc_meta)
|
|
return NULL;
|
|
|
|
return &alloc_meta->free_track[0];
|
|
}
|