Skip to content

Commit 271b955

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2018-07-01 The following pull-request contains BPF updates for your *net* tree. The main changes are: 1) A bpf_fib_lookup() helper fix to change the API before freeze to return an encoding of the FIB lookup result and return the nexthop device index in the params struct (instead of device index as return code that we had before), from David. 2) Various BPF JIT fixes to address syzkaller fallout, that is, do not reject progs when set_memory_*() fails since it could still be RO. Also arm32 JIT was not using bpf_jit_binary_lock_ro() API which was an issue, and a memory leak in s390 JIT found during review, from Daniel. 3) Multiple fixes for sockmap/hash to address most of the syzkaller triggered bugs. Usage with IPv6 was crashing, a GPF in bpf_tcp_close(), a missing sock_map_release() routine to hook up to callbacks, and a fix for an omitted bucket lock in sock_close(), from John. 4) Two bpftool fixes to remove duplicated error message on program load, and another one to close the libbpf object after program load. One additional fix for nfp driver's BPF offload to avoid stopping offload completely if replace of program failed, from Jakub. 5) Couple of BPF selftest fixes that bail out in some of the test scripts if the user does not have the right privileges, from Jeffrin. 6) Fixes in test_bpf for s390 when CONFIG_BPF_JIT_ALWAYS_ON is set where we need to set the flag that some of the test cases are expected to fail, from Kleber. 7) Fix to detangle BPF_LIRC_MODE2 dependency from CONFIG_CGROUP_BPF since it has no relation to it and lirc2 users often have configs without cgroups enabled and thus would not be able to use it, from Sean. 8) Fix a selftest failure in sockmap by removing a useless setrlimit() call that would set a too low limit where at the same time we are already including bpf_rlimit.h that does the job, from Yonghong. 9) Fix BPF selftest config with missing missing NET_SCHED, from Anders. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 35e8c7b + bf2b866 commit 271b955

File tree

22 files changed

+449
-294
lines changed

22 files changed

+449
-294
lines changed

arch/arm/net/bpf_jit_32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
18441844
/* there are 2 passes here */
18451845
bpf_jit_dump(prog->len, image_size, 2, ctx.target);
18461846

1847-
set_memory_ro((unsigned long)header, header->pages);
1847+
bpf_jit_binary_lock_ro(header);
18481848
prog->bpf_func = (void *)ctx.target;
18491849
prog->jited = 1;
18501850
prog->jited_len = image_size;

arch/s390/net/bpf_jit_comp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
12861286
goto free_addrs;
12871287
}
12881288
if (bpf_jit_prog(&jit, fp)) {
1289+
bpf_jit_binary_free(header);
12891290
fp = orig_fp;
12901291
goto free_addrs;
12911292
}

drivers/media/rc/bpf-lirc.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,29 +207,19 @@ void lirc_bpf_free(struct rc_dev *rcdev)
207207
bpf_prog_array_free(rcdev->raw->progs);
208208
}
209209

210-
int lirc_prog_attach(const union bpf_attr *attr)
210+
int lirc_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
211211
{
212-
struct bpf_prog *prog;
213212
struct rc_dev *rcdev;
214213
int ret;
215214

216215
if (attr->attach_flags)
217216
return -EINVAL;
218217

219-
prog = bpf_prog_get_type(attr->attach_bpf_fd,
220-
BPF_PROG_TYPE_LIRC_MODE2);
221-
if (IS_ERR(prog))
222-
return PTR_ERR(prog);
223-
224218
rcdev = rc_dev_get_from_fd(attr->target_fd);
225-
if (IS_ERR(rcdev)) {
226-
bpf_prog_put(prog);
219+
if (IS_ERR(rcdev))
227220
return PTR_ERR(rcdev);
228-
}
229221

230222
ret = lirc_bpf_attach(rcdev, prog);
231-
if (ret)
232-
bpf_prog_put(prog);
233223

234224
put_device(&rcdev->dev);
235225

drivers/net/ethernet/netronome/nfp/bpf/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
8181

8282
ret = nfp_net_bpf_offload(nn, prog, running, extack);
8383
/* Stop offload if replace not possible */
84-
if (ret && prog)
85-
nfp_bpf_xdp_offload(app, nn, NULL, extack);
84+
if (ret)
85+
return ret;
8686

87-
nn->dp.bpf_offload_xdp = prog && !ret;
87+
nn->dp.bpf_offload_xdp = !!prog;
8888
return ret;
8989
}
9090

include/linux/bpf-cgroup.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,38 @@ int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
188188
\
189189
__ret; \
190190
})
191+
int cgroup_bpf_prog_attach(const union bpf_attr *attr,
192+
enum bpf_prog_type ptype, struct bpf_prog *prog);
193+
int cgroup_bpf_prog_detach(const union bpf_attr *attr,
194+
enum bpf_prog_type ptype);
195+
int cgroup_bpf_prog_query(const union bpf_attr *attr,
196+
union bpf_attr __user *uattr);
191197
#else
192198

199+
struct bpf_prog;
193200
struct cgroup_bpf {};
194201
static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
195202
static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
196203

204+
static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
205+
enum bpf_prog_type ptype,
206+
struct bpf_prog *prog)
207+
{
208+
return -EINVAL;
209+
}
210+
211+
static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
212+
enum bpf_prog_type ptype)
213+
{
214+
return -EINVAL;
215+
}
216+
217+
static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
218+
union bpf_attr __user *uattr)
219+
{
220+
return -EINVAL;
221+
}
222+
197223
#define cgroup_bpf_enabled (0)
198224
#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
199225
#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })

include/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)
696696
struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key);
697697
struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key);
698698
int sock_map_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type);
699+
int sockmap_get_from_fd(const union bpf_attr *attr, int type,
700+
struct bpf_prog *prog);
699701
#else
700702
static inline struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
701703
{
@@ -714,6 +716,12 @@ static inline int sock_map_prog(struct bpf_map *map,
714716
{
715717
return -EOPNOTSUPP;
716718
}
719+
720+
static inline int sockmap_get_from_fd(const union bpf_attr *attr, int type,
721+
struct bpf_prog *prog)
722+
{
723+
return -EINVAL;
724+
}
717725
#endif
718726

719727
#if defined(CONFIG_XDP_SOCKETS)

include/linux/bpf_lirc.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#include <uapi/linux/bpf.h>
66

77
#ifdef CONFIG_BPF_LIRC_MODE2
8-
int lirc_prog_attach(const union bpf_attr *attr);
8+
int lirc_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);
99
int lirc_prog_detach(const union bpf_attr *attr);
1010
int lirc_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr);
1111
#else
12-
static inline int lirc_prog_attach(const union bpf_attr *attr)
12+
static inline int lirc_prog_attach(const union bpf_attr *attr,
13+
struct bpf_prog *prog)
1314
{
1415
return -EINVAL;
1516
}

include/linux/filter.h

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,7 @@ struct sock_fprog_kern {
470470
};
471471

472472
struct bpf_binary_header {
473-
u16 pages;
474-
u16 locked:1;
475-
473+
u32 pages;
476474
/* Some arches need word alignment for their instructions */
477475
u8 image[] __aligned(4);
478476
};
@@ -481,7 +479,7 @@ struct bpf_prog {
481479
u16 pages; /* Number of allocated pages */
482480
u16 jited:1, /* Is our filter JIT'ed? */
483481
jit_requested:1,/* archs need to JIT the prog */
484-
locked:1, /* Program image locked? */
482+
undo_set_mem:1, /* Passed set_memory_ro() checkpoint */
485483
gpl_compatible:1, /* Is filter GPL compatible? */
486484
cb_access:1, /* Is control block accessed? */
487485
dst_needed:1, /* Do we need dst entry? */
@@ -677,46 +675,24 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
677675

678676
static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
679677
{
680-
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
681-
fp->locked = 1;
682-
if (set_memory_ro((unsigned long)fp, fp->pages))
683-
fp->locked = 0;
684-
#endif
678+
fp->undo_set_mem = 1;
679+
set_memory_ro((unsigned long)fp, fp->pages);
685680
}
686681

687682
static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
688683
{
689-
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
690-
if (fp->locked) {
691-
WARN_ON_ONCE(set_memory_rw((unsigned long)fp, fp->pages));
692-
/* In case set_memory_rw() fails, we want to be the first
693-
* to crash here instead of some random place later on.
694-
*/
695-
fp->locked = 0;
696-
}
697-
#endif
684+
if (fp->undo_set_mem)
685+
set_memory_rw((unsigned long)fp, fp->pages);
698686
}
699687

700688
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
701689
{
702-
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
703-
hdr->locked = 1;
704-
if (set_memory_ro((unsigned long)hdr, hdr->pages))
705-
hdr->locked = 0;
706-
#endif
690+
set_memory_ro((unsigned long)hdr, hdr->pages);
707691
}
708692

709693
static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
710694
{
711-
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
712-
if (hdr->locked) {
713-
WARN_ON_ONCE(set_memory_rw((unsigned long)hdr, hdr->pages));
714-
/* In case set_memory_rw() fails, we want to be the first
715-
* to crash here instead of some random place later on.
716-
*/
717-
hdr->locked = 0;
718-
}
719-
#endif
695+
set_memory_rw((unsigned long)hdr, hdr->pages);
720696
}
721697

722698
static inline struct bpf_binary_header *
@@ -728,22 +704,6 @@ bpf_jit_binary_hdr(const struct bpf_prog *fp)
728704
return (void *)addr;
729705
}
730706

731-
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
732-
static inline int bpf_prog_check_pages_ro_single(const struct bpf_prog *fp)
733-
{
734-
if (!fp->locked)
735-
return -ENOLCK;
736-
if (fp->jited) {
737-
const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
738-
739-
if (!hdr->locked)
740-
return -ENOLCK;
741-
}
742-
743-
return 0;
744-
}
745-
#endif
746-
747707
int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
748708
static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
749709
{

include/uapi/linux/bpf.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,8 @@ union bpf_attr {
18571857
* is resolved), the nexthop address is returned in ipv4_dst
18581858
* or ipv6_dst based on family, smac is set to mac address of
18591859
* egress device, dmac is set to nexthop mac address, rt_metric
1860-
* is set to metric from route (IPv4/IPv6 only).
1860+
* is set to metric from route (IPv4/IPv6 only), and ifindex
1861+
* is set to the device index of the nexthop from the FIB lookup.
18611862
*
18621863
* *plen* argument is the size of the passed in struct.
18631864
* *flags* argument can be a combination of one or more of the
@@ -1873,9 +1874,10 @@ union bpf_attr {
18731874
* *ctx* is either **struct xdp_md** for XDP programs or
18741875
* **struct sk_buff** tc cls_act programs.
18751876
* Return
1876-
* Egress device index on success, 0 if packet needs to continue
1877-
* up the stack for further processing or a negative error in case
1878-
* of failure.
1877+
* * < 0 if any input argument is invalid
1878+
* * 0 on success (packet is forwarded, nexthop neighbor exists)
1879+
* * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the
1880+
* * packet is not forwarded or needs assist from full stack
18791881
*
18801882
* int bpf_sock_hash_update(struct bpf_sock_ops_kern *skops, struct bpf_map *map, void *key, u64 flags)
18811883
* Description
@@ -2612,6 +2614,18 @@ struct bpf_raw_tracepoint_args {
26122614
#define BPF_FIB_LOOKUP_DIRECT BIT(0)
26132615
#define BPF_FIB_LOOKUP_OUTPUT BIT(1)
26142616

2617+
enum {
2618+
BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */
2619+
BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */
2620+
BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */
2621+
BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */
2622+
BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */
2623+
BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */
2624+
BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */
2625+
BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */
2626+
BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */
2627+
};
2628+
26152629
struct bpf_fib_lookup {
26162630
/* input: network family for lookup (AF_INET, AF_INET6)
26172631
* output: network family of egress nexthop
@@ -2625,7 +2639,11 @@ struct bpf_fib_lookup {
26252639

26262640
/* total length of packet from network header - used for MTU check */
26272641
__u16 tot_len;
2628-
__u32 ifindex; /* L3 device index for lookup */
2642+
2643+
/* input: L3 device index for lookup
2644+
* output: device index from FIB lookup
2645+
*/
2646+
__u32 ifindex;
26292647

26302648
union {
26312649
/* inputs to lookup */

kernel/bpf/cgroup.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,60 @@ int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
428428
return ret;
429429
}
430430

431+
int cgroup_bpf_prog_attach(const union bpf_attr *attr,
432+
enum bpf_prog_type ptype, struct bpf_prog *prog)
433+
{
434+
struct cgroup *cgrp;
435+
int ret;
436+
437+
cgrp = cgroup_get_from_fd(attr->target_fd);
438+
if (IS_ERR(cgrp))
439+
return PTR_ERR(cgrp);
440+
441+
ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
442+
attr->attach_flags);
443+
cgroup_put(cgrp);
444+
return ret;
445+
}
446+
447+
int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
448+
{
449+
struct bpf_prog *prog;
450+
struct cgroup *cgrp;
451+
int ret;
452+
453+
cgrp = cgroup_get_from_fd(attr->target_fd);
454+
if (IS_ERR(cgrp))
455+
return PTR_ERR(cgrp);
456+
457+
prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
458+
if (IS_ERR(prog))
459+
prog = NULL;
460+
461+
ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
462+
if (prog)
463+
bpf_prog_put(prog);
464+
465+
cgroup_put(cgrp);
466+
return ret;
467+
}
468+
469+
int cgroup_bpf_prog_query(const union bpf_attr *attr,
470+
union bpf_attr __user *uattr)
471+
{
472+
struct cgroup *cgrp;
473+
int ret;
474+
475+
cgrp = cgroup_get_from_fd(attr->query.target_fd);
476+
if (IS_ERR(cgrp))
477+
return PTR_ERR(cgrp);
478+
479+
ret = cgroup_bpf_query(cgrp, attr, uattr);
480+
481+
cgroup_put(cgrp);
482+
return ret;
483+
}
484+
431485
/**
432486
* __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
433487
* @sk: The socket sending or receiving traffic

0 commit comments

Comments
 (0)