linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Potapenko <glider@google.com>
To: glider@google.com
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
	Alexei Starovoitov <ast@kernel.org>,
	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 v6 24/44] kmsan: handle memory sent to/from USB
Date: Mon,  5 Sep 2022 14:24:32 +0200	[thread overview]
Message-ID: <20220905122452.2258262-25-glider@google.com> (raw)
In-Reply-To: <20220905122452.2258262-1-glider@google.com>

Depending on the value of is_out kmsan_handle_urb() KMSAN either
marks the data copied to the kernel from a USB device as initialized,
or checks the data sent to the device for being initialized.

Signed-off-by: Alexander Potapenko <glider@google.com>

---
v2:
 -- move kmsan_handle_urb() implementation to this patch

v5:
 -- do not export KMSAN hooks that are not called from modules

v6:
 -- use <linux/kmsan.h> instead of <linux/kmsan-checks.h>

Link: https://linux-review.googlesource.com/id/Ifa67fb72015d4de14c30e971556f99fc8b2ee506
---
 drivers/usb/core/urb.c |  2 ++
 include/linux/kmsan.h  | 15 +++++++++++++++
 mm/kmsan/hooks.c       | 16 ++++++++++++++++
 3 files changed, 33 insertions(+)

diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 33d62d7e3929f..9f3c54032556e 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -8,6 +8,7 @@
 #include <linux/bitops.h>
 #include <linux/slab.h>
 #include <linux/log2.h>
+#include <linux/kmsan.h>
 #include <linux/usb.h>
 #include <linux/wait.h>
 #include <linux/usb/hcd.h>
@@ -426,6 +427,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
 			URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL |
 			URB_DMA_SG_COMBINED);
 	urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN);
+	kmsan_handle_urb(urb, is_out);
 
 	if (xfertype != USB_ENDPOINT_XFER_CONTROL &&
 			dev->state < USB_STATE_CONFIGURED)
diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h
index dac296da45c55..c473e0e21683c 100644
--- a/include/linux/kmsan.h
+++ b/include/linux/kmsan.h
@@ -18,6 +18,7 @@ struct page;
 struct kmem_cache;
 struct task_struct;
 struct scatterlist;
+struct urb;
 
 #ifdef CONFIG_KMSAN
 
@@ -203,6 +204,16 @@ void kmsan_handle_dma(struct page *page, size_t offset, size_t size,
 void kmsan_handle_dma_sg(struct scatterlist *sg, int nents,
 			 enum dma_data_direction dir);
 
+/**
+ * kmsan_handle_urb() - Handle a USB data transfer.
+ * @urb:    struct urb pointer.
+ * @is_out: data transfer direction (true means output to hardware).
+ *
+ * If @is_out is true, KMSAN checks the transfer buffer of @urb. Otherwise,
+ * KMSAN initializes the transfer buffer.
+ */
+void kmsan_handle_urb(const struct urb *urb, bool is_out);
+
 #else
 
 static inline void kmsan_init_shadow(void)
@@ -295,6 +306,10 @@ static inline void kmsan_handle_dma_sg(struct scatterlist *sg, int nents,
 {
 }
 
+static inline void kmsan_handle_urb(const struct urb *urb, bool is_out)
+{
+}
+
 #endif
 
 #endif /* _LINUX_KMSAN_H */
diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c
index 563c09443a37a..79d7e73e2cfd8 100644
--- a/mm/kmsan/hooks.c
+++ b/mm/kmsan/hooks.c
@@ -18,6 +18,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/usb.h>
 
 #include "../internal.h"
 #include "../slab.h"
@@ -245,6 +246,21 @@ void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy,
 }
 EXPORT_SYMBOL(kmsan_copy_to_user);
 
+/* Helper function to check an URB. */
+void kmsan_handle_urb(const struct urb *urb, bool is_out)
+{
+	if (!urb)
+		return;
+	if (is_out)
+		kmsan_internal_check_memory(urb->transfer_buffer,
+					    urb->transfer_buffer_length,
+					    /*user_addr*/ 0, REASON_SUBMIT_URB);
+	else
+		kmsan_internal_unpoison_memory(urb->transfer_buffer,
+					       urb->transfer_buffer_length,
+					       /*checked*/ false);
+}
+
 static void kmsan_handle_dma_page(const void *addr, size_t size,
 				  enum dma_data_direction dir)
 {
-- 
2.37.2.789.g6183377224-goog


  parent reply	other threads:[~2022-09-05 12:30 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-05 12:24 [PATCH v6 00/44] Add KernelMemorySanitizer infrastructure Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 01/44] x86: add missing include to sparsemem.h Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 02/44] stackdepot: reserve 5 extra bits in depot_stack_handle_t Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 03/44] instrumented.h: allow instrumenting both sides of copy_from_user() Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 04/44] x86: asm: instrument usercopy in get_user() and put_user() Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 05/44] asm-generic: instrument usercopy in cacheflush.h Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 06/44] kmsan: add ReST documentation Alexander Potapenko
2022-09-06  3:10   ` [PATCH v6 6/44] " Bagas Sanjaya
2022-11-07 14:24     ` Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 07/44] kmsan: introduce __no_sanitize_memory and __no_kmsan_checks Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 08/44] kmsan: mark noinstr as __no_sanitize_memory Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 09/44] x86: kmsan: pgtable: reduce vmalloc space Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 10/44] libnvdimm/pfn_dev: increase MAX_STRUCT_PAGE_SIZE Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 11/44] kmsan: add KMSAN runtime core Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 12/44] kmsan: disable instrumentation of unsupported common kernel code Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 13/44] MAINTAINERS: add entry for KMSAN Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 14/44] mm: kmsan: maintain KMSAN metadata for page operations Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 15/44] mm: kmsan: call KMSAN hooks from SLUB code Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 16/44] kmsan: handle task creation and exiting Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 17/44] init: kmsan: call KMSAN initialization routines Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 18/44] instrumented.h: add KMSAN support Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 19/44] kmsan: unpoison @tlb in arch_tlb_gather_mmu() Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 20/44] kmsan: add iomap support Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 21/44] Input: libps2: mark data received in __ps2_command() as initialized Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 22/44] dma: kmsan: unpoison DMA mappings Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 23/44] virtio: kmsan: check/unpoison scatterlist in vring_map_one_sg() Alexander Potapenko
2022-09-05 12:24 ` Alexander Potapenko [this message]
2022-09-05 12:24 ` [PATCH v6 25/44] kmsan: add tests for KMSAN Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 26/44] kmsan: disable strscpy() optimization under KMSAN Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 27/44] crypto: kmsan: disable accelerated configs " Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 28/44] kmsan: disable physical page merging in biovec Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 29/44] block: kmsan: skip bio block merging logic for KMSAN Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 30/44] kcov: kmsan: unpoison area->list in kcov_remote_area_put() Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 31/44] security: kmsan: fix interoperability with auto-initialization Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 32/44] objtool: kmsan: list KMSAN API functions as uaccess-safe Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 33/44] x86: kmsan: disable instrumentation of unsupported code Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 34/44] x86: kmsan: skip shadow checks in __switch_to() Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 35/44] x86: kmsan: handle open-coded assembly in lib/iomem.c Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 36/44] x86: kmsan: use __msan_ string functions where possible Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 37/44] x86: kmsan: sync metadata pages on page fault Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 38/44] x86: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 39/44] x86: fs: kmsan: disable CONFIG_DCACHE_WORD_ACCESS Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 40/44] x86: kmsan: don't instrument stack walking functions Alexander Potapenko
2022-09-09  8:57   ` Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 41/44] entry: kmsan: introduce kmsan_unpoison_entry_regs() Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 42/44] bpf: kmsan: initialize BPF registers with zeroes Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 43/44] mm: fs: initialize fsdata passed to write_begin/write_end interface Alexander Potapenko
2022-09-05 12:24 ` [PATCH v6 44/44] 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=20220905122452.2258262-25-glider@google.com \
    --to=glider@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=arnd@arndb.de \
    --cc=ast@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).