bpf: generalize MAYBE_NULL vs non-MAYBE_NULL rule
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 23 Dec 2022 05:49:17 +0000 (21:49 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 28 Dec 2022 01:37:07 +0000 (17:37 -0800)
Make generic check to prevent XXX_OR_NULL and XXX register types to be
intermixed. While technically in some situations it could be safe, it's
impossible to enforce due to the loss of an ID when converting
XXX_OR_NULL to its non-NULL variant. So prevent this in general, not
just for PTR_TO_MAP_KEY and PTR_TO_MAP_VALUE.

PTR_TO_MAP_KEY_OR_NULL and PTR_TO_MAP_VALUE_OR_NULL checks, which were
previously special-cased, are simplified to generic check that takes
into account range_within() and tnum_in(). This is correct as BPF
verifier doesn't allow arithmetic on XXX_OR_NULL register types, so
var_off and ranges should stay zero. But even if in the future this
restriction is lifted, it's even more important to enforce that var_off
and ranges are compatible, otherwise it's possible to construct case
where this can be exploited to bypass verifier's memory range safety
checks.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20221223054921.958283-4-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c

index e419e60..218a7ac 100644 (file)
@@ -13074,6 +13074,21 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
                return true;
        if (rcur->type == NOT_INIT)
                return false;
+
+       /* Register types that are *not* MAYBE_NULL could technically be safe
+        * to use as their MAYBE_NULL variants (e.g., PTR_TO_MAP_VALUE  is
+        * safe to be used as PTR_TO_MAP_VALUE_OR_NULL, provided both point to
+        * the same map).
+        * However, if the old MAYBE_NULL register then got NULL checked,
+        * doing so could have affected others with the same id, and we can't
+        * check for that because we lost the id when we converted to
+        * a non-MAYBE_NULL variant.
+        * So, as a general rule we don't allow mixing MAYBE_NULL and
+        * non-MAYBE_NULL registers.
+        */
+       if (type_may_be_null(rold->type) != type_may_be_null(rcur->type))
+               return false;
+
        switch (base_type(rold->type)) {
        case SCALAR_VALUE:
                if (equal)
@@ -13098,22 +13113,6 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
                }
        case PTR_TO_MAP_KEY:
        case PTR_TO_MAP_VALUE:
-               /* a PTR_TO_MAP_VALUE could be safe to use as a
-                * PTR_TO_MAP_VALUE_OR_NULL into the same map.
-                * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
-                * checked, doing so could have affected others with the same
-                * id, and we can't check for that because we lost the id when
-                * we converted to a PTR_TO_MAP_VALUE.
-                */
-               if (type_may_be_null(rold->type)) {
-                       if (!type_may_be_null(rcur->type))
-                               return false;
-                       if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, var_off)))
-                               return false;
-                       /* Check our ids match any regs they're supposed to */
-                       return check_ids(rold->id, rcur->id, idmap);
-               }
-
                /* If the new min/max/var_off satisfy the old ones and
                 * everything else matches, we are OK.
                 */