Skip to contentSkip to conversation

bpf: whitelist syscalls for error injection

PATCH Superseded Howard McLauchlan 5 participants 9 messages Mar 13, 2018 – Mar 19, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: whitelist 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, -ENOENT);
    return 0;
}

""" % pid

b = BPF(text = prog)
while 1:
b.perf_buffer_poll()

This patch whitelists all syscalls defined with SYSCALL_DEFINE for error injection.

---
based on 4.16-rc5

 include/linux/syscalls.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/syscalls.h +3 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)					\
 	SYSCALL_METADATA(_##sname, 0);				\
+	asmlinkage long sys_##sname(void);			\
+	ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);		\
 	asmlinkage long sys_##sname(void)
 
 #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)
 #define __SYSCALL_DEFINEx(x, name, ...)					\
 	asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))	\
 		__attribute__((alias(__stringify(SyS##name))));		\
+	ALLOW_ERROR_INJECTION(sys##name, ERRNO);			\
 	static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__));	\
 	asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
 	asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
Signature · 1 line
2.14.1
#1 lore raw
Quoted 63 lines · Howard McLauchlan

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, -ENOENT);
    return 0;
}

""" % pid

b = BPF(text = prog)
while 1:
b.perf_buffer_poll()

This patch whitelists all syscalls defined with SYSCALL_DEFINE for error injection.

---
based on 4.16-rc5

 include/linux/syscalls.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/syscalls.h +3 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)					\
 	SYSCALL_METADATA(_##sname, 0);				\
+	asmlinkage long sys_##sname(void);			\
+	ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);		\
 	asmlinkage long sys_##sname(void)
 
 #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)
 #define __SYSCALL_DEFINEx(x, name, ...)					\
 	asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))	\
 		__attribute__((alias(__stringify(SyS##name))));		\
+	ALLOW_ERROR_INJECTION(sys##name, ERRNO);			\
 	static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__));	\
 	asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
 	asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
Signature · 1 line
2.14.1

Adding a few more people to Cc

#2 lore raw
Quoted 46 lines · Omar Sandoval
Quoted 45 lines · Howard McLauchlan

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, -ENOENT);
     return 0;
}

""" % pid

b = BPF(text = prog)
while 1:
b.perf_buffer_poll()

This patch whitelists all syscalls defined with SYSCALL_DEFINE for error injection.

---
based on 4.16-rc5

  include/linux/syscalls.h | 3 +++
  1 file changed, 3 insertions(+)
include/linux/syscalls.h +0 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
Quoted 5 lines

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 196

Quoted 5 lines
include/linux/syscalls.h +2 -0
  #define SYSCALL_DEFINE0(sname)					\
  	SYSCALL_METADATA(_##sname, 0);				\
+	asmlinkage long sys_##sname(void);			\
+	ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);		\
  	asmlinkage long sys_##sname(void)

duplication of asmlinkage in the above?

Quoted 16 lines
Quoted 13 lines
include/linux/syscalls.h +1 -0
  #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)
  #define __SYSCALL_DEFINEx(x, name, ...)					\
  	asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))	\
  		__attribute__((alias(__stringify(SyS##name))));		\
+	ALLOW_ERROR_INJECTION(sys##name, ERRNO);			\
  	static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__));	\
  	asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
  	asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
Signature · 1 line
2.14.1

Adding a few more people to Cc

#3 lore raw
Quoted 43 lines · Yonghong Song
Quoted 40 lines · Omar Sandoval
Quoted 39 lines · Howard McLauchlan

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, -ENOENT);
     return 0;
}
""" % pid

b = BPF(text = prog)
while 1:
     b.perf_buffer_poll()

This patch whitelists all syscalls defined with SYSCALL_DEFINE for error injection.

---
based on 4.16-rc5
  include/linux/syscalls.h | 3 +++

  1 file changed, 3 insertions(+)
Quoted 13 lines
Quoted 10 lines

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 195

Quoted 10 lines
include/linux/syscalls.h +2 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
     #define SYSCALL_DEFINE0(sname)                    \
       SYSCALL_METADATA(_##sname, 0);                \
+    asmlinkage long sys_##sname(void);            \
+    ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);        \
       asmlinkage long sys_##sname(void)

duplication of asmlinkage in the above?

The pre-declaration is necessary to ensure ALLOW_ERROR_INJECTION works appropriately. There can be syscalls that are not pre-declared elsewhere which will fail compilation if not declared in this block.

Quoted 15 lines
Quoted 15 lines
Quoted 9 lines
include/linux/syscalls.h +1 -0
     #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)
   #define __SYSCALL_DEFINEx(x, name, ...)                    \
       asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))    \
           __attribute__((alias(__stringify(SyS##name))));        \
+    ALLOW_ERROR_INJECTION(sys##name, ERRNO);            \
       static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__));    \
       asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__));    \
       asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__))    \
Quoted 3 lines

--
2.14.1

Adding a few more people to Cc

#4 lore raw
In reply to Howard McLauchlan
Quoted 6 lines · Howard McLauchlan

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.

Temporary NAK IMO. Specifically:

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 195

Quoted 10 lines
include/linux/syscalls.h +2 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)                                 \
        SYSCALL_METADATA(_##sname, 0);                          \
+       asmlinkage long sys_##sname(void);                      \
+       ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);              \

sys_xyz() is not just the syscall itself; it's also a helper that's used for entirely silly reasons by various bits of kernel code for quite a few syscalls. Fortunately, Dominik has patches to fix that, and Linus is even considering pulling them for 4.16. This patch will most likely conflict with the final result of Dominik's series.

Can you and Dominik coordinate a bit to get this patch or its equivalent landed on top of Dominik's work? It might make sense for Dominik to just add this patch to his series so it can land with the rest of it. Dominik, Ingo, what do you think?

--Andy

#5 lore raw
Quoted 27 lines · Andy Lutomirski
Quoted 6 lines · Howard McLauchlan

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.

Temporary NAK IMO. Specifically:

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 195

Quoted 10 lines
include/linux/syscalls.h +2 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)                                 \
        SYSCALL_METADATA(_##sname, 0);                          \
+       asmlinkage long sys_##sname(void);                      \
+       ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);              \

sys_xyz() is not just the syscall itself; it's also a helper that's used for entirely silly reasons by various bits of kernel code for quite a few syscalls. Fortunately, Dominik has patches to fix that, and Linus is even considering pulling them for 4.16. This patch will most likely conflict with the final result of Dominik's series.

Quoted 7 lines

Can you and Dominik coordinate a bit to get this patch or its equivalent landed on top of Dominik's work? It might make sense for Dominik to just add this patch to his series so it can land with the rest of it. Dominik, Ingo, what do you think?

--Andy

Dominik,

This patch applies cleanly on top of your patch series. Is there anything you'd need from me to get this in on top of your work?

Howard

#6 lore raw
Quoted 33 lines · Howard McLauchlan
Quoted 32 lines · Andy Lutomirski
Quoted 6 lines · Howard McLauchlan

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.

Temporary NAK IMO. Specifically:

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 195

Quoted 10 lines
include/linux/syscalls.h +2 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)                                 \
        SYSCALL_METADATA(_##sname, 0);                          \
+       asmlinkage long sys_##sname(void);                      \
+       ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);              \

sys_xyz() is not just the syscall itself; it's also a helper that's used for entirely silly reasons by various bits of kernel code for quite a few syscalls. Fortunately, Dominik has patches to fix that, and Linus is even considering pulling them for 4.16. This patch will most likely conflict with the final result of Dominik's series.

Can you and Dominik coordinate a bit to get this patch or its equivalent landed on top of Dominik's work? It might make sense for Dominik to just add this patch to his series so it can land with the rest of it. Dominik, Ingo, what do you think?

Quoted 6 lines
Quoted 2 lines

--Andy

Dominik,

This patch applies cleanly on top of your patch series. Is there anything you'd need from me to get this in on top of your work?

Howard,

would this form part of the kernel<->userspace interface and therefore needs to be kept stable? If so, this patch should wait until the arch-specific syscall calling convention is agreed upon.

Moreover, the patches I sent out already do not cover all syscalls yet. Until all in-kernel users of sys_*() are gone (or at least outside arch/), I'd prefer to postpone this patch.

Thanks,
Dominik

#7 lore raw
Quoted 43 lines · Dominik Brodowski
Quoted 33 lines · Howard McLauchlan
Quoted 32 lines · Andy Lutomirski
Quoted 6 lines · Howard McLauchlan

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.

Temporary NAK IMO. Specifically:

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 195

Quoted 10 lines
include/linux/syscalls.h +2 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)                                 \
        SYSCALL_METADATA(_##sname, 0);                          \
+       asmlinkage long sys_##sname(void);                      \
+       ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);              \

sys_xyz() is not just the syscall itself; it's also a helper that's used for entirely silly reasons by various bits of kernel code for quite a few syscalls. Fortunately, Dominik has patches to fix that, and Linus is even considering pulling them for 4.16. This patch will most likely conflict with the final result of Dominik's series.

Can you and Dominik coordinate a bit to get this patch or its equivalent landed on top of Dominik's work? It might make sense for Dominik to just add this patch to his series so it can land with the rest of it. Dominik, Ingo, what do you think?

Quoted 6 lines
Quoted 2 lines

--Andy

Dominik,

This patch applies cleanly on top of your patch series. Is there anything you'd need from me to get this in on top of your work?

Howard,

Quoted 8 lines

would this form part of the kernel<->userspace interface and therefore needs to be kept stable? If so, this patch should wait until the arch-specific syscall calling convention is agreed upon.

Moreover, the patches I sent out already do not cover all syscalls yet. Until all in-kernel users of sys_*() are gone (or at least outside arch/), I'd prefer to postpone this patch.

I was assuming that this ALLOW_ERROR_INJECTION thing is *not* considered stable ABI. We should be free to change the way that the syscall entry code calls syscalls whenever we like.

If you want a stable syscall error injection mechanism, make it work like seccomp instead, please.

#8 lore raw
Quoted 54 lines · Andy Lutomirski
Quoted 43 lines · Dominik Brodowski
Quoted 33 lines · Howard McLauchlan
Quoted 32 lines · Andy Lutomirski
Quoted 6 lines · Howard McLauchlan

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.

Temporary NAK IMO. Specifically:

include/linux/syscalls.h static inline int is_syscall_trace_event(struct trace_event_call *tp_event) · line 195

Quoted 10 lines
include/linux/syscalls.h +2 -0
index a78186d826d7..e8c6d63ace78 100644
@@ -191,6 +191,8 @@ static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
 
 #define SYSCALL_DEFINE0(sname)                                 \
        SYSCALL_METADATA(_##sname, 0);                          \
+       asmlinkage long sys_##sname(void);                      \
+       ALLOW_ERROR_INJECTION(sys_##sname, ERRNO);              \

sys_xyz() is not just the syscall itself; it's also a helper that's used for entirely silly reasons by various bits of kernel code for quite a few syscalls. Fortunately, Dominik has patches to fix that, and Linus is even considering pulling them for 4.16. This patch will most likely conflict with the final result of Dominik's series.

Can you and Dominik coordinate a bit to get this patch or its equivalent landed on top of Dominik's work? It might make sense for Dominik to just add this patch to his series so it can land with the rest of it. Dominik, Ingo, what do you think?

Quoted 6 lines
Quoted 2 lines

--Andy

Dominik,

This patch applies cleanly on top of your patch series. Is there anything you'd need from me to get this in on top of your work?

Howard,

Quoted 8 lines

would this form part of the kernel<->userspace interface and therefore needs to be kept stable? If so, this patch should wait until the arch-specific syscall calling convention is agreed upon.

Moreover, the patches I sent out already do not cover all syscalls yet. Until all in-kernel users of sys_*() are gone (or at least outside arch/), I'd prefer to postpone this patch.

Quoted 7 lines

I was assuming that this ALLOW_ERROR_INJECTION thing is *not* considered stable ABI. We should be free to change the way that the syscall entry code calls syscalls whenever we like.

If you want a stable syscall error injection mechanism, make it work like seccomp instead, please.

This is not supposed to be considered stable. It's for debug purposes only and would normally be configured off.

Howard