filter: add Extended BPF interpreter and converter
Subsystems (7)
v5 cover filter: add Extended BPF interpreter and converter
Hi All,
V1 patches:
http://thread.gmane.org/gmane.linux.kernel/1605783
V2 patches:
http://thread.gmane.org/gmane.linux.kernel/1642325
V3 patches:
http://thread.gmane.org/gmane.linux.kernel/1656538
V4 summary:
- addressed Daniel comments
- RFC for seccomp with extended BPF
- added extended BPF design doc
V5 summary:
- fixed commit one-liner, removed empty line
- added Hagen's ack
Please review.
Thanks!
Alexei Starovoitov (3):
filter: add Extended BPF interpreter and converter [RFC] seccomp: convert seccomp to use extended BPF
doc: filter: add Extended BPF documentation
Documentation/networking/filter.txt | 181 ++++++++ include/linux/filter.h | 6 +- include/linux/netdevice.h | 1 + include/linux/seccomp.h | 1 - include/uapi/linux/filter.h | 33 +- kernel/seccomp.c | 112 +++-- net/core/filter.c | 807 ++++++++++++++++++++++++++++++++++- net/core/sysctl_net_core.c | 7 + 8 files changed, 1059 insertions(+), 89 deletions(-)
Signature · 1 line
1.7.9.5
PATCH v5 1/3 filter: add Extended BPF interpreter and converter
Extended BPF extends old BPF in the following ways:
- from 2 to 10 registers
Original BPF has two registers (A and X) and hidden frame pointer. Extended BPF has ten registers and read-only frame pointer.
- from 32-bit registers to 64-bit registers
semantics of old 32-bit ALU operations are preserved via 32-bit subregisters
- if (cond) jump_true; else jump_false;
old BPF insns are replaced with:
if (cond) jump_true; /* else fallthrough */
- adds signed > and >= insns
- 16 4-byte stack slots for register spill-fill replaced with up to 512 bytes of multi-use stack space
- introduces bpf_call insn and register passing convention for zero overhead calls from/to other kernel functions (not part of this patch)
- adds arithmetic right shift insn
- adds swab32/swab64 insns
- adds atomic_add insn
- old tax/txa insns are replaced with 'mov dst,src' insn
Extended BPF is designed to be JITed with one to one mapping, which allows GCC/LLVM backends to generate optimized BPF code that performs almost as fast as natively compiled code
sk_convert_filter() remaps old style insns into extended:
'sock_filter' instructions are remapped on the fly to 'sock_filter_ext' extended instructions when sysctl net.core.bpf_ext_enable=1
Old filter comes through sk_attach_filter() or sk_unattached_filter_create()
if (bpf_ext_enable) {
convert to new
sk_chk_filter() - check old bpf
use sk_run_filter_ext() - new interpreter
} else {
sk_chk_filter() - check old bpf
if (bpf_jit_enable)
use old jit
else
use sk_run_filter() - old interpreter
}
sk_run_filter_ext() interpreter is noticeably faster than sk_run_filter() for two reasons:
1.fall-through jumps
Old BPF jump instructions are forced to go either 'true' or 'false' branch which causes branch-miss penalty.
Extended BPF jump instructions have one branch and fall-through, which fit CPU branch predictor logic better.
'perf stat' shows drastic difference for branch-misses.
2.jump-threaded implementation of interpreter vs switch statement
Instead of single tablejump at the top of 'switch' statement, GCC will generate multiple tablejump instructions, which helps CPU branch predictor
Performance of two BPF filters generated by libpcap was measured on x86_64, i386 and arm32.
fprog #1 is taken from Documentation/networking/filter.txt:
tcpdump -i eth0 port 22 -dd
fprog #2 is taken from 'man tcpdump':
tcpdump -i eth0 'tcp port 22 and (((ip[2:2] - ((ip[0]&0xf)<<2)) -
((tcp[12]&0xf0)>>2)) != 0)' -dd
Other libpcap programs have similar performance differences.
Raw performance data from BPF micro-benchmark:SK_RUN_FILTER on same SKB (cache-hit) or 10k SKBs (cache-miss) time in nsec per call, smaller is better
--x86_64--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-miss
old BPF 90 101 192 202 ext BPF 31 71 47 97 old BPF jit 12 34 17 44
ext BPF jit TBD
--i386--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-miss
old BPF 107 136 227 252 ext BPF 40 119 69 172
--arm32--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-miss
old BPF 202 300 475 540 ext BPF 139 270 296 470 old BPF jit 26 182 37 202
new BPF jit TBD
Tested with trinify BPF fuzzer
Future work:
0. seccomp
1. add extended BPF JIT for x86_64
2. add inband old/new demux and extended BPF verifier, so that new programs can be loaded through old sk_attach_filter() and sk_unattached_filter_create() interfaces
3. tracing filters systemtap-like with extended BPF
4. OVS with extended BPF
5. nftables with extended BPF
--- include/linux/filter.h | 6 +- include/linux/netdevice.h | 1 + include/uapi/linux/filter.h | 33 +- net/core/filter.c | 802 ++++++++++++++++++++++++++++++++++++++++++- net/core/sysctl_net_core.c | 7 + 5 files changed, 827 insertions(+), 22 deletions(-)
include/linux/filter.h +4 -2
@@ -52,7 +52,11 @@ extern int sk_detach_filter(struct sock *sk);extern int sk_chk_filter (struct sock_filter *filter,unsigned int flen);extern int sk_get_filter (struct sock *sk,struct sock_filter __user *filter,unsigned len);extern void sk_decode_filter (struct sock_filter *filt,struct sock_filter *to); +int sk_convert_filter (struct sock_filter *old_prog,int len, +struct sock_filter_ext *new_prog,int *p_new_len); +unsigned int sk_run_filter_ext (void *ctx,const struct sock_filter_ext *insn); +#define SK_RUN_FILTER (FILTER ,SKB ) (*FILTER->bpf_func)(SKB, FILTER->insns)#ifdef CONFIG_BPF_JIT #include <stdarg.h> #include <linux/linkage.h>
@@ -70,7 +74,6 @@ static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,print_hex_dump (KERN_ERR ,"JIT code: " ,DUMP_PREFIX_OFFSET , 16, 1, image, proglen, false); } -#define SK_RUN_FILTER (FILTER ,SKB ) (*FILTER->bpf_func)(SKB, FILTER->insns)#else #include <linux/slab.h> static inline void bpf_jit_compile (struct sk_filter *fp)
@@ -80,7 +83,6 @@ static inline void bpf_jit_free(struct sk_filter *fp) {kfree (fp); } -#define SK_RUN_FILTER (FILTER ,SKB ) sk_run_filter(SKB, FILTER->insns)#endif static inline int bpf_tell_extensions (void )
include/linux/netdevice.h +1 -0
@@ -3054,6 +3054,7 @@ extern int netdev_max_backlog;extern int netdev_tstamp_prequeue;extern int weight_p;extern int bpf_jit_enable; +extern int bpf_ext_enable;bool netdev_has_upper_dev (struct net_device *dev,struct net_device *upper_dev); struct net_device *netdev_all_upper_get_next_dev_rcu(struct net_device *dev,
include/uapi/linux/filter.h +26 -7
@@ -1,5 +1,6 @@/* * Linux Socket Filter Data Structures +* Extended BPF is Copyright (c) 2011-2014, PLUMgrid, http://plumgrid.com */ #ifndef _UAPI__LINUX_FILTER_H__
@@ -19,7 +20,7 @@* Try and keep these values and structures similar to BSD, especially * the BPF code definitions which need to match so you can share filters */ - +struct sock_filter {/* Filter block */ __u16 code;/* Actual filter code */ __u8 jt;/* Jump true */
@@ -27,6 +28,14 @@ struct sock_filter { /* Filter block */__u32 k;/* Generic multiuse field */ }; +struct sock_filter_ext { +__u8 code;/* opcode */ +__u8 a_reg:4 ;/* dest register */ +__u8 x_reg:4 ;/* source register */ +__s16 off;/* signed offset */ +__s32 imm;/* signed immediate constant */ +}; +struct sock_fprog {/* Required for SO_ATTACH_FILTER. */ unsigned short len;/* Number of filter blocks */ struct sock_filter __user *filter;
@@ -45,12 +54,14 @@ struct sock_fprog { /* Required for SO_ATTACH_FILTER. */#define BPF_JMP 0x05#define BPF_RET 0x06#define BPF_MISC 0x07 +#define BPF_ALU64 0x07/* ld/ldx fields */ #define BPF_SIZE (code) ((code) & 0x18)#define BPF_W 0x00#define BPF_H 0x08#define BPF_B 0x10 +#define BPF_DW 0x18#define BPF_MODE (code) ((code) & 0xe0)#define BPF_IMM 0x00#define BPF_ABS 0x20
@@ -58,6 +69,7 @@ struct sock_fprog { /* Required for SO_ATTACH_FILTER. */#define BPF_MEM 0x60#define BPF_LEN 0x80#define BPF_MSH 0xa0 +#define BPF_XADD 0xc0/* exclusive add */ /* alu/jmp fields */ #define BPF_OP (code) ((code) & 0xf0)
@@ -68,16 +80,24 @@ struct sock_fprog { /* Required for SO_ATTACH_FILTER. */#define BPF_OR 0x40#define BPF_AND 0x50#define BPF_LSH 0x60 -#define BPF_RSH 0x70 +#define BPF_RSH 0x70/* logical shift right */ #define BPF_NEG 0x80#define BPF_MOD 0x90#define BPF_XOR 0xa0 +#define BPF_MOV 0xb0/* mov reg to reg */ +#define BPF_ARSH 0xc0/* sign extending arithmetic shift right */ +#define BPF_BSWAP32 0xd0/* swap lower 4 bytes of 64-bit register */ +#define BPF_BSWAP64 0xe0/* swap all 8 bytes of 64-bit register */ #define BPF_JA 0x00 -#define BPF_JEQ 0x10 -#define BPF_JGT 0x20 -#define BPF_JGE 0x30 -#define BPF_JSET 0x40 +#define BPF_JEQ 0x10/* jump == */ +#define BPF_JGT 0x20/* GT is unsigned '>', JA in x86 */ +#define BPF_JGE 0x30/* GE is unsigned '>=', JAE in x86 */ +#define BPF_JSET 0x40/* if (A & X) */ +#define BPF_JNE 0x50/* jump != */ +#define BPF_JSGT 0x60/* SGT is signed '>', GT in x86 */ +#define BPF_JSGE 0x70/* SGE is signed '>=', GE in x86 */ +#define BPF_CALL 0x80/* function call */ #define BPF_SRC (code) ((code) & 0x08)#define BPF_K 0x00#define BPF_X 0x08
@@ -134,5 +154,4 @@ struct sock_fprog { /* Required for SO_ATTACH_FILTER. */#define SKF_NET_OFF (-0x100000)#define SKF_LL_OFF (-0x200000) -#endif /* _UAPI__LINUX_FILTER_H__ */
net/core/filter.c +789 -13
848 lines not on this page · raw message
net/core/sysctl_net_core.c +7 -0
@@ -273,6 +273,13 @@ static struct ctl_table net_core_table[] = { },#endif { + .procname = "bpf_ext_enable", + .data = &bpf_ext_enable, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec + }, + { .procname = "netdev_tstamp_prequeue", .data = &netdev_tstamp_prequeue, .maxlen = sizeof(int),
Signature · 1 line
1.7.9.5
Quoted 105 lines · Alexei Starovoitov
Extended BPF extends old BPF in the following ways:
- from 2 to 10 registers
Original BPF has two registers (A and X) and hidden frame pointer. Extended BPF has ten registers and read-only frame pointer.
- from 32-bit registers to 64-bit registers
semantics of old 32-bit ALU operations are preserved via 32-bit subregisters
- if (cond)jump_true; elsejump_false;
old BPF insns are replaced with:
if (cond)jump_true; /* else fallthrough */
- adds signed > and >= insns
- 16 4-byte stack slots for register spill-fill replaced with up to 512 bytes of multi-use stack space
- introducesbpf_callinsn and register passing convention for zero overhead calls from/to other kernel functions (not part of this patch)
- adds arithmetic right shift insn
- adds swab32/swab64 insns
- addsatomic_addinsn
- old tax/txa insns are replaced with 'mov dst,src' insnExtended BPF is designed to be JITed with one to one mapping, which allows GCC/LLVM backends to generate optimized BPF code that performs almost as fast as natively compiled code
sk_convert_filter()remaps old style insns into extended:
'sock_filter' instructions are remapped on the fly to 'sock_filter_ext' extended instructions when sysctl net.core.bpf_ext_enable=1Old filter comes through
sk_attach_filter()orsk_unattached_filter_create()
if (bpf_ext_enable) {
convert to new
sk_chk_filter()- check old bpf
usesk_run_filter_ext()- new interpreter
} else {
sk_chk_filter()- check old bpf
if (bpf_jit_enable)
use old jit
else
usesk_run_filter()- old interpreter
}
sk_run_filter_ext()interpreter is noticeably faster thansk_run_filter()for two reasons:1.fall-through jumps
Old BPF jump instructions are forced to go either 'true' or 'false' branch which causes branch-miss penalty.
Extended BPF jump instructions have one branch and fall-through, which fit CPU branch predictor logic better.
'perf stat' shows drastic difference for branch-misses.2.jump-threaded implementation of interpreter vs switch statement
Instead of single tablejump at the top of 'switch' statement, GCC will generate multiple tablejump instructions, which helps CPU branch predictorPerformance of two BPF filters generated by libpcap was measured on
x86_64, i386 and arm32.fprog #1 is taken from
Documentation/networking/filter.txt:
tcpdump -i eth0 port 22 -ddfprog #2 is taken from 'man tcpdump':
tcpdump -i eth0 'tcp port 22 and (((ip[2:2] - ((ip[0]&0xf)<<2)) -((tcp[12]&0xf0)>>2)) != 0)' -ddOther libpcap programs have similar performance differences.
Raw performance data from BPF micro-benchmark:
SK_RUN_FILTERon same SKB (cache-hit) or 10k SKBs (cache-miss) time in nsec per call, smaller is better
--x86_64--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-missold BPF 90 101 192 202 ext BPF 31 71 47 97 old BPF jit 12 34 17 44ext BPF jit TBD
--i386--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-missold BPF 107 136 227 252 ext BPF 40 119 69 172--arm32--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-missold BPF 202 300 475 540 ext BPF 139 270 296 470 old BPF jit 26 182 37 202new BPF jit TBD
Tested with trinify BPF fuzzer
Future work:
0. seccomp
1. add extended BPF JIT for
x86_642. add inband old/new demux and extended BPF verifier, so that new programs can be loaded through old
sk_attach_filter()andsk_unattached_filter_create()interfaces
Quoted 8 lines
3. tracing filters systemtap-like with extended BPF
4. OVS with extended BPF
5. nftables with extended BPF
Signed-off-by Alexei Starovoitov <[email protected]>
From what I can tell, looks good to me:
So next step would be to add selftests and then after that JIT?
...
net/core/filter.c out: · line 1660
Quoted 4 lines
net/core/filter.c +4
-0+#undef LOAD_IMM +} +EXPORT_SYMBOL (sk_run_filter_ext); +
One minor thing I noticed when I git-am'ed your patch is the newline at the end of file, but perhaps this can be fixed up in directly patchwork.
Quoted 4 lines
net/core/sysctl_net_core.c +0
-0
Quoted 117 lines · Daniel Borkmann
Quoted 116 lines · Alexei Starovoitov
Extended BPF extends old BPF in the following ways:
- from 2 to 10 registers
Original BPF has two registers (A and X) and hidden frame pointer. Extended BPF has ten registers and read-only frame pointer.
- from 32-bit registers to 64-bit registers
semantics of old 32-bit ALU operations are preserved via 32-bit subregisters
- if (cond)jump_true; elsejump_false;
old BPF insns are replaced with:
if (cond)jump_true; /* else fallthrough */
- adds signed > and >= insns
- 16 4-byte stack slots for register spill-fill replaced with up to 512 bytes of multi-use stack space
- introducesbpf_callinsn and register passing convention for zero overhead calls from/to other kernel functions (not part of this patch)
- adds arithmetic right shift insn
- adds swab32/swab64 insns
- addsatomic_addinsn
- old tax/txa insns are replaced with 'mov dst,src' insnExtended BPF is designed to be JITed with one to one mapping, which allows GCC/LLVM backends to generate optimized BPF code that performs almost as fast as natively compiled code
sk_convert_filter()remaps old style insns into extended:
'sock_filter' instructions are remapped on the fly to 'sock_filter_ext' extended instructions when sysctl net.core.bpf_ext_enable=1Old filter comes through
sk_attach_filter()orsk_unattached_filter_create()
if (bpf_ext_enable) {
convert to new
sk_chk_filter()- check old bpf
usesk_run_filter_ext()- new interpreter
} else {
sk_chk_filter()- check old bpf
if (bpf_jit_enable)
use old jit
else
usesk_run_filter()- old interpreter
}
sk_run_filter_ext()interpreter is noticeably faster thansk_run_filter()for two reasons:1.fall-through jumps
Old BPF jump instructions are forced to go either 'true' or 'false' branch which causes branch-miss penalty.
Extended BPF jump instructions have one branch and fall-through, which fit CPU branch predictor logic better.
'perf stat' shows drastic difference for branch-misses.2.jump-threaded implementation of interpreter vs switch statement
Instead of single tablejump at the top of 'switch' statement, GCC will generate multiple tablejump instructions, which helps CPU branch
predictorPerformance of two BPF filters generated by libpcap was measured on
x86_64, i386 and arm32.fprog #1 is taken from
Documentation/networking/filter.txt:
tcpdump -i eth0 port 22 -ddfprog #2 is taken from 'man tcpdump':
tcpdump -i eth0 'tcp port 22 and (((ip[2:2] - ((ip[0]&0xf)<<2)) -((tcp[12]&0xf0)>>2)) != 0)' -ddOther libpcap programs have similar performance differences.
Raw performance data from BPF micro-benchmark:
SK_RUN_FILTERon same SKB (cache-hit) or 10k SKBs (cache-miss) time in nsec per call, smaller is better
--x86_64--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-missold BPF 90 101 192 202 ext BPF 31 71 47 97 old BPF jit 12 34 17 44ext BPF jit TBD
--i386--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-missold BPF 107 136 227 252 ext BPF 40 119 69 172--arm32--
fprog #1 fprog #1 fprog #2 fprog #2
cache-hit cache-miss cache-hit cache-missold BPF 202 300 475 540 ext BPF 139 270 296 470 old BPF jit 26 182 37 202new BPF jit TBD
Tested with trinify BPF fuzzer
Future work:
0. seccomp
1. add extended BPF JIT for
x86_642. add inband old/new demux and extended BPF verifier, so that new programs
can be loaded through oldsk_attach_filter()andsk_unattached_filter_create()
interfaces3. tracing filters systemtap-like with extended BPF
4. OVS with extended BPF
5. nftables with extended BPF
Quoted 7 lines
Quoted 2 lines
Signed-off-by Alexei Starovoitov <[email protected]>From what I can tell, looks good to me:
Reviewed-by Daniel Borkmann <[email protected]>
Thanks!
Quoted 1 line
So next step would be to add selftests and then after that JIT?
yes. that's the plan.
Will probably split selftest into few patches to simplify review.
JIT should go into the existing bpf_jit_comp.c and call it bpf_ext_jit_compile(), right?
Quoted 8 lines
net/core/filter.c out: · line 1660
Quoted 4 lines
net/core/filter.c +4
-0+#undef LOAD_IMM +} +EXPORT_SYMBOL (sk_run_filter_ext); +One minor thing I noticed when I git-am'ed your patch is the newline at the end of file, but perhaps this can be fixed up in directly patchwork.
oops. Too bad checkpatch.pl didn't complain about that... will address in the future.
Thanks!
Alexei
RFC PATCH v5 2/3 seccomp: convert seccomp to use extended BPF
In reply to Alexei Starovoitovuse sk_convert_filter() to convert seccomp BPF into extended BPF
05-sim-long_jumps.c of libseccomp was used as micro-benchmark:
seccomp_rule_add_exact(ctx,... seccomp_rule_add_exact(ctx,...
rc = seccomp_load(ctx);
for (i = 0; i < 10000000; i++)
syscall(199, 100);
--x86_64--
old BPF: 8.6 seconds
73.38% bench [kernel.kallsyms] [k] sk_run_filter
10.70% bench libc-2.15.so [.] syscall
5.09% bench [kernel.kallsyms] [k] seccomp_bpf_load
1.97% bench [kernel.kallsyms] [k] system_call
ext BPF: 5.7 seconds
66.20% bench [kernel.kallsyms] [k] sk_run_filter_ext
16.75% bench libc-2.15.so [.] syscall
3.31% bench [kernel.kallsyms] [k] system_call
2.88% bench [kernel.kallsyms] [k] __secure_computing--i386---
old BPF: 5.4 sec
ext BPF: 3.8 sec
BPF filters generated by seccomp are very branchy, so ext BPF performance is better than old BPF.
Performance gains will be even higher when extended BPF JIT is committed.
---
This patch is an RFC to use extended BPF in seccomp. Change it to do it conditionally with bpf_ext_enable knob ?
--- include/linux/seccomp.h | 1 - kernel/seccomp.c | 112 +++++++++++++++++++++-------------------------- net/core/filter.c | 5 --- 3 files changed, 51 insertions(+), 67 deletions(-)
include/linux/seccomp.h +0 -1
@@ -76,7 +76,6 @@ static inline int seccomp_mode(struct seccomp *s)#ifdef CONFIG_SECCOMP_FILTER extern void put_seccomp_filter (struct task_struct *tsk);extern void get_seccomp_filter (struct task_struct *tsk); -extern u32 seccomp_bpf_load (int off);#else /* CONFIG_SECCOMP_FILTER */ static inline void put_seccomp_filter (struct task_struct *tsk) {
kernel/seccomp.c +51 -61
194 lines not on this page · raw message
net/core/filter.c +0 -5
@@ -388,11 +388,6 @@ load_b:A =0 ;continue ; } -#ifdef CONFIG_SECCOMP_FILTER -case BPF_S_ANC_SECCOMP_LD_W : -A =seccomp_bpf_load (fentry->k); -continue ; -#endif default : WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n", fentry->code, fentry->jt,
Signature · 1 line
1.7.9.5
Re: [PATCH v5 net-next 2/3] [RFC] seccomp: convert seccomp to use extended BPF
Quoted 28 lines · Alexei Starovoitov
use
sk_convert_filter()to convert seccomp BPF into extended BPF
05-sim-long_jumps.cof libseccomp was used as micro-benchmark:
seccomp_rule_add_exact(ctx,... seccomp_rule_add_exact(ctx,...
rc =seccomp_load(ctx);
for (i = 0; i < 10000000; i++)syscall(199, 100);--x86_64--
old BPF: 8.6 seconds73.38% bench [kernel.kallsyms] [k] sk_run_filter 10.70% bench libc-2.15.so [.] syscall 5.09% bench [kernel.kallsyms] [k] seccomp_bpf_load 1.97% bench [kernel.kallsyms] [k] system_call ext BPF: 5.7 seconds 66.20% bench [kernel.kallsyms] [k] sk_run_filter_ext 16.75% bench libc-2.15.so [.] syscall 3.31% bench [kernel.kallsyms] [k] system_call 2.88% bench [kernel.kallsyms] [k] __secure_computing--i386---
old BPF: 5.4 sec
ext BPF: 3.8 secBPF filters generated by seccomp are very branchy, so ext BPF performance is better than old BPF.
Quoted 8 lines
Performance gains will be even higher when extended BPF JIT is committed.
Signed-off-by Alexei Starovoitov <[email protected]>---
This patch is an RFC to use extended BPF in seccomp. Change it to do it conditionally withbpf_ext_enableknob ?
Kees, Will,
I've played with libseccomp to test this patch and just found your other testsuite for bpf+seccomp:
https://github.com/redpig/seccomp
It passes as well on x86_64 and i386.
Not sure how real all 'seccomp' and libseccomp' bpf filters. Some of them are very short, some very long.
If average number of filtered syscalls is > 10, then upcoming 'ebpf table' will give another performance boost, since single table lookup will be faster than long sequence of 'if' conditions.
Please review.
Thanks
Alexei
Quoted 242 lines
--- include/linux/seccomp.h | 1 - kernel/seccomp.c | 112 +++++++++++++++++++++-------------------------- net/core/filter.c | 5 --- 3 files changed, 51 insertions(+), 67 deletions(-)include/linux/seccomp.h +0
-1@@ -76,7 +76,6 @@ static inline int seccomp_mode(struct seccomp *s)#ifdef CONFIG_SECCOMP_FILTER extern void put_seccomp_filter (struct task_struct *tsk);extern void get_seccomp_filter (struct task_struct *tsk); -extern u32 seccomp_bpf_load (int off);#else /* CONFIG_SECCOMP_FILTER */ static inline void put_seccomp_filter (struct task_struct *tsk) {kernel/seccomp.c +51
-61@@ -55,60 +55,27 @@ struct seccomp_filter {atomic_t usage;struct seccomp_filter *prev;unsigned short len;/* Instruction count */ -struct sock_filter insns[]; +struct sock_filter_ext insns[]; };/* Limit any path through the tree to 256KB worth of instructions. */ #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) -/** -* get_u32 - returns a u32 offset into data -* @data: a unsigned 64 bit value -* @index: 0 or 1 to return the first or second 32-bits -* -* This inline exists to hide the length of unsigned long. If a 32-bit -* unsigned long is passed in, it will be extended and the top 32-bits will be -* 0. If it is a 64-bit unsigned long, then whatever data is resident will be -* properly returned. -* +/* * Endianness is explicitly ignored and left for BPF program authors to manage * as per the specific architecture. */ -static inline u32 get_u32 (u64 data,int index) -{ -return ((u32 *)&data)[index]; -} - -/* Helper for bpf_load below. */ -#define BPF_DATA (_name) offsetof(struct seccomp_data, _name) -/** -* bpf_load: checks and returns a pointer to the requested offset -* @off: offset into struct seccomp_data to load from -* -* Returns the requested 32-bits of data. -* seccomp_check_filter() should assure that @off is 32-bit aligned -* and not out of bounds. Failure to do so is a BUG. -*/ -u32 seccomp_bpf_load (int off) +static void populate_seccomp_data (struct seccomp_data *sd) {struct pt_regs *regs =task_pt_regs (current); -if (off ==BPF_DATA (nr)) -return syscall_get_nr (current, regs); -if (off ==BPF_DATA (arch)) -return syscall_get_arch (current, regs); -if (off >=BPF_DATA (args[0 ]) && off <BPF_DATA (args[6 ])) { -unsigned long value; -int arg = (off -BPF_DATA (args[0 ])) /sizeof (u64); -int index = !!(off %sizeof (u64)); -syscall_get_arguments (current, regs, arg,1 , &value); -return get_u32 (value, index); - } -if (off ==BPF_DATA (instruction_pointer)) -return get_u32 (KSTK_EIP (current),0 ); -if (off ==BPF_DATA (instruction_pointer) +sizeof (u32)) -return get_u32 (KSTK_EIP (current),1 ); -/* seccomp_check_filter should make this impossible. */ -BUG (); +int i; + + sd->nr =syscall_get_nr (current, regs); + sd->arch =syscall_get_arch (current, regs); +for (i =0 ; i <6 ; i++) +syscall_get_arguments (current, regs, i,1 , + (unsigned long *)&sd->args[i]); + sd->instruction_pointer =KSTK_EIP (current); }/** @@ -133,17 +100,17 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)switch (code) {case BPF_S_LD_W_ABS : - ftest->code =BPF_S_ANC_SECCOMP_LD_W ; + ftest->code =BPF_LDX |BPF_W |BPF_ABS ;/* 32-bit aligned and not out of bounds. */ if (k >=sizeof (struct seccomp_data ) || k &3 )return -EINVAL ;continue ;case BPF_S_LD_W_LEN : - ftest->code =BPF_S_LD_IMM ; + ftest->code =BPF_LD |BPF_IMM ; ftest->k =sizeof (struct seccomp_data );continue ;case BPF_S_LDX_W_LEN : - ftest->code =BPF_S_LDX_IMM ; + ftest->code =BPF_LDX |BPF_IMM ; ftest->k =sizeof (struct seccomp_data );continue ;/* Explicitly include allowed calls. */ @@ -185,6 +152,7 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)case BPF_S_JMP_JGT_X :case BPF_S_JMP_JSET_K :case BPF_S_JMP_JSET_X : +sk_decode_filter (ftest, ftest);continue ;default :return -EINVAL ;@@ -202,18 +170,21 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)static u32 seccomp_run_filters (int syscall) {struct seccomp_filter *f; +struct seccomp_data sd;u32 ret =SECCOMP_RET_ALLOW ;/* Ensure unexpected behavior doesn't result in failing open. */ if (WARN_ON (current->seccomp.filter ==NULL ))return SECCOMP_RET_KILL ; +populate_seccomp_data (&sd); +/* * All filters in the list are evaluated and the lowest BPF return * value always takes priority (ignoring the DATA). */ for (f = current->seccomp.filter; f; f = f->prev) { -u32 cur_ret =sk_run_filter (NULL , f->insns); +u32 cur_ret =sk_run_filter_ext (&sd, f->insns);if ((cur_ret &SECCOMP_RET_ACTION ) < (ret &SECCOMP_RET_ACTION )) ret = cur_ret; }@@ -231,6 +202,8 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)struct seccomp_filter *filter;unsigned long fp_size = fprog->len *sizeof (struct sock_filter );unsigned long total_insns = fprog->len; +struct sock_filter *fp; +int new_len;long ret;if (fprog->len ==0 || fprog->len >BPF_MAXINSNS )@@ -252,28 +225,42 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) CAP_SYS_ADMIN) != 0)return -EACCES ; -/* Allocate a new seccomp_filter */ - filter =kzalloc (sizeof (struct seccomp_filter ) + fp_size, -GFP_KERNEL |__GFP_NOWARN); -if (!filter) + fp =kzalloc (fp_size,GFP_KERNEL |__GFP_NOWARN); +if (!fp)return -ENOMEM ; -atomic_set (&filter->usage,1 ); - filter->len = fprog->len;/* Copy the instructions from fprog. */ ret = -EFAULT ; -if (copy_from_user (filter->insns, fprog->filter, fp_size)) - goto fail; +if (copy_from_user (fp, fprog->filter, fp_size)) + goto free_prog;/* Check and rewrite the fprog via the skb checker */ - ret =sk_chk_filter (filter->insns, filter->len); + ret =sk_chk_filter (fp, fprog->len);if (ret) - goto fail; + goto free_prog;/* Check and rewrite the fprog for seccomp use */ - ret =seccomp_check_filter (filter->insns, filter->len); + ret =seccomp_check_filter (fp, fprog->len);if (ret) - goto fail; + goto free_prog; + +/* convert 'sock_filter' insns to 'sock_filter_ext' insns */ + ret =sk_convert_filter (fp, fprog->len,NULL , &new_len); +if (ret) + goto free_prog; + +/* Allocate a new seccomp_filter */ + filter =kzalloc (sizeof (struct seccomp_filter ) + +sizeof (struct sock_filter_ext ) * new_len, +GFP_KERNEL |__GFP_NOWARN); +if (!filter) + goto free_prog; + + ret =sk_convert_filter (fp, fprog->len, filter->insns, &new_len); +if (ret) + goto free_filter; +atomic_set (&filter->usage,1 ); + filter->len = new_len;/* * If there is an existing filter, make it the prev and don't drop its @@ -282,8 +269,11 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter->prev = current->seccomp.filter; current->seccomp.filter = filter;return 0 ; -fail: + +free_filter:kfree (filter); +free_prog: +kfree (fp);return ret; }net/core/filter.c +0
-6@@ -388,11 +388,6 @@ load_b:A =0 ;continue ; } -#ifdef CONFIG_SECCOMP_FILTER -case BPF_S_ANC_SECCOMP_LD_W : -A =seccomp_bpf_load (fentry->k); -continue ; -#endif default : WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n", fentry->code, fentry->jt, --1.7.9.5
Kees Cook
Reviewed-byRe: [PATCH v5 net-next 2/3] [RFC] seccomp: convert seccomp to use extended BPF
Quoted 26 lines · Alexei Starovoitov
Quoted 25 lines · Alexei Starovoitov
use
sk_convert_filter()to convert seccomp BPF into extended BPF
05-sim-long_jumps.cof libseccomp was used as micro-benchmark:
seccomp_rule_add_exact(ctx,... seccomp_rule_add_exact(ctx,...
rc =seccomp_load(ctx);
for (i = 0; i < 10000000; i++)syscall(199, 100);--x86_64--
old BPF: 8.6 seconds73.38% bench [kernel.kallsyms] [k] sk_run_filter 10.70% bench libc-2.15.so [.] syscall 5.09% bench [kernel.kallsyms] [k] seccomp_bpf_load 1.97% bench [kernel.kallsyms] [k] system_call ext BPF: 5.7 seconds 66.20% bench [kernel.kallsyms] [k] sk_run_filter_ext 16.75% bench libc-2.15.so [.] syscall 3.31% bench [kernel.kallsyms] [k] system_call 2.88% bench [kernel.kallsyms] [k] __secure_computing--i386---
old BPF: 5.4 sec
ext BPF: 3.8 sec
Quoted 5 lines
Quoted 5 lines
BPF filters generated by seccomp are very branchy, so ext BPF performance is better than old BPF.
Performance gains will be even higher when extended BPF JIT is committed.
Very cool! Have you had a chance to compare on ARM too?
Quoted 6 lines
Quoted 5 lines
Signed-off-by Alexei Starovoitov <[email protected]>---
This patch is an RFC to use extended BPF in seccomp. Change it to do it conditionally withbpf_ext_enableknob ?
Quoted 6 lines
Kees, Will,
I've played with libseccomp to test this patch and just found your other testsuite for bpf+seccomp:
https://github.com/redpig/seccomp
It passes as well onx86_64and i386.
Great! If it's passing those tests, then things should be in good shape.
Quoted 5 lines
Not sure how real all 'seccomp' and libseccomp' bpf filters. Some of them are very short, some very long.
If average number of filtered syscalls is > 10, then upcoming 'ebpf table' will give another performance boost, since single table lookup will be faster than long sequence of 'if' conditions.
To take advantage of that, will seccomp need a new (prctl) interface to pass in a seccomp ebpf?
Quoted 98 lines
Please review.
Thanks
AlexeiQuoted 93 lines
--- include/linux/seccomp.h | 1 - kernel/seccomp.c | 112 +++++++++++++++++++++-------------------------- net/core/filter.c | 5 --- 3 files changed, 51 insertions(+), 67 deletions(-)include/linux/seccomp.h +0
-1@@ -76,7 +76,6 @@ static inline int seccomp_mode(struct seccomp *s)#ifdef CONFIG_SECCOMP_FILTER extern void put_seccomp_filter (struct task_struct *tsk);extern void get_seccomp_filter (struct task_struct *tsk); -extern u32 seccomp_bpf_load (int off);#else /* CONFIG_SECCOMP_FILTER */ static inline void put_seccomp_filter (struct task_struct *tsk) {kernel/seccomp.c +11
-44@@ -55,60 +55,27 @@ struct seccomp_filter {atomic_t usage;struct seccomp_filter *prev;unsigned short len;/* Instruction count */ -struct sock_filter insns[]; +struct sock_filter_ext insns[]; };/* Limit any path through the tree to 256KB worth of instructions. */ #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) -/** -* get_u32 - returns a u32 offset into data -* @data: a unsigned 64 bit value -* @index: 0 or 1 to return the first or second 32-bits -* -* This inline exists to hide the length of unsigned long. If a 32-bit -* unsigned long is passed in, it will be extended and the top 32-bits will be -* 0. If it is a 64-bit unsigned long, then whatever data is resident will be -* properly returned. -* +/* * Endianness is explicitly ignored and left for BPF program authors to manage * as per the specific architecture. */ -static inline u32 get_u32 (u64 data,int index) -{ -return ((u32 *)&data)[index]; -} - -/* Helper for bpf_load below. */ -#define BPF_DATA (_name) offsetof(struct seccomp_data, _name) -/** -* bpf_load: checks and returns a pointer to the requested offset -* @off: offset into struct seccomp_data to load from -* -* Returns the requested 32-bits of data. -* seccomp_check_filter() should assure that @off is 32-bit aligned -* and not out of bounds. Failure to do so is a BUG. -*/ -u32 seccomp_bpf_load (int off) +static void populate_seccomp_data (struct seccomp_data *sd) {struct pt_regs *regs =task_pt_regs (current); -if (off ==BPF_DATA (nr)) -return syscall_get_nr (current, regs); -if (off ==BPF_DATA (arch)) -return syscall_get_arch (current, regs); -if (off >=BPF_DATA (args[0 ]) && off <BPF_DATA (args[6 ])) { -unsigned long value; -int arg = (off -BPF_DATA (args[0 ])) /sizeof (u64); -int index = !!(off %sizeof (u64)); -syscall_get_arguments (current, regs, arg,1 , &value); -return get_u32 (value, index); - } -if (off ==BPF_DATA (instruction_pointer)) -return get_u32 (KSTK_EIP (current),0 ); -if (off ==BPF_DATA (instruction_pointer) +sizeof (u32)) -return get_u32 (KSTK_EIP (current),1 ); -/* seccomp_check_filter should make this impossible. */ -BUG (); +int i; + + sd->nr =syscall_get_nr (current, regs); + sd->arch =syscall_get_arch (current, regs); +for (i =0 ; i <6 ; i++) +syscall_get_arguments (current, regs, i,1 , + (unsigned long *)&sd->args[i]); + sd->instruction_pointer =KSTK_EIP (current); }
Quoted 7 lines
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 103
Quoted 7 lines
kernel/seccomp.c +1
-1/** @@ -133,17 +100,17 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)switch (code) {case BPF_S_LD_W_ABS : - ftest->code =BPF_S_ANC_SECCOMP_LD_W ; + ftest->code =BPF_LDX |BPF_W |BPF_ABS ;
So, with this change, the removal of BPF_S_ANC_SECCOMP_LD_W, and the unconditional use of populate_seccomp_data(), I'm surprised there isn't a larger hit on small filters. Currently, seccomp only loads what it needs into the filter (via BPF_S_ANC_SECCOMP_LD_W + offset via seccomp_bpf_load). Your benchmarks seem to show that this isn't a problem, though. Is the ebpf gain just that much larger than the additional unconditional loads happening in populate_seccomp_data()?
Quoted 95 lines
Quoted 95 lines
kernel/seccomp.c +23
-15/* 32-bit aligned and not out of bounds. */ if (k >=sizeof (struct seccomp_data ) || k &3 )return -EINVAL ;continue ;case BPF_S_LD_W_LEN : - ftest->code =BPF_S_LD_IMM ; + ftest->code =BPF_LD |BPF_IMM ; ftest->k =sizeof (struct seccomp_data );continue ;case BPF_S_LDX_W_LEN : - ftest->code =BPF_S_LDX_IMM ; + ftest->code =BPF_LDX |BPF_IMM ; ftest->k =sizeof (struct seccomp_data );continue ;/* Explicitly include allowed calls. */ @@ -185,6 +152,7 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)case BPF_S_JMP_JGT_X :case BPF_S_JMP_JSET_K :case BPF_S_JMP_JSET_X : +sk_decode_filter (ftest, ftest);continue ;default :return -EINVAL ;@@ -202,18 +170,21 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)static u32 seccomp_run_filters (int syscall) {struct seccomp_filter *f; +struct seccomp_data sd;u32 ret =SECCOMP_RET_ALLOW ;/* Ensure unexpected behavior doesn't result in failing open. */ if (WARN_ON (current->seccomp.filter ==NULL ))return SECCOMP_RET_KILL ; +populate_seccomp_data (&sd); +/* * All filters in the list are evaluated and the lowest BPF return * value always takes priority (ignoring the DATA). */ for (f = current->seccomp.filter; f; f = f->prev) { -u32 cur_ret =sk_run_filter (NULL , f->insns); +u32 cur_ret =sk_run_filter_ext (&sd, f->insns);if ((cur_ret &SECCOMP_RET_ACTION ) < (ret &SECCOMP_RET_ACTION )) ret = cur_ret; }@@ -231,6 +202,8 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)struct seccomp_filter *filter;unsigned long fp_size = fprog->len *sizeof (struct sock_filter );unsigned long total_insns = fprog->len; +struct sock_filter *fp; +int new_len;long ret;if (fprog->len ==0 || fprog->len >BPF_MAXINSNS )@@ -252,28 +225,42 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) CAP_SYS_ADMIN) != 0)return -EACCES ; -/* Allocate a new seccomp_filter */ - filter =kzalloc (sizeof (struct seccomp_filter ) + fp_size, -GFP_KERNEL |__GFP_NOWARN); -if (!filter) + fp =kzalloc (fp_size,GFP_KERNEL |__GFP_NOWARN); +if (!fp)return -ENOMEM ; -atomic_set (&filter->usage,1 ); - filter->len = fprog->len;/* Copy the instructions from fprog. */ ret = -EFAULT ; -if (copy_from_user (filter->insns, fprog->filter, fp_size)) - goto fail; +if (copy_from_user (fp, fprog->filter, fp_size)) + goto free_prog;/* Check and rewrite the fprog via the skb checker */ - ret =sk_chk_filter (filter->insns, filter->len); + ret =sk_chk_filter (fp, fprog->len);if (ret) - goto fail; + goto free_prog;/* Check and rewrite the fprog for seccomp use */ - ret =seccomp_check_filter (filter->insns, filter->len); + ret =seccomp_check_filter (fp, fprog->len);if (ret) - goto fail; + goto free_prog; + +/* convert 'sock_filter' insns to 'sock_filter_ext' insns */ + ret =sk_convert_filter (fp, fprog->len,NULL , &new_len); +if (ret) + goto free_prog; +
Quoted 8 lines
kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 259
Quoted 8 lines
kernel/seccomp.c +8
-0+/* Allocate a new seccomp_filter */ + filter =kzalloc (sizeof (struct seccomp_filter ) + +sizeof (struct sock_filter_ext ) * new_len, +GFP_KERNEL |__GFP_NOWARN); +if (!filter) + goto free_prog; + + ret =sk_convert_filter (fp, fprog->len, filter->insns, &new_len);
Seems like it'd be more readable to set filter->len = new_len first and use &filter->len as the last argument here. I would find that more readable, but if nothing else needs changing in the series, this is fine to leave as-is.
Quoted 31 lines
Quoted 31 lines
kernel/seccomp.c +8
-1+if (ret) + goto free_filter; +atomic_set (&filter->usage,1 ); + filter->len = new_len;/* * If there is an existing filter, make it the prev and don't drop its @@ -282,8 +269,11 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter->prev = current->seccomp.filter; current->seccomp.filter = filter;return 0 ; -fail: + +free_filter:kfree (filter); +free_prog: +kfree (fp);return ret; }net/core/filter.c +0
-3@@ -388,11 +388,6 @@ load_b:A =0 ;continue ; } -#ifdef CONFIG_SECCOMP_FILTER -case BPF_S_ANC_SECCOMP_LD_W : -A =seccomp_bpf_load (fentry->k);
Quoted 8 lines
Quoted 8 lines
net/core/filter.c +0
-3-continue ; -#endif default : WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n", fentry->code, fentry->jt, --1.7.9.5
Cool!
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCH v5 net-next 2/3] [RFC] seccomp: convert seccomp to use extended BPF
Quoted 27 lines · Kees Cook
Quoted 26 lines · Alexei Starovoitov
Quoted 25 lines · Alexei Starovoitov
use
sk_convert_filter()to convert seccomp BPF into extended BPF
05-sim-long_jumps.cof libseccomp was used as micro-benchmark:
seccomp_rule_add_exact(ctx,... seccomp_rule_add_exact(ctx,...
rc =seccomp_load(ctx);
for (i = 0; i < 10000000; i++)syscall(199, 100);--x86_64--
old BPF: 8.6 seconds73.38% bench [kernel.kallsyms] [k] sk_run_filter 10.70% bench libc-2.15.so [.] syscall 5.09% bench [kernel.kallsyms] [k] seccomp_bpf_load 1.97% bench [kernel.kallsyms] [k] system_call ext BPF: 5.7 seconds 66.20% bench [kernel.kallsyms] [k] sk_run_filter_ext 16.75% bench libc-2.15.so [.] syscall 3.31% bench [kernel.kallsyms] [k] system_call 2.88% bench [kernel.kallsyms] [k] __secure_computing--i386---
old BPF: 5.4 sec
ext BPF: 3.8 sec
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
BPF filters generated by seccomp are very branchy, so ext BPF performance is better than old BPF.
Performance gains will be even higher when extended BPF JIT is committed.
Very cool! Have you had a chance to compare on ARM too?
tried arm and was surprised to see 7% regression. grr turned out gcc wasn't unrolling this loop:
for (i = 0; i < 6; i++)
syscall_get_arguments(current, regs, i, 1, ...);which was causing memcpy() of 4 bytes to be called in the hot path for my micro-benchmark.
So have to manually unroll it.
Performance is the following:
--arm32-- short filter
old BPF: 4.0 sec
39.92% bench [kernel.kallsyms] [k] vector_swi 16.60% bench [kernel.kallsyms] [k] sk_run_filter 14.66% bench libc-2.17.so [.] syscall 5.42% bench [kernel.kallsyms] [k] seccomp_bpf_load 5.10% bench [kernel.kallsyms] [k] __secure_computing new BPF: 3.7 sec 35.93% bench [kernel.kallsyms] [k] vector_swi 21.89% bench libc-2.17.so [.] syscall 13.45% bench [kernel.kallsyms] [k] sk_run_filter_ext 6.25% bench [kernel.kallsyms] [k] __secure_computing 3.96% bench [kernel.kallsyms] [k] syscall_trace_exit
--arm32-- large filter
old BPF: 13.5 sec
73.88% bench [kernel.kallsyms] [k] sk_run_filter 10.29% bench [kernel.kallsyms] [k] vector_swi 6.46% bench libc-2.17.so [.] syscall 2.94% bench [kernel.kallsyms] [k] seccomp_bpf_load 1.19% bench [kernel.kallsyms] [k] __secure_computing 0.87% bench [kernel.kallsyms] [k] sys_getuid new BPF: 13.5 sec 76.08% bench [kernel.kallsyms] [k] sk_run_filter_ext 10.98% bench [kernel.kallsyms] [k] vector_swi 5.87% bench libc-2.17.so [.] syscall 1.77% bench [kernel.kallsyms] [k] __secure_computing 0.93% bench [kernel.kallsyms] [k] sys_getuid
So will resend the V6 for this patch set.
Quoted 16 lines
Quoted 7 lines
Quoted 6 lines
Signed-off-by Alexei Starovoitov <[email protected]>---
This patch is an RFC to use extended BPF in seccomp. Change it to do it conditionally withbpf_ext_enableknob ?Quoted 6 lines
Kees, Will,
I've played with libseccomp to test this patch and just found your other testsuite for bpf+seccomp:
https://github.com/redpig/seccomp
It passes as well onx86_64and i386.Great! If it's passing those tests, then things should be in good shape.
Quoted 8 lines
Quoted 5 lines
Not sure how real all 'seccomp' and libseccomp' bpf filters. Some of them are very short, some very long.
If average number of filtered syscalls is > 10, then upcoming 'ebpf table' will give another performance boost, since single table lookup will be faster than long sequence of 'if' conditions.To take advantage of that, will seccomp need a new (prctl) interface to pass in a seccomp ebpf?
I hope not.
I think it's better to have inband signaling for ext vs old program. Like 'struct sock_fprog -> len' must be < 4096 for old programs.
if len == 4096, this can mean that this is extended bpf program in a new format. This way prctl() for seccomp and socket attach mechanisms can stay the same.
Quoted 101 lines
Quoted 101 lines
Please review.
Thanks
AlexeiQuoted 96 lines
--- include/linux/seccomp.h | 1 - kernel/seccomp.c | 112 +++++++++++++++++++++-------------------------- net/core/filter.c | 5 --- 3 files changed, 51 insertions(+), 67 deletions(-)include/linux/seccomp.h +0
-1@@ -76,7 +76,6 @@ static inline int seccomp_mode(struct seccomp *s)#ifdef CONFIG_SECCOMP_FILTER extern void put_seccomp_filter (struct task_struct *tsk);extern void get_seccomp_filter (struct task_struct *tsk); -extern u32 seccomp_bpf_load (int off);#else /* CONFIG_SECCOMP_FILTER */ static inline void put_seccomp_filter (struct task_struct *tsk) {kernel/seccomp.c +11
-44@@ -55,60 +55,27 @@ struct seccomp_filter {atomic_t usage;struct seccomp_filter *prev;unsigned short len;/* Instruction count */ -struct sock_filter insns[]; +struct sock_filter_ext insns[]; };/* Limit any path through the tree to 256KB worth of instructions. */ #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) -/** -* get_u32 - returns a u32 offset into data -* @data: a unsigned 64 bit value -* @index: 0 or 1 to return the first or second 32-bits -* -* This inline exists to hide the length of unsigned long. If a 32-bit -* unsigned long is passed in, it will be extended and the top 32-bits will be -* 0. If it is a 64-bit unsigned long, then whatever data is resident will be -* properly returned. -* +/* * Endianness is explicitly ignored and left for BPF program authors to manage * as per the specific architecture. */ -static inline u32 get_u32 (u64 data,int index) -{ -return ((u32 *)&data)[index]; -} - -/* Helper for bpf_load below. */ -#define BPF_DATA (_name) offsetof(struct seccomp_data, _name) -/** -* bpf_load: checks and returns a pointer to the requested offset -* @off: offset into struct seccomp_data to load from -* -* Returns the requested 32-bits of data. -* seccomp_check_filter() should assure that @off is 32-bit aligned -* and not out of bounds. Failure to do so is a BUG. -*/ -u32 seccomp_bpf_load (int off) +static void populate_seccomp_data (struct seccomp_data *sd) {struct pt_regs *regs =task_pt_regs (current); -if (off ==BPF_DATA (nr)) -return syscall_get_nr (current, regs); -if (off ==BPF_DATA (arch)) -return syscall_get_arch (current, regs); -if (off >=BPF_DATA (args[0 ]) && off <BPF_DATA (args[6 ])) { -unsigned long value; -int arg = (off -BPF_DATA (args[0 ])) /sizeof (u64); -int index = !!(off %sizeof (u64)); -syscall_get_arguments (current, regs, arg,1 , &value); -return get_u32 (value, index); - } -if (off ==BPF_DATA (instruction_pointer)) -return get_u32 (KSTK_EIP (current),0 ); -if (off ==BPF_DATA (instruction_pointer) +sizeof (u32)) -return get_u32 (KSTK_EIP (current),1 ); -/* seccomp_check_filter should make this impossible. */ -BUG (); +int i; + + sd->nr =syscall_get_nr (current, regs); + sd->arch =syscall_get_arch (current, regs); +for (i =0 ; i <6 ; i++) +syscall_get_arguments (current, regs, i,1 , + (unsigned long *)&sd->args[i]); + sd->instruction_pointer =KSTK_EIP (current); }/** @@ -133,17 +100,17 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
Quoted 12 lines
Quoted 4 lines
kernel/seccomp.c static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) · line 103
Quoted 4 lines
kernel/seccomp.c +1
-1switch (code) {case BPF_S_LD_W_ABS : - ftest->code =BPF_S_ANC_SECCOMP_LD_W ; + ftest->code =BPF_LDX |BPF_W |BPF_ABS ;So, with this change, the removal of
BPF_S_ANC_SECCOMP_LD_W, and the unconditional use ofpopulate_seccomp_data(), I'm surprised there isn't a larger hit on small filters. Currently, seccomp only loads what it needs into the filter (viaBPF_S_ANC_SECCOMP_LD_W+ offset viaseccomp_bpf_load). Your benchmarks seem to show that this isn't a problem, though. Is the ebpf gain just that much larger than the additional unconditional loads happening inpopulate_seccomp_data()?
on arm there is a small gain for short programs.
for x86_64 there is also small gain:
--x86_64-- short filter
old BPF: 2.7 sec
39.12% bench libc-2.15.so [.] syscall 8.10% bench [kernel.kallsyms] [k] sk_run_filter 6.31% bench [kernel.kallsyms] [k] system_call 5.59% bench [kernel.kallsyms] [k] trace_hardirqs_on_caller 4.37% bench [kernel.kallsyms] [k] trace_hardirqs_off_caller 3.70% bench [kernel.kallsyms] [k] __secure_computing 3.67% bench [kernel.kallsyms] [k] lock_is_held 3.03% bench [kernel.kallsyms] [k] seccomp_bpf_load new BPF: 2.49 sec 42.05% bench libc-2.15.so [.] syscall 6.91% bench [kernel.kallsyms] [k] system_call 6.25% bench [kernel.kallsyms] [k] trace_hardirqs_on_caller 6.07% bench [kernel.kallsyms] [k] __secure_computing 5.08% bench [kernel.kallsyms] [k] sk_run_filter_ext
obviously most of the time is spent in 'syscal' instruction in syscall() function
but having populate_seccomp_data() is actually beneficial, since 'current' and 'pt_regs' are hot in cache anyway,
so copying them into seccomp_data is cheaper than extra 'if' conditions and extra calls from within the filter.
faster interpreter helps too.
Quoted 100 lines
Quoted 100 lines
Quoted 100 lines
kernel/seccomp.c +28
-15/* 32-bit aligned and not out of bounds. */ if (k >=sizeof (struct seccomp_data ) || k &3 )return -EINVAL ;continue ;case BPF_S_LD_W_LEN : - ftest->code =BPF_S_LD_IMM ; + ftest->code =BPF_LD |BPF_IMM ; ftest->k =sizeof (struct seccomp_data );continue ;case BPF_S_LDX_W_LEN : - ftest->code =BPF_S_LDX_IMM ; + ftest->code =BPF_LDX |BPF_IMM ; ftest->k =sizeof (struct seccomp_data );continue ;/* Explicitly include allowed calls. */ @@ -185,6 +152,7 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)case BPF_S_JMP_JGT_X :case BPF_S_JMP_JSET_K :case BPF_S_JMP_JSET_X : +sk_decode_filter (ftest, ftest);continue ;default :return -EINVAL ;@@ -202,18 +170,21 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)static u32 seccomp_run_filters (int syscall) {struct seccomp_filter *f; +struct seccomp_data sd;u32 ret =SECCOMP_RET_ALLOW ;/* Ensure unexpected behavior doesn't result in failing open. */ if (WARN_ON (current->seccomp.filter ==NULL ))return SECCOMP_RET_KILL ; +populate_seccomp_data (&sd); +/* * All filters in the list are evaluated and the lowest BPF return * value always takes priority (ignoring the DATA). */ for (f = current->seccomp.filter; f; f = f->prev) { -u32 cur_ret =sk_run_filter (NULL , f->insns); +u32 cur_ret =sk_run_filter_ext (&sd, f->insns);if ((cur_ret &SECCOMP_RET_ACTION ) < (ret &SECCOMP_RET_ACTION )) ret = cur_ret; }@@ -231,6 +202,8 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)struct seccomp_filter *filter;unsigned long fp_size = fprog->len *sizeof (struct sock_filter );unsigned long total_insns = fprog->len; +struct sock_filter *fp; +int new_len;long ret;if (fprog->len ==0 || fprog->len >BPF_MAXINSNS )@@ -252,28 +225,42 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) CAP_SYS_ADMIN) != 0)return -EACCES ; -/* Allocate a new seccomp_filter */ - filter =kzalloc (sizeof (struct seccomp_filter ) + fp_size, -GFP_KERNEL |__GFP_NOWARN); -if (!filter) + fp =kzalloc (fp_size,GFP_KERNEL |__GFP_NOWARN); +if (!fp)return -ENOMEM ; -atomic_set (&filter->usage,1 ); - filter->len = fprog->len;/* Copy the instructions from fprog. */ ret = -EFAULT ; -if (copy_from_user (filter->insns, fprog->filter, fp_size)) - goto fail; +if (copy_from_user (fp, fprog->filter, fp_size)) + goto free_prog;/* Check and rewrite the fprog via the skb checker */ - ret =sk_chk_filter (filter->insns, filter->len); + ret =sk_chk_filter (fp, fprog->len);if (ret) - goto fail; + goto free_prog;/* Check and rewrite the fprog for seccomp use */ - ret =seccomp_check_filter (filter->insns, filter->len); + ret =seccomp_check_filter (fp, fprog->len);if (ret) - goto fail; + goto free_prog; + +/* convert 'sock_filter' insns to 'sock_filter_ext' insns */ + ret =sk_convert_filter (fp, fprog->len,NULL , &new_len); +if (ret) + goto free_prog; + +/* Allocate a new seccomp_filter */ + filter =kzalloc (sizeof (struct seccomp_filter ) + +sizeof (struct sock_filter_ext ) * new_len, +GFP_KERNEL |__GFP_NOWARN); +if (!filter)
Quoted 8 lines
Quoted 3 lines
kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 259
Quoted 3 lines
kernel/seccomp.c +3
-0+ goto free_prog; + + ret =sk_convert_filter (fp, fprog->len, filter->insns, &new_len);Seems like it'd be more readable to set
filter->len=new_lenfirst and use&filter->lenas the last argument here. I would find that more readable, but if nothing else needs changing in the series, this is fine to leave as-is.
cannot do that, since filter->len is 'short', but new_len and sk_convert_filter()
expects 'int'
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines
kernel/seccomp.c +8
-1+if (ret) + goto free_filter; +atomic_set (&filter->usage,1 ); + filter->len = new_len;/* * If there is an existing filter, make it the prev and don't drop its @@ -282,8 +269,11 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter->prev = current->seccomp.filter; current->seccomp.filter = filter;return 0 ; -fail: + +free_filter:kfree (filter); +free_prog: +kfree (fp);return ret; }net/core/filter.c +0
-3@@ -388,11 +388,6 @@ load_b:A =0 ;continue ; } -#ifdef CONFIG_SECCOMP_FILTER -case BPF_S_ANC_SECCOMP_LD_W : -A =seccomp_bpf_load (fentry->k);Quoted 8 lines
Quoted 8 lines
net/core/filter.c +0
-3-continue ; -#endif default : WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n", fentry->code, fentry->jt, --1.7.9.5
Cool!
Quoted 1 line
Reviewed-by Kees Cook <[email protected]>
Thank you for review!
Will send V6 with unrolled loop,
fix empty new line at the end of the file caught by Daniel and update commit log with arm seccomp data.
Thanks!
Alexei
PATCH v5 3/3 doc: filter: add Extended BPF documentation
In reply to Alexei Starovoitov--- Documentation/networking/filter.txt | 181 +++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+)
Documentation/networking/filter.txt +181 -0
@@ -546,6 +546,186 @@ ffffffffa0069c8f + <x>: For BPF JIT developers, bpf_jit_disasm, bpf_asm and bpf_dbg provides a useful toolchain for developing and testing the kernel's JIT compiler. +Extended BPF +------------ +Extended BPF extends BPF in the following ways: +- from 2 to 10 registers + Original BPF has two registers (A and X) and hidden frame pointer. + Extended BPF has ten registers and read-only frame pointer. +- from 32-bit registers to 64-bit registers + semantics of old 32-bit ALU operations are preserved via 32-bit + subregisters +- if (cond) jump_true; else jump_false; + old BPF insns are replaced with: + if (cond) jump_true; /* else fallthrough */ +- adds signed > and >= insns +- 16 4-byte stack slots for register spill-fill replaced with + up to 512 bytes of multi-use stack space +- introduces bpf_call insn and register passing convention for zero + overhead calls from/to other kernel functions (not part of this patch) +- adds arithmetic right shift insn +- adds swab32/swab64 insns +- adds atomic_add insn +- old tax/txa insns are replaced with 'mov dst,src' insn + +Extended BPF is designed to be JITed with one to one mapping, which +allows GCC/LLVM compilers to generate optimized BPF code that performs +almost as fast as natively compiled code + +sysctl net.core.bpf_ext_enable=1 +controls whether filters attached to sockets will be automatically +converted to extended BPF or not. + +BPF is safe dynamically loadable program that can call fixed set +of kernel functions and takes a pointer to data as an input, +where data is skb, seccomp_data, kprobe function arguments or else. + +Extended Instruction Set was designed with these goals: +- write programs in restricted C and compile into BPF with GCC/LLVM +- just-in-time map to modern 64-bit CPU with minimal performance overhead + over two steps: C -> BPF -> native code +- guarantee termination and safety of BPF program in kernel + with simple algorithm + +GCC/LLVM-bpf backend is optional. +Extended BPF can be coded with macroses from filter.h just like original BPF, +though the same filter done in C is easier to understand. +sk_convert_filter() remaps original BPF insns into extended. + +Minimal performance overhead is achieved by having one to one mapping +between BPF insns and native insns, and one to one mapping between BPF +registers and native registers on 64-bit CPUs + +Extended BPF may allow jump forward and backward for two reasons: +to reduce branch mispredict penalty compiler moves cold basic blocks out of +fall-through path and to reduce code duplication that would be hard to avoid +if only jump forward was available. +To guarantee termination simple non-recursive depth-first-search verifies +that there are no back-edges (no loops in the program), program is a DAG +with root at the first insn, all branches end at the last RET insn and +all instructions are reachable. +Original BPF actually allows unreachable insns. Though it's safe, it will be +fixed when extended BPF replaces BPF completely. + +Original BPF has two registers (A and X) and hidden frame pointer. +Extended BPF has ten registers and read-only frame pointer. +Since 64-bit CPUs are passing arguments to the functions via registers +the number of args from BPF program to in-kernel function is restricted to 5 +and one register is used to accept return value from in-kernel function. +x86_64 passes first 6 arguments in registers. +aarch64/sparcv9/mips64 have 7-8 registers for arguments. +x86_64 has 6 callee saved registers. +aarch64/sparcv9/mips64 have 11 or more callee saved registers. + +Therefore extended BPF calling convention is defined as: +R0 - return value from in-kernel function +R1-R5 - arguments from BPF program to in-kernel function +R6-R9 - callee saved registers that in-kernel function will preserve +R10 - read-only frame pointer to access stack + +so that all BPF registers map one to one to HW registers on x86_64,aarch64,etc +and BPF calling convention maps directly to ABIs used by kernel on 64-bit +architectures. +On 32-bit architectures JIT may map programs that use only 32-bit arithmetic +and let more complex programs to be interpreted. + +R0-R5 are scratch registers and BPF program needs spill/fill them if necessary +across calls. +Note that there is only one BPF program == one BPF function and it cannot call +other BPF functions. It can only call predefined in-kernel functions. + +All BPF registers are 64-bit with 32-bit lower subregister that zero-extends +into 64-bit if written to. That behavior maps directly to x86_64 and arm64 +subregister defintion, but makes other JITs more difficult. + +Original BPF and extended BPF are two operand instructions, which helps +to do one-to-one mapping between BPF insn and x86 insn during JIT. + +Extended BPF doesn't have pre-defined endianness not to favor one +architecture vs another. Therefore bswap insn is available. +Original BPF doesn't have such insn and does bswap as part of sk_load_word call +which is often unnecessary if we want to compare the value with the constant. +Restricted C code might be written differently depending on endianness +and GCC/LLVM-bpf will take an endianness flag. + +32-bit architectures run 64-bit extended BPF programs via interpreter. +Their JITs may convert BPF programs that only use 32-bit subregs into native +instruction set and let the rest being interpreted. + +Extended BPF is 64-bit, because on 64-bit architectures, pointers are 64-bit +and we want to pass 64-bit values in/out kernel functions, so 32-bit BPF +registers would require to define register-pair ABI, there won't be a direct +BPF register to HW register mapping and JIT would need to do +combine/split/move operations for every register in and out of the function, +which is complex, bug prone and slow. +Another reason is atomic 64-bit counters + +Just like original BPF, extended BPF is safe, deterministic and kernel can +easily prove that. The safety of the program is determined in two steps. +First step does depth-first-search to disallow loops and other CFG validation. +Second step starts from the first insn and descends all possible paths. +It simulates execution of every insn and observes the state change of +registers and stack. +At the start of the program the register R1 contains a pointer to context +and has type PTR_TO_CTX. If checker sees an insn that does R2=R1, then R2 has +now type PTR_TO_CTX as well and can be used on right hand side of expression. +If R1=PTR_TO_CTX and insn is R2=R1+1, then R2=INVALID_PTR and it is readable. +If register was never written to, it's not readable. +After kernel function call, R1-R5 are reset to unreadable and R0 has a return +type of the function. Since R6-R9 are callee saved, their state is preserved +across the call. +load/store instructions are allowed only with registers of valid types, which +are PTR_TO_CTX, PTR_TO_TABLE, PTR_TO_STACK. They are bounds and alginment +checked. + +Input context pointer is generic. Its contents are defined by specific use case. +For seccomp R1 points to seccomp_data +For converted BPF filters R1 points to skb +Through get_context_access callback BPF checker is customized, so that BPF +program can only access certain fields of input context with specified size +and alignment. +For example, the following insn: + BPF_INSN_LD(BPF_W, R0, R6, 8) +intends to load word from address R6 + 8 and store it into R0 +If R6=PTR_TO_CTX, then get_context_access callback should let the checker know +that offset 8 of size 4 bytes can be accessed for reading, otherwise the checker +will reject the program. + +If R6=PTR_TO_STACK, then access should be aligned and be within stack bounds, +which are hard coded to [-480, 0]. In this example offset is 8, so it will fail +verification. +The checker will allow BPF program to read data from stack only after it wrote +into it. +Pointer register spill/fill is tracked as well, since four (R6-R9) callee saved +registers may not be enough for some programs. + +Allowed function calls are customized via get_func_proto callback. + +One of the useful functions that can be made available to BPF program +are bpf_table_lookup/bpf_table_update. +They can help tracing filters collect different types of statistics. +Example: pc addresses for drop_monitor filter + +In seccomp and socket filter use cases extended BPF program consists +of intructions only, but for tracing filters case BPF program may contain +BPF tables as well. +There are no special instructions to access BPF tables. The access is done +via function calls. + +BPF program identifies the table by table_id and accesses it in C like: +elem = bpf_table_lookup(ctx, table_id, key); + +BPF checker matches 'table_id' against known tables, verifies that 'key' points +to stack and table->key_size bytes are initialized. +bpf_table_lookup() is a normal kernel function. It needs to do a lookup and +return either valid pointer to the element or NULL. +BPF checker will verify that the program accesses the pointer only +after comparing it to NULL. +It's up to implementation to decide how lookup is done and meaning of the key. + +Just like original, extended BPF is limited to 4096 insns, which means that any +program will terminate quickly and will call fixed number of kernel functions. + Misc ----
@@ -561,3 +741,4 @@ the underlying architecture. Jay Schulist <[email protected]> Daniel Borkmann <[email protected]> +Alexei Starovoitov <[email protected]>
Signature · 1 line
1.7.9.5
Re: [PATCH v5 net-next 3/3] doc: filter: add Extended BPF documentation
Quoted 1 line · Alexei Starovoitov
Signed-off-by Alexei Starovoitov <[email protected]>