]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bpf: Propagate untrusted pointer state in commuted arithmetic
authorYiyang Chen <chenyy23@mails.tsinghua.edu.cn>
Wed, 29 Jul 2026 15:18:29 +0000 (15:18 +0000)
committerEduard Zingerman <eddyz87@gmail.com>
Fri, 31 Jul 2026 19:45:28 +0000 (12:45 -0700)
The untrusted PTR_TO_MEM early return skips pointer offset tracking
because accesses go through probe-read handling. Moving it after full
pointer-state propagation ensures scalar += untrusted_pointer leaves the
destination as PTR_TO_MEM instead of an unrelated scalar.

Fixes: f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()")
Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
Tested-by: Daniel Wade <danjwade95@gmail.com>
Link: https://patch.msgid.link/20260729-c3-035-public-bpf-v4-v4-3-8ee297e2346b@mails.tsinghua.edu.cn
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
kernel/bpf/verifier.c

index cdb61fab843518989da5913091f16ccd33c7bf38..fdc5fbb1f78ca7888c14654db82fbd30a14a8db1 100644 (file)
@@ -13707,13 +13707,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                return -EACCES;
        }
 
-       /*
-        * Accesses to untrusted PTR_TO_MEM are done through probe
-        * instructions, hence no need to track offsets.
-        */
-       if (base_type(ptr_reg->type) == PTR_TO_MEM && (ptr_reg->type & PTR_UNTRUSTED))
-               return 0;
-
        switch (base_type(ptr_reg->type)) {
        case PTR_TO_CTX:
        case PTR_TO_MAP_VALUE:
@@ -13750,6 +13743,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
        if (dst_reg != ptr_reg)
                *dst_reg = *ptr_reg;
 
+       /*
+        * Accesses to untrusted PTR_TO_MEM are done through probe
+        * instructions, hence no need to track offsets.
+        */
+       if (base_type(ptr_reg->type) == PTR_TO_MEM && (ptr_reg->type & PTR_UNTRUSTED))
+               return 0;
+
        if (!check_reg_sane_offset_scalar(env, off_reg, ptr_reg->type) ||
            !check_reg_sane_offset_ptr(env, ptr_reg, ptr_reg->type))
                return -EINVAL;