On Sun, Mar 20, 2022 at 09:25:02PM +0530, Kumar Kartikeya Dwivedi wrote: > static int map_kptr_match_type(struct bpf_verifier_env *env, > struct bpf_map_value_off_desc *off_desc, > - struct bpf_reg_state *reg, u32 regno) > + struct bpf_reg_state *reg, u32 regno, > + bool ref_ptr) > { > const char *targ_name = kernel_type_name(off_desc->btf, off_desc->btf_id); > const char *reg_name = ""; > + bool fixed_off_ok = true; > > if (reg->type != PTR_TO_BTF_ID && reg->type != PTR_TO_BTF_ID_OR_NULL) > goto bad_type; > @@ -3525,7 +3530,26 @@ static int map_kptr_match_type(struct bpf_verifier_env *env, > /* We need to verify reg->type and reg->btf, before accessing reg->btf */ > reg_name = kernel_type_name(reg->btf, reg->btf_id); > > - if (__check_ptr_off_reg(env, reg, regno, true)) > + if (ref_ptr) { > + if (!reg->ref_obj_id) { > + verbose(env, "R%d must be referenced %s%s\n", regno, > + reg_type_str(env, PTR_TO_BTF_ID), targ_name); > + return -EACCES; > + } The is_release_function() checkings under check_helper_call() is not the same? > + /* reg->off can be used to store pointer to a certain type formed by > + * incrementing pointer of a parent structure the object is embedded in, > + * e.g. map may expect unreferenced struct path *, and user should be > + * allowed a store using &file->f_path. However, in the case of > + * referenced pointer, we cannot do this, because the reference is only > + * for the parent structure, not its embedded object(s), and because > + * the transfer of ownership happens for the original pointer to and > + * from the map (before its eventual release). > + */ > + if (reg->off) > + fixed_off_ok = false; I thought the new check_func_arg_reg_off() is supposed to handle the is_release_function() case. The check_func_arg_reg_off() called in check_func_arg() can not handle this case? > + } > + /* var_off is rejected by __check_ptr_off_reg for PTR_TO_BTF_ID */ > + if (__check_ptr_off_reg(env, reg, regno, fixed_off_ok)) > return -EACCES; > > if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, [ ... ] > @@ -5390,6 +5473,7 @@ static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; > static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; > static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; > static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; > +static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } }; > > static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { > [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, > @@ -5417,11 +5501,13 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { > [ARG_PTR_TO_STACK] = &stack_ptr_types, > [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, > [ARG_PTR_TO_TIMER] = &timer_types, > + [ARG_PTR_TO_KPTR] = &kptr_types, > }; > > static int check_reg_type(struct bpf_verifier_env *env, u32 regno, > enum bpf_arg_type arg_type, > - const u32 *arg_btf_id) > + const u32 *arg_btf_id, > + struct bpf_call_arg_meta *meta) > { > struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; > enum bpf_reg_type expected, type = reg->type; > @@ -5474,8 +5560,11 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, > arg_btf_id = compatible->btf_id; > } > > - if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, > - btf_vmlinux, *arg_btf_id)) { > + if (meta->func_id == BPF_FUNC_kptr_xchg) { > + if (map_kptr_match_type(env, meta->kptr_off_desc, reg, regno, true)) > + return -EACCES; > + } else if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, > + btf_vmlinux, *arg_btf_id)) { > verbose(env, "R%d is of type %s but %s is expected\n", > regno, kernel_type_name(reg->btf, reg->btf_id), > kernel_type_name(btf_vmlinux, *arg_btf_id));