• Xu Kuohai's avatar
    bpf: Fix compare error in function retval_range_within · 763aa759
    Xu Kuohai authored
    After checking lsm hook return range in verifier, the test case
    "test_progs -t test_lsm" failed, and the failure log says:
    
    libbpf: prog 'test_int_hook': BPF program load failed: Invalid argument
    libbpf: prog 'test_int_hook': -- BEGIN PROG LOAD LOG --
    0: R1=ctx() R10=fp0
    ; int BPF_PROG(test_int_hook, struct vm_area_struct *vma, @ lsm.c:89
    0: (79) r0 = *(u64 *)(r1 +24)         ; R0_w=scalar(smin=smin32=-4095,smax=smax32=0) R1=ctx()
    
    [...]
    
    24: (b4) w0 = -1                      ; R0_w=0xffffffff
    ; int BPF_PROG(test_int_hook, struct vm_area_struct *vma, @ lsm.c:89
    25: (95) exit
    At program exit the register R0 has smin=4294967295 smax=4294967295 should have been in [-4095, 0]
    
    It can be seen that instruction "w0 = -1" zero extended -1 to 64-bit
    register r0, setting both smin and smax values of r0 to 4294967295.
    This resulted in a false reject when r0 was checked with range [-4095, 0].
    
    Given bpf lsm does not return 64-bit values, this patch fixes it by changing
    the compare between r0 and return range from 64-bit operation to 32-bit
    operation for bpf lsm.
    
    Fixes: 8fa4ecd4 ("bpf: enforce exact retval range on subprog/callback exit")
    Signed-off-by: default avatarXu Kuohai <xukuohai@huawei.com>
    Acked-by: default avatarShung-Hsi Yu <shung-hsi.yu@suse.com>
    Link: https://lore.kernel.org/r/20240719110059.797546-5-xukuohai@huaweicloud.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    763aa759
verifier.c 656 KB