Skip to content

Commit 167ff13

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2020-04-24 The following pull-request contains BPF updates for your *net* tree. We've added 17 non-merge commits during the last 5 day(s) which contain a total of 19 files changed, 203 insertions(+), 85 deletions(-). The main changes are: 1) link_update fix, from Andrii. 2) libbpf get_xdp_id fix, from David. 3) xadd verifier fix, from Jann. 4) x86-32 JIT fixes, from Luke and Wang. 5) test_btf fix, from Stanislav. 6) freplace verifier fix, from Toke. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 3554e54 + e1cebd8 commit 167ff13

File tree

19 files changed

+203
-85
lines changed

19 files changed

+203
-85
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ static bool is_ereg(u32 reg)
158158
BIT(BPF_REG_AX));
159159
}
160160

161+
/*
162+
* is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
163+
* lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
164+
* of encoding. al,cl,dl,bl have simpler encoding.
165+
*/
166+
static bool is_ereg_8l(u32 reg)
167+
{
168+
return is_ereg(reg) ||
169+
(1 << reg) & (BIT(BPF_REG_1) |
170+
BIT(BPF_REG_2) |
171+
BIT(BPF_REG_FP));
172+
}
173+
161174
static bool is_axreg(u32 reg)
162175
{
163176
return reg == BPF_REG_0;
@@ -598,9 +611,8 @@ static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
598611
switch (size) {
599612
case BPF_B:
600613
/* Emit 'mov byte ptr [rax + off], al' */
601-
if (is_ereg(dst_reg) || is_ereg(src_reg) ||
602-
/* We have to add extra byte for x86 SIL, DIL regs */
603-
src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
614+
if (is_ereg(dst_reg) || is_ereg_8l(src_reg))
615+
/* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
604616
EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
605617
else
606618
EMIT1(0x88);

arch/x86/net/bpf_jit_comp32.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,14 +1847,16 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
18471847
case BPF_B:
18481848
case BPF_H:
18491849
case BPF_W:
1850-
if (!bpf_prog->aux->verifier_zext)
1850+
if (bpf_prog->aux->verifier_zext)
18511851
break;
18521852
if (dstk) {
18531853
EMIT3(0xC7, add_1reg(0x40, IA32_EBP),
18541854
STACK_VAR(dst_hi));
18551855
EMIT(0x0, 4);
18561856
} else {
1857-
EMIT3(0xC7, add_1reg(0xC0, dst_hi), 0);
1857+
/* xor dst_hi,dst_hi */
1858+
EMIT2(0x33,
1859+
add_2reg(0xC0, dst_hi, dst_hi));
18581860
}
18591861
break;
18601862
case BPF_DW:
@@ -2013,8 +2015,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
20132015
case BPF_JMP | BPF_JSET | BPF_X:
20142016
case BPF_JMP32 | BPF_JSET | BPF_X: {
20152017
bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2016-
u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2017-
u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2018+
u8 dreg_lo = IA32_EAX;
2019+
u8 dreg_hi = IA32_EDX;
20182020
u8 sreg_lo = sstk ? IA32_ECX : src_lo;
20192021
u8 sreg_hi = sstk ? IA32_EBX : src_hi;
20202022

@@ -2026,6 +2028,13 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
20262028
add_2reg(0x40, IA32_EBP,
20272029
IA32_EDX),
20282030
STACK_VAR(dst_hi));
2031+
} else {
2032+
/* mov dreg_lo,dst_lo */
2033+
EMIT2(0x89, add_2reg(0xC0, dreg_lo, dst_lo));
2034+
if (is_jmp64)
2035+
/* mov dreg_hi,dst_hi */
2036+
EMIT2(0x89,
2037+
add_2reg(0xC0, dreg_hi, dst_hi));
20292038
}
20302039

20312040
if (sstk) {
@@ -2050,8 +2059,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
20502059
case BPF_JMP | BPF_JSET | BPF_K:
20512060
case BPF_JMP32 | BPF_JSET | BPF_K: {
20522061
bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2053-
u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2054-
u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2062+
u8 dreg_lo = IA32_EAX;
2063+
u8 dreg_hi = IA32_EDX;
20552064
u8 sreg_lo = IA32_ECX;
20562065
u8 sreg_hi = IA32_EBX;
20572066
u32 hi;
@@ -2064,6 +2073,13 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
20642073
add_2reg(0x40, IA32_EBP,
20652074
IA32_EDX),
20662075
STACK_VAR(dst_hi));
2076+
} else {
2077+
/* mov dreg_lo,dst_lo */
2078+
EMIT2(0x89, add_2reg(0xC0, dreg_lo, dst_lo));
2079+
if (is_jmp64)
2080+
/* mov dreg_hi,dst_hi */
2081+
EMIT2(0x89,
2082+
add_2reg(0xC0, dreg_hi, dst_hi));
20672083
}
20682084

20692085
/* mov ecx,imm32 */

include/uapi/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ union bpf_attr {
16421642
* ifindex, but doesn't require a map to do so.
16431643
* Return
16441644
* **XDP_REDIRECT** on success, or the value of the two lower bits
1645-
* of the **flags* argument on error.
1645+
* of the *flags* argument on error.
16461646
*
16471647
* int bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key, u64 flags)
16481648
* Description

kernel/bpf/cpumap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static int cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
469469
return -EOVERFLOW;
470470

471471
/* Make sure CPU is a valid possible cpu */
472-
if (!cpu_possible(key_cpu))
472+
if (key_cpu >= nr_cpumask_bits || !cpu_possible(key_cpu))
473473
return -ENODEV;
474474

475475
if (qsize == 0) {

kernel/bpf/syscall.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,7 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
22832283
}
22842284
#endif
22852285

2286-
const struct file_operations bpf_link_fops = {
2286+
static const struct file_operations bpf_link_fops = {
22872287
#ifdef CONFIG_PROC_FS
22882288
.show_fdinfo = bpf_link_show_fdinfo,
22892289
#endif
@@ -3628,8 +3628,10 @@ static int link_update(union bpf_attr *attr)
36283628
return PTR_ERR(link);
36293629

36303630
new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
3631-
if (IS_ERR(new_prog))
3632-
return PTR_ERR(new_prog);
3631+
if (IS_ERR(new_prog)) {
3632+
ret = PTR_ERR(new_prog);
3633+
goto out_put_link;
3634+
}
36333635

36343636
if (flags & BPF_F_REPLACE) {
36353637
old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
@@ -3638,6 +3640,9 @@ static int link_update(union bpf_attr *attr)
36383640
old_prog = NULL;
36393641
goto out_put_progs;
36403642
}
3643+
} else if (attr->link_update.old_prog_fd) {
3644+
ret = -EINVAL;
3645+
goto out_put_progs;
36413646
}
36423647

36433648
#ifdef CONFIG_CGROUP_BPF
@@ -3653,6 +3658,8 @@ static int link_update(union bpf_attr *attr)
36533658
bpf_prog_put(old_prog);
36543659
if (ret)
36553660
bpf_prog_put(new_prog);
3661+
out_put_link:
3662+
bpf_link_put(link);
36563663
return ret;
36573664
}
36583665

kernel/bpf/verifier.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,15 @@ static bool register_is_const(struct bpf_reg_state *reg)
21182118
return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off);
21192119
}
21202120

2121+
static bool __is_pointer_value(bool allow_ptr_leaks,
2122+
const struct bpf_reg_state *reg)
2123+
{
2124+
if (allow_ptr_leaks)
2125+
return false;
2126+
2127+
return reg->type != SCALAR_VALUE;
2128+
}
2129+
21212130
static void save_register_state(struct bpf_func_state *state,
21222131
int spi, struct bpf_reg_state *reg)
21232132
{
@@ -2308,6 +2317,16 @@ static int check_stack_read(struct bpf_verifier_env *env,
23082317
* which resets stack/reg liveness for state transitions
23092318
*/
23102319
state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2320+
} else if (__is_pointer_value(env->allow_ptr_leaks, reg)) {
2321+
/* If value_regno==-1, the caller is asking us whether
2322+
* it is acceptable to use this value as a SCALAR_VALUE
2323+
* (e.g. for XADD).
2324+
* We must not allow unprivileged callers to do that
2325+
* with spilled pointers.
2326+
*/
2327+
verbose(env, "leaking pointer from stack off %d\n",
2328+
off);
2329+
return -EACCES;
23112330
}
23122331
mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
23132332
} else {
@@ -2673,15 +2692,6 @@ static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
26732692
return -EACCES;
26742693
}
26752694

2676-
static bool __is_pointer_value(bool allow_ptr_leaks,
2677-
const struct bpf_reg_state *reg)
2678-
{
2679-
if (allow_ptr_leaks)
2680-
return false;
2681-
2682-
return reg->type != SCALAR_VALUE;
2683-
}
2684-
26852695
static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
26862696
{
26872697
return cur_regs(env) + regno;
@@ -3089,7 +3099,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
30893099
if (ret < 0)
30903100
return ret;
30913101

3092-
if (atype == BPF_READ) {
3102+
if (atype == BPF_READ && value_regno >= 0) {
30933103
if (ret == SCALAR_VALUE) {
30943104
mark_reg_unknown(env, regs, value_regno);
30953105
return 0;
@@ -10487,6 +10497,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
1048710497
return -EINVAL;
1048810498
}
1048910499
env->ops = bpf_verifier_ops[tgt_prog->type];
10500+
prog->expected_attach_type = tgt_prog->expected_attach_type;
1049010501
}
1049110502
if (!tgt_prog->jited) {
1049210503
verbose(env, "Can attach to only JITed progs\n");
@@ -10831,6 +10842,13 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1083110842
* them now. Otherwise free_used_maps() will release them.
1083210843
*/
1083310844
release_maps(env);
10845+
10846+
/* extension progs temporarily inherit the attach_type of their targets
10847+
for verification purposes, so set it back to zero before returning
10848+
*/
10849+
if (env->prog->type == BPF_PROG_TYPE_EXT)
10850+
env->prog->expected_attach_type = 0;
10851+
1083410852
*prog = env->prog;
1083510853
err_unlock:
1083610854
if (!is_priv)

tools/bpf/bpftool/struct_ops.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ static int do_unregister(int argc, char **argv)
479479

480480
static int do_register(int argc, char **argv)
481481
{
482+
struct bpf_object_load_attr load_attr = {};
482483
const struct bpf_map_def *def;
483484
struct bpf_map_info info = {};
484485
__u32 info_len = sizeof(info);
@@ -499,7 +500,12 @@ static int do_register(int argc, char **argv)
499500

500501
set_max_rlimit();
501502

502-
if (bpf_object__load(obj)) {
503+
load_attr.obj = obj;
504+
if (verifier_logs)
505+
/* log_level1 + log_level2 + stats, but not stable UAPI */
506+
load_attr.log_level = 1 + 2 + 4;
507+
508+
if (bpf_object__load_xattr(&load_attr)) {
503509
bpf_object__close(obj);
504510
return -1;
505511
}

tools/bpf/runqslower/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BPFTOOL ?= $(DEFAULT_BPFTOOL)
88
LIBBPF_SRC := $(abspath ../../lib/bpf)
99
BPFOBJ := $(OUTPUT)/libbpf.a
1010
BPF_INCLUDE := $(OUTPUT)
11-
INCLUDES := -I$(BPF_INCLUDE) -I$(OUTPUT) -I$(abspath ../../lib)
11+
INCLUDES := -I$(OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../lib)
1212
CFLAGS := -g -Wall
1313

1414
# Try to detect best kernel BTF source

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ union bpf_attr {
16421642
* ifindex, but doesn't require a map to do so.
16431643
* Return
16441644
* **XDP_REDIRECT** on success, or the value of the two lower bits
1645-
* of the **flags* argument on error.
1645+
* of the *flags* argument on error.
16461646
*
16471647
* int bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key, u64 flags)
16481648
* Description

tools/lib/bpf/netlink.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
321321

322322
static __u32 get_xdp_id(struct xdp_link_info *info, __u32 flags)
323323
{
324+
flags &= XDP_FLAGS_MODES;
325+
324326
if (info->attach_mode != XDP_ATTACHED_MULTI && !flags)
325327
return info->prog_id;
326328
if (flags & XDP_FLAGS_DRV_MODE)

0 commit comments

Comments
 (0)