All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Potapenko <glider@google.com>
To: glider@google.com
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andrey Konovalov <andreyknvl@google.com>,
	Andy Lutomirski <luto@kernel.org>, Arnd Bergmann <arnd@arndb.de>,
	Borislav Petkov <bp@alien8.de>, Christoph Hellwig <hch@lst.de>,
	Christoph Lameter <cl@linux.com>,
	David Rientjes <rientjes@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Eric Dumazet <edumazet@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Ilya Leoshkevich <iii@linux.ibm.com>,
	Ingo Molnar <mingo@redhat.com>, Jens Axboe <axboe@kernel.dk>,
	Joonsoo Kim <iamjoonsoo.kim@lge.com>,
	Kees Cook <keescook@chromium.org>, Marco Elver <elver@google.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Matthew Wilcox <willy@infradead.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Pekka Enberg <penberg@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Petr Mladek <pmladek@suse.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Vegard Nossum <vegard.nossum@oracle.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	kasan-dev@googlegroups.com, linux-mm@kvack.org,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 13/46] kmsan: implement kmsan_init(), initialize READ_ONCE_NOCHECK()
Date: Tue, 26 Apr 2022 18:42:42 +0200	[thread overview]
Message-ID: <20220426164315.625149-14-glider@google.com> (raw)
In-Reply-To: <20220426164315.625149-1-glider@google.com>

kmsan_init() is a macro that takes a possibly uninitialized value and
returns an initialized value of the same type. It can be used e.g. in
cases when a value comes from non-instrumented code to avoid false
positive reports.

In particular, we use kmsan_init() in READ_ONCE_NOCHECK() so that it
returns initialized values. This helps defeat false positives e.g. from
leftover stack contents accessed by stack unwinders.

Signed-off-by: Alexander Potapenko <glider@google.com>
---
Link: https://linux-review.googlesource.com/id/Icd1260073666f944922f031bfb6762379ba1fa38
---
 include/asm-generic/rwonce.h |  5 +++--
 include/linux/kmsan-checks.h | 40 ++++++++++++++++++++++++++++++++++++
 mm/kmsan/Makefile            |  5 ++++-
 mm/kmsan/annotations.c       | 28 +++++++++++++++++++++++++
 4 files changed, 75 insertions(+), 3 deletions(-)
 create mode 100644 mm/kmsan/annotations.c

diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h
index 8d0a6280e9824..7cf993af8e1ea 100644
--- a/include/asm-generic/rwonce.h
+++ b/include/asm-generic/rwonce.h
@@ -25,6 +25,7 @@
 #include <linux/compiler_types.h>
 #include <linux/kasan-checks.h>
 #include <linux/kcsan-checks.h>
+#include <linux/kmsan-checks.h>
 
 /*
  * Yes, this permits 64-bit accesses on 32-bit architectures. These will
@@ -69,14 +70,14 @@ unsigned long __read_once_word_nocheck(const void *addr)
 
 /*
  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need to load a
- * word from memory atomically but without telling KASAN/KCSAN. This is
+ * word from memory atomically but without telling KASAN/KCSAN/KMSAN. This is
  * usually used by unwinding code when walking the stack of a running process.
  */
 #define READ_ONCE_NOCHECK(x)						\
 ({									\
 	compiletime_assert(sizeof(x) == sizeof(unsigned long),		\
 		"Unsupported access size for READ_ONCE_NOCHECK().");	\
-	(typeof(x))__read_once_word_nocheck(&(x));			\
+	kmsan_init((typeof(x))__read_once_word_nocheck(&(x)));		\
 })
 
 static __no_kasan_or_inline
diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h
index a6522a0c28df9..ecd8336190fc0 100644
--- a/include/linux/kmsan-checks.h
+++ b/include/linux/kmsan-checks.h
@@ -14,6 +14,44 @@
 
 #ifdef CONFIG_KMSAN
 
+/*
+ * Helper functions that mark the return value initialized.
+ * See mm/kmsan/annotations.c.
+ */
+u8 kmsan_init_1(u8 value);
+u16 kmsan_init_2(u16 value);
+u32 kmsan_init_4(u32 value);
+u64 kmsan_init_8(u64 value);
+
+static inline void *kmsan_init_ptr(void *ptr)
+{
+	return (void *)kmsan_init_8((u64)ptr);
+}
+
+static inline char kmsan_init_char(char value)
+{
+	return (u8)kmsan_init_1((u8)value);
+}
+
+#define __decl_kmsan_init_type(type, fn) unsigned type : fn, signed type : fn
+
+/**
+ * kmsan_init - Make the value initialized.
+ * @val: 1-, 2-, 4- or 8-byte integer that may be treated as uninitialized by
+ *       KMSAN.
+ *
+ * Return: value of @val that KMSAN treats as initialized.
+ */
+#define kmsan_init(val)                                                        \
+	(							\
+	(typeof(val))(_Generic((val),				\
+		__decl_kmsan_init_type(char, kmsan_init_1),	\
+		__decl_kmsan_init_type(short, kmsan_init_2),	\
+		__decl_kmsan_init_type(int, kmsan_init_4),	\
+		__decl_kmsan_init_type(long, kmsan_init_8),	\
+		char : kmsan_init_char,				\
+		void * : kmsan_init_ptr)(val)))
+
 /**
  * kmsan_poison_memory() - Mark the memory range as uninitialized.
  * @address: address to start with.
@@ -48,6 +86,8 @@ void kmsan_check_memory(const void *address, size_t size);
 
 #else
 
+#define kmsan_init(value) (value)
+
 static inline void kmsan_poison_memory(const void *address, size_t size,
 				       gfp_t flags)
 {
diff --git a/mm/kmsan/Makefile b/mm/kmsan/Makefile
index a80dde1de7048..73b705cbf75b9 100644
--- a/mm/kmsan/Makefile
+++ b/mm/kmsan/Makefile
@@ -1,9 +1,11 @@
-obj-y := core.o instrumentation.o hooks.o report.o shadow.o
+obj-y := core.o instrumentation.o hooks.o report.o shadow.o annotations.o
 
 KMSAN_SANITIZE := n
 KCOV_INSTRUMENT := n
 UBSAN_SANITIZE := n
 
+KMSAN_SANITIZE_kmsan_annotations.o := y
+
 # Disable instrumentation of KMSAN runtime with other tools.
 CC_FLAGS_KMSAN_RUNTIME := -fno-stack-protector
 CC_FLAGS_KMSAN_RUNTIME += $(call cc-option,-fno-conserve-stack)
@@ -11,6 +13,7 @@ CC_FLAGS_KMSAN_RUNTIME += -DDISABLE_BRANCH_PROFILING
 
 CFLAGS_REMOVE.o = $(CC_FLAGS_FTRACE)
 
+CFLAGS_annotations.o := $(CC_FLAGS_KMSAN_RUNTIME)
 CFLAGS_core.o := $(CC_FLAGS_KMSAN_RUNTIME)
 CFLAGS_hooks.o := $(CC_FLAGS_KMSAN_RUNTIME)
 CFLAGS_instrumentation.o := $(CC_FLAGS_KMSAN_RUNTIME)
diff --git a/mm/kmsan/annotations.c b/mm/kmsan/annotations.c
new file mode 100644
index 0000000000000..8ccde90bcd12b
--- /dev/null
+++ b/mm/kmsan/annotations.c
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KMSAN annotations.
+ *
+ * The kmsan_init_SIZE functions reside in a separate translation unit to
+ * prevent inlining them. Clang may inline functions marked with
+ * __no_sanitize_memory attribute into functions without it, which effectively
+ * results in ignoring the attribute.
+ *
+ * Copyright (C) 2017-2022 Google LLC
+ * Author: Alexander Potapenko <glider@google.com>
+ *
+ */
+
+#include <linux/export.h>
+#include <linux/kmsan-checks.h>
+
+#define DECLARE_KMSAN_INIT(size, t)                                            \
+	__no_sanitize_memory t kmsan_init_##size(t value)                      \
+	{                                                                      \
+		return value;                                                  \
+	}                                                                      \
+	EXPORT_SYMBOL(kmsan_init_##size)
+
+DECLARE_KMSAN_INIT(1, u8);
+DECLARE_KMSAN_INIT(2, u16);
+DECLARE_KMSAN_INIT(4, u32);
+DECLARE_KMSAN_INIT(8, u64);
-- 
2.36.0.rc2.479.g8af0fa9b8e-goog


  parent reply	other threads:[~2022-04-26 16:46 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26 16:42 [PATCH v3 00/46] Add KernelMemorySanitizer infrastructure Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 01/46] x86: add missing include to sparsemem.h Alexander Potapenko
2022-04-27 13:22   ` Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 02/46] stackdepot: reserve 5 extra bits in depot_stack_handle_t Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 03/46] kasan: common: adapt to the new prototype of __stack_depot_save() Alexander Potapenko
2022-04-27 12:47   ` Marco Elver
2022-04-26 16:42 ` [PATCH v3 04/46] instrumented.h: allow instrumenting both sides of copy_from_user() Alexander Potapenko
2022-04-27  4:36   ` kernel test robot
2022-06-01 17:09     ` Alexander Potapenko
2022-06-01 17:09       ` Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 05/46] x86: asm: instrument usercopy in get_user() and __put_user_size() Alexander Potapenko
2022-04-27  3:45   ` kernel test robot
2022-04-27  6:58   ` kernel test robot
2022-04-27  7:14   ` Arnd Bergmann
2022-06-02 11:20     ` Alexander Potapenko
2022-04-27 14:24   ` kernel test robot
2022-04-28  1:59   ` kernel test robot
2022-04-30 10:16   ` [x86] d216de19c8: kernel-selftests.x86.ioperm_32.fail kernel test robot
2022-04-30 10:16     ` kernel test robot
2022-04-26 16:42 ` [PATCH v3 06/46] asm-generic: instrument usercopy in cacheflush.h Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 07/46] kmsan: add ReST documentation Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 08/46] kmsan: introduce __no_sanitize_memory and __no_kmsan_checks Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 09/46] kmsan: mark noinstr as __no_sanitize_memory Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 10/46] x86: kmsan: pgtable: reduce vmalloc space Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 11/46] libnvdimm/pfn_dev: increase MAX_STRUCT_PAGE_SIZE Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 12/46] kmsan: add KMSAN runtime core Alexander Potapenko
2022-04-27  9:12   ` kernel test robot
2022-04-27 14:09   ` Marco Elver
2022-05-31 11:08     ` Alexander Potapenko
2022-04-26 16:42 ` Alexander Potapenko [this message]
2022-04-26 16:42 ` [PATCH v3 14/46] kmsan: disable instrumentation of unsupported common kernel code Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 15/46] MAINTAINERS: add entry for KMSAN Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 16/46] kmsan: mm: maintain KMSAN metadata for page operations Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 17/46] kmsan: mm: call KMSAN hooks from SLUB code Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 18/46] kmsan: handle task creation and exiting Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 19/46] kmsan: init: call KMSAN initialization routines Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 20/46] instrumented.h: add KMSAN support Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 21/46] kmsan: unpoison @tlb in arch_tlb_gather_mmu() Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 22/46] kmsan: add iomap support Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 23/46] Input: libps2: mark data received in __ps2_command() as initialized Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 24/46] kmsan: dma: unpoison DMA mappings Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 25/46] kmsan: virtio: check/unpoison scatterlist in vring_map_one_sg() Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 26/46] kmsan: handle memory sent to/from USB Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 27/46] kmsan: instrumentation.h: add instrumentation_begin_with_regs() Alexander Potapenko
2022-04-27 13:28   ` Thomas Gleixner
2022-05-16 11:49     ` Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 28/46] kmsan: entry: handle register passing from uninstrumented code Alexander Potapenko
2022-04-27 13:32   ` Thomas Gleixner
2022-05-02 17:00     ` Alexander Potapenko
2022-05-02 22:00       ` Thomas Gleixner
2022-05-05 18:04         ` Alexander Potapenko
2022-05-05 21:56           ` Thomas Gleixner
2022-05-06 14:52             ` Alexander Potapenko
2022-05-06 16:14               ` Thomas Gleixner
2022-05-06 17:41                 ` Alexander Potapenko
2022-05-06 18:41                   ` Thomas Gleixner
2022-05-09 16:50                     ` Alexander Potapenko
2022-05-09 16:51                       ` Alexander Potapenko
2022-05-09 19:09                       ` Thomas Gleixner
2022-05-12 12:24                         ` Alexander Potapenko
2022-05-12 16:17                           ` Thomas Gleixner
2022-05-12 16:48                             ` Thomas Gleixner
2022-06-01 11:27                               ` Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 29/46] kmsan: add tests for KMSAN Alexander Potapenko
2022-04-26 16:42 ` [PATCH v3 30/46] kmsan: disable strscpy() optimization under KMSAN Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 31/46] crypto: kmsan: disable accelerated configs " Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 32/46] kmsan: disable physical page merging in biovec Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 33/46] kmsan: block: skip bio block merging logic for KMSAN Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 34/46] kmsan: kcov: unpoison area->list in kcov_remote_area_put() Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 35/46] security: kmsan: fix interoperability with auto-initialization Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 36/46] objtool: kmsan: list KMSAN API functions as uaccess-safe Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 37/46] x86: kmsan: make READ_ONCE_TASK_STACK() return initialized values Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 38/46] x86: kmsan: disable instrumentation of unsupported code Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 39/46] x86: kmsan: skip shadow checks in __switch_to() Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 40/46] x86: kmsan: handle open-coded assembly in lib/iomem.c Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 41/46] x86: kmsan: use __msan_ string functions where possible Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 42/46] x86: kmsan: sync metadata pages on page fault Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 43/46] x86: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 44/46] x86: fs: kmsan: disable CONFIG_DCACHE_WORD_ACCESS Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 45/46] x86: kmsan: handle register passing from uninstrumented code Alexander Potapenko
2022-04-26 16:43 ` [PATCH v3 46/46] x86: kmsan: enable KMSAN builds for x86 Alexander Potapenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220426164315.625149-14-glider@google.com \
    --to=glider@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=arnd@arndb.de \
    --cc=axboe@kernel.dk \
    --cc=bp@alien8.de \
    --cc=cl@linux.com \
    --cc=dvyukov@google.com \
    --cc=edumazet@google.com \
    --cc=elver@google.com \
    --cc=gor@linux.ibm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=herbert@gondor.apana.org.au \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=iii@linux.ibm.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=keescook@chromium.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=mst@redhat.com \
    --cc=penberg@kernel.org \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rientjes@google.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=vbabka@suse.cz \
    --cc=vegard.nossum@oracle.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.