aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/bpf
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-10-28 23:17:49 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-10-28 23:17:49 -0400
commit9f51ae62c84a23ade0ba86457d30a30c9db0c50f (patch)
treed71bf5c81f17629d3d1b3131e0842c02bd9a7dd6 /kernel/bpf
parent53b3b6bbfde6aae8d1ededc86ad4e0e1e00eb5f8 (diff)
parent747569b0a7c537d680bc94a988be6caad9960488 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
Pull networking fixes from David Miller: 1) GRO overflow entries are not unlinked properly, resulting in list poison pointers being dereferenced. 2) Fix bridge build with ipv6 disabled, from Nikolay Aleksandrov. 3) Direct packet access and other fixes in BPF from Daniel Borkmann. 4) gred_change_table_def() gets passed the wrong pointer, a pointer to a set of unparsed attributes instead of the attribute itself. From Jakub Kicinski. 5) Allow macsec device to be brought up even if it's lowerdev is down, from Sabrina Dubroca. * git://git.kernel.org/pub/scm/linux/kernel/git/davem/net: net: diag: document swapped src/dst in udp_dump_one. macsec: let the administrator set UP state even if lowerdev is down macsec: update operstate when lower device changes net: sched: gred: pass the right attribute to gred_change_table_def() ptp: drop redundant kasprintf() to create worker name net: bridge: remove ipv6 zero address check in mcast queries net: Properly unlink GRO packets on overflow. bpf: fix wrong helper enablement in cgroup local storage bpf: add bpf_jit_limit knob to restrict unpriv allocations bpf: make direct packet write unclone more robust bpf: fix leaking uninitialized memory on pop/peek helpers bpf: fix direct packet write into pop/peek helpers bpf: fix cg_skb types to hint access type in may_access_direct_pkt_data bpf: fix direct packet access for flow dissector progs bpf: disallow direct packet access for unpriv in cg_skb bpf: fix test suite to enable all unpriv program types bpf, btf: fix a missing check bug in btf_parse selftests/bpf: add config fragments BPF_STREAM_PARSER and XDP_SOCKETS bpf: devmap: fix wrong interface selection in notifier_call
Diffstat (limited to 'kernel/bpf')
-rw-r--r--kernel/bpf/btf.c58
-rw-r--r--kernel/bpf/core.c49
-rw-r--r--kernel/bpf/devmap.c3
-rw-r--r--kernel/bpf/helpers.c2
-rw-r--r--kernel/bpf/queue_stack_maps.c2
-rw-r--r--kernel/bpf/verifier.c13
6 files changed, 84 insertions, 43 deletions
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 378cef70341c..ee4c82667d65 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -2067,56 +2067,47 @@ static int btf_check_sec_info(struct btf_verifier_env *env,
2067 return 0; 2067 return 0;
2068} 2068}
2069 2069
2070static int btf_parse_hdr(struct btf_verifier_env *env, void __user *btf_data, 2070static int btf_parse_hdr(struct btf_verifier_env *env)
2071 u32 btf_data_size)
2072{ 2071{
2072 u32 hdr_len, hdr_copy, btf_data_size;
2073 const struct btf_header *hdr; 2073 const struct btf_header *hdr;
2074 u32 hdr_len, hdr_copy;
2075 /*
2076 * Minimal part of the "struct btf_header" that
2077 * contains the hdr_len.
2078 */
2079 struct btf_min_header {
2080 u16 magic;
2081 u8 version;
2082 u8 flags;
2083 u32 hdr_len;
2084 } __user *min_hdr;
2085 struct btf *btf; 2074 struct btf *btf;
2086 int err; 2075 int err;
2087 2076
2088 btf = env->btf; 2077 btf = env->btf;
2089 min_hdr = btf_data; 2078 btf_data_size = btf->data_size;
2090 2079
2091 if (btf_data_size < sizeof(*min_hdr)) { 2080 if (btf_data_size <
2081 offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
2092 btf_verifier_log(env, "hdr_len not found"); 2082 btf_verifier_log(env, "hdr_len not found");
2093 return -EINVAL; 2083 return -EINVAL;
2094 } 2084 }
2095 2085
2096 if (get_user(hdr_len, &min_hdr->hdr_len)) 2086 hdr = btf->data;
2097 return -EFAULT; 2087 hdr_len = hdr->hdr_len;
2098
2099 if (btf_data_size < hdr_len) { 2088 if (btf_data_size < hdr_len) {
2100 btf_verifier_log(env, "btf_header not found"); 2089 btf_verifier_log(env, "btf_header not found");
2101 return -EINVAL; 2090 return -EINVAL;
2102 } 2091 }
2103 2092
2104 err = bpf_check_uarg_tail_zero(btf_data, sizeof(btf->hdr), hdr_len); 2093 /* Ensure the unsupported header fields are zero */
2105 if (err) { 2094 if (hdr_len > sizeof(btf->hdr)) {
2106 if (err == -E2BIG) 2095 u8 *expected_zero = btf->data + sizeof(btf->hdr);
2107 btf_verifier_log(env, "Unsupported btf_header"); 2096 u8 *end = btf->data + hdr_len;
2108 return err; 2097
2098 for (; expected_zero < end; expected_zero++) {
2099 if (*expected_zero) {
2100 btf_verifier_log(env, "Unsupported btf_header");
2101 return -E2BIG;
2102 }
2103 }
2109 } 2104 }
2110 2105
2111 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr)); 2106 hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
2112 if (copy_from_user(&btf->hdr, btf_data, hdr_copy)) 2107 memcpy(&btf->hdr, btf->data, hdr_copy);
2113 return -EFAULT;
2114 2108
2115 hdr = &btf->hdr; 2109 hdr = &btf->hdr;
2116 2110
2117 if (hdr->hdr_len != hdr_len)
2118 return -EINVAL;
2119
2120 btf_verifier_log_hdr(env, btf_data_size); 2111 btf_verifier_log_hdr(env, btf_data_size);
2121 2112
2122 if (hdr->magic != BTF_MAGIC) { 2113 if (hdr->magic != BTF_MAGIC) {
@@ -2186,10 +2177,6 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
2186 } 2177 }
2187 env->btf = btf; 2178 env->btf = btf;
2188 2179
2189 err = btf_parse_hdr(env, btf_data, btf_data_size);
2190 if (err)
2191 goto errout;
2192
2193 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN); 2180 data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
2194 if (!data) { 2181 if (!data) {
2195 err = -ENOMEM; 2182 err = -ENOMEM;
@@ -2198,13 +2185,18 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
2198 2185
2199 btf->data = data; 2186 btf->data = data;
2200 btf->data_size = btf_data_size; 2187 btf->data_size = btf_data_size;
2201 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
2202 2188
2203 if (copy_from_user(data, btf_data, btf_data_size)) { 2189 if (copy_from_user(data, btf_data, btf_data_size)) {
2204 err = -EFAULT; 2190 err = -EFAULT;
2205 goto errout; 2191 goto errout;
2206 } 2192 }
2207 2193
2194 err = btf_parse_hdr(env);
2195 if (err)
2196 goto errout;
2197
2198 btf->nohdr_data = btf->data + btf->hdr.hdr_len;
2199
2208 err = btf_parse_str_sec(env); 2200 err = btf_parse_str_sec(env);
2209 if (err) 2201 if (err)
2210 goto errout; 2202 goto errout;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 7c7eeea8cffc..6377225b2082 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -365,10 +365,13 @@ void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
365} 365}
366 366
367#ifdef CONFIG_BPF_JIT 367#ifdef CONFIG_BPF_JIT
368# define BPF_JIT_LIMIT_DEFAULT (PAGE_SIZE * 40000)
369
368/* All BPF JIT sysctl knobs here. */ 370/* All BPF JIT sysctl knobs here. */
369int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON); 371int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
370int bpf_jit_harden __read_mostly; 372int bpf_jit_harden __read_mostly;
371int bpf_jit_kallsyms __read_mostly; 373int bpf_jit_kallsyms __read_mostly;
374int bpf_jit_limit __read_mostly = BPF_JIT_LIMIT_DEFAULT;
372 375
373static __always_inline void 376static __always_inline void
374bpf_get_prog_addr_region(const struct bpf_prog *prog, 377bpf_get_prog_addr_region(const struct bpf_prog *prog,
@@ -577,27 +580,64 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
577 return ret; 580 return ret;
578} 581}
579 582
583static atomic_long_t bpf_jit_current;
584
585#if defined(MODULES_VADDR)
586static int __init bpf_jit_charge_init(void)
587{
588 /* Only used as heuristic here to derive limit. */
589 bpf_jit_limit = min_t(u64, round_up((MODULES_END - MODULES_VADDR) >> 2,
590 PAGE_SIZE), INT_MAX);
591 return 0;
592}
593pure_initcall(bpf_jit_charge_init);
594#endif
595
596static int bpf_jit_charge_modmem(u32 pages)
597{
598 if (atomic_long_add_return(pages, &bpf_jit_current) >
599 (bpf_jit_limit >> PAGE_SHIFT)) {
600 if (!capable(CAP_SYS_ADMIN)) {
601 atomic_long_sub(pages, &bpf_jit_current);
602 return -EPERM;
603 }
604 }
605
606 return 0;
607}
608
609static void bpf_jit_uncharge_modmem(u32 pages)
610{
611 atomic_long_sub(pages, &bpf_jit_current);
612}
613
580struct bpf_binary_header * 614struct bpf_binary_header *
581bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, 615bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
582 unsigned int alignment, 616 unsigned int alignment,
583 bpf_jit_fill_hole_t bpf_fill_ill_insns) 617 bpf_jit_fill_hole_t bpf_fill_ill_insns)
584{ 618{
585 struct bpf_binary_header *hdr; 619 struct bpf_binary_header *hdr;
586 unsigned int size, hole, start; 620 u32 size, hole, start, pages;
587 621
588 /* Most of BPF filters are really small, but if some of them 622 /* Most of BPF filters are really small, but if some of them
589 * fill a page, allow at least 128 extra bytes to insert a 623 * fill a page, allow at least 128 extra bytes to insert a
590 * random section of illegal instructions. 624 * random section of illegal instructions.
591 */ 625 */
592 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); 626 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
627 pages = size / PAGE_SIZE;
628
629 if (bpf_jit_charge_modmem(pages))
630 return NULL;
593 hdr = module_alloc(size); 631 hdr = module_alloc(size);
594 if (hdr == NULL) 632 if (!hdr) {
633 bpf_jit_uncharge_modmem(pages);
595 return NULL; 634 return NULL;
635 }
596 636
597 /* Fill space with illegal/arch-dep instructions. */ 637 /* Fill space with illegal/arch-dep instructions. */
598 bpf_fill_ill_insns(hdr, size); 638 bpf_fill_ill_insns(hdr, size);
599 639
600 hdr->pages = size / PAGE_SIZE; 640 hdr->pages = pages;
601 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), 641 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
602 PAGE_SIZE - sizeof(*hdr)); 642 PAGE_SIZE - sizeof(*hdr));
603 start = (get_random_int() % hole) & ~(alignment - 1); 643 start = (get_random_int() % hole) & ~(alignment - 1);
@@ -610,7 +650,10 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
610 650
611void bpf_jit_binary_free(struct bpf_binary_header *hdr) 651void bpf_jit_binary_free(struct bpf_binary_header *hdr)
612{ 652{
653 u32 pages = hdr->pages;
654
613 module_memfree(hdr); 655 module_memfree(hdr);
656 bpf_jit_uncharge_modmem(pages);
614} 657}
615 658
616/* This symbol is only overridden by archs that have different 659/* This symbol is only overridden by archs that have different
diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
index 141710b82a6c..191b79948424 100644
--- a/kernel/bpf/devmap.c
+++ b/kernel/bpf/devmap.c
@@ -512,8 +512,7 @@ static int dev_map_notification(struct notifier_block *notifier,
512 struct bpf_dtab_netdev *dev, *odev; 512 struct bpf_dtab_netdev *dev, *odev;
513 513
514 dev = READ_ONCE(dtab->netdev_map[i]); 514 dev = READ_ONCE(dtab->netdev_map[i]);
515 if (!dev || 515 if (!dev || netdev != dev->dev)
516 dev->dev->ifindex != netdev->ifindex)
517 continue; 516 continue;
518 odev = cmpxchg(&dtab->netdev_map[i], dev, NULL); 517 odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);
519 if (dev == odev) 518 if (dev == odev)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index ab0d5e3f9892..a74972b07e74 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -99,7 +99,6 @@ BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
99const struct bpf_func_proto bpf_map_pop_elem_proto = { 99const struct bpf_func_proto bpf_map_pop_elem_proto = {
100 .func = bpf_map_pop_elem, 100 .func = bpf_map_pop_elem,
101 .gpl_only = false, 101 .gpl_only = false,
102 .pkt_access = true,
103 .ret_type = RET_INTEGER, 102 .ret_type = RET_INTEGER,
104 .arg1_type = ARG_CONST_MAP_PTR, 103 .arg1_type = ARG_CONST_MAP_PTR,
105 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE, 104 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
@@ -113,7 +112,6 @@ BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
113const struct bpf_func_proto bpf_map_peek_elem_proto = { 112const struct bpf_func_proto bpf_map_peek_elem_proto = {
114 .func = bpf_map_pop_elem, 113 .func = bpf_map_pop_elem,
115 .gpl_only = false, 114 .gpl_only = false,
116 .pkt_access = true,
117 .ret_type = RET_INTEGER, 115 .ret_type = RET_INTEGER,
118 .arg1_type = ARG_CONST_MAP_PTR, 116 .arg1_type = ARG_CONST_MAP_PTR,
119 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE, 117 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
diff --git a/kernel/bpf/queue_stack_maps.c b/kernel/bpf/queue_stack_maps.c
index 12a93fb37449..8bbd72d3a121 100644
--- a/kernel/bpf/queue_stack_maps.c
+++ b/kernel/bpf/queue_stack_maps.c
@@ -122,6 +122,7 @@ static int __queue_map_get(struct bpf_map *map, void *value, bool delete)
122 raw_spin_lock_irqsave(&qs->lock, flags); 122 raw_spin_lock_irqsave(&qs->lock, flags);
123 123
124 if (queue_stack_map_is_empty(qs)) { 124 if (queue_stack_map_is_empty(qs)) {
125 memset(value, 0, qs->map.value_size);
125 err = -ENOENT; 126 err = -ENOENT;
126 goto out; 127 goto out;
127 } 128 }
@@ -151,6 +152,7 @@ static int __stack_map_get(struct bpf_map *map, void *value, bool delete)
151 raw_spin_lock_irqsave(&qs->lock, flags); 152 raw_spin_lock_irqsave(&qs->lock, flags);
152 153
153 if (queue_stack_map_is_empty(qs)) { 154 if (queue_stack_map_is_empty(qs)) {
155 memset(value, 0, qs->map.value_size);
154 err = -ENOENT; 156 err = -ENOENT;
155 goto out; 157 goto out;
156 } 158 }
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 98fa0be35370..171a2c88e77d 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1387,21 +1387,24 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
1387 enum bpf_access_type t) 1387 enum bpf_access_type t)
1388{ 1388{
1389 switch (env->prog->type) { 1389 switch (env->prog->type) {
1390 /* Program types only with direct read access go here! */
1390 case BPF_PROG_TYPE_LWT_IN: 1391 case BPF_PROG_TYPE_LWT_IN:
1391 case BPF_PROG_TYPE_LWT_OUT: 1392 case BPF_PROG_TYPE_LWT_OUT:
1392 case BPF_PROG_TYPE_LWT_SEG6LOCAL: 1393 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
1393 case BPF_PROG_TYPE_SK_REUSEPORT: 1394 case BPF_PROG_TYPE_SK_REUSEPORT:
1394 /* dst_input() and dst_output() can't write for now */ 1395 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1396 case BPF_PROG_TYPE_CGROUP_SKB:
1395 if (t == BPF_WRITE) 1397 if (t == BPF_WRITE)
1396 return false; 1398 return false;
1397 /* fallthrough */ 1399 /* fallthrough */
1400
1401 /* Program types with direct read + write access go here! */
1398 case BPF_PROG_TYPE_SCHED_CLS: 1402 case BPF_PROG_TYPE_SCHED_CLS:
1399 case BPF_PROG_TYPE_SCHED_ACT: 1403 case BPF_PROG_TYPE_SCHED_ACT:
1400 case BPF_PROG_TYPE_XDP: 1404 case BPF_PROG_TYPE_XDP:
1401 case BPF_PROG_TYPE_LWT_XMIT: 1405 case BPF_PROG_TYPE_LWT_XMIT:
1402 case BPF_PROG_TYPE_SK_SKB: 1406 case BPF_PROG_TYPE_SK_SKB:
1403 case BPF_PROG_TYPE_SK_MSG: 1407 case BPF_PROG_TYPE_SK_MSG:
1404 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1405 if (meta) 1408 if (meta)
1406 return meta->pkt_access; 1409 return meta->pkt_access;
1407 1410
@@ -5706,7 +5709,11 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
5706 bool is_narrower_load; 5709 bool is_narrower_load;
5707 u32 target_size; 5710 u32 target_size;
5708 5711
5709 if (ops->gen_prologue) { 5712 if (ops->gen_prologue || env->seen_direct_write) {
5713 if (!ops->gen_prologue) {
5714 verbose(env, "bpf verifier is misconfigured\n");
5715 return -EINVAL;
5716 }
5710 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, 5717 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
5711 env->prog); 5718 env->prog);
5712 if (cnt >= ARRAY_SIZE(insn_buf)) { 5719 if (cnt >= ARRAY_SIZE(insn_buf)) {