Skip to contentSkip to conversation

x86: add failure injection to get/put/clear_user

PATCH Superseded Alexander Potapenko 2 participants 3 messages Oct 23, 2020

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86: add failure injection to get/put/clear_user

From: Albert van der Linde <[email protected]>

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.

---
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(-)
arch/x86/include/asm/uaccess.h +23 -13
index f13659523108..7041ebc48b75 100644
@@ -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);					\
 })
 
arch/x86/lib/usercopy_64.c +3 -0
index 508c81e97ab1..5617b3864586 100644
@@ -7,6 +7,7 @@
  * Copyright 2002 Andi Kleen <[email protected]>
  */
 #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;
Signature · 1 line
2.29.0.rc2.309.g374f81d7ae-goog
#1 lore raw

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

Quoted 116 lines · Alexander Potapenko

From: Albert van der Linde <[email protected]>

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.

---
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(-)
arch/x86/include/asm/uaccess.h +23 -13
index f13659523108..7041ebc48b75 100644
@@ -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);                                  \
 })
 
arch/x86/lib/usercopy_64.c +3 -1
index 508c81e97ab1..5617b3864586 100644
@@ -7,6 +7,7 @@
  * Copyright 2002 Andi Kleen <[email protected]>
  */
 #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

Signature · 10 lines
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
#2 lore raw
In reply to Alexander Potapenko
Thank you for the patch! Perhaps something to improve:

Hi Alexander,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tip/master]
[also build test WARNING on next-20201023]
[cannot apply to tip/x86/core hnaz-linux-mm/master v5.9]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Alexander-Potapenko/x86-add-failure-injection-to-get-put-clear_user/20201023-161920
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 95d591cca3b1851eaf66af066149183f18ff5ab3
config: i386-randconfig-s002-20201023 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-17-g2d3af347-dirty
        # https://github.com/0day-ci/linux/commit/fde54c64ea35d06708ec79201776bedb1533cb91
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Alexander-Potapenko/x86-add-failure-injection-to-get-put-clear_user/20201023-161920
        git checkout fde54c64ea35d06708ec79201776bedb1533cb91
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"

>> arch/x86/events/core.c:2668:21: sparse: sparse: Using plain integer as NULL pointer
--
>> arch/x86/kernel/stacktrace.c:104:13: sparse: sparse: Using plain integer as NULL pointer

vim +2668 arch/x86/events/core.c

d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2635  
56962b4449af34 arch/x86/kernel/cpu/perf_event.c   Frederic Weisbecker      2010-06-30  2636  void
cfbcf468454ab4 arch/x86/events/core.c             Arnaldo Carvalho de Melo 2016-04-28  2637  perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2638  {
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2639  	struct stack_frame frame;
c8e3dd86600a1a arch/x86/events/core.c             Al Viro                  2020-02-15  2640  	const struct stack_frame __user *fp;
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2641  
927c7a9e92c4f6 arch/x86/kernel/cpu/perf_event.c   Frederic Weisbecker      2010-07-01  2642  	if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
927c7a9e92c4f6 arch/x86/kernel/cpu/perf_event.c   Frederic Weisbecker      2010-07-01  2643  		/* TODO: We don't support guest os callchain now */
ed8052616680e7 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2010-08-20  2644  		return;
927c7a9e92c4f6 arch/x86/kernel/cpu/perf_event.c   Frederic Weisbecker      2010-07-01  2645  	}
5a6cec3abbdb74 arch/x86/kernel/cpu/perf_counter.c Ingo Molnar              2009-05-29  2646  
d07bdfd322d307 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2012-07-10  2647  	/*
d07bdfd322d307 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2012-07-10  2648  	 * We don't know what to do with VM86 stacks.. ignore them for now.
d07bdfd322d307 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2012-07-10  2649  	 */
d07bdfd322d307 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2012-07-10  2650  	if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
d07bdfd322d307 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2012-07-10  2651  		return;
d07bdfd322d307 arch/x86/kernel/cpu/perf_event.c   Peter Zijlstra           2012-07-10  2652  
c8e3dd86600a1a arch/x86/events/core.c             Al Viro                  2020-02-15  2653  	fp = (void __user *)regs->bp;
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2654  
70791ce9ba68a5 arch/x86/kernel/cpu/perf_event.c   Frederic Weisbecker      2010-06-29  2655  	perf_callchain_store(entry, regs->ip);
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2656  
4012e77a903d11 arch/x86/events/core.c             Andy Lutomirski          2018-08-29  2657  	if (!nmi_uaccess_okay())
20afc60f892d28 arch/x86/kernel/cpu/perf_event.c   Andrey Vagin             2011-08-30  2658  		return;
20afc60f892d28 arch/x86/kernel/cpu/perf_event.c   Andrey Vagin             2011-08-30  2659  
257ef9d21f1b00 arch/x86/kernel/cpu/perf_event.c   Torok Edwin              2010-03-17  2660  	if (perf_callchain_user32(regs, entry))
257ef9d21f1b00 arch/x86/kernel/cpu/perf_event.c   Torok Edwin              2010-03-17  2661  		return;
257ef9d21f1b00 arch/x86/kernel/cpu/perf_event.c   Torok Edwin              2010-03-17  2662  
75925e1ad7f5a4 arch/x86/kernel/cpu/perf_event.c   Andi Kleen               2015-10-22  2663  	pagefault_disable();
3b1fff08038bd0 arch/x86/events/core.c             Arnaldo Carvalho de Melo 2016-05-10  2664  	while (entry->nr < entry->max_stack) {
ae31fe51a3ccea arch/x86/events/core.c             Johannes Weiner          2016-11-22  2665  		if (!valid_user_frame(fp, sizeof(frame)))
75925e1ad7f5a4 arch/x86/kernel/cpu/perf_event.c   Andi Kleen               2015-10-22  2666  			break;
75925e1ad7f5a4 arch/x86/kernel/cpu/perf_event.c   Andi Kleen               2015-10-22  2667  
c8e3dd86600a1a arch/x86/events/core.c             Al Viro                  2020-02-15 @2668  		if (__get_user(frame.next_frame, &fp->next_frame))
75925e1ad7f5a4 arch/x86/kernel/cpu/perf_event.c   Andi Kleen               2015-10-22  2669  			break;
c8e3dd86600a1a arch/x86/events/core.c             Al Viro                  2020-02-15  2670  		if (__get_user(frame.return_address, &fp->return_address))
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2671  			break;
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2672  
70791ce9ba68a5 arch/x86/kernel/cpu/perf_event.c   Frederic Weisbecker      2010-06-29  2673  		perf_callchain_store(entry, frame.return_address);
75925e1ad7f5a4 arch/x86/kernel/cpu/perf_event.c   Andi Kleen               2015-10-22  2674  		fp = (void __user *)frame.next_frame;
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2675  	}
75925e1ad7f5a4 arch/x86/kernel/cpu/perf_event.c   Andi Kleen               2015-10-22  2676  	pagefault_enable();
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2677  }
d7d59fb3238336 arch/x86/kernel/cpu/perf_counter.c Peter Zijlstra           2009-03-30  2678  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org