linux-mm.kvack.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 Biggers <ebiggers@kernel.org>,
	 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>,
	 Stephen Rothwell <sfr@canb.auug.org.au>,
	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 v7 03/43] instrumented.h: allow instrumenting both sides of copy_from_user()
Date: Thu, 15 Sep 2022 17:03:37 +0200	[thread overview]
Message-ID: <20220915150417.722975-4-glider@google.com> (raw)
In-Reply-To: <20220915150417.722975-1-glider@google.com>

Introduce instrument_copy_from_user_before() and
instrument_copy_from_user_after() hooks to be invoked before and after
the call to copy_from_user().

KASAN and KCSAN will be only using instrument_copy_from_user_before(),
but for KMSAN we'll need to insert code after copy_from_user().

Signed-off-by: Alexander Potapenko <glider@google.com>
Reviewed-by: Marco Elver <elver@google.com>

---
v4:
 -- fix _copy_from_user_key() in arch/s390/lib/uaccess.c (Reported-by:
    kernel test robot <lkp@intel.com>)

Link: https://linux-review.googlesource.com/id/I855034578f0b0f126734cbd734fb4ae1d3a6af99
---
 arch/s390/lib/uaccess.c      |  3 ++-
 include/linux/instrumented.h | 21 +++++++++++++++++++--
 include/linux/uaccess.h      | 19 ++++++++++++++-----
 lib/iov_iter.c               |  9 ++++++---
 lib/usercopy.c               |  3 ++-
 5 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/arch/s390/lib/uaccess.c b/arch/s390/lib/uaccess.c
index d7b3b193d1088..58033dfcb6d45 100644
--- a/arch/s390/lib/uaccess.c
+++ b/arch/s390/lib/uaccess.c
@@ -81,8 +81,9 @@ unsigned long _copy_from_user_key(void *to, const void __user *from,
 
 	might_fault();
 	if (!should_fail_usercopy()) {
-		instrument_copy_from_user(to, from, n);
+		instrument_copy_from_user_before(to, from, n);
 		res = raw_copy_from_user_key(to, from, n, key);
+		instrument_copy_from_user_after(to, from, n, res);
 	}
 	if (unlikely(res))
 		memset(to + (n - res), 0, res);
diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h
index 42faebbaa202a..ee8f7d17d34f5 100644
--- a/include/linux/instrumented.h
+++ b/include/linux/instrumented.h
@@ -120,7 +120,7 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
 }
 
 /**
- * instrument_copy_from_user - instrument writes of copy_from_user
+ * instrument_copy_from_user_before - add instrumentation before copy_from_user
  *
  * Instrument writes to kernel memory, that are due to copy_from_user (and
  * variants). The instrumentation should be inserted before the accesses.
@@ -130,10 +130,27 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
  * @n number of bytes to copy
  */
 static __always_inline void
-instrument_copy_from_user(const void *to, const void __user *from, unsigned long n)
+instrument_copy_from_user_before(const void *to, const void __user *from, unsigned long n)
 {
 	kasan_check_write(to, n);
 	kcsan_check_write(to, n);
 }
 
+/**
+ * instrument_copy_from_user_after - add instrumentation after copy_from_user
+ *
+ * Instrument writes to kernel memory, that are due to copy_from_user (and
+ * variants). The instrumentation should be inserted after the accesses.
+ *
+ * @to destination address
+ * @from source address
+ * @n number of bytes to copy
+ * @left number of bytes not copied (as returned by copy_from_user)
+ */
+static __always_inline void
+instrument_copy_from_user_after(const void *to, const void __user *from,
+				unsigned long n, unsigned long left)
+{
+}
+
 #endif /* _LINUX_INSTRUMENTED_H */
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 47e5d374c7ebe..afb18f198843b 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -58,20 +58,28 @@
 static __always_inline __must_check unsigned long
 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
 {
-	instrument_copy_from_user(to, from, n);
+	unsigned long res;
+
+	instrument_copy_from_user_before(to, from, n);
 	check_object_size(to, n, false);
-	return raw_copy_from_user(to, from, n);
+	res = raw_copy_from_user(to, from, n);
+	instrument_copy_from_user_after(to, from, n, res);
+	return res;
 }
 
 static __always_inline __must_check unsigned long
 __copy_from_user(void *to, const void __user *from, unsigned long n)
 {
+	unsigned long res;
+
 	might_fault();
+	instrument_copy_from_user_before(to, from, n);
 	if (should_fail_usercopy())
 		return n;
-	instrument_copy_from_user(to, from, n);
 	check_object_size(to, n, false);
-	return raw_copy_from_user(to, from, n);
+	res = raw_copy_from_user(to, from, n);
+	instrument_copy_from_user_after(to, from, n, res);
+	return res;
 }
 
 /**
@@ -115,8 +123,9 @@ _copy_from_user(void *to, const void __user *from, unsigned long n)
 	unsigned long res = n;
 	might_fault();
 	if (!should_fail_usercopy() && likely(access_ok(from, n))) {
-		instrument_copy_from_user(to, from, n);
+		instrument_copy_from_user_before(to, from, n);
 		res = raw_copy_from_user(to, from, n);
+		instrument_copy_from_user_after(to, from, n, res);
 	}
 	if (unlikely(res))
 		memset(to + (n - res), 0, res);
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 4b7fce72e3e52..c3ca28ca68a65 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -174,13 +174,16 @@ static int copyout(void __user *to, const void *from, size_t n)
 
 static int copyin(void *to, const void __user *from, size_t n)
 {
+	size_t res = n;
+
 	if (should_fail_usercopy())
 		return n;
 	if (access_ok(from, n)) {
-		instrument_copy_from_user(to, from, n);
-		n = raw_copy_from_user(to, from, n);
+		instrument_copy_from_user_before(to, from, n);
+		res = raw_copy_from_user(to, from, n);
+		instrument_copy_from_user_after(to, from, n, res);
 	}
-	return n;
+	return res;
 }
 
 static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe,
diff --git a/lib/usercopy.c b/lib/usercopy.c
index 7413dd300516e..1505a52f23a01 100644
--- a/lib/usercopy.c
+++ b/lib/usercopy.c
@@ -12,8 +12,9 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n
 	unsigned long res = n;
 	might_fault();
 	if (!should_fail_usercopy() && likely(access_ok(from, n))) {
-		instrument_copy_from_user(to, from, n);
+		instrument_copy_from_user_before(to, from, n);
 		res = raw_copy_from_user(to, from, n);
+		instrument_copy_from_user_after(to, from, n, res);
 	}
 	if (unlikely(res))
 		memset(to + (n - res), 0, res);
-- 
2.37.2.789.g6183377224-goog



  parent reply	other threads:[~2022-09-15 15:04 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-15 15:03 [PATCH v7 00/43] Add KernelMemorySanitizer infrastructure Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 01/43] x86: add missing include to sparsemem.h Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 02/43] stackdepot: reserve 5 extra bits in depot_stack_handle_t Alexander Potapenko
2022-09-15 15:03 ` Alexander Potapenko [this message]
2022-09-15 15:03 ` [PATCH v7 04/43] x86: asm: instrument usercopy in get_user() and put_user() Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 05/43] asm-generic: instrument usercopy in cacheflush.h Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 06/43] kmsan: add ReST documentation Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 07/43] kmsan: introduce __no_sanitize_memory and __no_kmsan_checks Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 08/43] kmsan: mark noinstr as __no_sanitize_memory Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 09/43] x86: kmsan: pgtable: reduce vmalloc space Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 10/43] libnvdimm/pfn_dev: increase MAX_STRUCT_PAGE_SIZE Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 11/43] kmsan: add KMSAN runtime core Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 12/43] kmsan: disable instrumentation of unsupported common kernel code Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 13/43] MAINTAINERS: add entry for KMSAN Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 14/43] mm: kmsan: maintain KMSAN metadata for page operations Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 15/43] mm: kmsan: call KMSAN hooks from SLUB code Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 16/43] kmsan: handle task creation and exiting Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 17/43] init: kmsan: call KMSAN initialization routines Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 18/43] instrumented.h: add KMSAN support Alexander Potapenko
     [not found]   ` <20221019173620.10167-1-youling257@gmail.com>
2022-10-19 17:37     ` Fwd: " youling 257
2022-10-19 17:58       ` Marco Elver
2022-10-19 19:29         ` youling 257
2022-10-19 20:00           ` Marco Elver
2022-10-19 20:07             ` youling 257
2022-10-19 21:36               ` Marco Elver
2022-10-20  5:53                 ` youling 257
2022-10-20 18:14                 ` Alexander Potapenko
2022-10-21  5:55                   ` youling 257
2022-10-21  6:16                     ` Marco Elver
2022-10-21  6:39                       ` youling 257
2022-10-21  7:37                         ` Marco Elver
2022-10-21 15:19                           ` youling 257
2022-10-21 17:02                             ` Alexander Potapenko
2022-10-21 17:21                               ` Kees Cook
2022-10-21 20:37                             ` Alexander Potapenko
2022-10-22  6:24                               ` youling 257
2022-10-19 21:44               ` Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 19/43] kmsan: add iomap support Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 20/43] Input: libps2: mark data received in __ps2_command() as initialized Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 21/43] dma: kmsan: unpoison DMA mappings Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 22/43] virtio: kmsan: check/unpoison scatterlist in vring_map_one_sg() Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 23/43] kmsan: handle memory sent to/from USB Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 24/43] kmsan: add tests for KMSAN Alexander Potapenko
2022-09-15 15:03 ` [PATCH v7 25/43] kmsan: disable strscpy() optimization under KMSAN Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 26/43] crypto: kmsan: disable accelerated configs " Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 27/43] kmsan: disable physical page merging in biovec Alexander Potapenko
2022-09-15 20:58   ` Andrew Morton
2022-09-16  9:12     ` Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 28/43] block: kmsan: skip bio block merging logic for KMSAN Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 29/43] kcov: kmsan: unpoison area->list in kcov_remote_area_put() Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 30/43] security: kmsan: fix interoperability with auto-initialization Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 31/43] objtool: kmsan: list KMSAN API functions as uaccess-safe Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 32/43] x86: kmsan: disable instrumentation of unsupported code Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 33/43] x86: kmsan: skip shadow checks in __switch_to() Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 34/43] x86: kmsan: handle open-coded assembly in lib/iomem.c Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 35/43] x86: kmsan: use __msan_ string functions where possible Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 36/43] x86: kmsan: sync metadata pages on page fault Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 37/43] x86: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 38/43] x86: fs: kmsan: disable CONFIG_DCACHE_WORD_ACCESS Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 39/43] x86: kmsan: don't instrument stack walking functions Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 40/43] entry: kmsan: introduce kmsan_unpoison_entry_regs() Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 41/43] bpf: kmsan: initialize BPF registers with zeroes Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 42/43] mm: fs: initialize fsdata passed to write_begin/write_end interface Alexander Potapenko
2022-09-15 15:04 ` [PATCH v7 43/43] x86: kmsan: enable KMSAN builds for x86 Alexander Potapenko
2022-09-15 21:05 ` [PATCH v7 00/43] Add KernelMemorySanitizer infrastructure Andrew Morton
2022-09-15 21:07   ` Andrew Morton

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=20220915150417.722975-4-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=ebiggers@kernel.org \
    --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=sfr@canb.auug.org.au \
    --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).