Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp4938869rwp; Sun, 16 Jul 2023 14:55:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlEHISElPUgwdd2odW0JsmxLVu828+4Zq51EInS/j8yT33VvuBtpSfSa09/jsTQlhla1WLxS X-Received: by 2002:a05:6a21:9988:b0:12d:2abe:549a with SMTP id ve8-20020a056a21998800b0012d2abe549amr14375405pzb.6.1689544528723; Sun, 16 Jul 2023 14:55:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689544528; cv=none; d=google.com; s=arc-20160816; b=J16uFoAnJj2DhKpYpvgoeqkogt2n4tcAWHIDHNk4Yov0nwEjsTYj8bnFXcyzOX8oJl /O+d/qLLGjVK/C0xBIK1WuW3VJEIZ7Ci9e2xJmAtKjIXHwIpGPGCfUFgqnA608hfp+YI znSo40tIXQwTCqGqp2xrSgXwbmtDBU7yv8pycWMq+76mHpUXzQC92j2PciEryhYCZx1O Lil81gnu90NKsB2ZaUIUIR9u2MlTsCmZysvBaol17Wk6/xeNeiiJxVgYKsoML72t8oW0 vOIRejrgTngr2Ui2YW6WgyoRKrVhgxAHNk5MVPNHceNUnDrxBlj5A/9gqbSnOynj6MsZ pAuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Y/RG0fwhq6RP4k68PkZXBA+qo75NORHzgTmdF79XS10=; fh=Le4f2H40BCv36sKAJBmtCaG5HifXt4UBD6RscRSFfuE=; b=FNmN+YtIhxN6vfEr+2A6xtapY9c08Hk8l6Or4NjHoWdQVQ5g6/HIMHQXlsU3xYEvst vzbFcTMcMwhUSCPt7ikaHAiQCqityo5NHoKVWUSGtj/j/h+0h3qZtghq6AXAd0tIT/hx 8MpZF0GNkXG15c+OVdvLv4WzjMCYbjh33EsFwMopVgQ+ERNVABOsKA27fVeKm3eDBFEZ 8bk466y0fV2M6S44t89/Svlk6c4fzyyvobNPaDDAzbJjcgokWsGKk7oyolhKTE+wZKFj n6W0CM9ykUOONNfVUC25Bi6LwtvruIMtJmB1ZTq5vRx1AH/qOPhP6gle0oEer0ud8wmi 1Yyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uheWh4Kg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t3-20020a634603000000b0053fee209655si6505045pga.664.2023.07.16.14.55.16; Sun, 16 Jul 2023 14:55:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uheWh4Kg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231384AbjGPUfK (ORCPT + 99 others); Sun, 16 Jul 2023 16:35:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230491AbjGPUfI (ORCPT ); Sun, 16 Jul 2023 16:35:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04B90D2; Sun, 16 Jul 2023 13:35:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8E1DC60EB0; Sun, 16 Jul 2023 20:35:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74E11C433C8; Sun, 16 Jul 2023 20:35:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1689539706; bh=O6TUdrDHfsVRCsKUxF9WP7MXPTpUIpz/o6QOt+IMsoY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uheWh4KgtbYvUhKlOF1CuIVDbpTkzcFdMb4sNiVisjv9A03fsYJKtUNjAg9mnBqfL 9WXQbN3dYskEPYP7GqsVM78do9ZJnMOpx7Wzo2VVqA5glmF6Bw8N+HrhJuyhozGw8t ImMdlfmPFRfCORVwzAgL6Ge28KpNhyLyWdvuBrcI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Daniel Borkmann , Christian Brauner , Stanislav Fomichev , Neil Horman , Marcelo Ricardo Leitner , Xin Long , linux-sctp@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Alexander Mikhalitsyn , "David S. Miller" , Sasha Levin Subject: [PATCH 6.1 075/591] sctp: add bpf_bypass_getsockopt proto callback Date: Sun, 16 Jul 2023 21:43:34 +0200 Message-ID: <20230716194925.824615271@linuxfoundation.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230716194923.861634455@linuxfoundation.org> References: <20230716194923.861634455@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Mikhalitsyn [ Upstream commit 2598619e012cee5273a2821441b9a051ad931249 ] Implement ->bpf_bypass_getsockopt proto callback and filter out SCTP_SOCKOPT_PEELOFF, SCTP_SOCKOPT_PEELOFF_FLAGS and SCTP_SOCKOPT_CONNECTX3 socket options from running eBPF hook on them. SCTP_SOCKOPT_PEELOFF and SCTP_SOCKOPT_PEELOFF_FLAGS 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 a fdtable. As pointed by Marcelo Ricardo Leitner it seems reasonable to skip bpf getsockopt hook for SCTP_SOCKOPT_CONNECTX3 sockopt too. Because internaly, it triggers connect() and if error is masked then userspace will be confused. 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 Cc: Christian Brauner Cc: Stanislav Fomichev Cc: Neil Horman Cc: Marcelo Ricardo Leitner Cc: Xin Long Cc: linux-sctp@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Suggested-by: Stanislav Fomichev Acked-by: Stanislav Fomichev Signed-off-by: Alexander Mikhalitsyn Acked-by: Xin Long Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/sctp/socket.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index bc3d08bd7cef3..e1011311bc877 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -8279,6 +8279,22 @@ 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) { + case SCTP_SOCKOPT_PEELOFF: + case SCTP_SOCKOPT_PEELOFF_FLAGS: + case SCTP_SOCKOPT_CONNECTX3: + return true; + default: + return false; + } + } + + return false; +} + static int sctp_hash(struct sock *sk) { /* STUB */ @@ -9643,6 +9659,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, @@ -9698,6 +9715,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.39.2