]> git.baikalelectronics.ru Git - kernel.git/commitdiff
instrumented.h: allow instrumenting both sides of copy_from_user()
authorAlexander Potapenko <glider@google.com>
Thu, 15 Sep 2022 15:03:37 +0000 (17:03 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 3 Oct 2022 21:03:18 +0000 (14:03 -0700)
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().

Link: https://lkml.kernel.org/r/20220915150417.722975-4-glider@google.com
Signed-off-by: Alexander Potapenko <glider@google.com>
Reviewed-by: Marco Elver <elver@google.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Konovalov <andreyknvl@google.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Eric Biggers <ebiggers@google.com>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vegard Nossum <vegard.nossum@oracle.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
arch/s390/lib/uaccess.c
include/linux/instrumented.h
include/linux/uaccess.h
lib/iov_iter.c
lib/usercopy.c

index d7b3b193d1088ca498d735f63e7a3b7bff744393..58033dfcb6d45f4b6d28e2bd8a865729d9baf572 100644 (file)
@@ -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);
index 42faebbaa202a978e2e004ce2d402f26f089ec2b..ee8f7d17d34f5cd26b6881d7d09bf195759e36a0 100644 (file)
@@ -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 */
index 47e5d374c7ebec86306b31d3d46665ca1c39f583..afb18f198843b4a508abda6efe9c575f02085c32 100644 (file)
 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);
index 4b7fce72e3e521348851c56f4a6335f8728c07b4..c3ca28ca68a65d70865d19accea5f07a9ff39b4b 100644 (file)
@@ -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,
index 7413dd300516e5405d92858a033b36c0eeb0399c..1505a52f23a01945ef30bb675d647a524e01680d 100644 (file)
@@ -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);