• Yonghong Song's avatar
    bpf: Add missed var_off setting in set_sext32_default_val() · 380d5f89
    Yonghong Song authored
    Zac reported a verification failure and Alexei reproduced the issue
    with a simple reproducer ([1]). The verification failure is due to missed
    setting for var_off.
    
    The following is the reproducer in [1]:
      0: R1=ctx() R10=fp0
      0: (71) r3 = *(u8 *)(r10 -387)        ;
         R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff)) R10=fp0
      1: (bc) w7 = (s8)w3                   ;
         R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff))
         R7_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=127,var_off=(0x0; 0x7f))
      2: (36) if w7 >= 0x2533823b goto pc-3
         mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1
         mark_precise: frame0: regs=r7 stack= before 1: (bc) w7 = (s8)w3
         mark_precise: frame0: regs=r3 stack= before 0: (71) r3 = *(u8 *)(r10 -387)
      2: R7_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=127,var_off=(0x0; 0x7f))
      3: (b4) w0 = 0                        ; R0_w=0
      4: (95) exit
    
    Note that after insn 1, the var_off for R7 is (0x0; 0x7f). This is not correct
    since upper 24 bits of w7 could be 0 or 1. So correct var_off should be
    (0x0; 0xffffffff). Missing var_off setting in set_sext32_default_val() caused later
    incorrect analysis in zext_32_to_64(dst_reg) and reg_bounds_sync(dst_reg).
    
    To fix the issue, set var_off correctly in set_sext32_default_val(). The correct
    reg state after insn 1 becomes:
      1: (bc) w7 = (s8)w3                   ;
         R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff))
         R7_w=scalar(smin=0,smax=umax=0xffffffff,smin32=-128,smax32=127,var_off=(0x0; 0xffffffff))
    and at insn 2, the verifier correctly determines either branch is possible.
    
      [1] https://lore.kernel.org/bpf/CAADnVQLPU0Shz7dWV4bn2BgtGdxN3uFHPeobGBA72tpg5Xoykw@mail.gmail.com/
    
    Fixes: 8100928c ("bpf: Support new sign-extension mov insns")
    Reported-by: default avatarZac Ecob <zacecob@protonmail.com>
    Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20240615174626.3994813-1-yonghong.song@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    380d5f89
verifier.c 649 KB