diff mbox series

[net-next] net: core: add SOL_SOCKET filter for bpf getsockopt hook

Message ID 20230510152216.1392682-1-aleksandr.mikhalitsyn@canonical.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [net-next] net: core: add SOL_SOCKET filter for bpf getsockopt hook | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-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: 2539 this patch: 2539
netdev/cc_maintainers warning 8 maintainers not CCed: yhs@fb.com kpsingh@kernel.org martin.lau@linux.dev john.fastabend@gmail.com song@kernel.org andrii@kernel.org jolsa@kernel.org haoluo@google.com
netdev/build_clang success Errors and warnings before: 543 this patch: 543
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: 2693 this patch: 2693
netdev/checkpatch warning CHECK: Unnecessary parentheses around 'level != SOL_SOCKET' WARNING: line length of 84 exceeds 80 columns
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-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-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
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-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-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-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-VM_Test-36 success Logs for veristat
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-17
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success 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-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-17
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-17
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-17
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_verifier 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-20 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on s390x with gcc

Commit Message

Aleksandr Mikhalitsyn May 10, 2023, 3:22 p.m. UTC
We have per struct proto ->bpf_bypass_getsockopt callback
to filter out bpf socket cgroup getsockopt hook from being called.

It seems worthwhile to add analogical helper for SOL_SOCKET
level socket options. First user will be SO_PEERPIDFD.

This patch was born as a result of discussion around a new SCM_PIDFD interface:
https://lore.kernel.org/all/20230413133355.350571-3-aleksandr.mikhalitsyn@canonical.com/

Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Stanislav Fomichev <sdf@google.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
Cc: linux-kernel@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: bpf@vger.kernel.org
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
---
 include/linux/bpf-cgroup.h | 8 +++++---
 include/net/sock.h         | 1 +
 net/core/sock.c            | 5 +++++
 3 files changed, 11 insertions(+), 3 deletions(-)

Comments

Kuniyuki Iwashima May 10, 2023, 3:38 p.m. UTC | #1
From: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Date: Wed, 10 May 2023 17:22:16 +0200
> We have per struct proto ->bpf_bypass_getsockopt callback
> to filter out bpf socket cgroup getsockopt hook from being called.
> 
> It seems worthwhile to add analogical helper for SOL_SOCKET
> level socket options. First user will be SO_PEERPIDFD.

I think this patch should be posted within the series below as
there is no real user of sock_bpf_bypass_getsockopt() for now.

Thanks,
Kuniyuki


> 
> This patch was born as a result of discussion around a new SCM_PIDFD interface:
> https://lore.kernel.org/all/20230413133355.350571-3-aleksandr.mikhalitsyn@canonical.com/
> 
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Stanislav Fomichev <sdf@google.com>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: Eric Dumazet <edumazet@google.com>
> Cc: Jakub Kicinski <kuba@kernel.org>
> Cc: Paolo Abeni <pabeni@redhat.com>
> Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> Cc: linux-kernel@vger.kernel.org
> Cc: netdev@vger.kernel.org
> Cc: bpf@vger.kernel.org
> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> ---
>  include/linux/bpf-cgroup.h | 8 +++++---
>  include/net/sock.h         | 1 +
>  net/core/sock.c            | 5 +++++
>  3 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> index 57e9e109257e..97d8a49b35bf 100644
> --- a/include/linux/bpf-cgroup.h
> +++ b/include/linux/bpf-cgroup.h
> @@ -387,10 +387,12 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
>  	int __ret = retval;						       \
>  	if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&			       \
>  	    cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))		       \
> -		if (!(sock)->sk_prot->bpf_bypass_getsockopt ||		       \
> -		    !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> +		if (((level != SOL_SOCKET) ||				       \
> +		     !sock_bpf_bypass_getsockopt(level, optname)) &&	       \
> +		    (!(sock)->sk_prot->bpf_bypass_getsockopt ||		       \
> +		     !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
>  					tcp_bpf_bypass_getsockopt,	       \
> -					level, optname))		       \
> +					level, optname)))		       \
>  			__ret = __cgroup_bpf_run_filter_getsockopt(	       \
>  				sock, level, optname, optval, optlen,	       \
>  				max_optlen, retval);			       \
> diff --git a/include/net/sock.h b/include/net/sock.h
> index 8b7ed7167243..530d6d22f42d 100644
> --- a/include/net/sock.h
> +++ b/include/net/sock.h
> @@ -1847,6 +1847,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
>  		  sockptr_t optval, sockptr_t optlen);
>  int sock_getsockopt(struct socket *sock, int level, int op,
>  		    char __user *optval, int __user *optlen);
> +bool sock_bpf_bypass_getsockopt(int level, int optname);
>  int sock_gettstamp(struct socket *sock, void __user *userstamp,
>  		   bool timeval, bool time32);
>  struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 5440e67bcfe3..194a423eb6e5 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -1963,6 +1963,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
>  			     USER_SOCKPTR(optlen));
>  }
>  
> +bool sock_bpf_bypass_getsockopt(int level, int optname)
> +{
> +	return false;
> +}
> +
>  /*
>   * Initialize an sk_lock.
>   *
> -- 
> 2.34.1
Stanislav Fomichev May 10, 2023, 9:31 p.m. UTC | #2
On Wed, May 10, 2023 at 8:23 AM Alexander Mikhalitsyn
<aleksandr.mikhalitsyn@canonical.com> wrote:
>
> We have per struct proto ->bpf_bypass_getsockopt callback
> to filter out bpf socket cgroup getsockopt hook from being called.
>
> It seems worthwhile to add analogical helper for SOL_SOCKET
> level socket options. First user will be SO_PEERPIDFD.
>
> This patch was born as a result of discussion around a new SCM_PIDFD interface:
> https://lore.kernel.org/all/20230413133355.350571-3-aleksandr.mikhalitsyn@canonical.com/
>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Stanislav Fomichev <sdf@google.com>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: Eric Dumazet <edumazet@google.com>
> Cc: Jakub Kicinski <kuba@kernel.org>
> Cc: Paolo Abeni <pabeni@redhat.com>
> Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> Cc: linux-kernel@vger.kernel.org
> Cc: netdev@vger.kernel.org
> Cc: bpf@vger.kernel.org
> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> ---
>  include/linux/bpf-cgroup.h | 8 +++++---
>  include/net/sock.h         | 1 +
>  net/core/sock.c            | 5 +++++
>  3 files changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> index 57e9e109257e..97d8a49b35bf 100644
> --- a/include/linux/bpf-cgroup.h
> +++ b/include/linux/bpf-cgroup.h
> @@ -387,10 +387,12 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
>         int __ret = retval;                                                    \
>         if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&                           \
>             cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))                  \
> -               if (!(sock)->sk_prot->bpf_bypass_getsockopt ||                 \
> -                   !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> +               if (((level != SOL_SOCKET) ||                                  \
> +                    !sock_bpf_bypass_getsockopt(level, optname)) &&           \
> +                   (!(sock)->sk_prot->bpf_bypass_getsockopt ||                \

Any reason we are not putting this into bpf_bypass_getsockopt for
af_unix struct proto? SO_PEERPIDFD seems relevant only for af_unix?

> +                    !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
>                                         tcp_bpf_bypass_getsockopt,             \
> -                                       level, optname))                       \
> +                                       level, optname)))                      \
>                         __ret = __cgroup_bpf_run_filter_getsockopt(            \
>                                 sock, level, optname, optval, optlen,          \
>                                 max_optlen, retval);                           \
> diff --git a/include/net/sock.h b/include/net/sock.h
> index 8b7ed7167243..530d6d22f42d 100644
> --- a/include/net/sock.h
> +++ b/include/net/sock.h
> @@ -1847,6 +1847,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
>                   sockptr_t optval, sockptr_t optlen);
>  int sock_getsockopt(struct socket *sock, int level, int op,
>                     char __user *optval, int __user *optlen);
> +bool sock_bpf_bypass_getsockopt(int level, int optname);
>  int sock_gettstamp(struct socket *sock, void __user *userstamp,
>                    bool timeval, bool time32);
>  struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 5440e67bcfe3..194a423eb6e5 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -1963,6 +1963,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
>                              USER_SOCKPTR(optlen));
>  }
>
> +bool sock_bpf_bypass_getsockopt(int level, int optname)
> +{
> +       return false;
> +}
> +
>  /*
>   * Initialize an sk_lock.
>   *
> --
> 2.34.1
>
Aleksandr Mikhalitsyn May 10, 2023, 9:40 p.m. UTC | #3
On Wed, May 10, 2023 at 11:31 PM Stanislav Fomichev <sdf@google.com> wrote:
>
> On Wed, May 10, 2023 at 8:23 AM Alexander Mikhalitsyn
> <aleksandr.mikhalitsyn@canonical.com> wrote:
> >
> > We have per struct proto ->bpf_bypass_getsockopt callback
> > to filter out bpf socket cgroup getsockopt hook from being called.
> >
> > It seems worthwhile to add analogical helper for SOL_SOCKET
> > level socket options. First user will be SO_PEERPIDFD.
> >
> > This patch was born as a result of discussion around a new SCM_PIDFD interface:
> > https://lore.kernel.org/all/20230413133355.350571-3-aleksandr.mikhalitsyn@canonical.com/
> >
> > Cc: Alexei Starovoitov <ast@kernel.org>
> > Cc: Daniel Borkmann <daniel@iogearbox.net>
> > Cc: Christian Brauner <brauner@kernel.org>
> > Cc: Stanislav Fomichev <sdf@google.com>
> > Cc: "David S. Miller" <davem@davemloft.net>
> > Cc: Eric Dumazet <edumazet@google.com>
> > Cc: Jakub Kicinski <kuba@kernel.org>
> > Cc: Paolo Abeni <pabeni@redhat.com>
> > Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> > Cc: linux-kernel@vger.kernel.org
> > Cc: netdev@vger.kernel.org
> > Cc: bpf@vger.kernel.org
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  include/linux/bpf-cgroup.h | 8 +++++---
> >  include/net/sock.h         | 1 +
> >  net/core/sock.c            | 5 +++++
> >  3 files changed, 11 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> > index 57e9e109257e..97d8a49b35bf 100644
> > --- a/include/linux/bpf-cgroup.h
> > +++ b/include/linux/bpf-cgroup.h
> > @@ -387,10 +387,12 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
> >         int __ret = retval;                                                    \
> >         if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&                           \
> >             cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))                  \
> > -               if (!(sock)->sk_prot->bpf_bypass_getsockopt ||                 \
> > -                   !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> > +               if (((level != SOL_SOCKET) ||                                  \
> > +                    !sock_bpf_bypass_getsockopt(level, optname)) &&           \
> > +                   (!(sock)->sk_prot->bpf_bypass_getsockopt ||                \
>
> Any reason we are not putting this into bpf_bypass_getsockopt for
> af_unix struct proto? SO_PEERPIDFD seems relevant only for af_unix?

Yes, that should work perfectly well. The reason why I'm going this
way is that we are
declaring all SOL_SOCKET-level options in the net/core/sock.c which is
not specific to any address family.
It seems reasonable to have a way to filter out getsockopt for these
options too.

But I'm not insisting on that way.

Kind regards,
Alex

>
> > +                    !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> >                                         tcp_bpf_bypass_getsockopt,             \
> > -                                       level, optname))                       \
> > +                                       level, optname)))                      \
> >                         __ret = __cgroup_bpf_run_filter_getsockopt(            \
> >                                 sock, level, optname, optval, optlen,          \
> >                                 max_optlen, retval);                           \
> > diff --git a/include/net/sock.h b/include/net/sock.h
> > index 8b7ed7167243..530d6d22f42d 100644
> > --- a/include/net/sock.h
> > +++ b/include/net/sock.h
> > @@ -1847,6 +1847,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
> >                   sockptr_t optval, sockptr_t optlen);
> >  int sock_getsockopt(struct socket *sock, int level, int op,
> >                     char __user *optval, int __user *optlen);
> > +bool sock_bpf_bypass_getsockopt(int level, int optname);
> >  int sock_gettstamp(struct socket *sock, void __user *userstamp,
> >                    bool timeval, bool time32);
> >  struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
> > diff --git a/net/core/sock.c b/net/core/sock.c
> > index 5440e67bcfe3..194a423eb6e5 100644
> > --- a/net/core/sock.c
> > +++ b/net/core/sock.c
> > @@ -1963,6 +1963,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
> >                              USER_SOCKPTR(optlen));
> >  }
> >
> > +bool sock_bpf_bypass_getsockopt(int level, int optname)
> > +{
> > +       return false;
> > +}
> > +
> >  /*
> >   * Initialize an sk_lock.
> >   *
> > --
> > 2.34.1
> >
Stanislav Fomichev May 10, 2023, 9:58 p.m. UTC | #4
On Wed, May 10, 2023 at 2:41 PM Aleksandr Mikhalitsyn
<aleksandr.mikhalitsyn@canonical.com> wrote:
>
> On Wed, May 10, 2023 at 11:31 PM Stanislav Fomichev <sdf@google.com> wrote:
> >
> > On Wed, May 10, 2023 at 8:23 AM Alexander Mikhalitsyn
> > <aleksandr.mikhalitsyn@canonical.com> wrote:
> > >
> > > We have per struct proto ->bpf_bypass_getsockopt callback
> > > to filter out bpf socket cgroup getsockopt hook from being called.
> > >
> > > It seems worthwhile to add analogical helper for SOL_SOCKET
> > > level socket options. First user will be SO_PEERPIDFD.
> > >
> > > This patch was born as a result of discussion around a new SCM_PIDFD interface:
> > > https://lore.kernel.org/all/20230413133355.350571-3-aleksandr.mikhalitsyn@canonical.com/
> > >
> > > Cc: Alexei Starovoitov <ast@kernel.org>
> > > Cc: Daniel Borkmann <daniel@iogearbox.net>
> > > Cc: Christian Brauner <brauner@kernel.org>
> > > Cc: Stanislav Fomichev <sdf@google.com>
> > > Cc: "David S. Miller" <davem@davemloft.net>
> > > Cc: Eric Dumazet <edumazet@google.com>
> > > Cc: Jakub Kicinski <kuba@kernel.org>
> > > Cc: Paolo Abeni <pabeni@redhat.com>
> > > Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> > > Cc: linux-kernel@vger.kernel.org
> > > Cc: netdev@vger.kernel.org
> > > Cc: bpf@vger.kernel.org
> > > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > > ---
> > >  include/linux/bpf-cgroup.h | 8 +++++---
> > >  include/net/sock.h         | 1 +
> > >  net/core/sock.c            | 5 +++++
> > >  3 files changed, 11 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> > > index 57e9e109257e..97d8a49b35bf 100644
> > > --- a/include/linux/bpf-cgroup.h
> > > +++ b/include/linux/bpf-cgroup.h
> > > @@ -387,10 +387,12 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
> > >         int __ret = retval;                                                    \
> > >         if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&                           \
> > >             cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))                  \
> > > -               if (!(sock)->sk_prot->bpf_bypass_getsockopt ||                 \
> > > -                   !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> > > +               if (((level != SOL_SOCKET) ||                                  \
> > > +                    !sock_bpf_bypass_getsockopt(level, optname)) &&           \
> > > +                   (!(sock)->sk_prot->bpf_bypass_getsockopt ||                \
> >
> > Any reason we are not putting this into bpf_bypass_getsockopt for
> > af_unix struct proto? SO_PEERPIDFD seems relevant only for af_unix?
>
> Yes, that should work perfectly well. The reason why I'm going this
> way is that we are
> declaring all SOL_SOCKET-level options in the net/core/sock.c which is
> not specific to any address family.
> It seems reasonable to have a way to filter out getsockopt for these
> options too.
>
> But I'm not insisting on that way.

Yeah, let's move it into af_unix struct proto for now. That should
avoid adding extra conditionals for a few places that care about
performance (tcp zerocopy fastpath).
If we'd ever need to filter out generic SOL_SOCKET level options that
apply for all sockets, we might put (and copy-paste) them in the
respective {tcp,udp,unix,etc}_bpf_bypass_getsockopt.

> Kind regards,
> Alex
>
> >
> > > +                    !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> > >                                         tcp_bpf_bypass_getsockopt,             \
> > > -                                       level, optname))                       \
> > > +                                       level, optname)))                      \
> > >                         __ret = __cgroup_bpf_run_filter_getsockopt(            \
> > >                                 sock, level, optname, optval, optlen,          \
> > >                                 max_optlen, retval);                           \
> > > diff --git a/include/net/sock.h b/include/net/sock.h
> > > index 8b7ed7167243..530d6d22f42d 100644
> > > --- a/include/net/sock.h
> > > +++ b/include/net/sock.h
> > > @@ -1847,6 +1847,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
> > >                   sockptr_t optval, sockptr_t optlen);
> > >  int sock_getsockopt(struct socket *sock, int level, int op,
> > >                     char __user *optval, int __user *optlen);
> > > +bool sock_bpf_bypass_getsockopt(int level, int optname);
> > >  int sock_gettstamp(struct socket *sock, void __user *userstamp,
> > >                    bool timeval, bool time32);
> > >  struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
> > > diff --git a/net/core/sock.c b/net/core/sock.c
> > > index 5440e67bcfe3..194a423eb6e5 100644
> > > --- a/net/core/sock.c
> > > +++ b/net/core/sock.c
> > > @@ -1963,6 +1963,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
> > >                              USER_SOCKPTR(optlen));
> > >  }
> > >
> > > +bool sock_bpf_bypass_getsockopt(int level, int optname)
> > > +{
> > > +       return false;
> > > +}
> > > +
> > >  /*
> > >   * Initialize an sk_lock.
> > >   *
> > > --
> > > 2.34.1
> > >
Aleksandr Mikhalitsyn May 11, 2023, 6:36 a.m. UTC | #5
On Wed, May 10, 2023 at 11:58 PM Stanislav Fomichev <sdf@google.com> wrote:
>
> On Wed, May 10, 2023 at 2:41 PM Aleksandr Mikhalitsyn
> <aleksandr.mikhalitsyn@canonical.com> wrote:
> >
> > On Wed, May 10, 2023 at 11:31 PM Stanislav Fomichev <sdf@google.com> wrote:
> > >
> > > On Wed, May 10, 2023 at 8:23 AM Alexander Mikhalitsyn
> > > <aleksandr.mikhalitsyn@canonical.com> wrote:
> > > >
> > > > We have per struct proto ->bpf_bypass_getsockopt callback
> > > > to filter out bpf socket cgroup getsockopt hook from being called.
> > > >
> > > > It seems worthwhile to add analogical helper for SOL_SOCKET
> > > > level socket options. First user will be SO_PEERPIDFD.
> > > >
> > > > This patch was born as a result of discussion around a new SCM_PIDFD interface:
> > > > https://lore.kernel.org/all/20230413133355.350571-3-aleksandr.mikhalitsyn@canonical.com/
> > > >
> > > > Cc: Alexei Starovoitov <ast@kernel.org>
> > > > Cc: Daniel Borkmann <daniel@iogearbox.net>
> > > > Cc: Christian Brauner <brauner@kernel.org>
> > > > Cc: Stanislav Fomichev <sdf@google.com>
> > > > Cc: "David S. Miller" <davem@davemloft.net>
> > > > Cc: Eric Dumazet <edumazet@google.com>
> > > > Cc: Jakub Kicinski <kuba@kernel.org>
> > > > Cc: Paolo Abeni <pabeni@redhat.com>
> > > > Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> > > > Cc: linux-kernel@vger.kernel.org
> > > > Cc: netdev@vger.kernel.org
> > > > Cc: bpf@vger.kernel.org
> > > > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > > > ---
> > > >  include/linux/bpf-cgroup.h | 8 +++++---
> > > >  include/net/sock.h         | 1 +
> > > >  net/core/sock.c            | 5 +++++
> > > >  3 files changed, 11 insertions(+), 3 deletions(-)
> > > >
> > > > diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> > > > index 57e9e109257e..97d8a49b35bf 100644
> > > > --- a/include/linux/bpf-cgroup.h
> > > > +++ b/include/linux/bpf-cgroup.h
> > > > @@ -387,10 +387,12 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
> > > >         int __ret = retval;                                                    \
> > > >         if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&                           \
> > > >             cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))                  \
> > > > -               if (!(sock)->sk_prot->bpf_bypass_getsockopt ||                 \
> > > > -                   !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> > > > +               if (((level != SOL_SOCKET) ||                                  \
> > > > +                    !sock_bpf_bypass_getsockopt(level, optname)) &&           \
> > > > +                   (!(sock)->sk_prot->bpf_bypass_getsockopt ||                \
> > >
> > > Any reason we are not putting this into bpf_bypass_getsockopt for
> > > af_unix struct proto? SO_PEERPIDFD seems relevant only for af_unix?
> >
> > Yes, that should work perfectly well. The reason why I'm going this
> > way is that we are
> > declaring all SOL_SOCKET-level options in the net/core/sock.c which is
> > not specific to any address family.
> > It seems reasonable to have a way to filter out getsockopt for these
> > options too.
> >
> > But I'm not insisting on that way.
>
> Yeah, let's move it into af_unix struct proto for now. That should
> avoid adding extra conditionals for a few places that care about
> performance (tcp zerocopy fastpath).
> If we'd ever need to filter out generic SOL_SOCKET level options that
> apply for all sockets, we might put (and copy-paste) them in the
> respective {tcp,udp,unix,etc}_bpf_bypass_getsockopt.

Will do.

Thanks!

Kind regards,
Alex

>
> > Kind regards,
> > Alex
> >
> > >
> > > > +                    !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
> > > >                                         tcp_bpf_bypass_getsockopt,             \
> > > > -                                       level, optname))                       \
> > > > +                                       level, optname)))                      \
> > > >                         __ret = __cgroup_bpf_run_filter_getsockopt(            \
> > > >                                 sock, level, optname, optval, optlen,          \
> > > >                                 max_optlen, retval);                           \
> > > > diff --git a/include/net/sock.h b/include/net/sock.h
> > > > index 8b7ed7167243..530d6d22f42d 100644
> > > > --- a/include/net/sock.h
> > > > +++ b/include/net/sock.h
> > > > @@ -1847,6 +1847,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
> > > >                   sockptr_t optval, sockptr_t optlen);
> > > >  int sock_getsockopt(struct socket *sock, int level, int op,
> > > >                     char __user *optval, int __user *optlen);
> > > > +bool sock_bpf_bypass_getsockopt(int level, int optname);
> > > >  int sock_gettstamp(struct socket *sock, void __user *userstamp,
> > > >                    bool timeval, bool time32);
> > > >  struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
> > > > diff --git a/net/core/sock.c b/net/core/sock.c
> > > > index 5440e67bcfe3..194a423eb6e5 100644
> > > > --- a/net/core/sock.c
> > > > +++ b/net/core/sock.c
> > > > @@ -1963,6 +1963,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
> > > >                              USER_SOCKPTR(optlen));
> > > >  }
> > > >
> > > > +bool sock_bpf_bypass_getsockopt(int level, int optname)
> > > > +{
> > > > +       return false;
> > > > +}
> > > > +
> > > >  /*
> > > >   * Initialize an sk_lock.
> > > >   *
> > > > --
> > > > 2.34.1
> > > >
diff mbox series

Patch

diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
index 57e9e109257e..97d8a49b35bf 100644
--- a/include/linux/bpf-cgroup.h
+++ b/include/linux/bpf-cgroup.h
@@ -387,10 +387,12 @@  static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
 	int __ret = retval;						       \
 	if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&			       \
 	    cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))		       \
-		if (!(sock)->sk_prot->bpf_bypass_getsockopt ||		       \
-		    !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
+		if (((level != SOL_SOCKET) ||				       \
+		     !sock_bpf_bypass_getsockopt(level, optname)) &&	       \
+		    (!(sock)->sk_prot->bpf_bypass_getsockopt ||		       \
+		     !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
 					tcp_bpf_bypass_getsockopt,	       \
-					level, optname))		       \
+					level, optname)))		       \
 			__ret = __cgroup_bpf_run_filter_getsockopt(	       \
 				sock, level, optname, optval, optlen,	       \
 				max_optlen, retval);			       \
diff --git a/include/net/sock.h b/include/net/sock.h
index 8b7ed7167243..530d6d22f42d 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1847,6 +1847,7 @@  int sk_getsockopt(struct sock *sk, int level, int optname,
 		  sockptr_t optval, sockptr_t optlen);
 int sock_getsockopt(struct socket *sock, int level, int op,
 		    char __user *optval, int __user *optlen);
+bool sock_bpf_bypass_getsockopt(int level, int optname);
 int sock_gettstamp(struct socket *sock, void __user *userstamp,
 		   bool timeval, bool time32);
 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
diff --git a/net/core/sock.c b/net/core/sock.c
index 5440e67bcfe3..194a423eb6e5 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -1963,6 +1963,11 @@  int sock_getsockopt(struct socket *sock, int level, int optname,
 			     USER_SOCKPTR(optlen));
 }
 
+bool sock_bpf_bypass_getsockopt(int level, int optname)
+{
+	return false;
+}
+
 /*
  * Initialize an sk_lock.
  *