]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
bpf: preserve constant zero when doing partial register restore
authorAndrii Nakryiko <andrii@kernel.org>
Tue, 5 Dec 2023 18:42:44 +0000 (10:42 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 5 Dec 2023 21:40:21 +0000 (13:40 -0800)
Similar to special handling of STACK_ZERO, when reading 1/2/4 bytes from
stack from slot that has register spilled into it and that register has
a constant value zero, preserve that zero and mark spilled register as
precise for that. This makes spilled const zero register and STACK_ZERO
cases equivalent in their behavior.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20231205184248.1502704-7-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c

index 93de39a6e36e5c96845ab2abe10dc495bef0cfcc..1ebe76c984517c47a4c586e6794743f78a7d2b62 100644 (file)
@@ -4767,22 +4767,39 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
                                copy_register_state(&state->regs[dst_regno], reg);
                                state->regs[dst_regno].subreg_def = subreg_def;
                        } else {
+                               int spill_cnt = 0, zero_cnt = 0;
+
                                for (i = 0; i < size; i++) {
                                        type = stype[(slot - i) % BPF_REG_SIZE];
-                                       if (type == STACK_SPILL)
+                                       if (type == STACK_SPILL) {
+                                               spill_cnt++;
                                                continue;
+                                       }
                                        if (type == STACK_MISC)
                                                continue;
-                                       if (type == STACK_ZERO)
+                                       if (type == STACK_ZERO) {
+                                               zero_cnt++;
                                                continue;
+                                       }
                                        if (type == STACK_INVALID && env->allow_uninit_stack)
                                                continue;
                                        verbose(env, "invalid read from stack off %d+%d size %d\n",
                                                off, i, size);
                                        return -EACCES;
                                }
-                               mark_reg_unknown(env, state->regs, dst_regno);
-                               insn_flags = 0; /* not restoring original register state */
+
+                               if (spill_cnt == size &&
+                                   tnum_is_const(reg->var_off) && reg->var_off.value == 0) {
+                                       __mark_reg_const_zero(&state->regs[dst_regno]);
+                                       /* this IS register fill, so keep insn_flags */
+                               } else if (zero_cnt == size) {
+                                       /* similarly to mark_reg_stack_read(), preserve zeroes */
+                                       __mark_reg_const_zero(&state->regs[dst_regno]);
+                                       insn_flags = 0; /* not restoring original register state */
+                               } else {
+                                       mark_reg_unknown(env, state->regs, dst_regno);
+                                       insn_flags = 0; /* not restoring original register state */
+                               }
                        }
                        state->regs[dst_regno].live |= REG_LIVE_WRITTEN;
                } else if (dst_regno >= 0) {