diff mbox series

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

Message ID 20230404045029.82870-5-alexei.starovoitov@gmail.com (mailing list archive)
State Accepted
Commit 91571a515d1bcdc280bb46423bb697ea7eb42ff3
Delegated to: BPF
Headers show
Series bpf: Follow up to RCU enforcement in the verifier. | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 46 this patch: 46
netdev/cc_maintainers warning 12 maintainers not CCed: pabeni@redhat.com song@kernel.org sdf@google.com haoluo@google.com yhs@fb.com kuba@kernel.org edumazet@google.com john.fastabend@gmail.com houtao1@huawei.com kpsingh@kernel.org jolsa@kernel.org martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 46 this patch: 46
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 85 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-PR fail merge-conflict
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_verifier on s390x with gcc

Commit Message

Alexei Starovoitov April 4, 2023, 4:50 a.m. UTC
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(-)

Comments

David Vernet April 4, 2023, 2:46 p.m. UTC | #1
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?

> diff --git a/net/core/filter.c b/net/core/filter.c
> index 1f2abf0f60e6..727c5269867d 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -4998,7 +4998,7 @@ const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
>  	.func		= bpf_get_socket_ptr_cookie,
>  	.gpl_only	= false,
>  	.ret_type	= RET_INTEGER,
> -	.arg1_type	= ARG_PTR_TO_BTF_ID_SOCK_COMMON,
> +	.arg1_type	= ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,
>  };
>  
>  BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
> -- 
> 2.34.1
>
Alexei Starovoitov April 4, 2023, 8:17 p.m. UTC | #2
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.
David Vernet April 4, 2023, 8:44 p.m. UTC | #3
On Tue, Apr 04, 2023 at 01:17:25PM -0700, Alexei Starovoitov wrote:
> 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.

Sounds good

> While this set is good to go imo.

Agreed, the series LGTM.
Martin KaFai Lau April 5, 2023, 12:10 a.m. UTC | #4
On 4/3/23 9:50 PM, Alexei Starovoitov wrote:
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 1f2abf0f60e6..727c5269867d 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -4998,7 +4998,7 @@ const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
>   	.func		= bpf_get_socket_ptr_cookie,
>   	.gpl_only	= false,
>   	.ret_type	= RET_INTEGER,
> -	.arg1_type	= ARG_PTR_TO_BTF_ID_SOCK_COMMON,
> +	.arg1_type	= ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,

I think the bpf_skc_to_* helpers (eg. bpf_skc_to_tcp_sock) also need similar 
change. They are available to tracing also. It can be a follow-up. The patch set 
lgtm.
Alexei Starovoitov April 5, 2023, 12:17 a.m. UTC | #5
On Tue, Apr 4, 2023 at 5:10 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>
> On 4/3/23 9:50 PM, Alexei Starovoitov wrote:
> > diff --git a/net/core/filter.c b/net/core/filter.c
> > index 1f2abf0f60e6..727c5269867d 100644
> > --- a/net/core/filter.c
> > +++ b/net/core/filter.c
> > @@ -4998,7 +4998,7 @@ const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
> >       .func           = bpf_get_socket_ptr_cookie,
> >       .gpl_only       = false,
> >       .ret_type       = RET_INTEGER,
> > -     .arg1_type      = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
> > +     .arg1_type      = ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,
>
> I think the bpf_skc_to_* helpers (eg. bpf_skc_to_tcp_sock) also need similar
> change. They are available to tracing also. It can be a follow-up. The patch set
> lgtm.

Ok. I'll take a look.
diff mbox series

Patch

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,
 };
diff --git a/net/core/filter.c b/net/core/filter.c
index 1f2abf0f60e6..727c5269867d 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4998,7 +4998,7 @@  const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
 	.func		= bpf_get_socket_ptr_cookie,
 	.gpl_only	= false,
 	.ret_type	= RET_INTEGER,
-	.arg1_type	= ARG_PTR_TO_BTF_ID_SOCK_COMMON,
+	.arg1_type	= ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,
 };
 
 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)