Skip to content

Commit 0841d98

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2018-06-16 The following pull-request contains BPF updates for your *net* tree. The main changes are: 1) Fix a panic in devmap handling in generic XDP where return type of __devmap_lookup_elem() got changed recently but generic XDP code missed the related update, from Toshiaki. 2) Fix a freeze when BPF progs are loaded that include BPF to BPF calls when JIT is enabled where we would later bail out via error path w/o dropping kallsyms, and another one to silence syzkaller splats from locking prog read-only, from Daniel. 3) Fix a bug in test_offloads.py BPF selftest which must not assume that the underlying system have no BPF progs loaded prior to test, and one in bpftool to fix accuracy of program load time, from Jakub. 4) Fix a bug in bpftool's probe for availability of the bpf(2) BPF_TASK_FD_QUERY subcommand, from Yonghong. 5) Fix a regression in AF_XDP's XDP_SKB receive path where queue id check got erroneously removed, from Björn. 6) Fix missing state cleanup in BPF's xfrm tunnel test, from William. 7) Check tunnel type more accurately in BPF's tunnel collect metadata kselftest, from Jian. 8) Fix missing Kconfig fragments for BPF kselftests, from Anders. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 35773c9 + 6d5fc19 commit 0841d98

File tree

12 files changed

+195
-72
lines changed

12 files changed

+195
-72
lines changed

include/linux/bpf.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,15 @@ void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
488488

489489
/* Map specifics */
490490
struct xdp_buff;
491+
struct sk_buff;
491492

492493
struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
493494
void __dev_map_insert_ctx(struct bpf_map *map, u32 index);
494495
void __dev_map_flush(struct bpf_map *map);
495496
int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
496497
struct net_device *dev_rx);
498+
int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
499+
struct bpf_prog *xdp_prog);
497500

498501
struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
499502
void __cpu_map_insert_ctx(struct bpf_map *map, u32 index);
@@ -586,6 +589,15 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
586589
return 0;
587590
}
588591

592+
struct sk_buff;
593+
594+
static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
595+
struct sk_buff *skb,
596+
struct bpf_prog *xdp_prog)
597+
{
598+
return 0;
599+
}
600+
589601
static inline
590602
struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
591603
{

include/linux/filter.h

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/cryptohash.h>
2020
#include <linux/set_memory.h>
2121
#include <linux/kallsyms.h>
22+
#include <linux/if_vlan.h>
2223

2324
#include <net/sch_generic.h>
2425

@@ -469,7 +470,8 @@ struct sock_fprog_kern {
469470
};
470471

471472
struct bpf_binary_header {
472-
unsigned int pages;
473+
u16 pages;
474+
u16 locked:1;
473475
u8 image[];
474476
};
475477

@@ -671,50 +673,49 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
671673

672674
#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
673675

674-
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
675676
static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
676677
{
678+
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
677679
fp->locked = 1;
678-
WARN_ON_ONCE(set_memory_ro((unsigned long)fp, fp->pages));
680+
if (set_memory_ro((unsigned long)fp, fp->pages))
681+
fp->locked = 0;
682+
#endif
679683
}
680684

681685
static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
682686
{
687+
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
683688
if (fp->locked) {
684689
WARN_ON_ONCE(set_memory_rw((unsigned long)fp, fp->pages));
685690
/* In case set_memory_rw() fails, we want to be the first
686691
* to crash here instead of some random place later on.
687692
*/
688693
fp->locked = 0;
689694
}
695+
#endif
690696
}
691697

692698
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
693699
{
694-
WARN_ON_ONCE(set_memory_ro((unsigned long)hdr, hdr->pages));
695-
}
696-
697-
static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
698-
{
699-
WARN_ON_ONCE(set_memory_rw((unsigned long)hdr, hdr->pages));
700-
}
701-
#else
702-
static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
703-
{
704-
}
705-
706-
static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
707-
{
708-
}
709-
710-
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
711-
{
700+
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
701+
hdr->locked = 1;
702+
if (set_memory_ro((unsigned long)hdr, hdr->pages))
703+
hdr->locked = 0;
704+
#endif
712705
}
713706

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

719720
static inline struct bpf_binary_header *
720721
bpf_jit_binary_hdr(const struct bpf_prog *fp)
@@ -725,6 +726,22 @@ bpf_jit_binary_hdr(const struct bpf_prog *fp)
725726
return (void *)addr;
726727
}
727728

729+
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
730+
static inline int bpf_prog_check_pages_ro_single(const struct bpf_prog *fp)
731+
{
732+
if (!fp->locked)
733+
return -ENOLCK;
734+
if (fp->jited) {
735+
const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
736+
737+
if (!hdr->locked)
738+
return -ENOLCK;
739+
}
740+
741+
return 0;
742+
}
743+
#endif
744+
728745
int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
729746
static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
730747
{
@@ -786,6 +803,21 @@ static inline bool bpf_dump_raw_ok(void)
786803
struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
787804
const struct bpf_insn *patch, u32 len);
788805

806+
static inline int __xdp_generic_ok_fwd_dev(struct sk_buff *skb,
807+
struct net_device *fwd)
808+
{
809+
unsigned int len;
810+
811+
if (unlikely(!(fwd->flags & IFF_UP)))
812+
return -ENETDOWN;
813+
814+
len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
815+
if (skb->len > len)
816+
return -EMSGSIZE;
817+
818+
return 0;
819+
}
820+
789821
/* The pair of xdp_do_redirect and xdp_do_flush_map MUST be called in the
790822
* same cpu context. Further for best results no more than a single map
791823
* for the do_redirect/do_flush pair should be used. This limitation is
@@ -961,6 +993,9 @@ static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
961993
}
962994
#endif /* CONFIG_BPF_JIT */
963995

996+
void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp);
997+
void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);
998+
964999
#define BPF_ANC BIT(15)
9651000

9661001
static inline bool bpf_needs_clear_a(const struct sock_filter *first)

kernel/bpf/core.c

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,20 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
350350
return prog_adj;
351351
}
352352

353+
void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
354+
{
355+
int i;
356+
357+
for (i = 0; i < fp->aux->func_cnt; i++)
358+
bpf_prog_kallsyms_del(fp->aux->func[i]);
359+
}
360+
361+
void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
362+
{
363+
bpf_prog_kallsyms_del_subprogs(fp);
364+
bpf_prog_kallsyms_del(fp);
365+
}
366+
353367
#ifdef CONFIG_BPF_JIT
354368
/* All BPF JIT sysctl knobs here. */
355369
int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
@@ -584,6 +598,8 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
584598
bpf_fill_ill_insns(hdr, size);
585599

586600
hdr->pages = size / PAGE_SIZE;
601+
hdr->locked = 0;
602+
587603
hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
588604
PAGE_SIZE - sizeof(*hdr));
589605
start = (get_random_int() % hole) & ~(alignment - 1);
@@ -1434,6 +1450,33 @@ static int bpf_check_tail_call(const struct bpf_prog *fp)
14341450
return 0;
14351451
}
14361452

1453+
static int bpf_prog_check_pages_ro_locked(const struct bpf_prog *fp)
1454+
{
1455+
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
1456+
int i, err;
1457+
1458+
for (i = 0; i < fp->aux->func_cnt; i++) {
1459+
err = bpf_prog_check_pages_ro_single(fp->aux->func[i]);
1460+
if (err)
1461+
return err;
1462+
}
1463+
1464+
return bpf_prog_check_pages_ro_single(fp);
1465+
#endif
1466+
return 0;
1467+
}
1468+
1469+
static void bpf_prog_select_func(struct bpf_prog *fp)
1470+
{
1471+
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1472+
u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1473+
1474+
fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1475+
#else
1476+
fp->bpf_func = __bpf_prog_ret0_warn;
1477+
#endif
1478+
}
1479+
14371480
/**
14381481
* bpf_prog_select_runtime - select exec runtime for BPF program
14391482
* @fp: bpf_prog populated with internal BPF program
@@ -1444,13 +1487,13 @@ static int bpf_check_tail_call(const struct bpf_prog *fp)
14441487
*/
14451488
struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
14461489
{
1447-
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1448-
u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1490+
/* In case of BPF to BPF calls, verifier did all the prep
1491+
* work with regards to JITing, etc.
1492+
*/
1493+
if (fp->bpf_func)
1494+
goto finalize;
14491495

1450-
fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1451-
#else
1452-
fp->bpf_func = __bpf_prog_ret0_warn;
1453-
#endif
1496+
bpf_prog_select_func(fp);
14541497

14551498
/* eBPF JITs can rewrite the program in case constant
14561499
* blinding is active. However, in case of error during
@@ -1471,6 +1514,8 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
14711514
if (*err)
14721515
return fp;
14731516
}
1517+
1518+
finalize:
14741519
bpf_prog_lock_ro(fp);
14751520

14761521
/* The tail call compatibility check can only be done at
@@ -1479,7 +1524,17 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
14791524
* all eBPF JITs might immediately support all features.
14801525
*/
14811526
*err = bpf_check_tail_call(fp);
1482-
1527+
if (*err)
1528+
return fp;
1529+
1530+
/* Checkpoint: at this point onwards any cBPF -> eBPF or
1531+
* native eBPF program is read-only. If we failed to change
1532+
* the page attributes (e.g. allocation failure from
1533+
* splitting large pages), then reject the whole program
1534+
* in order to guarantee not ending up with any W+X pages
1535+
* from BPF side in kernel.
1536+
*/
1537+
*err = bpf_prog_check_pages_ro_locked(fp);
14831538
return fp;
14841539
}
14851540
EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);

kernel/bpf/devmap.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
345345
return bq_enqueue(dst, xdpf, dev_rx);
346346
}
347347

348+
int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
349+
struct bpf_prog *xdp_prog)
350+
{
351+
int err;
352+
353+
err = __xdp_generic_ok_fwd_dev(skb, dst->dev);
354+
if (unlikely(err))
355+
return err;
356+
skb->dev = dst->dev;
357+
generic_xdp_tx(skb, xdp_prog);
358+
359+
return 0;
360+
}
361+
348362
static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
349363
{
350364
struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key);

kernel/bpf/syscall.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,14 +1034,9 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
10341034
static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
10351035
{
10361036
if (atomic_dec_and_test(&prog->aux->refcnt)) {
1037-
int i;
1038-
10391037
/* bpf_prog_free_id() must be called first */
10401038
bpf_prog_free_id(prog, do_idr_lock);
1041-
1042-
for (i = 0; i < prog->aux->func_cnt; i++)
1043-
bpf_prog_kallsyms_del(prog->aux->func[i]);
1044-
bpf_prog_kallsyms_del(prog);
1039+
bpf_prog_kallsyms_del_all(prog);
10451040

10461041
call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
10471042
}
@@ -1358,9 +1353,7 @@ static int bpf_prog_load(union bpf_attr *attr)
13581353
if (err < 0)
13591354
goto free_used_maps;
13601355

1361-
/* eBPF program is ready to be JITed */
1362-
if (!prog->bpf_func)
1363-
prog = bpf_prog_select_runtime(prog, &err);
1356+
prog = bpf_prog_select_runtime(prog, &err);
13641357
if (err < 0)
13651358
goto free_used_maps;
13661359

@@ -1384,6 +1377,7 @@ static int bpf_prog_load(union bpf_attr *attr)
13841377
return err;
13851378

13861379
free_used_maps:
1380+
bpf_prog_kallsyms_del_subprogs(prog);
13871381
free_used_maps(prog->aux);
13881382
free_prog:
13891383
bpf_prog_uncharge_memlock(prog);

net/core/filter.c

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,20 +3214,6 @@ int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
32143214
}
32153215
EXPORT_SYMBOL_GPL(xdp_do_redirect);
32163216

3217-
static int __xdp_generic_ok_fwd_dev(struct sk_buff *skb, struct net_device *fwd)
3218-
{
3219-
unsigned int len;
3220-
3221-
if (unlikely(!(fwd->flags & IFF_UP)))
3222-
return -ENETDOWN;
3223-
3224-
len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
3225-
if (skb->len > len)
3226-
return -EMSGSIZE;
3227-
3228-
return 0;
3229-
}
3230-
32313217
static int xdp_do_generic_redirect_map(struct net_device *dev,
32323218
struct sk_buff *skb,
32333219
struct xdp_buff *xdp,
@@ -3256,10 +3242,11 @@ static int xdp_do_generic_redirect_map(struct net_device *dev,
32563242
}
32573243

32583244
if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3259-
if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3245+
struct bpf_dtab_netdev *dst = fwd;
3246+
3247+
err = dev_map_generic_redirect(dst, skb, xdp_prog);
3248+
if (unlikely(err))
32603249
goto err;
3261-
skb->dev = fwd;
3262-
generic_xdp_tx(skb, xdp_prog);
32633250
} else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
32643251
struct xdp_sock *xs = fwd;
32653252

net/xdp/xsk.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
118118
u64 addr;
119119
int err;
120120

121+
if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
122+
return -EINVAL;
123+
121124
if (!xskq_peek_addr(xs->umem->fq, &addr) ||
122125
len > xs->umem->chunk_size_nohr) {
123126
xs->rx_dropped++;

0 commit comments

Comments
 (0)