public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] x86: add failure injection to get/put/clear_user
@ 2020-10-23  8:16 Alexander Potapenko
  2020-10-23  8:19 ` Alexander Potapenko
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Potapenko @ 2020-10-23  8:16 UTC (permalink / raw)
  To: akpm, bp, mingo, corbet, tglx, arnd
  Cc: akinobu.mita, hpa, viro, glider, andreyknvl, dvyukov, elver,
	linux-doc, linux-kernel, linux-arch, x86, albert.linde,
	Albert van der Linde

From: Albert van der Linde <alinde@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.mita@gmail.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Albert van der Linde <alinde@google.com>
Signed-off-by: Alexander Potapenko <glider@google.com>

---
v2:
 - no significant changes

v3:
 - no changes

v4:
 - instrument the new out-of-line implementations of get_user()/put_user()
 - fix a minor checkpatch warning in the inline assembly

---
---
 arch/x86/include/asm/uaccess.h | 36 ++++++++++++++++++++++------------
 arch/x86/lib/usercopy_64.c     |  3 +++
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index f13659523108..7041ebc48b75 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>
@@ -126,11 +127,16 @@ extern int __get_user_bad(void);
 	int __ret_gu;							\
 	register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);		\
 	__chk_user_ptr(ptr);						\
-	asm volatile("call __" #fn "_%P4"				\
-		     : "=a" (__ret_gu), "=r" (__val_gu),		\
-			ASM_CALL_CONSTRAINT				\
-		     : "0" (ptr), "i" (sizeof(*(ptr))));		\
-	(x) = (__force __typeof__(*(ptr))) __val_gu;			\
+	if (should_fail_usercopy()) {					\
+		(x) = 0;						\
+		__ret_gu = -EFAULT;					\
+	} else {							\
+		asm volatile("call __" #fn "_%P4"			\
+			     : "=a" (__ret_gu), "=r" (__val_gu),	\
+				ASM_CALL_CONSTRAINT			\
+			     : "0" (ptr), "i" (sizeof(*(ptr))));	\
+		(x) = (__force __typeof__(*(ptr))) __val_gu;		\
+	}								\
 	__builtin_expect(__ret_gu, 0);					\
 })
 
@@ -213,14 +219,18 @@ extern void __put_user_nocheck_8(void);
 	int __ret_pu;							\
 	register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);		\
 	__chk_user_ptr(ptr);						\
-	__val_pu = (x);							\
-	asm volatile("call __" #fn "_%P[size]"				\
-		     : "=c" (__ret_pu),					\
-			ASM_CALL_CONSTRAINT				\
-		     : "0" (ptr),					\
-		       "r" (__val_pu),					\
-		       [size] "i" (sizeof(*(ptr)))			\
-		     :"ebx");						\
+	if (unlikely(should_fail_usercopy())) {				\
+		__ret_pu = -EFAULT;					\
+	} else {							\
+		__val_pu = (x);						\
+		asm volatile("call __" #fn "_%P[size]"			\
+			     : "=c" (__ret_pu),				\
+				ASM_CALL_CONSTRAINT			\
+			     : "0" (ptr),				\
+			       "r" (__val_pu),				\
+			       [size] "i" (sizeof(*(ptr)))		\
+			     : "ebx");					\
+	}								\
 	__builtin_expect(__ret_pu, 0);					\
 })
 
diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c
index 508c81e97ab1..5617b3864586 100644
--- a/arch/x86/lib/usercopy_64.c
+++ b/arch/x86/lib/usercopy_64.c
@@ -7,6 +7,7 @@
  * Copyright 2002 Andi Kleen <ak@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.29.0.rc2.309.g374f81d7ae-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v4] x86: add failure injection to get/put/clear_user
  2020-10-23  8:16 [PATCH v4] x86: add failure injection to get/put/clear_user Alexander Potapenko
@ 2020-10-23  8:19 ` Alexander Potapenko
  0 siblings, 0 replies; 2+ messages in thread
From: Alexander Potapenko @ 2020-10-23  8:19 UTC (permalink / raw)
  To: Andrew Morton, Borislav Petkov, Ingo Molnar, Jonathan Corbet,
	Thomas Gleixner, Arnd Bergmann
  Cc: Akinobu Mita, H. Peter Anvin, Al Viro, Andrey Konovalov,
	Dmitriy Vyukov, Marco Elver, open list:DOCUMENTATION, LKML,
	Linux-Arch, the arch/x86 maintainers, Albert Linde

(Albert's @google.com address is gone, removing it from CC list)

On Fri, Oct 23, 2020 at 10:16 AM Alexander Potapenko <glider@google.com> wrote:
>
> From: Albert van der Linde <alinde@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.mita@gmail.com>
> Reviewed-by: Alexander Potapenko <glider@google.com>
> Signed-off-by: Albert van der Linde <alinde@google.com>
> Signed-off-by: Alexander Potapenko <glider@google.com>
>
> ---
> v2:
>  - no significant changes
>
> v3:
>  - no changes
>
> v4:
>  - instrument the new out-of-line implementations of get_user()/put_user()
>  - fix a minor checkpatch warning in the inline assembly
>
> ---
> ---
>  arch/x86/include/asm/uaccess.h | 36 ++++++++++++++++++++++------------
>  arch/x86/lib/usercopy_64.c     |  3 +++
>  2 files changed, 26 insertions(+), 13 deletions(-)
>
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> index f13659523108..7041ebc48b75 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>
> @@ -126,11 +127,16 @@ extern int __get_user_bad(void);
>         int __ret_gu;                                                   \
>         register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);            \
>         __chk_user_ptr(ptr);                                            \
> -       asm volatile("call __" #fn "_%P4"                               \
> -                    : "=a" (__ret_gu), "=r" (__val_gu),                \
> -                       ASM_CALL_CONSTRAINT                             \
> -                    : "0" (ptr), "i" (sizeof(*(ptr))));                \
> -       (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
> +       if (should_fail_usercopy()) {                                   \
> +               (x) = 0;                                                \
> +               __ret_gu = -EFAULT;                                     \
> +       } else {                                                        \
> +               asm volatile("call __" #fn "_%P4"                       \
> +                            : "=a" (__ret_gu), "=r" (__val_gu),        \
> +                               ASM_CALL_CONSTRAINT                     \
> +                            : "0" (ptr), "i" (sizeof(*(ptr))));        \
> +               (x) = (__force __typeof__(*(ptr))) __val_gu;            \
> +       }                                                               \
>         __builtin_expect(__ret_gu, 0);                                  \
>  })
>
> @@ -213,14 +219,18 @@ extern void __put_user_nocheck_8(void);
>         int __ret_pu;                                                   \
>         register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
>         __chk_user_ptr(ptr);                                            \
> -       __val_pu = (x);                                                 \
> -       asm volatile("call __" #fn "_%P[size]"                          \
> -                    : "=c" (__ret_pu),                                 \
> -                       ASM_CALL_CONSTRAINT                             \
> -                    : "0" (ptr),                                       \
> -                      "r" (__val_pu),                                  \
> -                      [size] "i" (sizeof(*(ptr)))                      \
> -                    :"ebx");                                           \
> +       if (unlikely(should_fail_usercopy())) {                         \
> +               __ret_pu = -EFAULT;                                     \
> +       } else {                                                        \
> +               __val_pu = (x);                                         \
> +               asm volatile("call __" #fn "_%P[size]"                  \
> +                            : "=c" (__ret_pu),                         \
> +                               ASM_CALL_CONSTRAINT                     \
> +                            : "0" (ptr),                               \
> +                              "r" (__val_pu),                          \
> +                              [size] "i" (sizeof(*(ptr)))              \
> +                            : "ebx");                                  \
> +       }                                                               \
>         __builtin_expect(__ret_pu, 0);                                  \
>  })
>
> diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c
> index 508c81e97ab1..5617b3864586 100644
> --- a/arch/x86/lib/usercopy_64.c
> +++ b/arch/x86/lib/usercopy_64.c
> @@ -7,6 +7,7 @@
>   * Copyright 2002 Andi Kleen <ak@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.29.0.rc2.309.g374f81d7ae-goog
>


-- 
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-23  8:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-23  8:16 [PATCH v4] x86: add failure injection to get/put/clear_user Alexander Potapenko
2020-10-23  8:19 ` Alexander Potapenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox