Re: [PATCH v4 11/45] kmsan: add KMSAN runtime core

Marco Elver
2022-07-13T10:04:57Z

On Fri, Jul 01, 2022 at 04:22PM +0200, 'Alexander Potapenko' via kasan-dev wrote:
[...]
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 2e24db4bff192..59819e6fa5865 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -963,6 +963,7 @@ config DEBUG_STACKOVERFLOW
>
> source "lib/Kconfig.kasan"
> source "lib/Kconfig.kfence"
> +source "lib/Kconfig.kmsan"
>
> endmenu # "Memory Debugging"
>
> diff --git a/lib/Kconfig.kmsan b/lib/Kconfig.kmsan
> new file mode 100644
> index 0000000000000..8f768d4034e3c
> --- /dev/null
> +++ b/lib/Kconfig.kmsan
> @@ -0,0 +1,50 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +config HAVE_ARCH_KMSAN
> + bool
> +
> +config HAVE_KMSAN_COMPILER
> + # Clang versions <14.0.0 also support -fsanitize=kernel-memory, but not
> + # all the features necessary to build the kernel with KMSAN.
> + depends on CC_IS_CLANG && CLANG_VERSION >= 140000
> + def_bool $(cc-option,-fsanitize=kernel-memory -mllvm -msan-disable-checks=1)
> +
> +config HAVE_KMSAN_PARAM_RETVAL
> + # Separate check for -fsanitize-memory-param-retval support.

This comment doesn't add much value, maybe instead say that "Supported
only by Clang >= 15."

> + depends on CC_IS_CLANG && CLANG_VERSION >= 140000

Why not just "depends on HAVE_KMSAN_COMPILER"? (All
fsanitize-memory-param-retval supporting compilers must also be KMSAN
compilers.)

> + def_bool $(cc-option,-fsanitize=kernel-memory -fsanitize-memory-param-retval)
> +
> +

HAVE_KMSAN_PARAM_RETVAL should be moved under "if KMSAN" so that this
isn't unnecessarily evaluated in every kernel build (saving 1 shelling
out to clang in most builds).

> +config KMSAN
> + bool "KMSAN: detector of uninitialized values use"
> + depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER
> + depends on SLUB && DEBUG_KERNEL && !KASAN && !KCSAN
> + select STACKDEPOT
> + select STACKDEPOT_ALWAYS_INIT
> + help
> + KernelMemorySanitizer (KMSAN) is a dynamic detector of uses of
> + uninitialized values in the kernel. It is based on compiler
> + instrumentation provided by Clang and thus requires Clang to build.
> +
> + An important note is that KMSAN is not intended for production use,
> + because it drastically increases kernel memory footprint and slows
> + the whole system down.
> +
> + See <file:Documentation/dev-tools/kmsan.rst> for more details.
> +
> +if KMSAN
> +
> +config KMSAN_CHECK_PARAM_RETVAL
> + bool "Check for uninitialized values passed to and returned from functions"
> + default HAVE_KMSAN_PARAM_RETVAL

This can be enabled even if !HAVE_KMSAN_PARAM_RETVAL. Should this be:

default y
depends on HAVE_KMSAN_PARAM_RETVAL

instead?

> + help
> + If the compiler supports -fsanitize-memory-param-retval, KMSAN will
> + eagerly check every function parameter passed by value and every
> + function return value.
> +
> + Disabling KMSAN_CHECK_PARAM_RETVAL will result in tracking shadow for
> + function parameters and return values across function borders. This
> + is a more relaxed mode, but it generates more instrumentation code and
> + may potentially report errors in corner cases when non-instrumented
> + functions call instrumented ones.
> +