mirror of
https://github.com/torvalds/linux.git
synced 2026-05-23 06:31:58 +02:00
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: 8fa4ecd49b ("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>
This commit is contained in:
parent
28ead3eaab
commit
763aa759d3
|
|
@ -9984,9 +9984,13 @@ static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env)
|
|||
return is_rbtree_lock_required_kfunc(kfunc_btf_id);
|
||||
}
|
||||
|
||||
static bool retval_range_within(struct bpf_retval_range range, const struct bpf_reg_state *reg)
|
||||
static bool retval_range_within(struct bpf_retval_range range, const struct bpf_reg_state *reg,
|
||||
bool return_32bit)
|
||||
{
|
||||
return range.minval <= reg->smin_value && reg->smax_value <= range.maxval;
|
||||
if (return_32bit)
|
||||
return range.minval <= reg->s32_min_value && reg->s32_max_value <= range.maxval;
|
||||
else
|
||||
return range.minval <= reg->smin_value && reg->smax_value <= range.maxval;
|
||||
}
|
||||
|
||||
static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
|
||||
|
|
@ -10023,8 +10027,8 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
|
|||
if (err)
|
||||
return err;
|
||||
|
||||
/* enforce R0 return value range */
|
||||
if (!retval_range_within(callee->callback_ret_range, r0)) {
|
||||
/* enforce R0 return value range, and bpf_callback_t returns 64bit */
|
||||
if (!retval_range_within(callee->callback_ret_range, r0, false)) {
|
||||
verbose_invalid_scalar(env, r0, callee->callback_ret_range,
|
||||
"At callback return", "R0");
|
||||
return -EINVAL;
|
||||
|
|
@ -15698,6 +15702,7 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
|
|||
int err;
|
||||
struct bpf_func_state *frame = env->cur_state->frame[0];
|
||||
const bool is_subprog = frame->subprogno;
|
||||
bool return_32bit = false;
|
||||
|
||||
/* LSM and struct_ops func-ptr's return type could be "void" */
|
||||
if (!is_subprog || frame->in_exception_callback_fn) {
|
||||
|
|
@ -15809,6 +15814,7 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
|
|||
/* no restricted range, any return value is allowed */
|
||||
if (range.minval == S32_MIN && range.maxval == S32_MAX)
|
||||
return 0;
|
||||
return_32bit = true;
|
||||
} else if (!env->prog->aux->attach_func_proto->type) {
|
||||
/* Make sure programs that attach to void
|
||||
* hooks don't try to modify return value.
|
||||
|
|
@ -15839,7 +15845,7 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
|
|||
if (err)
|
||||
return err;
|
||||
|
||||
if (!retval_range_within(range, reg)) {
|
||||
if (!retval_range_within(range, reg, return_32bit)) {
|
||||
verbose_invalid_scalar(env, reg, range, exit_ctx, reg_name);
|
||||
if (!is_subprog &&
|
||||
prog->expected_attach_type == BPF_LSM_CGROUP &&
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user