summaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-12-09 18:12:33 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2018-12-09 18:12:33 -0500
commitd48f782e4fb20dc7ec935ca0ca41ae31e4a69362 (patch)
tree482270b85d4ab9b1284e07e4cb439b4dc7af919f /kernel
parent8586ca8a214471e4573d76356aabe890bfecdc8a (diff)
parent35cc3cefc4de90001c9137e2d01dd9d06b11acfb (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
Pull networking fixes from David Miller: "A decent batch of fixes here. I'd say about half are for problems that have existed for a while, and half are for new regressions added in the 4.20 merge window. 1) Fix 10G SFP phy module detection in mvpp2, from Baruch Siach. 2) Revert bogus emac driver change, from Benjamin Herrenschmidt. 3) Handle BPF exported data structure with pointers when building 32-bit userland, from Daniel Borkmann. 4) Memory leak fix in act_police, from Davide Caratti. 5) Check RX checksum offload in RX descriptors properly in aquantia driver, from Dmitry Bogdanov. 6) SKB unlink fix in various spots, from Edward Cree. 7) ndo_dflt_fdb_dump() only works with ethernet, enforce this, from Eric Dumazet. 8) Fix FID leak in mlxsw driver, from Ido Schimmel. 9) IOTLB locking fix in vhost, from Jean-Philippe Brucker. 10) Fix SKB truesize accounting in ipv4/ipv6/netfilter frag memory limits otherwise namespace exit can hang. From Jiri Wiesner. 11) Address block parsing length fixes in x25 from Martin Schiller. 12) IRQ and ring accounting fixes in bnxt_en, from Michael Chan. 13) For tun interfaces, only iface delete works with rtnl ops, enforce this by disallowing add. From Nicolas Dichtel. 14) Use after free in liquidio, from Pan Bian. 15) Fix SKB use after passing to netif_receive_skb(), from Prashant Bhole. 16) Static key accounting and other fixes in XPS from Sabrina Dubroca. 17) Partially initialized flow key passed to ip6_route_output(), from Shmulik Ladkani. 18) Fix RTNL deadlock during reset in ibmvnic driver, from Thomas Falcon. 19) Several small TCP fixes (off-by-one on window probe abort, NULL deref in tail loss probe, SNMP mis-estimations) from Yuchung Cheng" * git://git.kernel.org/pub/scm/linux/kernel/git/davem/net: (93 commits) net/sched: cls_flower: Reject duplicated rules also under skip_sw bnxt_en: Fix _bnxt_get_max_rings() for 57500 chips. bnxt_en: Fix NQ/CP rings accounting on the new 57500 chips. bnxt_en: Keep track of reserved IRQs. bnxt_en: Fix CNP CoS queue regression. net/mlx4_core: Correctly set PFC param if global pause is turned off. Revert "net/ibm/emac: wrong bit is used for STA control" neighbour: Avoid writing before skb->head in neigh_hh_output() ipv6: Check available headroom in ip6_xmit() even without options tcp: lack of available data can also cause TSO defer ipv6: sr: properly initialize flowi6 prior passing to ip6_route_output mlxsw: spectrum_switchdev: Fix VLAN device deletion via ioctl mlxsw: spectrum_router: Relax GRE decap matching check mlxsw: spectrum_switchdev: Avoid leaking FID's reference count mlxsw: spectrum_nve: Remove easily triggerable warnings ipv4: ipv6: netfilter: Adjust the frag mem limit when truesize changes sctp: frag_point sanity check tcp: fix NULL ref in tail loss probe tcp: Do not underestimate rwnd_limited net: use skb_list_del_init() to remove from RX sublists ...
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/btf.c82
-rw-r--r--kernel/bpf/verifier.c103
2 files changed, 171 insertions, 14 deletions
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index ee4c82667d65..4da543d6bea2 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5,6 +5,7 @@
5#include <uapi/linux/types.h> 5#include <uapi/linux/types.h>
6#include <linux/seq_file.h> 6#include <linux/seq_file.h>
7#include <linux/compiler.h> 7#include <linux/compiler.h>
8#include <linux/ctype.h>
8#include <linux/errno.h> 9#include <linux/errno.h>
9#include <linux/slab.h> 10#include <linux/slab.h>
10#include <linux/anon_inodes.h> 11#include <linux/anon_inodes.h>
@@ -426,6 +427,30 @@ static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
426 offset < btf->hdr.str_len; 427 offset < btf->hdr.str_len;
427} 428}
428 429
430/* Only C-style identifier is permitted. This can be relaxed if
431 * necessary.
432 */
433static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
434{
435 /* offset must be valid */
436 const char *src = &btf->strings[offset];
437 const char *src_limit;
438
439 if (!isalpha(*src) && *src != '_')
440 return false;
441
442 /* set a limit on identifier length */
443 src_limit = src + KSYM_NAME_LEN;
444 src++;
445 while (*src && src < src_limit) {
446 if (!isalnum(*src) && *src != '_')
447 return false;
448 src++;
449 }
450
451 return !*src;
452}
453
429static const char *btf_name_by_offset(const struct btf *btf, u32 offset) 454static const char *btf_name_by_offset(const struct btf *btf, u32 offset)
430{ 455{
431 if (!offset) 456 if (!offset)
@@ -1143,6 +1168,22 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
1143 return -EINVAL; 1168 return -EINVAL;
1144 } 1169 }
1145 1170
1171 /* typedef type must have a valid name, and other ref types,
1172 * volatile, const, restrict, should have a null name.
1173 */
1174 if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
1175 if (!t->name_off ||
1176 !btf_name_valid_identifier(env->btf, t->name_off)) {
1177 btf_verifier_log_type(env, t, "Invalid name");
1178 return -EINVAL;
1179 }
1180 } else {
1181 if (t->name_off) {
1182 btf_verifier_log_type(env, t, "Invalid name");
1183 return -EINVAL;
1184 }
1185 }
1186
1146 btf_verifier_log_type(env, t, NULL); 1187 btf_verifier_log_type(env, t, NULL);
1147 1188
1148 return 0; 1189 return 0;
@@ -1300,6 +1341,13 @@ static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
1300 return -EINVAL; 1341 return -EINVAL;
1301 } 1342 }
1302 1343
1344 /* fwd type must have a valid name */
1345 if (!t->name_off ||
1346 !btf_name_valid_identifier(env->btf, t->name_off)) {
1347 btf_verifier_log_type(env, t, "Invalid name");
1348 return -EINVAL;
1349 }
1350
1303 btf_verifier_log_type(env, t, NULL); 1351 btf_verifier_log_type(env, t, NULL);
1304 1352
1305 return 0; 1353 return 0;
@@ -1356,6 +1404,12 @@ static s32 btf_array_check_meta(struct btf_verifier_env *env,
1356 return -EINVAL; 1404 return -EINVAL;
1357 } 1405 }
1358 1406
1407 /* array type should not have a name */
1408 if (t->name_off) {
1409 btf_verifier_log_type(env, t, "Invalid name");
1410 return -EINVAL;
1411 }
1412
1359 if (btf_type_vlen(t)) { 1413 if (btf_type_vlen(t)) {
1360 btf_verifier_log_type(env, t, "vlen != 0"); 1414 btf_verifier_log_type(env, t, "vlen != 0");
1361 return -EINVAL; 1415 return -EINVAL;
@@ -1532,6 +1586,13 @@ static s32 btf_struct_check_meta(struct btf_verifier_env *env,
1532 return -EINVAL; 1586 return -EINVAL;
1533 } 1587 }
1534 1588
1589 /* struct type either no name or a valid one */
1590 if (t->name_off &&
1591 !btf_name_valid_identifier(env->btf, t->name_off)) {
1592 btf_verifier_log_type(env, t, "Invalid name");
1593 return -EINVAL;
1594 }
1595
1535 btf_verifier_log_type(env, t, NULL); 1596 btf_verifier_log_type(env, t, NULL);
1536 1597
1537 last_offset = 0; 1598 last_offset = 0;
@@ -1543,6 +1604,12 @@ static s32 btf_struct_check_meta(struct btf_verifier_env *env,
1543 return -EINVAL; 1604 return -EINVAL;
1544 } 1605 }
1545 1606
1607 /* struct member either no name or a valid one */
1608 if (member->name_off &&
1609 !btf_name_valid_identifier(btf, member->name_off)) {
1610 btf_verifier_log_member(env, t, member, "Invalid name");
1611 return -EINVAL;
1612 }
1546 /* A member cannot be in type void */ 1613 /* A member cannot be in type void */
1547 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { 1614 if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
1548 btf_verifier_log_member(env, t, member, 1615 btf_verifier_log_member(env, t, member,
@@ -1730,6 +1797,13 @@ static s32 btf_enum_check_meta(struct btf_verifier_env *env,
1730 return -EINVAL; 1797 return -EINVAL;
1731 } 1798 }
1732 1799
1800 /* enum type either no name or a valid one */
1801 if (t->name_off &&
1802 !btf_name_valid_identifier(env->btf, t->name_off)) {
1803 btf_verifier_log_type(env, t, "Invalid name");
1804 return -EINVAL;
1805 }
1806
1733 btf_verifier_log_type(env, t, NULL); 1807 btf_verifier_log_type(env, t, NULL);
1734 1808
1735 for (i = 0; i < nr_enums; i++) { 1809 for (i = 0; i < nr_enums; i++) {
@@ -1739,6 +1813,14 @@ static s32 btf_enum_check_meta(struct btf_verifier_env *env,
1739 return -EINVAL; 1813 return -EINVAL;
1740 } 1814 }
1741 1815
1816 /* enum member must have a valid name */
1817 if (!enums[i].name_off ||
1818 !btf_name_valid_identifier(btf, enums[i].name_off)) {
1819 btf_verifier_log_type(env, t, "Invalid name");
1820 return -EINVAL;
1821 }
1822
1823
1742 btf_verifier_log(env, "\t%s val=%d\n", 1824 btf_verifier_log(env, "\t%s val=%d\n",
1743 btf_name_by_offset(btf, enums[i].name_off), 1825 btf_name_by_offset(btf, enums[i].name_off),
1744 enums[i].val); 1826 enums[i].val);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6dd419550aba..fc760d00a38c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -175,6 +175,7 @@ struct bpf_verifier_stack_elem {
175 175
176#define BPF_COMPLEXITY_LIMIT_INSNS 131072 176#define BPF_COMPLEXITY_LIMIT_INSNS 131072
177#define BPF_COMPLEXITY_LIMIT_STACK 1024 177#define BPF_COMPLEXITY_LIMIT_STACK 1024
178#define BPF_COMPLEXITY_LIMIT_STATES 64
178 179
179#define BPF_MAP_PTR_UNPRIV 1UL 180#define BPF_MAP_PTR_UNPRIV 1UL
180#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ 181#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
@@ -3751,6 +3752,79 @@ static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
3751 } 3752 }
3752} 3753}
3753 3754
3755/* compute branch direction of the expression "if (reg opcode val) goto target;"
3756 * and return:
3757 * 1 - branch will be taken and "goto target" will be executed
3758 * 0 - branch will not be taken and fall-through to next insn
3759 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
3760 */
3761static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode)
3762{
3763 if (__is_pointer_value(false, reg))
3764 return -1;
3765
3766 switch (opcode) {
3767 case BPF_JEQ:
3768 if (tnum_is_const(reg->var_off))
3769 return !!tnum_equals_const(reg->var_off, val);
3770 break;
3771 case BPF_JNE:
3772 if (tnum_is_const(reg->var_off))
3773 return !tnum_equals_const(reg->var_off, val);
3774 break;
3775 case BPF_JGT:
3776 if (reg->umin_value > val)
3777 return 1;
3778 else if (reg->umax_value <= val)
3779 return 0;
3780 break;
3781 case BPF_JSGT:
3782 if (reg->smin_value > (s64)val)
3783 return 1;
3784 else if (reg->smax_value < (s64)val)
3785 return 0;
3786 break;
3787 case BPF_JLT:
3788 if (reg->umax_value < val)
3789 return 1;
3790 else if (reg->umin_value >= val)
3791 return 0;
3792 break;
3793 case BPF_JSLT:
3794 if (reg->smax_value < (s64)val)
3795 return 1;
3796 else if (reg->smin_value >= (s64)val)
3797 return 0;
3798 break;
3799 case BPF_JGE:
3800 if (reg->umin_value >= val)
3801 return 1;
3802 else if (reg->umax_value < val)
3803 return 0;
3804 break;
3805 case BPF_JSGE:
3806 if (reg->smin_value >= (s64)val)
3807 return 1;
3808 else if (reg->smax_value < (s64)val)
3809 return 0;
3810 break;
3811 case BPF_JLE:
3812 if (reg->umax_value <= val)
3813 return 1;
3814 else if (reg->umin_value > val)
3815 return 0;
3816 break;
3817 case BPF_JSLE:
3818 if (reg->smax_value <= (s64)val)
3819 return 1;
3820 else if (reg->smin_value > (s64)val)
3821 return 0;
3822 break;
3823 }
3824
3825 return -1;
3826}
3827
3754/* Adjusts the register min/max values in the case that the dst_reg is the 3828/* Adjusts the register min/max values in the case that the dst_reg is the
3755 * variable register that we are working on, and src_reg is a constant or we're 3829 * variable register that we are working on, and src_reg is a constant or we're
3756 * simply doing a BPF_K check. 3830 * simply doing a BPF_K check.
@@ -4152,21 +4226,15 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
4152 4226
4153 dst_reg = &regs[insn->dst_reg]; 4227 dst_reg = &regs[insn->dst_reg];
4154 4228
4155 /* detect if R == 0 where R was initialized to zero earlier */ 4229 if (BPF_SRC(insn->code) == BPF_K) {
4156 if (BPF_SRC(insn->code) == BPF_K && 4230 int pred = is_branch_taken(dst_reg, insn->imm, opcode);
4157 (opcode == BPF_JEQ || opcode == BPF_JNE) && 4231
4158 dst_reg->type == SCALAR_VALUE && 4232 if (pred == 1) {
4159 tnum_is_const(dst_reg->var_off)) { 4233 /* only follow the goto, ignore fall-through */
4160 if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) ||
4161 (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) {
4162 /* if (imm == imm) goto pc+off;
4163 * only follow the goto, ignore fall-through
4164 */
4165 *insn_idx += insn->off; 4234 *insn_idx += insn->off;
4166 return 0; 4235 return 0;
4167 } else { 4236 } else if (pred == 0) {
4168 /* if (imm != imm) goto pc+off; 4237 /* only follow fall-through branch, since
4169 * only follow fall-through branch, since
4170 * that's where the program will go 4238 * that's where the program will go
4171 */ 4239 */
4172 return 0; 4240 return 0;
@@ -4980,7 +5048,7 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
4980 struct bpf_verifier_state_list *new_sl; 5048 struct bpf_verifier_state_list *new_sl;
4981 struct bpf_verifier_state_list *sl; 5049 struct bpf_verifier_state_list *sl;
4982 struct bpf_verifier_state *cur = env->cur_state, *new; 5050 struct bpf_verifier_state *cur = env->cur_state, *new;
4983 int i, j, err; 5051 int i, j, err, states_cnt = 0;
4984 5052
4985 sl = env->explored_states[insn_idx]; 5053 sl = env->explored_states[insn_idx];
4986 if (!sl) 5054 if (!sl)
@@ -5007,8 +5075,12 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
5007 return 1; 5075 return 1;
5008 } 5076 }
5009 sl = sl->next; 5077 sl = sl->next;
5078 states_cnt++;
5010 } 5079 }
5011 5080
5081 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
5082 return 0;
5083
5012 /* there were no equivalent states, remember current one. 5084 /* there were no equivalent states, remember current one.
5013 * technically the current state is not proven to be safe yet, 5085 * technically the current state is not proven to be safe yet,
5014 * but it will either reach outer most bpf_exit (which means it's safe) 5086 * but it will either reach outer most bpf_exit (which means it's safe)
@@ -5148,6 +5220,9 @@ static int do_check(struct bpf_verifier_env *env)
5148 goto process_bpf_exit; 5220 goto process_bpf_exit;
5149 } 5221 }
5150 5222
5223 if (signal_pending(current))
5224 return -EAGAIN;
5225
5151 if (need_resched()) 5226 if (need_resched())
5152 cond_resched(); 5227 cond_resched();
5153 5228