Skip to content

Commit f5d2871

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2021-05-26 The following pull-request contains BPF updates for your *net* tree. We've added 14 non-merge commits during the last 14 day(s) which contain a total of 17 files changed, 513 insertions(+), 231 deletions(-). The main changes are: 1) Fix bpf_skb_change_head() helper to reset mac_len, from Jussi Maki. 2) Fix masking direction swap upon off-reg sign change, from Daniel Borkmann. 3) Fix BPF offloads in verifier by reordering driver callback, from Yinjun Zhang. 4) BPF selftest for ringbuf mmap ro/rw restrictions, from Andrii Nakryiko. 5) Follow-up fixes to nested bprintf per-cpu buffers, from Florent Revest. 6) Fix bpftool sock_release attach point help info, from Liu Jian. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 6dfa87b + 1bad6fd commit f5d2871

17 files changed

Lines changed: 513 additions & 231 deletions

File tree

‎arch/arm64/Kbuild‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2-
obj-y += kernel/ mm/
3-
obj-$(CONFIG_NET) += net/
2+
obj-y += kernel/ mm/ net/
43
obj-$(CONFIG_KVM) += kvm/
54
obj-$(CONFIG_XEN) += xen/
65
obj-$(CONFIG_CRYPTO) += crypto/

‎kernel/bpf/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ config BPF_SYSCALL
3737

3838
config BPF_JIT
3939
bool "Enable BPF Just In Time compiler"
40+
depends on BPF
4041
depends on HAVE_CBPF_JIT || HAVE_EBPF_JIT
4142
depends on MODULES
4243
help

‎kernel/bpf/bpf_lsm.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
107107
return &bpf_inode_storage_get_proto;
108108
case BPF_FUNC_inode_storage_delete:
109109
return &bpf_inode_storage_delete_proto;
110+
#ifdef CONFIG_NET
110111
case BPF_FUNC_sk_storage_get:
111112
return &bpf_sk_storage_get_proto;
112113
case BPF_FUNC_sk_storage_delete:
113114
return &bpf_sk_storage_delete_proto;
115+
#endif /* CONFIG_NET */
114116
case BPF_FUNC_spin_lock:
115117
return &bpf_spin_lock_proto;
116118
case BPF_FUNC_spin_unlock:

‎kernel/bpf/helpers.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,15 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
692692
return -EINVAL;
693693
}
694694

695-
/* Per-cpu temp buffers which can be used by printf-like helpers for %s or %p
695+
/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
696+
* arguments representation.
696697
*/
697-
#define MAX_PRINTF_BUF_LEN 512
698+
#define MAX_BPRINTF_BUF_LEN 512
698699

699700
/* Support executing three nested bprintf helper calls on a given CPU */
701+
#define MAX_BPRINTF_NEST_LEVEL 3
700702
struct bpf_bprintf_buffers {
701-
char tmp_bufs[3][MAX_PRINTF_BUF_LEN];
703+
char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
702704
};
703705
static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
704706
static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
@@ -710,7 +712,7 @@ static int try_get_fmt_tmp_buf(char **tmp_buf)
710712

711713
preempt_disable();
712714
nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
713-
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
715+
if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
714716
this_cpu_dec(bpf_bprintf_nest_level);
715717
preempt_enable();
716718
return -EBUSY;
@@ -761,7 +763,7 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
761763
if (num_args && try_get_fmt_tmp_buf(&tmp_buf))
762764
return -EBUSY;
763765

764-
tmp_buf_end = tmp_buf + MAX_PRINTF_BUF_LEN;
766+
tmp_buf_end = tmp_buf + MAX_BPRINTF_BUF_LEN;
765767
*bin_args = (u32 *)tmp_buf;
766768
}
767769

‎kernel/bpf/verifier.c‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6409,18 +6409,10 @@ enum {
64096409
};
64106410

64116411
static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
6412-
const struct bpf_reg_state *off_reg,
6413-
u32 *alu_limit, u8 opcode)
6412+
u32 *alu_limit, bool mask_to_left)
64146413
{
6415-
bool off_is_neg = off_reg->smin_value < 0;
6416-
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
6417-
(opcode == BPF_SUB && !off_is_neg);
64186414
u32 max = 0, ptr_limit = 0;
64196415

6420-
if (!tnum_is_const(off_reg->var_off) &&
6421-
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
6422-
return REASON_BOUNDS;
6423-
64246416
switch (ptr_reg->type) {
64256417
case PTR_TO_STACK:
64266418
/* Offset 0 is out-of-bounds, but acceptable start for the
@@ -6486,15 +6478,20 @@ static bool sanitize_needed(u8 opcode)
64866478
return opcode == BPF_ADD || opcode == BPF_SUB;
64876479
}
64886480

6481+
struct bpf_sanitize_info {
6482+
struct bpf_insn_aux_data aux;
6483+
bool mask_to_left;
6484+
};
6485+
64896486
static int sanitize_ptr_alu(struct bpf_verifier_env *env,
64906487
struct bpf_insn *insn,
64916488
const struct bpf_reg_state *ptr_reg,
64926489
const struct bpf_reg_state *off_reg,
64936490
struct bpf_reg_state *dst_reg,
6494-
struct bpf_insn_aux_data *tmp_aux,
6491+
struct bpf_sanitize_info *info,
64956492
const bool commit_window)
64966493
{
6497-
struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : tmp_aux;
6494+
struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux;
64986495
struct bpf_verifier_state *vstate = env->cur_state;
64996496
bool off_is_imm = tnum_is_const(off_reg->var_off);
65006497
bool off_is_neg = off_reg->smin_value < 0;
@@ -6515,16 +6512,25 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
65156512
if (vstate->speculative)
65166513
goto do_sim;
65176514

6518-
err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
6515+
if (!commit_window) {
6516+
if (!tnum_is_const(off_reg->var_off) &&
6517+
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
6518+
return REASON_BOUNDS;
6519+
6520+
info->mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
6521+
(opcode == BPF_SUB && !off_is_neg);
6522+
}
6523+
6524+
err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left);
65196525
if (err < 0)
65206526
return err;
65216527

65226528
if (commit_window) {
65236529
/* In commit phase we narrow the masking window based on
65246530
* the observed pointer move after the simulated operation.
65256531
*/
6526-
alu_state = tmp_aux->alu_state;
6527-
alu_limit = abs(tmp_aux->alu_limit - alu_limit);
6532+
alu_state = info->aux.alu_state;
6533+
alu_limit = abs(info->aux.alu_limit - alu_limit);
65286534
} else {
65296535
alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
65306536
alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0;
@@ -6539,8 +6545,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
65396545
/* If we're in commit phase, we're done here given we already
65406546
* pushed the truncated dst_reg into the speculative verification
65416547
* stack.
6548+
*
6549+
* Also, when register is a known constant, we rewrite register-based
6550+
* operation to immediate-based, and thus do not need masking (and as
6551+
* a consequence, do not need to simulate the zero-truncation either).
65426552
*/
6543-
if (commit_window)
6553+
if (commit_window || off_is_imm)
65446554
return 0;
65456555

65466556
/* Simulate and find potential out-of-bounds access under
@@ -6685,7 +6695,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
66856695
smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
66866696
u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
66876697
umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
6688-
struct bpf_insn_aux_data tmp_aux = {};
6698+
struct bpf_sanitize_info info = {};
66896699
u8 opcode = BPF_OP(insn->code);
66906700
u32 dst = insn->dst_reg;
66916701
int ret;
@@ -6754,7 +6764,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
67546764

67556765
if (sanitize_needed(opcode)) {
67566766
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg,
6757-
&tmp_aux, false);
6767+
&info, false);
67586768
if (ret < 0)
67596769
return sanitize_err(env, insn, ret, off_reg, dst_reg);
67606770
}
@@ -6895,7 +6905,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
68956905
return -EACCES;
68966906
if (sanitize_needed(opcode)) {
68976907
ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg,
6898-
&tmp_aux, true);
6908+
&info, true);
68996909
if (ret < 0)
69006910
return sanitize_err(env, insn, ret, off_reg, dst_reg);
69016911
}
@@ -13368,12 +13378,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1336813378
if (is_priv)
1336913379
env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
1337013380

13371-
if (bpf_prog_is_dev_bound(env->prog->aux)) {
13372-
ret = bpf_prog_offload_verifier_prep(env->prog);
13373-
if (ret)
13374-
goto skip_full_check;
13375-
}
13376-
1337713381
env->explored_states = kvcalloc(state_htab_size(env),
1337813382
sizeof(struct bpf_verifier_state_list *),
1337913383
GFP_USER);
@@ -13401,6 +13405,12 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1340113405
if (ret < 0)
1340213406
goto skip_full_check;
1340313407

13408+
if (bpf_prog_is_dev_bound(env->prog->aux)) {
13409+
ret = bpf_prog_offload_verifier_prep(env->prog);
13410+
if (ret)
13411+
goto skip_full_check;
13412+
}
13413+
1340413414
ret = check_cfg(env);
1340513415
if (ret < 0)
1340613416
goto skip_full_check;

‎net/core/filter.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,6 +3784,7 @@ static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
37843784
__skb_push(skb, head_room);
37853785
memset(skb->data, 0, head_room);
37863786
skb_reset_mac_header(skb);
3787+
skb_reset_mac_len(skb);
37873788
}
37883789

37893790
return ret;

‎tools/bpf/bpftool/Documentation/bpftool-cgroup.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ CGROUP COMMANDS
3030
| *ATTACH_TYPE* := { **ingress** | **egress** | **sock_create** | **sock_ops** | **device** |
3131
| **bind4** | **bind6** | **post_bind4** | **post_bind6** | **connect4** | **connect6** |
3232
| **getpeername4** | **getpeername6** | **getsockname4** | **getsockname6** | **sendmsg4** |
33-
| **sendmsg6** | **recvmsg4** | **recvmsg6** | **sysctl** | **getsockopt** | **setsockopt** }
33+
| **sendmsg6** | **recvmsg4** | **recvmsg6** | **sysctl** | **getsockopt** | **setsockopt** |
34+
| **sock_release** }
3435
| *ATTACH_FLAGS* := { **multi** | **override** }
3536
3637
DESCRIPTION
@@ -106,6 +107,7 @@ DESCRIPTION
106107
**getpeername6** call to getpeername(2) for an inet6 socket (since 5.8);
107108
**getsockname4** call to getsockname(2) for an inet4 socket (since 5.8);
108109
**getsockname6** call to getsockname(2) for an inet6 socket (since 5.8).
110+
**sock_release** closing an userspace inet socket (since 5.9).
109111

110112
**bpftool cgroup detach** *CGROUP* *ATTACH_TYPE* *PROG*
111113
Detach *PROG* from the cgroup *CGROUP* and attach type

‎tools/bpf/bpftool/Documentation/bpftool-prog.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ PROG COMMANDS
4444
| **cgroup/connect4** | **cgroup/connect6** | **cgroup/getpeername4** | **cgroup/getpeername6** |
4545
| **cgroup/getsockname4** | **cgroup/getsockname6** | **cgroup/sendmsg4** | **cgroup/sendmsg6** |
4646
| **cgroup/recvmsg4** | **cgroup/recvmsg6** | **cgroup/sysctl** |
47-
| **cgroup/getsockopt** | **cgroup/setsockopt** |
47+
| **cgroup/getsockopt** | **cgroup/setsockopt** | **cgroup/sock_release** |
4848
| **struct_ops** | **fentry** | **fexit** | **freplace** | **sk_lookup**
4949
| }
5050
| *ATTACH_TYPE* := {

‎tools/bpf/bpftool/bash-completion/bpftool‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ _bpftool()
478478
cgroup/recvmsg4 cgroup/recvmsg6 \
479479
cgroup/post_bind4 cgroup/post_bind6 \
480480
cgroup/sysctl cgroup/getsockopt \
481-
cgroup/setsockopt struct_ops \
481+
cgroup/setsockopt cgroup/sock_release struct_ops \
482482
fentry fexit freplace sk_lookup" -- \
483483
"$cur" ) )
484484
return 0
@@ -1021,7 +1021,7 @@ _bpftool()
10211021
device bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
10221022
getpeername4 getpeername6 getsockname4 getsockname6 \
10231023
sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl getsockopt \
1024-
setsockopt'
1024+
setsockopt sock_release'
10251025
local ATTACH_FLAGS='multi override'
10261026
local PROG_TYPE='id pinned tag name'
10271027
case $prev in
@@ -1032,7 +1032,7 @@ _bpftool()
10321032
ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
10331033
post_bind4|post_bind6|connect4|connect6|getpeername4|\
10341034
getpeername6|getsockname4|getsockname6|sendmsg4|sendmsg6|\
1035-
recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt)
1035+
recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt|sock_release)
10361036
COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
10371037
"$cur" ) )
10381038
return 0

‎tools/bpf/bpftool/cgroup.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
" connect6 | getpeername4 | getpeername6 |\n" \
2929
" getsockname4 | getsockname6 | sendmsg4 |\n" \
3030
" sendmsg6 | recvmsg4 | recvmsg6 |\n" \
31-
" sysctl | getsockopt | setsockopt }"
31+
" sysctl | getsockopt | setsockopt |\n" \
32+
" sock_release }"
3233

3334
static unsigned int query_flags;
3435

0 commit comments

Comments
 (0)