Skip to content

Commit 211c462

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2019-08-24 The following pull-request contains BPF updates for your *net* tree. The main changes are: 1) Fix verifier precision tracking with BPF-to-BPF calls, from Alexei. 2) Fix a use-after-free in prog symbol exposure, from Daniel. 3) Several s390x JIT fixes plus BE related fixes in BPF kselftests, from Ilya. 4) Fix memory leak by unpinning XDP umem pages in error path, from Ivan. 5) Fix a potential use-after-free on flow dissector detach, from Jakub. 6) Fix bpftool to close prog fd after showing metadata, from Quentin. 7) BPF kselftest config and TEST_PROGS_EXTENDED fixes, from Anders. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 345b932 + 2c23817 commit 211c462

12 files changed

Lines changed: 62 additions & 34 deletions

File tree

‎arch/s390/net/bpf_jit_comp.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i
863863
break;
864864
case BPF_ALU64 | BPF_NEG: /* dst = -dst */
865865
/* lcgr %dst,%dst */
866-
EMIT4(0xb9130000, dst_reg, dst_reg);
866+
EMIT4(0xb9030000, dst_reg, dst_reg);
867867
break;
868868
/*
869869
* BPF_FROM_BE/LE
@@ -1049,8 +1049,8 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i
10491049
/* llgf %w1,map.max_entries(%b2) */
10501050
EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1, REG_0, BPF_REG_2,
10511051
offsetof(struct bpf_array, map.max_entries));
1052-
/* clgrj %b3,%w1,0xa,label0: if %b3 >= %w1 goto out */
1053-
EMIT6_PCREL_LABEL(0xec000000, 0x0065, BPF_REG_3,
1052+
/* clrj %b3,%w1,0xa,label0: if (u32)%b3 >= (u32)%w1 goto out */
1053+
EMIT6_PCREL_LABEL(0xec000000, 0x0077, BPF_REG_3,
10541054
REG_W1, 0, 0xa);
10551055

10561056
/*
@@ -1076,8 +1076,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i
10761076
* goto out;
10771077
*/
10781078

1079-
/* sllg %r1,%b3,3: %r1 = index * 8 */
1080-
EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1, BPF_REG_3, REG_0, 3);
1079+
/* llgfr %r1,%b3: %r1 = (u32) index */
1080+
EMIT4(0xb9160000, REG_1, BPF_REG_3);
1081+
/* sllg %r1,%r1,3: %r1 *= 8 */
1082+
EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1, REG_1, REG_0, 3);
10811083
/* lg %r1,prog(%b2,%r1) */
10821084
EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1, BPF_REG_2,
10831085
REG_1, offsetof(struct bpf_array, ptrs));

‎kernel/bpf/syscall.c‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,20 +1707,26 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
17071707
if (err)
17081708
goto free_used_maps;
17091709

1710-
err = bpf_prog_new_fd(prog);
1711-
if (err < 0) {
1712-
/* failed to allocate fd.
1713-
* bpf_prog_put() is needed because the above
1714-
* bpf_prog_alloc_id() has published the prog
1715-
* to the userspace and the userspace may
1716-
* have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1717-
*/
1718-
bpf_prog_put(prog);
1719-
return err;
1720-
}
1721-
1710+
/* Upon success of bpf_prog_alloc_id(), the BPF prog is
1711+
* effectively publicly exposed. However, retrieving via
1712+
* bpf_prog_get_fd_by_id() will take another reference,
1713+
* therefore it cannot be gone underneath us.
1714+
*
1715+
* Only for the time /after/ successful bpf_prog_new_fd()
1716+
* and before returning to userspace, we might just hold
1717+
* one reference and any parallel close on that fd could
1718+
* rip everything out. Hence, below notifications must
1719+
* happen before bpf_prog_new_fd().
1720+
*
1721+
* Also, any failure handling from this point onwards must
1722+
* be using bpf_prog_put() given the program is exposed.
1723+
*/
17221724
bpf_prog_kallsyms_add(prog);
17231725
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1726+
1727+
err = bpf_prog_new_fd(prog);
1728+
if (err < 0)
1729+
bpf_prog_put(prog);
17241730
return err;
17251731

17261732
free_used_maps:

‎kernel/bpf/verifier.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,9 +985,6 @@ static void __mark_reg_unbounded(struct bpf_reg_state *reg)
985985
reg->smax_value = S64_MAX;
986986
reg->umin_value = 0;
987987
reg->umax_value = U64_MAX;
988-
989-
/* constant backtracking is enabled for root only for now */
990-
reg->precise = capable(CAP_SYS_ADMIN) ? false : true;
991988
}
992989

993990
/* Mark a register as having a completely unknown (scalar) value. */
@@ -1014,7 +1011,11 @@ static void mark_reg_unknown(struct bpf_verifier_env *env,
10141011
__mark_reg_not_init(regs + regno);
10151012
return;
10161013
}
1017-
__mark_reg_unknown(regs + regno);
1014+
regs += regno;
1015+
__mark_reg_unknown(regs);
1016+
/* constant backtracking is enabled for root without bpf2bpf calls */
1017+
regs->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ?
1018+
true : false;
10181019
}
10191020

10201021
static void __mark_reg_not_init(struct bpf_reg_state *reg)

‎net/core/filter.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8757,13 +8757,13 @@ sk_reuseport_is_valid_access(int off, int size,
87578757
return size == size_default;
87588758

87598759
/* Fields that allow narrowing */
8760-
case offsetof(struct sk_reuseport_md, eth_protocol):
8760+
case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
87618761
if (size < FIELD_SIZEOF(struct sk_buff, protocol))
87628762
return false;
87638763
/* fall through */
8764-
case offsetof(struct sk_reuseport_md, ip_protocol):
8765-
case offsetof(struct sk_reuseport_md, bind_inany):
8766-
case offsetof(struct sk_reuseport_md, len):
8764+
case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
8765+
case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
8766+
case bpf_ctx_range(struct sk_reuseport_md, len):
87678767
bpf_ctx_record_field_size(info, size_default);
87688768
return bpf_ctx_narrow_access_ok(off, size, size_default);
87698769

‎net/core/flow_dissector.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
142142
mutex_unlock(&flow_dissector_mutex);
143143
return -ENOENT;
144144
}
145-
bpf_prog_put(attached);
146145
RCU_INIT_POINTER(net->flow_dissector_prog, NULL);
146+
bpf_prog_put(attached);
147147
mutex_unlock(&flow_dissector_mutex);
148148
return 0;
149149
}

‎net/xdp/xdp_umem.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,16 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
365365
umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL);
366366
if (!umem->pages) {
367367
err = -ENOMEM;
368-
goto out_account;
368+
goto out_pin;
369369
}
370370

371371
for (i = 0; i < umem->npgs; i++)
372372
umem->pages[i].addr = page_address(umem->pgs[i]);
373373

374374
return 0;
375375

376+
out_pin:
377+
xdp_umem_unpin_pages(umem);
376378
out_account:
377379
xdp_umem_unaccount_pages(umem);
378380
return err;

‎tools/bpf/bpftool/prog.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ static int do_show(int argc, char **argv)
363363
if (fd < 0)
364364
return -1;
365365

366-
return show_prog(fd);
366+
err = show_prog(fd);
367+
close(fd);
368+
return err;
367369
}
368370

369371
if (argc)

‎tools/testing/selftests/bpf/Makefile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
3434
BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
3535
TEST_GEN_FILES = $(BPF_OBJ_FILES)
3636

37+
BTF_C_FILES = $(wildcard progs/btf_dump_test_case_*.c)
38+
TEST_FILES = $(BTF_C_FILES)
39+
3740
# Also test sub-register code-gen if LLVM has eBPF v3 processor support which
3841
# contains both ALU32 and JMP32 instructions.
3942
SUBREG_CODEGEN := $(shell echo "int cal(int a) { return a > 0; }" | \
@@ -68,7 +71,8 @@ TEST_PROGS := test_kmod.sh \
6871
TEST_PROGS_EXTENDED := with_addr.sh \
6972
with_tunnels.sh \
7073
tcp_client.py \
71-
tcp_server.py
74+
tcp_server.py \
75+
test_xdp_vlan.sh
7276

7377
# Compile but not part of 'make run_tests'
7478
TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr test_skb_cgroup_id_user \

‎tools/testing/selftests/bpf/config‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ CONFIG_NET_MPLS_GSO=m
3434
CONFIG_MPLS_ROUTING=m
3535
CONFIG_MPLS_IPTUNNEL=m
3636
CONFIG_IPV6_SIT=m
37+
CONFIG_BPF_JIT=y

‎tools/testing/selftests/bpf/test_btf_dump.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ int test_btf_dump_case(int n, struct btf_dump_test_case *test_case)
9797
}
9898

9999
snprintf(test_file, sizeof(test_file), "progs/%s.c", test_case->name);
100+
if (access(test_file, R_OK) == -1)
101+
/*
102+
* When the test is run with O=, kselftest copies TEST_FILES
103+
* without preserving the directory structure.
104+
*/
105+
snprintf(test_file, sizeof(test_file), "%s.c",
106+
test_case->name);
100107
/*
101108
* Diff test output and expected test output, contained between
102109
* START-EXPECTED-OUTPUT and END-EXPECTED-OUTPUT lines in test case.

0 commit comments

Comments
 (0)