Re: [PATCH bpf-next] bpf: use type_may_be_null() helper for nullable-param check
From: Shung-Hsi Yu
Date: Thu Sep 05 2024 - 22:11:08 EST
On Thu, Sep 05, 2024 at 08:00:09AM GMT, Matt Bobrowski wrote:
> On Thu, Sep 05, 2024 at 01:52:32PM +0800, Shung-Hsi Yu wrote:
[...]
> > --- a/net/bpf/bpf_dummy_struct_ops.c
> > +++ b/net/bpf/bpf_dummy_struct_ops.c
> > @@ -115,7 +115,7 @@ static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_
> >
> > offset = btf_ctx_arg_offset(bpf_dummy_ops_btf, func_proto, arg_no);
> > info = find_ctx_arg_info(prog->aux, offset);
> > - if (info && (info->reg_type & PTR_MAYBE_NULL))
> > + if (info && type_may_be_null(info->reg_type))
>
> Maybe as part of this clean up, we should also consider replacing all
> the open-coded & PTR_MAYBE_NULL checks with type_may_be_null() which
> we have sprinkled throughout kernel/bpf/verifier.c?
Agree we should. Usage like this could be replaced
if (ptr_reg->type & PTR_MAYBE_NULL) {
verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
dst, reg_type_str(env, ptr_reg->type));
return -EACCES;
}
OTOH replacing & PTR_MAYBE_NULL here probably won't help improve
clarity.
if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
reg->type = PTR_TO_BTF_ID;
if (arg->arg_type & PTR_MAYBE_NULL)
reg->type |= PTR_MAYBE_NULL;
if (arg->arg_type & PTR_UNTRUSTED)
reg->type |= PTR_UNTRUSTED;
if (arg->arg_type & PTR_TRUSTED)
reg->type |= PTR_TRUSTED;
...
For such case we might need to introduce another helper (bitwise-OR
between enum bpf_type_flag should be free of compiler warning).
reg->type = type_flag_apply(PTR_TO_BTF_ID, arg->arg_type,
PTR_MAYBE_NULL | PTR_UNTRUSTED | PTR_TRUSTED);
WDYT?