More flexible BPF cb access
Subsystems (10)
Daniel Borkmann
maintainerPATCH 2/2 bpf: allow b/h/w/dw access for bpf's cb in ctx
In reply to Daniel BorkmannWhen structs are used to store temporary state in cb[] buffer that is used with programs and among tail calls, then the generated code will not always access the buffer in bpf_w chunks. We can ease programming of it and let this act more natural by allowing for aligned b/h/w/dw sized access for cb[] ctx member. Various test cases are attached as well for the selftest suite. Potentially, this can also be reused for other program types to pass data around.
--- kernel/bpf/verifier.c | 8 +- net/core/filter.c | 41 ++- tools/testing/selftests/bpf/test_verifier.c | 442 +++++++++++++++++++++++++++- 3 files changed, 478 insertions(+), 13 deletions(-)
kernel/bpf/verifier.c +6 -2
@@ -3165,10 +3165,14 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) insn = env->prog->insnsi + delta;for (i =0 ; i < insn_cnt; i++, insn++) { -if (insn->code == (BPF_LDX |BPF_MEM |BPF_W ) || +if (insn->code == (BPF_LDX |BPF_MEM |BPF_B ) || + insn->code == (BPF_LDX |BPF_MEM |BPF_H ) || + insn->code == (BPF_LDX |BPF_MEM |BPF_W ) || insn->code == (BPF_LDX |BPF_MEM |BPF_DW )) type =BPF_READ ; -else if (insn->code == (BPF_STX |BPF_MEM |BPF_W ) || +else if (insn->code == (BPF_STX |BPF_MEM |BPF_B ) || + insn->code == (BPF_STX |BPF_MEM |BPF_H ) || + insn->code == (BPF_STX |BPF_MEM |BPF_W ) || insn->code == (BPF_STX |BPF_MEM |BPF_DW )) type =BPF_WRITE ;else
net/core/filter.c +33 -8
83 lines not on this page · raw message
tools/testing/selftests/bpf/test_verifier.c +439 -3
454 lines not on this page · raw message
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 1/2 bpf: pass original insn directly to convert_ctx_access
In reply to Daniel BorkmannCurrently, when calling convert_ctx_access() callback for the various program types, we pass in insn->dst_reg, insn->src_reg, insn->off from the original instruction. This information is needed to rewrite the instruction that is based on the user ctx structure into a kernel representation for the ctx. As we'd like to allow access size beyond just BPF_W, we'd need also insn->code for that in order to decode the original access size. Given that, lets just pass insn directly to the convert_ctx_access() callback and work on that to not clutter the callback with even more arguments we need to pass when everything is already contained in insn. So lets go through that once, no functional change.
--- include/linux/bpf.h | 7 ++- kernel/bpf/verifier.c | 3 +- kernel/trace/bpf_trace.c | 15 ++--- net/core/filter.c | 139 +++++++++++++++++++++++++---------------------- 4 files changed, 87 insertions(+), 77 deletions(-)
include/linux/bpf.h +4 -3
@@ -161,9 +161,10 @@ struct bpf_verifier_ops { enum bpf_reg_type *reg_type);int (*gen_prologue)(struct bpf_insn *insn,bool direct_write,const struct bpf_prog *prog); -u32 (*convert_ctx_access)(enum bpf_access_type type,int dst_reg, -int src_reg,int ctx_off, -struct bpf_insn *insn,struct bpf_prog *prog); +u32 (*convert_ctx_access)(enum bpf_access_type type, +const struct bpf_insn *src, +struct bpf_insn *dst, +struct bpf_prog *prog); };struct bpf_prog_type_list {
kernel/bpf/verifier.c +1 -2
@@ -3177,8 +3177,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)if (env->insn_aux_data[i].ptr_type !=PTR_TO_CTX )continue ; - cnt = ops->convert_ctx_access (type, insn->dst_reg, insn->src_reg, - insn->off, insn_buf, env->prog); + cnt = ops->convert_ctx_access (type, insn, insn_buf, env->prog);if (cnt ==0 || cnt >=ARRAY_SIZE (insn_buf)) {verbose ("bpf verifier is misconfigured\n" );return -EINVAL ;
kernel/trace/bpf_trace.c +8 -7
@@ -572,28 +572,29 @@ static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type typereturn true; } -static u32 pe_prog_convert_ctx_access (enum bpf_access_type type,int dst_reg, -int src_reg,int ctx_off, +static u32 pe_prog_convert_ctx_access (enum bpf_access_type type, +const struct bpf_insn *si,struct bpf_insn *insn_buf,struct bpf_prog *prog) {struct bpf_insn *insn = insn_buf; -switch (ctx_off) { +switch (si->off) {case offsetof(struct bpf_perf_event_data , sample_period): BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64)); *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, - data), dst_reg, src_reg, + data), si->dst_reg, si->src_reg, offsetof(struct bpf_perf_event_data_kern , data)); - *insn++ =BPF_LDX_MEM (BPF_DW , dst_reg, dst_reg, + *insn++ =BPF_LDX_MEM (BPF_DW , si->dst_reg, si->dst_reg, offsetof(struct perf_sample_data , period));break ;default : *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, - regs), dst_reg, src_reg, + regs), si->dst_reg, si->src_reg, offsetof(struct bpf_perf_event_data_kern , regs)); - *insn++ =BPF_LDX_MEM (BPF_SIZEOF (long), dst_reg, dst_reg, ctx_off); + *insn++ =BPF_LDX_MEM (BPF_SIZEOF (long), si->dst_reg, si->dst_reg, + si->off);break ; }
net/core/filter.c +74 -65
313 lines not on this page · raw message
Signature · 1 line
1.9.3
Daniel Borkmann
maintainercover More flexible BPF cb access
This patch improves BPF's cb access by allowing b/h/w/dw access variants on it. For details, please see individual patches.
Thanks!
Daniel Borkmann (2):
bpf: pass original insn directly to convert_ctx_access
bpf: allow b/h/w/dw access for bpf's cb in ctx
include/linux/bpf.h | 7 +- kernel/bpf/verifier.c | 11 +- kernel/trace/bpf_trace.c | 15 +- net/core/filter.c | 176 ++++++----- tools/testing/selftests/bpf/test_verifier.c | 442 +++++++++++++++++++++++++++- 5 files changed, 563 insertions(+), 88 deletions(-)
Signature · 1 line
1.9.3
Re: [PATCH net-next 2/2] bpf: allow b/h/w/dw access for bpf's cb in ctx
In reply to Daniel BorkmannHi Daniel,
2017-01-12 (02:21 +0100) ~ Daniel Borkmann <[email protected]>
Quoted 16 lines
When structs are used to store temporary state in cb[] buffer that is used with programs and among tail calls, then the generated code will not always access the buffer in
bpf_wchunks. We can ease programming of it and let this act more natural by allowing for aligned b/h/w/dw sized access for cb[] ctx member. Various test cases are attached as well for the selftest suite. Potentially, this can also be reused for other program types to pass data around.Signed-off-by Daniel Borkmann <[email protected]>--- kernel/bpf/verifier.c | 8 +- net/core/filter.c | 41 ++- tools/testing/selftests/bpf/test_verifier.c | 442 +++++++++++++++++++++++++++- 3 files changed, 478 insertions(+), 13 deletions(-)
[...]
Quoted 5 lines
tools/testing/selftests/bpf/test_verifier.c +0
-0@@ -859,15 +859,451 @@ struct test_val {
[...]
tools/testing/selftests/bpf/test_verifier.c struct test_val { · line 1284
Quoted 11 lines
tools/testing/selftests/bpf/test_verifier.c +11
-0+ { + "check cb access: doulbe, oob 5", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct __sk_buff, cb[4]) + 8), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + },
Nitpicking: typo ("doulbe").
Regards,
Quentin
Daniel Borkmann
maintainerRe: [PATCH net-next 2/2] bpf: allow b/h/w/dw access for bpf's cb in ctx
Quoted 1 line · Quentin Monnet
2017-01-12 (02:21 +0100) ~ Daniel Borkmann <[email protected]>
[...]
Quoted 8 lines
Quoted 5 lines
tools/testing/selftests/bpf/test_verifier.c +0
-0@@ -859,15 +859,451 @@ struct test_val {[...]
Quoted 13 lines
tools/testing/selftests/bpf/test_verifier.c struct test_val { · line 1284
Quoted 11 lines
tools/testing/selftests/bpf/test_verifier.c +11
-0+ { + "check cb access: doulbe, oob 5", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct __sk_buff, cb[4]) + 8), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + },Nitpicking: typo ("doulbe").
Thanks for spotting, I've sent out a v2.