Misc improvements and few minor fixes
Subsystems (10)
Daniel Borkmann
maintainercover Misc improvements and few minor fixes
Last batch of misc patches I had in queue: first one removes some left-over bits from ULP, second is a fix in the verifier where we wrongly use register number as type to fetch the string for the dump, third disables xadd on flow keys and subsequent one removes the flow key type from check_helper_mem_access() as they cannot be passed into any helper as of today. Next one lets map push, pop, peek avoid having to go through retpoline, and last one has a couple of minor fixes and cleanups for the ring buffer walk.
Thanks!
Daniel Borkmann (6):
ulp: remove uid and user_visible members
bpf, verifier: fix register type dump in xadd and st bpf, verifier: reject xadd on flow key memory
bpf, verifier: remove unneeded flow key in check_helper_mem_access bpf, verifier: avoid retpoline for map push/pop/peek operation bpf, libbpf: simplify and cleanup perf ring buffer walk
include/net/tcp.h | 7 --- kernel/bpf/verifier.c | 57 +++++++++++++++++++----- net/tls/tls_main.c | 2 - tools/bpf/bpftool/map_perf_ring.c | 10 +++-- tools/lib/bpf/libbpf.c | 67 +++++++++++++---------------- tools/lib/bpf/libbpf.h | 15 ++++--- tools/testing/selftests/bpf/test_verifier.c | 10 ++--- tools/testing/selftests/bpf/trace_helpers.c | 7 +-- 8 files changed, 99 insertions(+), 76 deletions(-)
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 1/6 ulp: remove uid and user_visible members
They are not used anymore and therefore should be removed.
--- include/net/tcp.h | 7 ------- net/tls/tls_main.c | 2 -- 2 files changed, 9 deletions(-)
include/net/tcp.h +0 -7
@@ -2051,11 +2051,6 @@ enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);#define TCP_ULP_MAX 128#define TCP_ULP_BUF_MAX (TCP_ULP_NAME_MAX*TCP_ULP_MAX) -enum { -TCP_ULP_TLS , -TCP_ULP_BPF , -}; -struct tcp_ulp_ops {struct list_head list;
@@ -2064,9 +2059,7 @@ struct tcp_ulp_ops {/* cleanup ulp */ void (*release)(struct sock *sk); -int uid;char name[TCP_ULP_NAME_MAX ]; -bool user_visible;struct module *owner; };int tcp_register_ulp (struct tcp_ulp_ops *type);
net/tls/tls_main.c +0 -2
@@ -715,8 +715,6 @@ EXPORT_SYMBOL(tls_unregister_device);static struct tcp_ulp_ops tcp_tls_ulp_ops__read_mostly = { .name ="tls" , - .uid =TCP_ULP_TLS , - .user_visible = true, .owner =THIS_MODULE , .init = tls_init, };
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 2/6 bpf, verifier: fix register type dump in xadd and st
In reply to Daniel BorkmannUsing reg_type_str[insn->dst_reg] is incorrect since insn->dst_reg contains the register number but not the actual register type. Add a small reg_state() helper and use it to get to the type. Also fix up the test_verifier test cases that have an incorrect errstr.
--- kernel/bpf/verifier.c | 19 +++++++++++++------ tools/testing/selftests/bpf/test_verifier.c | 10 +++++----- 2 files changed, 18 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +13 -6
@@ -1528,14 +1528,19 @@ static bool __is_pointer_value(bool allow_ptr_leaks,return reg->type !=SCALAR_VALUE ; } +static struct bpf_reg_state *reg_state (struct bpf_verifier_env *env,int regno) +{ +return cur_regs (env) + regno; +} +static bool is_pointer_value (struct bpf_verifier_env *env,int regno) { -return __is_pointer_value (env->allow_ptr_leaks,cur_regs (env) + regno); +return __is_pointer_value (env->allow_ptr_leaks,reg_state (env, regno)); }static bool is_ctx_reg (struct bpf_verifier_env *env,int regno) { -const struct bpf_reg_state *reg =cur_regs (env) + regno; +const struct bpf_reg_state *reg =reg_state (env, regno);return reg->type ==PTR_TO_CTX || reg->type ==PTR_TO_SOCKET ;
@@ -1543,7 +1548,7 @@ static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)static bool is_pkt_reg (struct bpf_verifier_env *env,int regno) { -const struct bpf_reg_state *reg =cur_regs (env) + regno; +const struct bpf_reg_state *reg =reg_state (env, regno);return type_is_pkt_pointer (reg->type); }
@@ -1958,7 +1963,8 @@ static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insif (is_ctx_reg (env, insn->dst_reg) ||is_pkt_reg (env, insn->dst_reg)) {verbose (env,"BPF_XADD stores into R%d %s is not allowed\n" , - insn->dst_reg, reg_type_str[insn->dst_reg]); + insn->dst_reg, + reg_type_str[reg_state (env, insn->dst_reg)->type]);return -EACCES ; }
@@ -1983,7 +1989,7 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, int access_size, bool zero_size_allowed, struct bpf_call_arg_meta *meta) { - struct bpf_reg_state *reg = cur_regs(env) + regno; + struct bpf_reg_state *reg = reg_state(env, regno);struct bpf_func_state *state =func (env, reg);int off, i, slot, spi;
@@ -5264,7 +5270,8 @@ static int do_check(struct bpf_verifier_env *env)if (is_ctx_reg (env, insn->dst_reg)) {verbose (env,"BPF_ST stores into R%d %s is not allowed\n" , - insn->dst_reg, reg_type_str[insn->dst_reg]); + insn->dst_reg, + reg_type_str[reg_state (env, insn->dst_reg)->type]);return -EACCES ; }
tools/testing/selftests/bpf/test_verifier.c +5 -5
@@ -3430,7 +3430,7 @@ static struct bpf_test tests[] = { BPF_ST_MEM(BPF_DW, BPF_REG_1, offsetof(struct __sk_buff, mark), 0), BPF_EXIT_INSN(), }, - .errstr = "BPF_ST stores into R1 inv is not allowed", + .errstr = "BPF_ST stores into R1 ctx is not allowed", .result = REJECT, .prog_type = BPF_PROG_TYPE_SCHED_CLS, },
@@ -3442,7 +3442,7 @@ static struct bpf_test tests[] = { BPF_REG_0, offsetof(struct __sk_buff, mark), 0),BPF_EXIT_INSN (), }, - .errstr = "BPF_XADD stores into R1 inv is not allowed", + .errstr = "BPF_XADD stores into R1 ctx is not allowed", .result = REJECT, .prog_type = BPF_PROG_TYPE_SCHED_CLS, },
@@ -5670,7 +5670,7 @@ static struct bpf_test tests[] = { .errstr_unpriv = "R2 leaks addr into mem", .result_unpriv = REJECT, .result = REJECT, - .errstr = "BPF_XADD stores into R1 inv is not allowed", + .errstr = "BPF_XADD stores into R1 ctx is not allowed", }, { "leak pointer into ctx 2",
@@ -5685,7 +5685,7 @@ static struct bpf_test tests[] = { .errstr_unpriv = "R10 leaks addr into mem", .result_unpriv = REJECT, .result = REJECT, - .errstr = "BPF_XADD stores into R1 inv is not allowed", + .errstr = "BPF_XADD stores into R1 ctx is not allowed", }, { "leak pointer into ctx 3",
@@ -12634,7 +12634,7 @@ static struct bpf_test tests[] = { BPF_EXIT_INSN(), }, .result = REJECT, - .errstr = "BPF_XADD stores into R2 ctx", + .errstr = "BPF_XADD stores into R2 pkt is not allowed", .prog_type = BPF_PROG_TYPE_XDP, }, {
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 3/6 bpf, verifier: reject xadd on flow key memory
In reply to Daniel BorkmannWe should not enable xadd operation for flow key memory if not needed there anyway. There is no such issue as described in the commit f37a8cb84cce ("bpf: reject stores into ctx via st and xadd") since there's no context rewriter for flow keys today, but it also shouldn't become part of the user facing behavior to allow for it. After patch:
0: (79) r7 = *(u64 *)(r1 +144)
1: (b7) r3 = 4096
2: (db) lock *(u64 *)(r7 +0) += r3 BPF_XADD stores into R7 flow_keys is not allowed
--- kernel/bpf/verifier.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +10 -1
@@ -1553,6 +1553,14 @@ static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)return type_is_pkt_pointer (reg->type); } +static bool is_flow_key_reg (struct bpf_verifier_env *env,int regno) +{ +const struct bpf_reg_state *reg =reg_state (env, regno); + +/* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ +return reg->type ==PTR_TO_FLOW_KEYS ; +} +static int check_pkt_ptr_alignment (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,int off,int size,bool strict)
@@ -1961,7 +1969,8 @@ static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_ins }if (is_ctx_reg (env, insn->dst_reg) || -is_pkt_reg (env, insn->dst_reg)) { +is_pkt_reg (env, insn->dst_reg) || +is_flow_key_reg (env, insn->dst_reg)) {verbose (env,"BPF_XADD stores into R%d %s is not allowed\n" , insn->dst_reg, reg_type_str[reg_state (env, insn->dst_reg)->type]);
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 4/6 bpf, verifier: remove unneeded flow key in check_helper_mem_access
In reply to Daniel BorkmannThey PTR_TO_FLOW_KEYS is not used today to be passed into a helper as memory, so it can be removed from check_helper_mem_access().
--- kernel/bpf/verifier.c | 2 -- 1 file changed, 2 deletions(-)
kernel/bpf/verifier.c +0 -2
@@ -2077,8 +2077,6 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,case PTR_TO_PACKET_META :return check_packet_access (env, regno, reg->off, access_size, zero_size_allowed); -case PTR_TO_FLOW_KEYS : -return check_flow_keys_access (env, reg->off, access_size);case PTR_TO_MAP_VALUE :return check_map_access (env, regno, reg->off, access_size, zero_size_allowed);
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 5/6 bpf, verifier: avoid retpoline for map push/pop/peek operation
In reply to Daniel BorkmannExtend prior work from 09772d92cd5a ("bpf: avoid retpoline for lookup/update/delete calls on maps") to also apply to the recently added map helpers that perform push/pop/peek operations so that the indirect call can be avoided.
--- kernel/bpf/verifier.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +24 -1
@@ -6178,7 +6178,10 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)if (prog->jit_requested &&BITS_PER_LONG ==64 && (insn->imm == BPF_FUNC_map_lookup_elem || insn->imm == BPF_FUNC_map_update_elem || - insn->imm == BPF_FUNC_map_delete_elem)) { + insn->imm == BPF_FUNC_map_delete_elem || + insn->imm == BPF_FUNC_map_push_elem || + insn->imm == BPF_FUNC_map_pop_elem || + insn->imm == BPF_FUNC_map_peek_elem)) { aux = &env->insn_aux_data[i + delta];if (bpf_map_ptr_poisoned (aux)) goto patch_call_imm;
@@ -6211,6 +6214,14 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)BUILD_BUG_ON (!__same_type (ops->map_update_elem, (int (*)(struct bpf_map *map,void *key,void *value,u64 flags))NULL )); +BUILD_BUG_ON (!__same_type (ops->map_push_elem, + (int (*)(struct bpf_map *map,void *value, +u64 flags))NULL )); +BUILD_BUG_ON (!__same_type (ops->map_pop_elem, + (int (*)(struct bpf_map *map,void *value))NULL )); +BUILD_BUG_ON (!__same_type (ops->map_peek_elem, + (int (*)(struct bpf_map *map,void *value))NULL )); +switch (insn->imm) {case BPF_FUNC_map_lookup_elem: insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
@@ -6224,6 +6235,18 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) insn->imm =BPF_CAST_CALL (ops->map_delete_elem) - __bpf_call_base;continue ; +case BPF_FUNC_map_push_elem: + insn->imm =BPF_CAST_CALL (ops->map_push_elem) - + __bpf_call_base; +continue ; +case BPF_FUNC_map_pop_elem: + insn->imm =BPF_CAST_CALL (ops->map_pop_elem) - + __bpf_call_base; +continue ; +case BPF_FUNC_map_peek_elem: + insn->imm =BPF_CAST_CALL (ops->map_peek_elem) - + __bpf_call_base; +continue ; } goto patch_call_imm;
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 6/6 bpf, libbpf: simplify and cleanup perf ring buffer walk
In reply to Daniel BorkmannSimplify bpf_perf_event_read_simple() a bit and fix up some minor things along the way: the return code in the header is not of type int but enum bpf_perf_event_ret instead. Once callback indicated to break the loop walking event data, it also needs to be consumed in data_tail since it has been processed already.
Moreover, bpf_perf_event_print_t callback should avoid void * as we actually get a pointer to struct perf_event_header and thus applications can make use of container_of() to have type checks. The walk also doesn't have to use modulo op since the ring size is required to be power of two.
--- tools/bpf/bpftool/map_perf_ring.c | 10 +++-- tools/lib/bpf/libbpf.c | 67 +++++++++++++---------------- tools/lib/bpf/libbpf.h | 15 ++++--- tools/testing/selftests/bpf/trace_helpers.c | 7 +-- 4 files changed, 47 insertions(+), 52 deletions(-)
tools/bpf/bpftool/map_perf_ring.c +6 -4
@@ -50,15 +50,17 @@ static void int_exit(int signo) stop = true; } -static enum bpf_perf_event_ret print_bpf_output (void *event,void *priv) +static enum bpf_perf_event_ret +print_bpf_output (struct perf_event_header *event,void *private_data) { -struct event_ring_info *ring = priv; -struct perf_event_sample *e = event; + struct perf_event_sample *e = container_of(event, struct perf_event_sample, + header); +struct event_ring_info *ring = private_data;struct {struct perf_event_header header;__u64 id;__u64 lost; - } *lost = event; + } *lost = (typeof (lost ))event;if (json_output) {jsonw_start_object (json_wtr);
tools/lib/bpf/libbpf.c +29 -38
85 lines not on this page · raw message
tools/lib/bpf/libbpf.h +8 -7
@@ -297,13 +297,14 @@ enum bpf_perf_event_ret { LIBBPF_PERF_EVENT_CONT = -2, }; -typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t )(void *event, -void *priv); -LIBBPF_API int bpf_perf_event_read_simple(void *mem, unsigned long size, -unsigned long page_size, -void **buf,size_t *buf_len, -bpf_perf_event_print_t fn, -void *priv); +struct perf_event_header ; +typedef enum bpf_perf_event_ret + (*bpf_perf_event_print_t )(struct perf_event_header *hdr, +void *private_data); +LIBBPF_API enum bpf_perf_event_ret +bpf_perf_event_read_simple (void *mmap_mem,size_t mmap_size,size_t page_size, +void **copy_mem,size_t *copy_size, +bpf_perf_event_print_t fn,void *private_data);struct nlattr ;typedef int (*libbpf_dump_nlmsg_t )(void *cookie,void *msg,struct nlattr **tb);
tools/testing/selftests/bpf/trace_helpers.c +4 -3
@@ -125,10 +125,11 @@ struct perf_event_sample {char data[]; }; -static enum bpf_perf_event_ret bpf_perf_event_print (void *event,void *priv) +static enum bpf_perf_event_ret +bpf_perf_event_print (struct perf_event_header *hdr,void *private_data) { -struct perf_event_sample *e = event; -perf_event_print_fn fn = priv; +struct perf_event_sample *e = (struct perf_event_sample *)hdr; +perf_event_print_fn fn = private_data;int ret;if (e->header.type ==PERF_RECORD_SAMPLE ) {
Signature · 1 line
2.9.5
Alexei Starovoitov
AppliedQuoted 7 lines · Daniel Borkmann
Last batch of misc patches I had in queue: first one removes some left-over bits from ULP, second is a fix in the verifier where we wrongly use register number as type to fetch the string for the dump, third disables xadd on flow keys and subsequent one removes the flow key type from
check_helper_mem_access()as they cannot be passed into any helper as of today. Next one lets map push, pop, peek avoid having to go through retpoline, and last one has a couple of minor fixes and cleanups for the ring buffer walk.
Applied, Thanks