Skip to content

Commit c9a2119

Browse files
Howard McLauchlanDominik Brodowski
authored andcommitted
bpf: whitelist all syscalls for error injection
Error injection is a useful mechanism to fail arbitrary kernel functions. However, it is often hard to guarantee an error propagates appropriately to user space programs. By injecting into syscalls, we can return arbitrary values to user space directly; this increases flexibility and robustness in testing, allowing us to test user space error paths effectively. The following script, for example, fails calls to sys_open() from a given pid: from bcc import BPF from sys import argv pid = argv[1] prog = r""" int kprobe__SyS_open(struct pt_regs *ctx, const char *pathname, int flags) { u32 pid = bpf_get_current_pid_tgid(); if (pid == %s) bpf_override_return(ctx, -ENOMEM); return 0; } """ % pid b = BPF(text=prog) while 1: b.perf_buffer_poll() This patch whitelists all syscalls defined with SYSCALL_DEFINE and COMPAT_SYSCALL_DEFINE for error injection. These changes are not intended to be considered stable, and would normally be configured off. Signed-off-by: Howard McLauchlan <[email protected]> Signed-off-by: Dominik Brodowski <[email protected]>
1 parent 67a7acd commit c9a2119

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

include/linux/compat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#endif
3434

3535
#define COMPAT_SYSCALL_DEFINE0(name) \
36+
asmlinkage long compat_sys_##name(void); \
37+
ALLOW_ERROR_INJECTION(compat_sys_##name, ERRNO); \
3638
asmlinkage long compat_sys_##name(void)
3739

3840
#define COMPAT_SYSCALL_DEFINE1(name, ...) \
@@ -52,6 +54,7 @@
5254
asmlinkage long compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
5355
asmlinkage long compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))\
5456
__attribute__((alias(__stringify(compat_SyS##name)))); \
57+
ALLOW_ERROR_INJECTION(compat_sys##name, ERRNO); \
5558
static inline long C_SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
5659
asmlinkage long compat_SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__));\
5760
asmlinkage long compat_SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__))\

include/linux/syscalls.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
191191

192192
#define SYSCALL_DEFINE0(sname) \
193193
SYSCALL_METADATA(_##sname, 0); \
194+
asmlinkage long sys_##sname(void); \
195+
ALLOW_ERROR_INJECTION(sys_##sname, ERRNO); \
194196
asmlinkage long sys_##sname(void)
195197

196198
#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)
@@ -210,6 +212,7 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
210212
#define __SYSCALL_DEFINEx(x, name, ...) \
211213
asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) \
212214
__attribute__((alias(__stringify(SyS##name)))); \
215+
ALLOW_ERROR_INJECTION(sys##name, ERRNO); \
213216
static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__)); \
214217
asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
215218
asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \

0 commit comments

Comments
 (0)