bpf/verifier: fix min/max handling in BPF_SUB
authorEdward Cree <ecree@solarflare.com>
Fri, 21 Jul 2017 13:37:34 +0000 (14:37 +0100)
committerDavid S. Miller <davem@davemloft.net>
Mon, 24 Jul 2017 21:02:55 +0000 (14:02 -0700)
We have to subtract the src max from the dst min, and vice-versa, since
 (e.g.) the smallest result comes from the largest subtrahend.

Fixes: 484611357c19 ("bpf: allow access into map value arrays")
Signed-off-by: Edward Cree <ecree@solarflare.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
kernel/bpf/verifier.c

index af9e84a4944e60fdd617c0c69ecbc46d53e230ac..664d939723739e0333fb5921533e3327912a3b84 100644 (file)
@@ -1865,10 +1865,12 @@ static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
         * do our normal operations to the register, we need to set the values
         * to the min/max since they are undefined.
         */
-       if (min_val == BPF_REGISTER_MIN_RANGE)
-               dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
-       if (max_val == BPF_REGISTER_MAX_RANGE)
-               dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
+       if (opcode != BPF_SUB) {
+               if (min_val == BPF_REGISTER_MIN_RANGE)
+                       dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
+               if (max_val == BPF_REGISTER_MAX_RANGE)
+                       dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
+       }
 
        switch (opcode) {
        case BPF_ADD:
@@ -1879,10 +1881,17 @@ static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
                dst_reg->min_align = min(src_align, dst_align);
                break;
        case BPF_SUB:
+               /* If one of our values was at the end of our ranges, then the
+                * _opposite_ value in the dst_reg goes to the end of our range.
+                */
+               if (min_val == BPF_REGISTER_MIN_RANGE)
+                       dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
+               if (max_val == BPF_REGISTER_MAX_RANGE)
+                       dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
                if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
-                       dst_reg->min_value -= min_val;
+                       dst_reg->min_value -= max_val;
                if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
-                       dst_reg->max_value -= max_val;
+                       dst_reg->max_value -= min_val;
                dst_reg->min_align = min(src_align, dst_align);
                break;
        case BPF_MUL: