aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosef Bacik <jbacik@fb.com>2016-11-14 15:45:36 -0500
committerDavid S. Miller <davem@davemloft.net>2016-11-16 13:21:45 -0500
commitf23cc643f9baec7f71f2b74692da3cf03abbbfda (patch)
tree7a4064ef679c1a0394386eaf5dc538c47c9007a7
parentb3cfaa31e3851c743d3f9d3441710f7ff6f7e868 (diff)
bpf: fix range arithmetic for bpf map access
I made some invalid assumptions with BPF_AND and BPF_MOD that could result in invalid accesses to bpf map entries. Fix this up by doing a few things 1) Kill BPF_MOD support. This doesn't actually get used by the compiler in real life and just adds extra complexity. 2) Fix the logic for BPF_AND, don't allow AND of negative numbers and set the minimum value to 0 for positive AND's. 3) Don't do operations on the ranges if they are set to the limits, as they are by definition undefined, and allowing arithmetic operations on those values could make them appear valid when they really aren't. This fixes the testcase provided by Jann as well as a few other theoretical problems. Reported-by: Jann Horn <jannh@google.com> Signed-off-by: Josef Bacik <jbacik@fb.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/linux/bpf_verifier.h5
-rw-r--r--kernel/bpf/verifier.c70
2 files changed, 50 insertions, 25 deletions
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 7035b997aaa5..6aaf425cebc3 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -14,7 +14,7 @@
14 * are obviously wrong for any sort of memory access. 14 * are obviously wrong for any sort of memory access.
15 */ 15 */
16#define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024) 16#define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024)
17#define BPF_REGISTER_MIN_RANGE -(1024 * 1024 * 1024) 17#define BPF_REGISTER_MIN_RANGE -1
18 18
19struct bpf_reg_state { 19struct bpf_reg_state {
20 enum bpf_reg_type type; 20 enum bpf_reg_type type;
@@ -22,7 +22,8 @@ struct bpf_reg_state {
22 * Used to determine if any memory access using this register will 22 * Used to determine if any memory access using this register will
23 * result in a bad access. 23 * result in a bad access.
24 */ 24 */
25 u64 min_value, max_value; 25 s64 min_value;
26 u64 max_value;
26 union { 27 union {
27 /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */ 28 /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */
28 s64 imm; 29 s64 imm;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 99a7e5b388f2..6a936159c6e0 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -216,8 +216,8 @@ static void print_verifier_state(struct bpf_verifier_state *state)
216 reg->map_ptr->key_size, 216 reg->map_ptr->key_size,
217 reg->map_ptr->value_size); 217 reg->map_ptr->value_size);
218 if (reg->min_value != BPF_REGISTER_MIN_RANGE) 218 if (reg->min_value != BPF_REGISTER_MIN_RANGE)
219 verbose(",min_value=%llu", 219 verbose(",min_value=%lld",
220 (unsigned long long)reg->min_value); 220 (long long)reg->min_value);
221 if (reg->max_value != BPF_REGISTER_MAX_RANGE) 221 if (reg->max_value != BPF_REGISTER_MAX_RANGE)
222 verbose(",max_value=%llu", 222 verbose(",max_value=%llu",
223 (unsigned long long)reg->max_value); 223 (unsigned long long)reg->max_value);
@@ -758,7 +758,7 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
758 * index'es we need to make sure that whatever we use 758 * index'es we need to make sure that whatever we use
759 * will have a set floor within our range. 759 * will have a set floor within our range.
760 */ 760 */
761 if ((s64)reg->min_value < 0) { 761 if (reg->min_value < 0) {
762 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", 762 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
763 regno); 763 regno);
764 return -EACCES; 764 return -EACCES;
@@ -1468,7 +1468,8 @@ static void check_reg_overflow(struct bpf_reg_state *reg)
1468{ 1468{
1469 if (reg->max_value > BPF_REGISTER_MAX_RANGE) 1469 if (reg->max_value > BPF_REGISTER_MAX_RANGE)
1470 reg->max_value = BPF_REGISTER_MAX_RANGE; 1470 reg->max_value = BPF_REGISTER_MAX_RANGE;
1471 if ((s64)reg->min_value < BPF_REGISTER_MIN_RANGE) 1471 if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
1472 reg->min_value > BPF_REGISTER_MAX_RANGE)
1472 reg->min_value = BPF_REGISTER_MIN_RANGE; 1473 reg->min_value = BPF_REGISTER_MIN_RANGE;
1473} 1474}
1474 1475
@@ -1476,7 +1477,8 @@ static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1476 struct bpf_insn *insn) 1477 struct bpf_insn *insn)
1477{ 1478{
1478 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; 1479 struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
1479 u64 min_val = BPF_REGISTER_MIN_RANGE, max_val = BPF_REGISTER_MAX_RANGE; 1480 s64 min_val = BPF_REGISTER_MIN_RANGE;
1481 u64 max_val = BPF_REGISTER_MAX_RANGE;
1480 bool min_set = false, max_set = false; 1482 bool min_set = false, max_set = false;
1481 u8 opcode = BPF_OP(insn->code); 1483 u8 opcode = BPF_OP(insn->code);
1482 1484
@@ -1512,22 +1514,43 @@ static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1512 return; 1514 return;
1513 } 1515 }
1514 1516
1517 /* If one of our values was at the end of our ranges then we can't just
1518 * do our normal operations to the register, we need to set the values
1519 * to the min/max since they are undefined.
1520 */
1521 if (min_val == BPF_REGISTER_MIN_RANGE)
1522 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1523 if (max_val == BPF_REGISTER_MAX_RANGE)
1524 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1525
1515 switch (opcode) { 1526 switch (opcode) {
1516 case BPF_ADD: 1527 case BPF_ADD:
1517 dst_reg->min_value += min_val; 1528 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1518 dst_reg->max_value += max_val; 1529 dst_reg->min_value += min_val;
1530 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1531 dst_reg->max_value += max_val;
1519 break; 1532 break;
1520 case BPF_SUB: 1533 case BPF_SUB:
1521 dst_reg->min_value -= min_val; 1534 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1522 dst_reg->max_value -= max_val; 1535 dst_reg->min_value -= min_val;
1536 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1537 dst_reg->max_value -= max_val;
1523 break; 1538 break;
1524 case BPF_MUL: 1539 case BPF_MUL:
1525 dst_reg->min_value *= min_val; 1540 if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1526 dst_reg->max_value *= max_val; 1541 dst_reg->min_value *= min_val;
1542 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1543 dst_reg->max_value *= max_val;
1527 break; 1544 break;
1528 case BPF_AND: 1545 case BPF_AND:
1529 /* & is special since it could end up with 0 bits set. */ 1546 /* Disallow AND'ing of negative numbers, ain't nobody got time
1530 dst_reg->min_value &= min_val; 1547 * for that. Otherwise the minimum is 0 and the max is the max
1548 * value we could AND against.
1549 */
1550 if (min_val < 0)
1551 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1552 else
1553 dst_reg->min_value = 0;
1531 dst_reg->max_value = max_val; 1554 dst_reg->max_value = max_val;
1532 break; 1555 break;
1533 case BPF_LSH: 1556 case BPF_LSH:
@@ -1537,24 +1560,25 @@ static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1537 */ 1560 */
1538 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE)) 1561 if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
1539 dst_reg->min_value = BPF_REGISTER_MIN_RANGE; 1562 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1540 else 1563 else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
1541 dst_reg->min_value <<= min_val; 1564 dst_reg->min_value <<= min_val;
1542 1565
1543 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE)) 1566 if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
1544 dst_reg->max_value = BPF_REGISTER_MAX_RANGE; 1567 dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
1545 else 1568 else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1546 dst_reg->max_value <<= max_val; 1569 dst_reg->max_value <<= max_val;
1547 break; 1570 break;
1548 case BPF_RSH: 1571 case BPF_RSH:
1549 dst_reg->min_value >>= min_val; 1572 /* RSH by a negative number is undefined, and the BPF_RSH is an
1550 dst_reg->max_value >>= max_val; 1573 * unsigned shift, so make the appropriate casts.
1551 break;
1552 case BPF_MOD:
1553 /* % is special since it is an unsigned modulus, so the floor
1554 * will always be 0.
1555 */ 1574 */
1556 dst_reg->min_value = 0; 1575 if (min_val < 0 || dst_reg->min_value < 0)
1557 dst_reg->max_value = max_val - 1; 1576 dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
1577 else
1578 dst_reg->min_value =
1579 (u64)(dst_reg->min_value) >> min_val;
1580 if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
1581 dst_reg->max_value >>= max_val;
1558 break; 1582 break;
1559 default: 1583 default:
1560 reset_reg_range_values(regs, insn->dst_reg); 1584 reset_reg_range_values(regs, insn->dst_reg);