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>,
	linux-mm@kvack.org, linux-arch@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 21/48] instrumented.h: add KMSAN support
Date: Tue, 29 Mar 2022 14:39:50 +0200	[thread overview]
Message-ID: <20220329124017.737571-22-glider@google.com> (raw)
In-Reply-To: <20220329124017.737571-1-glider@google.com>

To avoid false positives, KMSAN needs to unpoison the data copied from
the userspace. To detect infoleaks - check the memory buffer passed to
copy_to_user().

Signed-off-by: Alexander Potapenko <glider@google.com>
---
v2:
 -- move implementation of kmsan_copy_to_user() here

Link: https://linux-review.googlesource.com/id/I43e93b9c02709e6be8d222342f1b044ac8bdbaaf
---
 include/linux/instrumented.h |  5 ++++-
 include/linux/kmsan-checks.h | 19 ++++++++++++++++++
 mm/kmsan/hooks.c             | 38 ++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h
index ee8f7d17d34f5..c73c1b19e9227 100644
--- a/include/linux/instrumented.h
+++ b/include/linux/instrumented.h
@@ -2,7 +2,7 @@
 
 /*
  * This header provides generic wrappers for memory access instrumentation that
- * the compiler cannot emit for: KASAN, KCSAN.
+ * the compiler cannot emit for: KASAN, KCSAN, KMSAN.
  */
 #ifndef _LINUX_INSTRUMENTED_H
 #define _LINUX_INSTRUMENTED_H
@@ -10,6 +10,7 @@
 #include <linux/compiler.h>
 #include <linux/kasan-checks.h>
 #include <linux/kcsan-checks.h>
+#include <linux/kmsan-checks.h>
 #include <linux/types.h>
 
 /**
@@ -117,6 +118,7 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
 {
 	kasan_check_read(from, n);
 	kcsan_check_read(from, n);
+	kmsan_copy_to_user(to, from, n, 0);
 }
 
 /**
@@ -151,6 +153,7 @@ static __always_inline void
 instrument_copy_from_user_after(const void *to, const void __user *from,
 				unsigned long n, unsigned long left)
 {
+	kmsan_unpoison_memory(to, n - left);
 }
 
 #endif /* _LINUX_INSTRUMENTED_H */
diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h
index ecd8336190fc0..aabaf1ba7c251 100644
--- a/include/linux/kmsan-checks.h
+++ b/include/linux/kmsan-checks.h
@@ -84,6 +84,21 @@ void kmsan_unpoison_memory(const void *address, size_t size);
  */
 void kmsan_check_memory(const void *address, size_t size);
 
+/**
+ * kmsan_copy_to_user() - Notify KMSAN about a data transfer to userspace.
+ * @to:      destination address in the userspace.
+ * @from:    source address in the kernel.
+ * @to_copy: number of bytes to copy.
+ * @left:    number of bytes not copied.
+ *
+ * If this is a real userspace data transfer, KMSAN checks the bytes that were
+ * actually copied to ensure there was no information leak. If @to belongs to
+ * the kernel space (which is possible for compat syscalls), KMSAN just copies
+ * the metadata.
+ */
+void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy,
+			size_t left);
+
 #else
 
 #define kmsan_init(value) (value)
@@ -98,6 +113,10 @@ static inline void kmsan_unpoison_memory(const void *address, size_t size)
 static inline void kmsan_check_memory(const void *address, size_t size)
 {
 }
+static inline void kmsan_copy_to_user(void __user *to, const void *from,
+				      size_t to_copy, size_t left)
+{
+}
 
 #endif
 
diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c
index a13e15ef2bfd5..365eedcb08953 100644
--- a/mm/kmsan/hooks.c
+++ b/mm/kmsan/hooks.c
@@ -212,6 +212,44 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end)
 }
 EXPORT_SYMBOL(kmsan_iounmap_page_range);
 
+void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy,
+			size_t left)
+{
+	unsigned long ua_flags;
+
+	if (!kmsan_enabled || kmsan_in_runtime())
+		return;
+	/*
+	 * At this point we've copied the memory already. It's hard to check it
+	 * before copying, as the size of actually copied buffer is unknown.
+	 */
+
+	/* copy_to_user() may copy zero bytes. No need to check. */
+	if (!to_copy)
+		return;
+	/* Or maybe copy_to_user() failed to copy anything. */
+	if (to_copy <= left)
+		return;
+
+	ua_flags = user_access_save();
+	if ((u64)to < TASK_SIZE) {
+		/* This is a user memory access, check it. */
+		kmsan_internal_check_memory((void *)from, to_copy - left, to,
+					    REASON_COPY_TO_USER);
+		user_access_restore(ua_flags);
+		return;
+	}
+	/* Otherwise this is a kernel memory access. This happens when a compat
+	 * syscall passes an argument allocated on the kernel stack to a real
+	 * syscall.
+	 * Don't check anything, just copy the shadow of the copied bytes.
+	 */
+	kmsan_internal_memmove_metadata((void *)to, (void *)from,
+					to_copy - left);
+	user_access_restore(ua_flags);
+}
+EXPORT_SYMBOL(kmsan_copy_to_user);
+
 /* Functions from kmsan-checks.h follow. */
 void kmsan_poison_memory(const void *address, size_t size, gfp_t flags)
 {
-- 
2.35.1.1021.g381101b075-goog


  parent reply	other threads:[~2022-03-29 12:43 UTC|newest]

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