Skip to content

Commit 4d0e9df

Browse files
albertlindetorvalds
authored andcommitted
lib, uaccess: add failure injection to usercopy functions
To test fault-tolerance of user memory access functions, introduce fault injection to usercopy functions. If a failure is expected return either -EFAULT or the total amount of bytes that were not copied. Signed-off-by: Albert van der Linde <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Akinobu Mita <[email protected]> Reviewed-by: Alexander Potapenko <[email protected]> Cc: Al Viro <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Marco Elver <[email protected]> Cc: Peter Zijlstra (Intel) <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Christoph Hellwig <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2c739ce commit 4d0e9df

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

include/linux/uaccess.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __LINUX_UACCESS_H__
33
#define __LINUX_UACCESS_H__
44

5+
#include <linux/fault-inject-usercopy.h>
56
#include <linux/instrumented.h>
67
#include <linux/minmax.h>
78
#include <linux/sched.h>
@@ -84,6 +85,8 @@ static __always_inline __must_check unsigned long
8485
__copy_from_user(void *to, const void __user *from, unsigned long n)
8586
{
8687
might_fault();
88+
if (should_fail_usercopy())
89+
return n;
8790
instrument_copy_from_user(to, from, n);
8891
check_object_size(to, n, false);
8992
return raw_copy_from_user(to, from, n);
@@ -105,6 +108,8 @@ __copy_from_user(void *to, const void __user *from, unsigned long n)
105108
static __always_inline __must_check unsigned long
106109
__copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
107110
{
111+
if (should_fail_usercopy())
112+
return n;
108113
instrument_copy_to_user(to, from, n);
109114
check_object_size(from, n, true);
110115
return raw_copy_to_user(to, from, n);
@@ -114,6 +119,8 @@ static __always_inline __must_check unsigned long
114119
__copy_to_user(void __user *to, const void *from, unsigned long n)
115120
{
116121
might_fault();
122+
if (should_fail_usercopy())
123+
return n;
117124
instrument_copy_to_user(to, from, n);
118125
check_object_size(from, n, true);
119126
return raw_copy_to_user(to, from, n);
@@ -125,7 +132,7 @@ _copy_from_user(void *to, const void __user *from, unsigned long n)
125132
{
126133
unsigned long res = n;
127134
might_fault();
128-
if (likely(access_ok(from, n))) {
135+
if (!should_fail_usercopy() && likely(access_ok(from, n))) {
129136
instrument_copy_from_user(to, from, n);
130137
res = raw_copy_from_user(to, from, n);
131138
}
@@ -143,6 +150,8 @@ static inline __must_check unsigned long
143150
_copy_to_user(void __user *to, const void *from, unsigned long n)
144151
{
145152
might_fault();
153+
if (should_fail_usercopy())
154+
return n;
146155
if (access_ok(to, n)) {
147156
instrument_copy_to_user(to, from, n);
148157
n = raw_copy_to_user(to, from, n);

lib/iov_iter.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <crypto/hash.h>
33
#include <linux/export.h>
44
#include <linux/bvec.h>
5+
#include <linux/fault-inject-usercopy.h>
56
#include <linux/uio.h>
67
#include <linux/pagemap.h>
78
#include <linux/slab.h>
@@ -140,6 +141,8 @@
140141

141142
static int copyout(void __user *to, const void *from, size_t n)
142143
{
144+
if (should_fail_usercopy())
145+
return n;
143146
if (access_ok(to, n)) {
144147
instrument_copy_to_user(to, from, n);
145148
n = raw_copy_to_user(to, from, n);
@@ -149,6 +152,8 @@ static int copyout(void __user *to, const void *from, size_t n)
149152

150153
static int copyin(void *to, const void __user *from, size_t n)
151154
{
155+
if (should_fail_usercopy())
156+
return n;
152157
if (access_ok(from, n)) {
153158
instrument_copy_from_user(to, from, n);
154159
n = raw_copy_from_user(to, from, n);

lib/strncpy_from_user.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/compiler.h>
33
#include <linux/export.h>
4+
#include <linux/fault-inject-usercopy.h>
45
#include <linux/kasan-checks.h>
56
#include <linux/thread_info.h>
67
#include <linux/uaccess.h>
@@ -99,6 +100,8 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
99100
unsigned long max_addr, src_addr;
100101

101102
might_fault();
103+
if (should_fail_usercopy())
104+
return -EFAULT;
102105
if (unlikely(count <= 0))
103106
return 0;
104107

lib/usercopy.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/bitops.h>
3+
#include <linux/fault-inject-usercopy.h>
34
#include <linux/instrumented.h>
45
#include <linux/uaccess.h>
56

@@ -10,7 +11,7 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n
1011
{
1112
unsigned long res = n;
1213
might_fault();
13-
if (likely(access_ok(from, n))) {
14+
if (!should_fail_usercopy() && likely(access_ok(from, n))) {
1415
instrument_copy_from_user(to, from, n);
1516
res = raw_copy_from_user(to, from, n);
1617
}
@@ -25,6 +26,8 @@ EXPORT_SYMBOL(_copy_from_user);
2526
unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
2627
{
2728
might_fault();
29+
if (should_fail_usercopy())
30+
return n;
2831
if (likely(access_ok(to, n))) {
2932
instrument_copy_to_user(to, from, n);
3033
n = raw_copy_to_user(to, from, n);

0 commit comments

Comments
 (0)