bpf: introduce direct packet access
Subsystems (7)
PATCH 7/7 samples/bpf: add verifier tests
In reply to Alexei Starovoitovadd few tests for "pointer to packet" logic of the verifier
--- samples/bpf/test_verifier.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
samples/bpf/test_verifier.c +80 -0
@@ -1448,6 +1448,86 @@ static struct bpf_test tests[] = { .result = ACCEPT, .prog_type = BPF_PROG_TYPE_SCHED_CLS, }, + { + "pkt: test1", + .insns = { +BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 , + offsetof(struct __sk_buff , data)), +BPF_LDX_MEM (BPF_W ,BPF_REG_3 ,BPF_REG_1 , + offsetof(struct __sk_buff , data_end)), +BPF_MOV64_REG (BPF_REG_0 ,BPF_REG_2 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_0 ,8 ), +BPF_JMP_REG (BPF_JGT ,BPF_REG_0 ,BPF_REG_3 ,1 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_2 ,0 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .result =ACCEPT , + .prog_type = BPF_PROG_TYPE_SCHED_CLS, + }, + { +"pkt: test2" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,1 ), +BPF_LDX_MEM (BPF_W ,BPF_REG_4 ,BPF_REG_1 , + offsetof(struct __sk_buff , data_end)), +BPF_LDX_MEM (BPF_W ,BPF_REG_3 ,BPF_REG_1 , + offsetof(struct __sk_buff , data)), +BPF_MOV64_REG (BPF_REG_5 ,BPF_REG_3 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_5 ,14 ), +BPF_JMP_REG (BPF_JGT ,BPF_REG_5 ,BPF_REG_4 ,15 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_3 ,7 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_4 ,BPF_REG_3 ,12 ), +BPF_ALU64_IMM (BPF_MUL ,BPF_REG_4 ,14 ), +BPF_LDX_MEM (BPF_W ,BPF_REG_3 ,BPF_REG_1 , + offsetof(struct __sk_buff , data)), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_3 ,BPF_REG_4 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_1 ), +BPF_ALU64_IMM (BPF_LSH ,BPF_REG_2 ,48 ), +BPF_ALU64_IMM (BPF_RSH ,BPF_REG_2 ,48 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_3 ,BPF_REG_2 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_3 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,8 ), +BPF_LDX_MEM (BPF_W ,BPF_REG_1 ,BPF_REG_1 , + offsetof(struct __sk_buff , data_end)), +BPF_JMP_REG (BPF_JGT ,BPF_REG_2 ,BPF_REG_1 ,1 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_1 ,BPF_REG_3 ,4 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .result =ACCEPT , + .prog_type = BPF_PROG_TYPE_SCHED_CLS, + }, + { +"pkt: test3" , + .insns = { +BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 , + offsetof(struct __sk_buff , data)), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access off=76" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, + }, + { +"pkt: test4" , + .insns = { +BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 , + offsetof(struct __sk_buff , data)), +BPF_LDX_MEM (BPF_W ,BPF_REG_3 ,BPF_REG_1 , + offsetof(struct __sk_buff , data_end)), +BPF_MOV64_REG (BPF_REG_0 ,BPF_REG_2 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_0 ,8 ), +BPF_JMP_REG (BPF_JGT ,BPF_REG_0 ,BPF_REG_3 ,1 ), +BPF_STX_MEM (BPF_B ,BPF_REG_2 ,BPF_REG_2 ,0 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .errstr ="cannot write" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_SCHED_CLS, + }, };static int probe_filter_length (struct bpf_insn *fp)
Signature · 1 line
2.8.0
cover bpf: introduce direct packet access
This set of patches introduce 'direct packet access' from cls_bpf and act_bpf programs (which are root only).
Current bpf programs use LD_ABS, LD_INS instructions which have to do 'if (off < skb_headlen)' for every packet access. It's ok for socket filters, but too slow for XDP, since single LD_ABS insn consumes 3% of cpu. Therefore we have to amortize the cost of length check over multiple packet accesses via direct access to skb->data, data_end pointers.
The existing packet parser typically look like:
if (load_half(skb, offsetof(struct ethhdr, h_proto)) != ETH_P_IP)
return 0;
if (load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol)) != IPPROTO_UDP ||
load_byte(skb, ETH_HLEN) != 0x45)
return 0; ...
with 'direct packet access' the bpf program becomes:
void *data = (void *)(long)skb->data; void *data_end = (void *)(long)skb->data_end; struct eth_hdr *eth = data;
struct iphdr *iph = data + sizeof(*eth);
if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end)
return 0;
if (eth->h_proto != htons(ETH_P_IP))
return 0;
if (iph->protocol != IPPROTO_UDP || iph->ihl != 5)
return 0; ...
which is more natural to write and significantly faster. See patch 6 for performance tests:
21Mpps(old) vs 24Mpps(new) with just 5 loads. For more complex parsers the performance gain is higher.
The other approach implemented in [1] was adding two new instructions to interpreter and JITs and was too hard to use from llvm side. The approach presented here doesn't need any instruction changes, but the verifier has to work harder to check safety of the packet access.
Patch 1 prepares the code and Patch 2 adds new checks for direct packet access and all of them are gated with 'env->allow_ptr_leaks' which is true for root only.
Patch 3 improves search pruning for large programs. Patch 4 wires in verifier's changes with net/core/filter side. Patch 5 updates docs
Patches 6 and 7 add tests.
[1] https://git.kernel.org/cgit/linux/kernel/git/ast/bpf.git/?h=ld_abs_dw
Alexei Starovoitov (7):
bpf: cleanup verifier code
bpf: direct packet access
bpf: improve verifier state equivalence
bpf: wire in data and data_end for cls_act_bpf
bpf: add documentation for 'direct packet access' samples/bpf: add 'pointer to packet' tests samples/bpf: add verifier tests
Documentation/networking/filter.txt | 85 +++++- include/linux/filter.h | 16 + include/uapi/linux/bpf.h | 2 + kernel/bpf/core.c | 5 + kernel/bpf/verifier.c | 562 +++++++++++++++++++++++++++++++----- net/core/filter.c | 51 +++- net/sched/act_bpf.c | 2 + net/sched/cls_bpf.c | 2 + samples/bpf/Makefile | 2 + samples/bpf/parse_ldabs.c | 41 +++ samples/bpf/parse_simple.c | 48 +++ samples/bpf/parse_varlen.c | 153 ++++++++++ samples/bpf/test_cls_bpf.sh | 37 +++ samples/bpf/test_verifier.c | 80 +++++ 14 files changed, 1004 insertions(+), 82 deletions(-) create mode 100644 samples/bpf/parse_ldabs.c create mode 100644 samples/bpf/parse_simple.c create mode 100644 samples/bpf/parse_varlen.c create mode 100755 samples/bpf/test_cls_bpf.sh
Signature · 1 line
2.8.0
PATCH 2/7 bpf: direct packet access
Extended BPF carried over two instructions from classic to access packet data: LD_ABS and LD_IND. They're highly optimized in JITs, but due to their design they have to do length check for every access. When BPF is processing 20M packets per second single LD_ABS after JIT is consuming 3% cpu. Hence the need to optimize it further by amortizing the cost of 'off < skb_headlen' over multiple packet accesses. One option is to introduce two new eBPF instructions LD_ABS_DW and LD_IND_DW with similar usage as skb_header_pointer().
The kernel part for interpreter and x64 JIT was implemented in [1], but such new insns behave like old ld_abs and abort the program with 'return 0' if access is beyond linear data. Such hidden control flow is hard to workaround plus changing JITs and rolling out new llvm is incovenient.
Therefore allow cls_bpf/act_bpf program access skb->data directly:
int bpf_prog(struct __sk_buff *skb)
{
struct iphdr *ip;
if (skb->data + sizeof(struct iphdr) + ETH_HLEN > skb->data_end)
/* packet too small */
return 0;ip = skb->data + ETH_HLEN;
/* access IP header fields with direct loads */ if (ip->version != 4 || ip->saddr == 0x7f000001)
return 1;
[...]
}
This solution avoids introduction of new instructions. llvm stays the same and all JITs stay the same, but verifier has to work extra hard to prove safety of the above program.
For XDP the direct store instructions can be allowed as well.
The skb->data is NET_IP_ALIGNED, so for common cases the verifier can check the alignment. The complex packet parsers where packet pointer is adjusted incrementally cannot be tracked for alignment, so allow byte access in such cases and misaligned access on architectures that define efficient_unaligned_access
[1] https://git.kernel.org/cgit/linux/kernel/git/ast/bpf.git/?h=ld_abs_dw
--- include/uapi/linux/bpf.h | 2 + kernel/bpf/core.c | 5 + kernel/bpf/verifier.c | 441 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 440 insertions(+), 8 deletions(-)
include/uapi/linux/bpf.h +2 -0
@@ -370,6 +370,8 @@ struct __sk_buff {__u32 cb[5 ];__u32 hash;__u32 tc_classid; +__u32 data; +__u32 data_end; };struct bpf_tunnel_key {
kernel/bpf/core.c +5 -0
@@ -794,6 +794,11 @@ void __weak bpf_int_jit_compile(struct bpf_prog *prog) { } +bool __weak bpf_helper_changes_skb_data (void *func) +{ +return false; +} +/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call * skb_copy_bits(), so provide a weak definition of it for NET-less config. */
kernel/bpf/verifier.c +433 -8
564 lines not on this page · raw message
Signature · 1 line
2.8.0
PATCH 3/7 bpf: improve verifier state equivalence
In reply to Alexei Starovoitovsince UNKNOWN_VALUE type is weaker than CONST_IMM we can un-teach verifier its recognition of constants in conditional branches without affecting safety.
Ex:
if (reg == 123) {
.. here verifier was marking reg->type as CONST_IMM
instead keep reg as UNKNOWN_VALUE
}
Two verifier states with UNKNOWN_VALUE are equivalent, whereas CONST_IMM_X != CONST_IMM_Y, since CONST_IMM is used for stack range verification and other cases.
So help search pruning by marking registers as UNKNOWN_VALUE where possible instead of CONST_IMM.
--- kernel/bpf/verifier.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-)
kernel/bpf/verifier.c +3 -20
@@ -1704,12 +1704,11 @@ static int check_cond_jmp_op(struct verifier_env *env,*/ regs[insn->dst_reg].type =PTR_TO_MAP_VALUE ;/* branch targer cannot access it, since reg == 0 */ - other_branch->regs[insn->dst_reg].type =CONST_IMM ; - other_branch->regs[insn->dst_reg].imm =0 ; +mark_reg_unknown_value (other_branch->regs, + insn->dst_reg); }else { other_branch->regs[insn->dst_reg].type =PTR_TO_MAP_VALUE ; - regs[insn->dst_reg].type =CONST_IMM ; - regs[insn->dst_reg].imm =0 ; +mark_reg_unknown_value (regs, insn->dst_reg); } } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && dst_reg->type == PTR_TO_PACKET &&
@@ -1718,22 +1717,6 @@ static int check_cond_jmp_op(struct verifier_env *env, }else if (is_pointer_value (env, insn->dst_reg)) {verbose ("R%d pointer comparison prohibited\n" , insn->dst_reg);return -EACCES ; - }else if (BPF_SRC (insn->code) ==BPF_K && - (opcode ==BPF_JEQ || opcode ==BPF_JNE )) { - -if (opcode ==BPF_JEQ ) { -/* detect if (R == imm) goto -* and in the target state recognize that R = imm -*/ - other_branch->regs[insn->dst_reg].type =CONST_IMM ; - other_branch->regs[insn->dst_reg].imm = insn->imm; - }else { -/* detect if (R != imm) goto -* and in the fall-through state recognize that R = imm -*/ - regs[insn->dst_reg].type =CONST_IMM ; - regs[insn->dst_reg].imm = insn->imm; - } }if (log_level)print_verifier_state (&env->cur_state);
Signature · 1 line
2.8.0
PATCH 6/7 samples/bpf: add 'pointer to packet' tests
In reply to Alexei Starovoitovparse_simple.c - packet parser exapmle with single length check that filters out udp packets for port 9
parse_varlen.c - variable length parser that understand multiple vlan headers, ipip, ipip6 and ip options to filter out udp or tcp packets on port 9. The packet is parsed layer by layer with multitple length checks.
parse_ldabs.c - classic style of packet parsing using LD_ABS instruction. Same functionality as parse_simple.
simple = 24.1Mpps per core
varlen = 22.7Mpps
ldabs = 21.4Mpps
Parser with LD_ABS instructions is slower than full direct access parser which does more packet accesses and checks.
These examples demonstrate the choice bpf program authors can make between flexibility of the parser vs speed.
--- samples/bpf/Makefile | 2 + samples/bpf/parse_ldabs.c | 41 ++++++++++++ samples/bpf/parse_simple.c | 48 ++++++++++++++ samples/bpf/parse_varlen.c | 153 ++++++++++++++++++++++++++++++++++++++++++++ samples/bpf/test_cls_bpf.sh | 37 +++++++++++ 5 files changed, 281 insertions(+) create mode 100644 samples/bpf/parse_ldabs.c create mode 100644 samples/bpf/parse_simple.c create mode 100644 samples/bpf/parse_varlen.c create mode 100755 samples/bpf/test_cls_bpf.sh
samples/bpf/Makefile +2 -0
@@ -60,6 +60,7 @@ always += spintest_kern.o always += map_perf_test_kern.o always += test_overhead_tp_kern.o always += test_overhead_kprobe_kern.o +always += parse_varlen.o parse_simple.o parse_ldabs.o HOSTCFLAGS += -I$(objtree)/usr/include
@@ -120,4 +121,5 @@ $(src)/*.c: verify_target_bpf $(obj)/%.o: $(src)/%.c $(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) \ -D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \ + -Wno-compare-distinct-pointer-types \ -O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf -filetype=obj -o $@
samples/bpf/parse_ldabs.c new file +41 -0
41 lines not on this page · raw message
samples/bpf/parse_simple.c new file +48 -0
48 lines not on this page · raw message
samples/bpf/parse_varlen.c new file +153 -0
153 lines not on this page · raw message
samples/bpf/test_cls_bpf.sh new file +37 -0
@@ -0,0 +1,37 @@ +#!/bin/bash + +function pktgen { + ../pktgen/pktgen_bench_xmit_mode_netif_receive.sh -i $IFC -s 64 \ + -m 90:e2:ba:ff:ff:ff -d 192.168.0.1 -t 4 + local dropped=`tc -s qdisc show dev $IFC | tail -3 | awk '/drop/{print $7}'` + if [ "$dropped" == "0," ]; then + echo "FAIL" + else + echo "Successfully filtered " $dropped " packets" + fi +} + +function test { + echo -n "Loading bpf program '$2'... " + tc qdisc add dev $IFC clsact + tc filter add dev $IFC ingress bpf da obj $1 sec $2 + local status=$? + if [ $status -ne 0 ]; then + echo "FAIL" + else + echo "ok" + pktgen + fi + tc qdisc del dev $IFC clsact +} + +IFC=test_veth + +ip link add name $IFC type veth peer name pair_$IFC +ip link set $IFC up +ip link set pair_$IFC up + +test ./parse_simple.o simple +test ./parse_varlen.o varlen +test ./parse_ldabs.o ldabs +ip link del dev $IFC
Signature · 1 line
2.8.0
PATCH 1/7 bpf: cleanup verifier code
In reply to Alexei Starovoitovcleanup verifier code and prepare it for addition of "pointer to packet" logic
--- kernel/bpf/verifier.c | 100 ++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 47 deletions(-)
kernel/bpf/verifier.c +53 -47
@@ -249,28 +249,30 @@ static const char * const reg_type_str[] = { [CONST_IMM] = "imm", }; -static void print_verifier_state (struct verifier_env *env) +static void print_verifier_state (struct verifier_state *state) { +struct reg_state *reg;enum bpf_reg_type t;int i;for (i =0 ; i <MAX_BPF_REG ; i++) { - t = env->cur_state.regs[i].type; + reg = &state->regs[i]; + t = reg->type;if (t ==NOT_INIT )continue ;verbose (" R%d=%s" , i, reg_type_str[t]);if (t ==CONST_IMM || t ==PTR_TO_STACK ) -verbose ("%ld" , env->cur_state.regs[i].imm); +verbose ("%ld" , reg->imm);else if (t ==CONST_PTR_TO_MAP || t ==PTR_TO_MAP_VALUE || t ==PTR_TO_MAP_VALUE_OR_NULL )verbose ("(ks=%d,vs=%d)" , - env->cur_state.regs[i].map_ptr->key_size, - env->cur_state.regs[i].map_ptr->value_size); + reg->map_ptr->key_size, + reg->map_ptr->value_size); }for (i =0 ; i <MAX_BPF_STACK ; i +=BPF_REG_SIZE ) { -if (env->cur_state.stack_slot_type[i] ==STACK_SPILL ) +if (state->stack_slot_type[i] ==STACK_SPILL )verbose (" fp%d=%s" , -MAX_BPF_STACK + i, - reg_type_str[env->cur_state.spilled_regs[i /BPF_REG_SIZE ].type]); + reg_type_str[state->spilled_regs[i /BPF_REG_SIZE ].type]); }verbose ("\n" ); }
@@ -686,10 +688,11 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off, int value_regno) {struct verifier_state *state = &env->cur_state; +struct reg_state *reg = &state->regs[regno];int size, err =0 ; -if (state->regs[regno].type ==PTR_TO_STACK ) - off += state->regs[regno].imm; +if (reg->type ==PTR_TO_STACK ) + off += reg->imm; size =bpf_size_to_bytes (bpf_size);if (size <0 )
@@ -700,7 +703,7 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off,return -EACCES ; } -if (state->regs[regno].type ==PTR_TO_MAP_VALUE ) { +if (reg->type ==PTR_TO_MAP_VALUE ) {if (t ==BPF_WRITE && value_regno >=0 &&is_pointer_value (env, value_regno)) {verbose ("R%d leaks addr into map\n" , value_regno);
@@ -710,7 +713,7 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off,if (!err && t ==BPF_READ && value_regno >=0 )mark_reg_unknown_value (state->regs, value_regno); - }else if (state->regs[regno].type ==PTR_TO_CTX ) { + }else if (reg->type ==PTR_TO_CTX ) {if (t ==BPF_WRITE && value_regno >=0 &&is_pointer_value (env, value_regno)) {verbose ("R%d leaks addr into ctx\n" , value_regno);
@@ -720,8 +723,7 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off,if (!err && t ==BPF_READ && value_regno >=0 )mark_reg_unknown_value (state->regs, value_regno); - }else if (state->regs[regno].type ==FRAME_PTR || - state->regs[regno].type ==PTR_TO_STACK ) { + }else if (reg->type ==FRAME_PTR || reg->type ==PTR_TO_STACK ) {if (off >=0 || off < -MAX_BPF_STACK ) {verbose ("invalid stack off=%d size=%d\n" , off, size);return -EACCES ;
@@ -739,7 +741,7 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off, } }else {verbose ("R%d invalid mem access '%s'\n" , - regno, reg_type_str[state->regs[regno].type]); + regno, reg_type_str[reg->type]);return -EACCES ; }return err;
@@ -1104,7 +1106,7 @@ static int check_call(struct verifier_env *env, int func_id)/* check validity of 32-bit and 64-bit arithmetic operations */ static int check_alu_op (struct verifier_env *env,struct bpf_insn *insn) { -struct reg_state *regs = env->cur_state.regs; +struct reg_state *regs = env->cur_state.regs, *dst_reg;u8 opcode =BPF_OP (insn->code);int err;
@@ -1193,8 +1195,6 @@ static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn) }else {/* all other ALU ops: and, sub, xor, add, ... */ -bool stack_relative = false; -if (BPF_SRC (insn->code) ==BPF_X ) {if (insn->imm !=0 || insn->off !=0 ) {verbose ("BPF_ALU uses reserved fields\n" );
@@ -1232,11 +1232,19 @@ static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn) } } +/* check dest operand */ + err =check_reg_arg (regs, insn->dst_reg,DST_OP_NO_MARK ); +if (err) +return err; + + dst_reg = ®s[insn->dst_reg]; +/* pattern match 'bpf_add Rx, imm' instruction */ if (opcode ==BPF_ADD &&BPF_CLASS (insn->code) ==BPF_ALU64 && - regs[insn->dst_reg].type ==FRAME_PTR && -BPF_SRC (insn->code) ==BPF_K ) { - stack_relative = true; + dst_reg->type ==FRAME_PTR &&BPF_SRC (insn->code) ==BPF_K ) { + dst_reg->type =PTR_TO_STACK ; + dst_reg->imm = insn->imm; +return 0 ; }else if (is_pointer_value (env, insn->dst_reg)) {verbose ("R%d pointer arithmetic prohibited\n" , insn->dst_reg);
@@ -1248,15 +1256,8 @@ static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn)return -EACCES ; } -/* check dest operand */ - err =check_reg_arg (regs, insn->dst_reg,DST_OP ); -if (err) -return err; - -if (stack_relative) { - regs[insn->dst_reg].type =PTR_TO_STACK ; - regs[insn->dst_reg].imm = insn->imm; - } +/* mark dest operand */ +mark_reg_unknown_value (regs, insn->dst_reg); }return 0 ;
@@ -1265,7 +1266,7 @@ static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn)static int check_cond_jmp_op (struct verifier_env *env,struct bpf_insn *insn,int *insn_idx) { -struct reg_state *regs = env->cur_state.regs; +struct reg_state *regs = env->cur_state.regs, *dst_reg;struct verifier_state *other_branch;u8 opcode =BPF_OP (insn->code);int err;
@@ -1303,11 +1304,12 @@ static int check_cond_jmp_op(struct verifier_env *env,if (err)return err; + dst_reg = ®s[insn->dst_reg]; +/* detect if R == 0 where R was initialized to zero earlier */ if (BPF_SRC (insn->code) ==BPF_K && (opcode ==BPF_JEQ || opcode ==BPF_JNE ) && - regs[insn->dst_reg].type ==CONST_IMM && - regs[insn->dst_reg].imm == insn->imm) { + dst_reg->type ==CONST_IMM && dst_reg->imm == insn->imm) {if (opcode ==BPF_JEQ ) {/* if (imm == imm) goto pc+off; * only follow the goto, ignore fall-through
@@ -1329,9 +1331,8 @@ static int check_cond_jmp_op(struct verifier_env *env,/* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */ if (BPF_SRC (insn->code) ==BPF_K && - insn->imm ==0 && (opcode ==BPF_JEQ || - opcode ==BPF_JNE ) && - regs[insn->dst_reg].type ==PTR_TO_MAP_VALUE_OR_NULL ) { + insn->imm ==0 && (opcode ==BPF_JEQ || opcode ==BPF_JNE ) && + dst_reg->type ==PTR_TO_MAP_VALUE_OR_NULL ) {if (opcode ==BPF_JEQ ) {/* next fallthrough insn can access memory via * this register
@@ -1366,7 +1367,7 @@ static int check_cond_jmp_op(struct verifier_env *env, } }if (log_level) -print_verifier_state (env); +print_verifier_state (&env->cur_state);return 0 ; }
@@ -1444,14 +1445,14 @@ static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn)int i, err;if (!may_access_skb (env->prog->type)) { -verbose ("BPF_LD_ABS|IND instructions not allowed for this program type\n" ); +verbose ("BPF_LD_[ABS|IND] instructions not allowed for this program type\n" );return -EINVAL ; }if (insn->dst_reg !=BPF_REG_0 || insn->off !=0 ||BPF_SIZE (insn->code) ==BPF_DW || (mode ==BPF_ABS && insn->src_reg !=BPF_REG_0 )) { -verbose ("BPF_LD_ABS uses reserved fields\n" ); +verbose ("BPF_LD_[ABS|IND] uses reserved fields\n" );return -EINVAL ; }
@@ -1712,17 +1713,21 @@ err_free:*/ static bool states_equal (struct verifier_state *old,struct verifier_state *cur) { +struct reg_state *rold, *rcur;int i;for (i =0 ; i <MAX_BPF_REG ; i++) { -if (memcmp (&old->regs[i], &cur->regs[i], -sizeof (old->regs[0 ])) !=0 ) { -if (old->regs[i].type ==NOT_INIT || - (old->regs[i].type ==UNKNOWN_VALUE && - cur->regs[i].type !=NOT_INIT )) -continue ; -return false; - } + rold = &old->regs[i]; + rcur = &cur->regs[i]; + +if (memcmp (rold, rcur,sizeof (*rold)) ==0 ) +continue ; + +if (rold->type ==NOT_INIT || + (rold->type ==UNKNOWN_VALUE && rcur->type !=NOT_INIT )) +continue ; + +return false; }for (i =0 ; i <MAX_BPF_STACK ; i++) {
@@ -1844,7 +1849,7 @@ static int do_check(struct verifier_env *env)if (log_level && do_print_state) {verbose ("\nfrom %d to %d:" , prev_insn_idx, insn_idx); -print_verifier_state (env); +print_verifier_state (&env->cur_state); do_print_state = false; }
@@ -2056,6 +2061,7 @@ process_bpf_exit: insn_idx++; } +verbose ("processed %d insns\n" , insn_processed);return 0 ; }
Signature · 1 line
2.8.0
PATCH 4/7 bpf: wire in data and data_end for cls_act_bpf
In reply to Alexei Starovoitovallow cls_bpf and act_bpf programs access skb->data and skb->data_end pointers. The bpf helpers that change skb->data need to update data_end pointer as well. The verifier checks that programs always reload data, data_end pointers after calls to such bpf helpers.
We cannot add 'data_end' pointer to struct qdisc_skb_cb directly, since it's embedded as-is by infiniband ipoib, so wrapper struct is needed.
--- include/linux/filter.h | 16 ++++++++++++++++ net/core/filter.c | 51 ++++++++++++++++++++++++++++++++++++++++++++------ net/sched/act_bpf.c | 2 ++ net/sched/cls_bpf.c | 2 ++ 4 files changed, 65 insertions(+), 6 deletions(-)
include/linux/filter.h +16 -0
@@ -352,6 +352,22 @@ struct sk_filter {#define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN +struct bpf_skb_data_end { +struct qdisc_skb_cb qdisc_cb; +void *data_end; +}; + +/* compute the linear packet data range [data, data_end) which +* will be accessed by cls_bpf and act_bpf programs +*/ +static inline void bpf_compute_data_end (struct sk_buff *skb) +{ +struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb; + + BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb)); + cb->data_end = skb->data +skb_headlen (skb); +} +static inline u8 *bpf_skb_cb (struct sk_buff *skb) {/* eBPF programs may read/write skb->cb[] area to transfer meta
net/core/filter.c +45 -6
106 lines not on this page · raw message
net/sched/act_bpf.c +2 -0
@@ -53,9 +53,11 @@ static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act, filter =rcu_dereference (prog->filter);if (at_ingress) {__skb_push (skb, skb->mac_len); +bpf_compute_data_end (skb); filter_res =BPF_PROG_RUN (filter, skb);__skb_pull (skb, skb->mac_len); }else { +bpf_compute_data_end (skb); filter_res =BPF_PROG_RUN (filter, skb); }rcu_read_unlock ();
net/sched/cls_bpf.c +2 -0
@@ -96,9 +96,11 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,if (at_ingress) {/* It is safe to push/pull even if skb_shared() */ __skb_push (skb, skb->mac_len); +bpf_compute_data_end (skb); filter_res =BPF_PROG_RUN (prog->filter, skb);__skb_pull (skb, skb->mac_len); }else { +bpf_compute_data_end (skb); filter_res =BPF_PROG_RUN (prog->filter, skb); }
Signature · 1 line
2.8.0
PATCH 5/7 bpf: add documentation for 'direct packet access'
In reply to Alexei Starovoitovexplain how verifier checks safety of packet access and update email addresses.
--- Documentation/networking/filter.txt | 85 ++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-)
Documentation/networking/filter.txt +83 -2
@@ -1095,6 +1095,87 @@ all use cases. See details of eBPF verifier in kernel/bpf/verifier.c +Direct packet access +-------------------- +In cls_bpf and act_bpf programs the verifier allows direct access to the packet +data via skb->data and skb->data_end pointers. +Ex: +1: r4 = *(u32 *)(r1 +80) /* load skb->data_end */ +2: r3 = *(u32 *)(r1 +76) /* load skb->data */ +3: r5 = r3 +4: r5 += 14 +5: if r5 > r4 goto pc+16 +R1=ctx R3=pkt(id=0,off=0,r=14) R4=pkt_end R5=pkt(id=0,off=14,r=14) R10=fp +6: r0 = *(u16 *)(r3 +12) /* access 12 and 13 bytes of the packet */ + +this 2byte load from the packet is safe to do, since the program author +did check 'if (skb->data + 14 > skb->data_end) goto err' at insn #5 which +means that in the fall-through case the register R3 (which points to skb->data) +has at least 14 directly accessible bytes. The verifier marks it +as R3=pkt(id=0,off=0,r=14). +id=0 means that no additional variables were added to the register. +off=0 means that no additional constants were added. +r=14 is the range of safe access which means that bytes [R3, R3 + 14) are ok. +Note that R5 is marked as R5=pkt(id=0,off=14,r=14). It also points +to the packet data, but constant 14 was added to the register, so +it now points to 'skb->data + 14' and accessible range is [R5, R5 + 14 - 14) +which is zero bytes. + +More complex packet access may look like: + R0=imm1 R1=ctx R3=pkt(id=0,off=0,r=14) R4=pkt_end R5=pkt(id=0,off=14,r=14) R10=fp + 6: r0 = *(u8 *)(r3 +7) /* load 7th byte from the packet */ + 7: r4 = *(u8 *)(r3 +12) + 8: r4 *= 14 + 9: r3 = *(u32 *)(r1 +76) /* load skb->data */ +10: r3 += r4 +11: r2 = r1 +12: r2 <<= 48 +13: r2 >>= 48 +14: r3 += r2 +15: r2 = r3 +16: r2 += 8 +17: r1 = *(u32 *)(r1 +80) /* load skb->data_end */ +18: if r2 > r1 goto pc+2 + R0=inv56 R1=pkt_end R2=pkt(id=2,off=8,r=8) R3=pkt(id=2,off=0,r=8) R4=inv52 R5=pkt(id=0,off=14,r=14) R10=fp +19: r1 = *(u8 *)(r3 +4) +The state of the register R3 is R3=pkt(id=2,off=0,r=8) +id=2 means that two 'r3 += rX' instructions were seen, so r3 points to some +offset within a packet and since the program author did +'if (r3 + 8 > r1) goto err' at insn #18, the safe range is [R3, R3 + 8). +The verifier only allows 'add' operation on packet registers. Any other +operation will set the register state to 'unknown_value' and it won't be +available for direct packet access. +Operation 'r3 += rX' may overflow and become less than original skb->data, +therefore the verifier has to prevent that. So it tracks the number of +upper zero bits in all 'uknown_value' registers, so when it sees +'r3 += rX' instruction and rX is more than 16-bit value, it will error as: +"cannot add integer value with N upper zero bits to ptr_to_packet" +Ex. after insn 'r4 = *(u8 *)(r3 +12)' (insn #7 above) the state of r4 is +R4=inv56 which means that upper 56 bits on the register are guaranteed +to be zero. After insn 'r4 *= 14' the state becomes R4=inv52, since +multiplying 8-bit value by constant 14 will keep upper 52 bits as zero. +Similarly 'r2 >>= 48' will make R2=inv48, since the shift is not sign +extending. This logic is implemented in evaluate_reg_alu() function. + +The end result is that bpf program author can access packet directly +using normal C code as: + void *data = (void *)(long)skb->data; + void *data_end = (void *)(long)skb->data_end; + struct eth_hdr *eth = data; + struct iphdr *iph = data + sizeof(*eth); + struct udphdr *udp = data + sizeof(*eth) + sizeof(*iph); + + if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end) + return 0; + if (eth->h_proto != htons(ETH_P_IP)) + return 0; + if (iph->protocol != IPPROTO_UDP || iph->ihl != 5) + return 0; + if (udp->dest == 53 || udp->source == 9) + ...; +which makes such programs easier to write comparing to LD_ABS insn +and significantly faster. + eBPF maps --------- 'maps' is a generic storage of different types for sharing data between kernel
@@ -1293,5 +1374,5 @@ to give potential BPF hackers or security auditors a better overview of the underlying architecture. Jay Schulist <[email protected]> -Daniel Borkmann <[email protected]> -Alexei Starovoitov <[email protected]> +Daniel Borkmann <[email protected]> +Alexei Starovoitov <[email protected]>
Signature · 1 line
2.8.0
David Miller
maintainerFrom: Alexei Starovoitov <[email protected]>
Date: Thu, 5 May 2016 19:49:08 -0700
Quoted 2 lines
This set of patches introduce 'direct packet access' from
cls_bpfandact_bpfprograms (which are root only).
Series applied, thanks Alexei.