diff mbox series

[net-next,v2] sctp: add bpf_bypass_getsockopt proto callback

Message ID 20230511123148.332043-1-aleksandr.mikhalitsyn@canonical.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next,v2] sctp: add bpf_bypass_getsockopt proto callback | 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
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: 9 this patch: 9
netdev/cc_maintainers fail 1 blamed authors not CCed: ast@kernel.org; 5 maintainers not CCed: kuba@kernel.org bpf@vger.kernel.org pabeni@redhat.com ast@kernel.org edumazet@google.com
netdev/build_clang success Errors and warnings before: 8 this patch: 8
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 Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 82 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 87 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns WARNING: line length of 92 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns WARNING: networking block comments don't use an empty /* line, use /* Comment...
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Alexander Mikhalitsyn May 11, 2023, 12:31 p.m. UTC
Add bpf_bypass_getsockopt proto callback and filter out
SCTP_SOCKOPT_PEELOFF and SCTP_SOCKOPT_PEELOFF_FLAGS socket options
from running eBPF hook on them.

These options do fd_install(), and if BPF_CGROUP_RUN_PROG_GETSOCKOPT
hook returns an error after success of the original handler
sctp_getsockopt(...), userspace will receive an error from getsockopt
syscall and will be not aware that fd was successfully installed into fdtable.

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/

Fixes: 0d01da6afc54 ("bpf: implement getsockopt and setsockopt hooks")
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Stanislav Fomichev <sdf@google.com>
Cc: Neil Horman <nhorman@tuxdriver.com>
Cc: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Cc: Xin Long <lucien.xin@gmail.com>
Cc: linux-sctp@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: netdev@vger.kernel.org
Suggested-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
---
 net/sctp/socket.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

Comments

Marcelo Ricardo Leitner May 11, 2023, 1:12 p.m. UTC | #1
Hi,

Two things:

On Thu, May 11, 2023 at 02:31:48PM +0200, Alexander Mikhalitsyn wrote:
> Add bpf_bypass_getsockopt proto callback and filter out
> SCTP_SOCKOPT_PEELOFF and SCTP_SOCKOPT_PEELOFF_FLAGS socket options
> from running eBPF hook on them.
> 
> These options do fd_install(), and if BPF_CGROUP_RUN_PROG_GETSOCKOPT
> hook returns an error after success of the original handler
> sctp_getsockopt(...), userspace will receive an error from getsockopt
> syscall and will be not aware that fd was successfully installed into fdtable.
> 
> 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/

Cool, but the description is mentioning the CONNECTX3 sockopt.

> 
> Fixes: 0d01da6afc54 ("bpf: implement getsockopt and setsockopt hooks")
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Stanislav Fomichev <sdf@google.com>
> Cc: Neil Horman <nhorman@tuxdriver.com>
> Cc: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
> Cc: Xin Long <lucien.xin@gmail.com>
> Cc: linux-sctp@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Cc: netdev@vger.kernel.org
> Suggested-by: Stanislav Fomichev <sdf@google.com>
> Acked-by: Stanislav Fomichev <sdf@google.com>
> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> ---
>  net/sctp/socket.c | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git a/net/sctp/socket.c b/net/sctp/socket.c
> index cda8c2874691..a211a203003c 100644
> --- a/net/sctp/socket.c
> +++ b/net/sctp/socket.c
> @@ -8281,6 +8281,35 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname,
>  	return retval;
>  }
>  
> +static bool sctp_bpf_bypass_getsockopt(int level, int optname)
> +{
> +	if (level == SOL_SCTP) {
> +		switch (optname) {
> +		/*
> +		 * These options do fd_install(), and if BPF_CGROUP_RUN_PROG_GETSOCKOPT
> +		 * hook returns an error after success of the original handler
> +		 * sctp_getsockopt(...), userspace will receive an error from getsockopt
> +		 * syscall and will be not aware that fd was successfully installed into fdtable.
> +		 *
> +		 * Let's prevent bpf cgroup hook from running on them.
> +		 */

This and..

> +		case SCTP_SOCKOPT_PEELOFF:
> +		case SCTP_SOCKOPT_PEELOFF_FLAGS:
> +		/*
> +		 * As pointed by Marcelo Ricardo Leitner it seems reasonable to skip
> +		 * bpf getsockopt hook for this sockopt too. Because internaly, it
> +		 * triggers connect() and if error will be masked userspace can be confused.
> +		 */

..this comments can be removed, as they are easily visible on the
description later on for who is interested on why such lines were
added.

Thanks,
Marcelo

> +		case SCTP_SOCKOPT_CONNECTX3:
> +			return true;
> +		default:
> +			return false;
> +		}
> +	}
> +
> +	return false;
> +}
> +
>  static int sctp_hash(struct sock *sk)
>  {
>  	/* STUB */
> @@ -9650,6 +9679,7 @@ struct proto sctp_prot = {
>  	.shutdown    =	sctp_shutdown,
>  	.setsockopt  =	sctp_setsockopt,
>  	.getsockopt  =	sctp_getsockopt,
> +	.bpf_bypass_getsockopt	= sctp_bpf_bypass_getsockopt,
>  	.sendmsg     =	sctp_sendmsg,
>  	.recvmsg     =	sctp_recvmsg,
>  	.bind        =	sctp_bind,
> @@ -9705,6 +9735,7 @@ struct proto sctpv6_prot = {
>  	.shutdown	= sctp_shutdown,
>  	.setsockopt	= sctp_setsockopt,
>  	.getsockopt	= sctp_getsockopt,
> +	.bpf_bypass_getsockopt	= sctp_bpf_bypass_getsockopt,
>  	.sendmsg	= sctp_sendmsg,
>  	.recvmsg	= sctp_recvmsg,
>  	.bind		= sctp_bind,
> -- 
> 2.34.1
>
Alexander Mikhalitsyn May 11, 2023, 1:26 p.m. UTC | #2
On Thu, May 11, 2023 at 3:12 PM Marcelo Ricardo Leitner
<marcelo.leitner@gmail.com> wrote:
>
> Hi,

Hi Marcelo,

thanks! Fixed in -v3.

Kind regards,
Alex

>
> Two things:
>
> On Thu, May 11, 2023 at 02:31:48PM +0200, Alexander Mikhalitsyn wrote:
> > Add bpf_bypass_getsockopt proto callback and filter out
> > SCTP_SOCKOPT_PEELOFF and SCTP_SOCKOPT_PEELOFF_FLAGS socket options
> > from running eBPF hook on them.
> >
> > These options do fd_install(), and if BPF_CGROUP_RUN_PROG_GETSOCKOPT
> > hook returns an error after success of the original handler
> > sctp_getsockopt(...), userspace will receive an error from getsockopt
> > syscall and will be not aware that fd was successfully installed into fdtable.
> >
> > 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/
>
> Cool, but the description is mentioning the CONNECTX3 sockopt.
>
> >
> > Fixes: 0d01da6afc54 ("bpf: implement getsockopt and setsockopt hooks")
> > Cc: Daniel Borkmann <daniel@iogearbox.net>
> > Cc: Christian Brauner <brauner@kernel.org>
> > Cc: Stanislav Fomichev <sdf@google.com>
> > Cc: Neil Horman <nhorman@tuxdriver.com>
> > Cc: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
> > Cc: Xin Long <lucien.xin@gmail.com>
> > Cc: linux-sctp@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > Cc: netdev@vger.kernel.org
> > Suggested-by: Stanislav Fomichev <sdf@google.com>
> > Acked-by: Stanislav Fomichev <sdf@google.com>
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  net/sctp/socket.c | 31 +++++++++++++++++++++++++++++++
> >  1 file changed, 31 insertions(+)
> >
> > diff --git a/net/sctp/socket.c b/net/sctp/socket.c
> > index cda8c2874691..a211a203003c 100644
> > --- a/net/sctp/socket.c
> > +++ b/net/sctp/socket.c
> > @@ -8281,6 +8281,35 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname,
> >       return retval;
> >  }
> >
> > +static bool sctp_bpf_bypass_getsockopt(int level, int optname)
> > +{
> > +     if (level == SOL_SCTP) {
> > +             switch (optname) {
> > +             /*
> > +              * These options do fd_install(), and if BPF_CGROUP_RUN_PROG_GETSOCKOPT
> > +              * hook returns an error after success of the original handler
> > +              * sctp_getsockopt(...), userspace will receive an error from getsockopt
> > +              * syscall and will be not aware that fd was successfully installed into fdtable.
> > +              *
> > +              * Let's prevent bpf cgroup hook from running on them.
> > +              */
>
> This and..
>
> > +             case SCTP_SOCKOPT_PEELOFF:
> > +             case SCTP_SOCKOPT_PEELOFF_FLAGS:
> > +             /*
> > +              * As pointed by Marcelo Ricardo Leitner it seems reasonable to skip
> > +              * bpf getsockopt hook for this sockopt too. Because internaly, it
> > +              * triggers connect() and if error will be masked userspace can be confused.
> > +              */
>
> ..this comments can be removed, as they are easily visible on the
> description later on for who is interested on why such lines were
> added.
>
> Thanks,
> Marcelo
>
> > +             case SCTP_SOCKOPT_CONNECTX3:
> > +                     return true;
> > +             default:
> > +                     return false;
> > +             }
> > +     }
> > +
> > +     return false;
> > +}
> > +
> >  static int sctp_hash(struct sock *sk)
> >  {
> >       /* STUB */
> > @@ -9650,6 +9679,7 @@ struct proto sctp_prot = {
> >       .shutdown    =  sctp_shutdown,
> >       .setsockopt  =  sctp_setsockopt,
> >       .getsockopt  =  sctp_getsockopt,
> > +     .bpf_bypass_getsockopt  = sctp_bpf_bypass_getsockopt,
> >       .sendmsg     =  sctp_sendmsg,
> >       .recvmsg     =  sctp_recvmsg,
> >       .bind        =  sctp_bind,
> > @@ -9705,6 +9735,7 @@ struct proto sctpv6_prot = {
> >       .shutdown       = sctp_shutdown,
> >       .setsockopt     = sctp_setsockopt,
> >       .getsockopt     = sctp_getsockopt,
> > +     .bpf_bypass_getsockopt  = sctp_bpf_bypass_getsockopt,
> >       .sendmsg        = sctp_sendmsg,
> >       .recvmsg        = sctp_recvmsg,
> >       .bind           = sctp_bind,
> > --
> > 2.34.1
> >
diff mbox series

Patch

diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index cda8c2874691..a211a203003c 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -8281,6 +8281,35 @@  static int sctp_getsockopt(struct sock *sk, int level, int optname,
 	return retval;
 }
 
+static bool sctp_bpf_bypass_getsockopt(int level, int optname)
+{
+	if (level == SOL_SCTP) {
+		switch (optname) {
+		/*
+		 * These options do fd_install(), and if BPF_CGROUP_RUN_PROG_GETSOCKOPT
+		 * hook returns an error after success of the original handler
+		 * sctp_getsockopt(...), userspace will receive an error from getsockopt
+		 * syscall and will be not aware that fd was successfully installed into fdtable.
+		 *
+		 * Let's prevent bpf cgroup hook from running on them.
+		 */
+		case SCTP_SOCKOPT_PEELOFF:
+		case SCTP_SOCKOPT_PEELOFF_FLAGS:
+		/*
+		 * As pointed by Marcelo Ricardo Leitner it seems reasonable to skip
+		 * bpf getsockopt hook for this sockopt too. Because internaly, it
+		 * triggers connect() and if error will be masked userspace can be confused.
+		 */
+		case SCTP_SOCKOPT_CONNECTX3:
+			return true;
+		default:
+			return false;
+		}
+	}
+
+	return false;
+}
+
 static int sctp_hash(struct sock *sk)
 {
 	/* STUB */
@@ -9650,6 +9679,7 @@  struct proto sctp_prot = {
 	.shutdown    =	sctp_shutdown,
 	.setsockopt  =	sctp_setsockopt,
 	.getsockopt  =	sctp_getsockopt,
+	.bpf_bypass_getsockopt	= sctp_bpf_bypass_getsockopt,
 	.sendmsg     =	sctp_sendmsg,
 	.recvmsg     =	sctp_recvmsg,
 	.bind        =	sctp_bind,
@@ -9705,6 +9735,7 @@  struct proto sctpv6_prot = {
 	.shutdown	= sctp_shutdown,
 	.setsockopt	= sctp_setsockopt,
 	.getsockopt	= sctp_getsockopt,
+	.bpf_bypass_getsockopt	= sctp_bpf_bypass_getsockopt,
 	.sendmsg	= sctp_sendmsg,
 	.recvmsg	= sctp_recvmsg,
 	.bind		= sctp_bind,