From: Albert van der Linde <ali...@google.com>

To test fault-tolerance of user memory acceses in x86, add support for
fault injection.

Make both put_user() and get_user() fail with -EFAULT, and clear_user()
fail by not clearing any bytes.

Reviewed-by: Akinobu Mita <akinobu.m...@gmail.com>
Reviewed-by: Alexander Potapenko <gli...@google.com>
Signed-off-by: Albert van der Linde <ali...@google.com>
---
v2:
 - no significant changes

v3:
 - no changes
---
 arch/x86/include/asm/uaccess.h | 68 +++++++++++++++++++---------------
 arch/x86/lib/usercopy_64.c     |  3 ++
 2 files changed, 42 insertions(+), 29 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index ecefaffd15d4..004eeee2199a 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -5,6 +5,7 @@
  * User space memory access functions
  */
 #include <linux/compiler.h>
+#include <linux/fault-inject-usercopy.h>
 #include <linux/kasan-checks.h>
 #include <linux/string.h>
 #include <asm/asm.h>
@@ -175,11 +176,16 @@ extern int __get_user_bad(void);
        register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);            \
        __chk_user_ptr(ptr);                                            \
        might_fault();                                                  \
-       asm volatile("call __get_user_%P4"                              \
-                    : "=a" (__ret_gu), "=r" (__val_gu),                \
+       if (should_fail_usercopy()) {                                   \
+               (x) = 0;                                                \
+               __ret_gu = -EFAULT;                                     \
+       } else {                                                        \
+               asm volatile("call __get_user_%P4"                      \
+                       : "=a" (__ret_gu), "=r" (__val_gu),             \
                        ASM_CALL_CONSTRAINT                             \
-                    : "0" (ptr), "i" (sizeof(*(ptr))));                \
-       (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
+                       : "0" (ptr), "i" (sizeof(*(ptr))));             \
+               (x) = (__force __typeof__(*(ptr))) __val_gu;            \
+       }                                                               \
        __builtin_expect(__ret_gu, 0);                                  \
 })
 
@@ -236,31 +242,35 @@ extern void __put_user_8(void);
  *
  * Return: zero on success, or -EFAULT on error.
  */
-#define put_user(x, ptr)                                       \
-({                                                             \
-       int __ret_pu;                                           \
-       __typeof__(*(ptr)) __pu_val;                            \
-       __chk_user_ptr(ptr);                                    \
-       might_fault();                                          \
-       __pu_val = x;                                           \
-       switch (sizeof(*(ptr))) {                               \
-       case 1:                                                 \
-               __put_user_x(1, __pu_val, ptr, __ret_pu);       \
-               break;                                          \
-       case 2:                                                 \
-               __put_user_x(2, __pu_val, ptr, __ret_pu);       \
-               break;                                          \
-       case 4:                                                 \
-               __put_user_x(4, __pu_val, ptr, __ret_pu);       \
-               break;                                          \
-       case 8:                                                 \
-               __put_user_x8(__pu_val, ptr, __ret_pu);         \
-               break;                                          \
-       default:                                                \
-               __put_user_x(X, __pu_val, ptr, __ret_pu);       \
-               break;                                          \
-       }                                                       \
-       __builtin_expect(__ret_pu, 0);                          \
+#define put_user(x, ptr)                                               \
+({                                                                     \
+       int __ret_pu;                                                   \
+       __typeof__(*(ptr)) __pu_val;                                    \
+       __chk_user_ptr(ptr);                                            \
+       might_fault();                                                  \
+       __pu_val = x;                                                   \
+       if (should_fail_usercopy()) {                                   \
+               __ret_pu = -EFAULT;                                     \
+       } else {                                                        \
+               switch (sizeof(*(ptr))) {                               \
+               case 1:                                                 \
+                       __put_user_x(1, __pu_val, ptr, __ret_pu);       \
+                       break;                                          \
+               case 2:                                                 \
+                       __put_user_x(2, __pu_val, ptr, __ret_pu);       \
+                       break;                                          \
+               case 4:                                                 \
+                       __put_user_x(4, __pu_val, ptr, __ret_pu);       \
+                       break;                                          \
+               case 8:                                                 \
+                       __put_user_x8(__pu_val, ptr, __ret_pu);         \
+                       break;                                          \
+               default:                                                \
+                       __put_user_x(X, __pu_val, ptr, __ret_pu);       \
+                       break;                                          \
+               }                                                       \
+       }                                                               \
+       __builtin_expect(__ret_pu, 0);                                  \
 })
 
 #define __put_user_size(x, ptr, size, label)                           \
diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c
index b0dfac3d3df7..7747cda5780d 100644
--- a/arch/x86/lib/usercopy_64.c
+++ b/arch/x86/lib/usercopy_64.c
@@ -7,6 +7,7 @@
  * Copyright 2002 Andi Kleen <a...@suse.de>
  */
 #include <linux/export.h>
+#include <linux/fault-inject-usercopy.h>
 #include <linux/uaccess.h>
 #include <linux/highmem.h>
 
@@ -50,6 +51,8 @@ EXPORT_SYMBOL(__clear_user);
 
 unsigned long clear_user(void __user *to, unsigned long n)
 {
+       if (should_fail_usercopy())
+               return n;
        if (access_ok(to, n))
                return __clear_user(to, n);
        return n;
-- 
2.28.0.402.g5ffc5be6b7-goog

Reply via email to