Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Fix compare error in function retval_range_within

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: 8fa4ecd49b81 ("bpf: enforce exact retval range on subprog/callback exit")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Link: https://lore.kernel.org/r/20240719110059.797546-5-xukuohai@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

authored by

Xu Kuohai and committed by
Andrii Nakryiko
763aa759 28ead3ea

+11 -5
+11 -5
kernel/bpf/verifier.c
··· 9984 9984 return is_rbtree_lock_required_kfunc(kfunc_btf_id); 9985 9985 } 9986 9986 9987 - static bool retval_range_within(struct bpf_retval_range range, const struct bpf_reg_state *reg) 9987 + static bool retval_range_within(struct bpf_retval_range range, const struct bpf_reg_state *reg, 9988 + bool return_32bit) 9988 9989 { 9989 - return range.minval <= reg->smin_value && reg->smax_value <= range.maxval; 9990 + if (return_32bit) 9991 + return range.minval <= reg->s32_min_value && reg->s32_max_value <= range.maxval; 9992 + else 9993 + return range.minval <= reg->smin_value && reg->smax_value <= range.maxval; 9990 9994 } 9991 9995 9992 9996 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) ··· 10027 10023 if (err) 10028 10024 return err; 10029 10025 10030 - /* enforce R0 return value range */ 10031 - if (!retval_range_within(callee->callback_ret_range, r0)) { 10026 + /* enforce R0 return value range, and bpf_callback_t returns 64bit */ 10027 + if (!retval_range_within(callee->callback_ret_range, r0, false)) { 10032 10028 verbose_invalid_scalar(env, r0, callee->callback_ret_range, 10033 10029 "At callback return", "R0"); 10034 10030 return -EINVAL; ··· 15702 15698 int err; 15703 15699 struct bpf_func_state *frame = env->cur_state->frame[0]; 15704 15700 const bool is_subprog = frame->subprogno; 15701 + bool return_32bit = false; 15705 15702 15706 15703 /* LSM and struct_ops func-ptr's return type could be "void" */ 15707 15704 if (!is_subprog || frame->in_exception_callback_fn) { ··· 15814 15809 /* no restricted range, any return value is allowed */ 15815 15810 if (range.minval == S32_MIN && range.maxval == S32_MAX) 15816 15811 return 0; 15812 + return_32bit = true; 15817 15813 } else if (!env->prog->aux->attach_func_proto->type) { 15818 15814 /* Make sure programs that attach to void 15819 15815 * hooks don't try to modify return value. ··· 15845 15839 if (err) 15846 15840 return err; 15847 15841 15848 - if (!retval_range_within(range, reg)) { 15842 + if (!retval_range_within(range, reg, return_32bit)) { 15849 15843 verbose_invalid_scalar(env, reg, range, exit_ctx, reg_name); 15850 15844 if (!is_subprog && 15851 15845 prog->expected_attach_type == BPF_LSM_CGROUP &&