Re: [PATCH bpf-next 4/8] bpf: Teach verifier that certain helpers accept NULL pointer.

Alexei Starovoitov
2023-04-04T20:17:43Z

On Tue, Apr 4, 2023 at 7:46 AM David Vernet <void@manifault.com> wrote:
>
> On Mon, Apr 03, 2023 at 09:50:25PM -0700, Alexei Starovoitov wrote:
> > From: Alexei Starovoitov <ast@kernel.org>
> >
> > bpf_[sk|inode|task|cgrp]_storage_[get|delete]() and bpf_get_socket_cookie() helpers
> > perform run-time check that sk|inode|task|cgrp pointer != NULL.
> > Teach verifier about this fact and allow bpf programs to pass
> > PTR_TO_BTF_ID | PTR_MAYBE_NULL into such helpers.
> > It will be used in the subsequent patch that will do
> > bpf_sk_storage_get(.., skb->sk, ...);
> > Even when 'skb' pointer is trusted the 'sk' pointer may be NULL.
> >
> > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> > ---
> > kernel/bpf/bpf_cgrp_storage.c | 4 ++--
> > kernel/bpf/bpf_inode_storage.c | 4 ++--
> > kernel/bpf/bpf_task_storage.c | 8 ++++----
> > net/core/bpf_sk_storage.c | 4 ++--
> > net/core/filter.c | 2 +-
> > 5 files changed, 11 insertions(+), 11 deletions(-)
> >
> > diff --git a/kernel/bpf/bpf_cgrp_storage.c b/kernel/bpf/bpf_cgrp_storage.c
> > index d17d5b694668..d44fe8dd9732 100644
> > --- a/kernel/bpf/bpf_cgrp_storage.c
> > +++ b/kernel/bpf/bpf_cgrp_storage.c
> > @@ -224,7 +224,7 @@ const struct bpf_func_proto bpf_cgrp_storage_get_proto = {
> > .gpl_only = false,
> > .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &bpf_cgroup_btf_id[0],
> > .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg4_type = ARG_ANYTHING,
> > @@ -235,6 +235,6 @@ const struct bpf_func_proto bpf_cgrp_storage_delete_proto = {
> > .gpl_only = false,
> > .ret_type = RET_INTEGER,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &bpf_cgroup_btf_id[0],
> > };
> > diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c
> > index e17ad581b9be..a4d93df78c75 100644
> > --- a/kernel/bpf/bpf_inode_storage.c
> > +++ b/kernel/bpf/bpf_inode_storage.c
> > @@ -229,7 +229,7 @@ const struct bpf_func_proto bpf_inode_storage_get_proto = {
> > .gpl_only = false,
> > .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
> > .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg4_type = ARG_ANYTHING,
> > @@ -240,6 +240,6 @@ const struct bpf_func_proto bpf_inode_storage_delete_proto = {
> > .gpl_only = false,
> > .ret_type = RET_INTEGER,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
> > };
> > diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c
> > index d1af0c8f9ce4..adf6dfe0ba68 100644
> > --- a/kernel/bpf/bpf_task_storage.c
> > +++ b/kernel/bpf/bpf_task_storage.c
> > @@ -338,7 +338,7 @@ const struct bpf_func_proto bpf_task_storage_get_recur_proto = {
> > .gpl_only = false,
> > .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
> > .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg4_type = ARG_ANYTHING,
> > @@ -349,7 +349,7 @@ const struct bpf_func_proto bpf_task_storage_get_proto = {
> > .gpl_only = false,
> > .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
> > .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg4_type = ARG_ANYTHING,
> > @@ -360,7 +360,7 @@ const struct bpf_func_proto bpf_task_storage_delete_recur_proto = {
> > .gpl_only = false,
> > .ret_type = RET_INTEGER,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
> > };
> >
> > @@ -369,6 +369,6 @@ const struct bpf_func_proto bpf_task_storage_delete_proto = {
> > .gpl_only = false,
> > .ret_type = RET_INTEGER,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
> > };
> > diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
> > index 085025c7130a..d4172534dfa8 100644
> > --- a/net/core/bpf_sk_storage.c
> > +++ b/net/core/bpf_sk_storage.c
> > @@ -412,7 +412,7 @@ const struct bpf_func_proto bpf_sk_storage_get_tracing_proto = {
> > .gpl_only = false,
> > .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON],
> > .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
> > .arg4_type = ARG_ANYTHING,
> > @@ -424,7 +424,7 @@ const struct bpf_func_proto bpf_sk_storage_delete_tracing_proto = {
> > .gpl_only = false,
> > .ret_type = RET_INTEGER,
> > .arg1_type = ARG_CONST_MAP_PTR,
> > - .arg2_type = ARG_PTR_TO_BTF_ID,
> > + .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
> > .arg2_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON],
> > .allowed = bpf_sk_storage_tracing_allowed,
> > };
>
> Should we also add PTR_MAYBE_NULL to the ARG_PTR_TO_BTF_ID_SOCK_COMMON
> arg in bpf_sk_storage_get_proto and bpf_sk_storage_delete_proto?

It makes sense. I'd like to do it in the follow up though.
I haven't seen networking progs passing null-able pointer into these helpers.
Only tracing progs do.
I need to craft a test case, etc.
While this set is good to go imo.