bpf: Make bpf_for_each_spilled_reg consider narrow spills
authorMaxim Mikityanskiy <maxim@isovalent.com>
Mon, 8 Jan 2024 20:51:58 +0000 (22:51 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 23 Jan 2024 22:40:22 +0000 (14:40 -0800)
Adjust the check in bpf_get_spilled_reg to take into account spilled
registers narrower than 64 bits. That allows find_equal_scalars to
properly adjust the range of all spilled registers that have the same
ID. Before this change, it was possible for a register and a spilled
register to have the same IDs but different ranges if the spill was
narrower than 64 bits and a range check was performed on the register.

Signed-off-by: Maxim Mikityanskiy <maxim@isovalent.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20240108205209.838365-5-maxtram95@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf_verifier.h

index d07d857..e11baec 100644 (file)
@@ -453,7 +453,7 @@ struct bpf_verifier_state {
 
 #define bpf_get_spilled_reg(slot, frame, mask)                         \
        (((slot < frame->allocated_stack / BPF_REG_SIZE) &&             \
-         ((1 << frame->stack[slot].slot_type[0]) & (mask))) \
+         ((1 << frame->stack[slot].slot_type[BPF_REG_SIZE - 1]) & (mask))) \
         ? &frame->stack[slot].spilled_ptr : NULL)
 
 /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */