Skip to content

Commit 2c739ce

Browse files
albertlindetorvalds
authored andcommitted
lib, include/linux: add usercopy failure capability
Patch series "add fault injection to user memory access", v3. The goal of this series is to improve testing of fault-tolerance in usages of user memory access functions, by adding support for fault injection. syzkaller/syzbot are using the existing fault injection modes and will use this particular feature also. The first patch adds failure injection capability for usercopy functions. The second changes usercopy functions to use this new failure capability (copy_from_user, ...). The third patch adds get/put/clear_user failures to x86. This patch (of 3): Add a failure injection capability to improve testing of fault-tolerance in usages of user memory access functions. Add CONFIG_FAULT_INJECTION_USERCOPY to enable faults in usercopy functions. The should_fail_usercopy function is to be called by these functions (copy_from_user, get_user, ...) in order to fail or not. 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: Borislav Petkov <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Peter Zijlstra (Intel) <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Al Viro <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Marco Elver <[email protected]> Cc: Christoph Hellwig <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent d9bc85d commit 2c739ce

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,7 @@
13431343
current integrity status.
13441344

13451345
failslab=
1346+
fail_usercopy=
13461347
fail_page_alloc=
13471348
fail_make_request=[KNL]
13481349
General fault injection mechanism.

Documentation/fault-injection/fault-injection.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Available fault injection capabilities
1616

1717
injects page allocation failures. (alloc_pages(), get_free_pages(), ...)
1818

19+
- fail_usercopy
20+
21+
injects failures in user memory access functions. (copy_from_user(), get_user(), ...)
22+
1923
- fail_futex
2024

2125
injects futex deadlock and uaddr fault errors.
@@ -177,6 +181,7 @@ use the boot option::
177181

178182
failslab=
179183
fail_page_alloc=
184+
fail_usercopy=
180185
fail_make_request=
181186
fail_futex=
182187
mmc_core.fail_request=<interval>,<probability>,<space>,<times>
@@ -222,7 +227,7 @@ How to add new fault injection capability
222227

223228
- debugfs entries
224229

225-
failslab, fail_page_alloc, and fail_make_request use this way.
230+
failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
226231
Helper functions:
227232

228233
fault_create_debugfs_attr(name, parent, attr);

include/linux/fault-inject-usercopy.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __LINUX_FAULT_INJECT_USERCOPY_H__
3+
#define __LINUX_FAULT_INJECT_USERCOPY_H__
4+
5+
/*
6+
* This header provides a wrapper for injecting failures to user space memory
7+
* access functions.
8+
*/
9+
10+
#include <linux/types.h>
11+
12+
#ifdef CONFIG_FAULT_INJECTION_USERCOPY
13+
14+
bool should_fail_usercopy(void);
15+
16+
#else
17+
18+
static inline bool should_fail_usercopy(void) { return false; }
19+
20+
#endif /* CONFIG_FAULT_INJECTION_USERCOPY */
21+
22+
#endif /* __LINUX_FAULT_INJECT_USERCOPY_H__ */

lib/Kconfig.debug

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,13 @@ config FAIL_PAGE_ALLOC
17681768
help
17691769
Provide fault-injection capability for alloc_pages().
17701770

1771+
config FAULT_INJECTION_USERCOPY
1772+
bool "Fault injection capability for usercopy functions"
1773+
depends on FAULT_INJECTION
1774+
help
1775+
Provides fault-injection capability to inject failures
1776+
in usercopy functions (copy_from_user(), get_user(), ...).
1777+
17711778
config FAIL_MAKE_REQUEST
17721779
bool "Fault-injection capability for disk IO"
17731780
depends on FAULT_INJECTION && BLOCK

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ obj-$(CONFIG_AUDIT_COMPAT_GENERIC) += compat_audit.o
210210

211211
obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
212212
obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
213+
obj-$(CONFIG_FAULT_INJECTION_USERCOPY) += fault-inject-usercopy.o
213214
obj-$(CONFIG_NOTIFIER_ERROR_INJECTION) += notifier-error-inject.o
214215
obj-$(CONFIG_PM_NOTIFIER_ERROR_INJECT) += pm-notifier-error-inject.o
215216
obj-$(CONFIG_NETDEV_NOTIFIER_ERROR_INJECT) += netdev-notifier-error-inject.o

lib/fault-inject-usercopy.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <linux/fault-inject.h>
3+
#include <linux/fault-inject-usercopy.h>
4+
5+
static struct {
6+
struct fault_attr attr;
7+
} fail_usercopy = {
8+
.attr = FAULT_ATTR_INITIALIZER,
9+
};
10+
11+
static int __init setup_fail_usercopy(char *str)
12+
{
13+
return setup_fault_attr(&fail_usercopy.attr, str);
14+
}
15+
__setup("fail_usercopy=", setup_fail_usercopy);
16+
17+
#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
18+
19+
static int __init fail_usercopy_debugfs(void)
20+
{
21+
struct dentry *dir;
22+
23+
dir = fault_create_debugfs_attr("fail_usercopy", NULL,
24+
&fail_usercopy.attr);
25+
if (IS_ERR(dir))
26+
return PTR_ERR(dir);
27+
28+
return 0;
29+
}
30+
31+
late_initcall(fail_usercopy_debugfs);
32+
33+
#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
34+
35+
bool should_fail_usercopy(void)
36+
{
37+
return should_fail(&fail_usercopy.attr, 1);
38+
}
39+
EXPORT_SYMBOL_GPL(should_fail_usercopy);

0 commit comments

Comments
 (0)