Skip to content

Commit

Permalink
sctp: add bpf_bypass_getsockopt proto callback
Browse files Browse the repository at this point in the history
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: 0d01da6 ("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
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
  • Loading branch information
mihalicyn authored and intel-lab-lkp committed May 10, 2023
1 parent 948b75b commit 8ad9818
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions net/sctp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -8281,6 +8281,29 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname,
return retval;
}

bool sctp_bpf_bypass_getsockopt(int level, int 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.
*/
if (level == SOL_SCTP) {
switch (optname) {
case SCTP_SOCKOPT_PEELOFF:
case SCTP_SOCKOPT_PEELOFF_FLAGS:
return true;
default:
return false;
}
}

return false;
}

static int sctp_hash(struct sock *sk)
{
/* STUB */
Expand Down Expand Up @@ -9650,6 +9673,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,
Expand Down Expand Up @@ -9705,6 +9729,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,
Expand Down

0 comments on commit 8ad9818

Please sign in to comment.