Linux-SCTP Archive mirror
 help / color / mirror / Atom feed
From: Aleksandr Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
To: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Cc: nhorman@tuxdriver.com, davem@davemloft.net,
	Daniel Borkmann <daniel@iogearbox.net>,
	Christian Brauner <brauner@kernel.org>,
	Stanislav Fomichev <sdf@google.com>,
	Xin Long <lucien.xin@gmail.com>,
	linux-sctp@vger.kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org
Subject: Re: [PATCH net-next v2] sctp: add bpf_bypass_getsockopt proto callback
Date: Thu, 11 May 2023 15:26:31 +0200	[thread overview]
Message-ID: <CAEivzxennLfeh=pbMBA2Qj_DQXLMijGKVcRiKuraacCxc=9mgA@mail.gmail.com> (raw)
In-Reply-To: <ZFzpqZCV6V+hwKjI@t14s.localdomain>

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
> >

  reply	other threads:[~2023-05-11 13:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-11 12:31 [PATCH net-next v2] sctp: add bpf_bypass_getsockopt proto callback Alexander Mikhalitsyn
2023-05-11 13:12 ` Marcelo Ricardo Leitner
2023-05-11 13:26   ` Aleksandr Mikhalitsyn [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-05-10 14:42 Alexander Mikhalitsyn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEivzxennLfeh=pbMBA2Qj_DQXLMijGKVcRiKuraacCxc=9mgA@mail.gmail.com' \
    --to=aleksandr.mikhalitsyn@canonical.com \
    --cc=brauner@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sctp@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=marcelo.leitner@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=nhorman@tuxdriver.com \
    --cc=sdf@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).