Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7416868rwr; Wed, 10 May 2023 08:02:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5gO7z4FapFGAma7GTfAToGIUTATpTmB+sMPKscHWkMFiRMcasluPJyt+lmChvkopy0ebPj X-Received: by 2002:a05:6808:913:b0:394:1c29:952b with SMTP id w19-20020a056808091300b003941c29952bmr2136199oih.49.1683730927247; Wed, 10 May 2023 08:02:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683730927; cv=none; d=google.com; s=arc-20160816; b=rhSS0M+tf1DqwzXGeT0d5wcRJaCA/O+y7Lv4WzK2TebSAOlwN1A76q02OhLDEESqee lwH06/I+dIyk4amTRSoa91UObHgL0vjt5JvEhQ8bSiedXZ1WOMpXd23E7lVAReuep0zJ CLjoMQsdV5RPIpk+m7nT1MkVtTZ0qzaHk7ulNQgoJzVt3IwNiR19uU6UwhGQlspYX/n4 EqKqXfrAujI99vcTvxTyZKxEz/kPwGJFUYPyirJZXnWqnoT+VWy1rXlF4COes4CvwgWl h9d4t5DsxRB+yQNHtGr3q/1UKgXmpazP9VGhupcyyMWfygg8tr7VKWDqWrCEfZPcXKC9 CNqA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=VsRr4UycG64Ad7Wwitdyuus/KJ2GRNbGK68qU7i6U64=; b=gIeIxoJ5EhGv2zeHlA0/aXrksb4umNR5UdJD73AErpcTHP9lbcEZo3eKCt39ue96Je ElumNa+t1yfdBmuhnvWW0/BwiTEcLNby1dTP4T22pU2pML8ZVXmXsnsPptKWLwYElthM flmS9O+rtcrYVSMtYGgFwFT6AnAnSFK6Fag+l/TZjnbFDeM1bs5LIX5IMfjAIgkU2ZRm mkrc0mxtJWwtOhiErgmA5NTM8PuRIVgSFJCtjTqlZwucFnMF3UowxNP1gVN8T6NwPFg2 ipu/y/2qBykif/FXAli/FqKEaLAQ9y1sG4bjfQQqvflZhbLFmYqiT/+cVGGqV4Tm1Eyl yTpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=pNcxgBXP; 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=canonical.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i28-20020a056808055c00b0039443c4d432si188536oig.291.2023.05.10.08.01.52; Wed, 10 May 2023 08:02:07 -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=@canonical.com header.s=20210705 header.b=pNcxgBXP; 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=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237471AbjEJOnk (ORCPT + 99 others); Wed, 10 May 2023 10:43:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237589AbjEJOnf (ORCPT ); Wed, 10 May 2023 10:43:35 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 755B159EB for ; Wed, 10 May 2023 07:43:31 -0700 (PDT) Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 6573C3F486 for ; Wed, 10 May 2023 14:43:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1683729807; bh=VsRr4UycG64Ad7Wwitdyuus/KJ2GRNbGK68qU7i6U64=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=pNcxgBXPFFodaeYlu/o2D06g8I4U1cFk935AI5+DBzamPez+oJXnjt7T0PNM5448A xjHFUWScYj/5EzuCjDilxYpQBx7drPoIm7NhZP1e3iFaYO/wO68QnkRo8jM2bGSn5U sq2Af2W2KRvy8DmYOSglmdGXVy5uynLY/+rdUGU6ErVZQhQndINeYjXByZHOsTV51e cSavG4n0B0LL7TtkAGKt1GQv36vZtNgh43D4DgjP8BzeCR/L05xN2nxFziMfdu9wdL 5A4kNy41+1iYDmGl02GHr5lpPR5cY9/us4gAT2OPrRQ5HzPkt3gOcaY30grtcwxZfM zCJaW69tRRjew== Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-965f0c4ae32so579892066b.1 for ; Wed, 10 May 2023 07:43:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683729804; x=1686321804; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VsRr4UycG64Ad7Wwitdyuus/KJ2GRNbGK68qU7i6U64=; b=LBDpzciRrPbmpYj5PCZFN6fF7dqVyHIP4tSi7t27Uk8QdV1mCW0ZenQHqEB+nzA3LV IcXj5U3zUQEmb9AQtmKgnZ1t4sYoThDZGtYa+1lmOcxT5UT5dzUtqU5hzUyfqUWDyhpl //IIFa6AaYtfOouMAlmYYL0zDngzxjzWdmHEevv1xgg719pEMdlYUTdPWclEadssQaYk 73+CQHUaav32blLsoio6FyMehJqqvOL8kBQg6dx6+1GNBoJQMEuxF8Ifw72JKZZeB0UZ zalIXz9dRkTpH9nT5swsFRmJFgYv4af2eGGwzGl0oVAZw4PAwwpyN5zYPJTqF+h+2E47 nKRQ== X-Gm-Message-State: AC+VfDxrWP3NIu1NJ1Pf0Lb1P1ajsQHdHXPl1QbbImcOx0gRo6yKLsvR Eh6U8/QQ81EA25WLJe90rJEAHF5nVbFPiIsf3P27wUfLP/k5p4eyDrdxXLrpmiWLeAvxYpF0gIu SxModCSHwkHr2NlniIukpSyX0QmelHnG2bRx9C/qivg== X-Received: by 2002:a17:907:94cf:b0:8b8:c06e:52d8 with SMTP id dn15-20020a17090794cf00b008b8c06e52d8mr18552876ejc.36.1683729804689; Wed, 10 May 2023 07:43:24 -0700 (PDT) X-Received: by 2002:a17:907:94cf:b0:8b8:c06e:52d8 with SMTP id dn15-20020a17090794cf00b008b8c06e52d8mr18552853ejc.36.1683729804415; Wed, 10 May 2023 07:43:24 -0700 (PDT) Received: from amikhalitsyn.. (ip5f5bf3d5.dynamic.kabel-deutschland.de. [95.91.243.213]) by smtp.gmail.com with ESMTPSA id ci18-20020a170907267200b009659ecdf29fsm2753044ejc.1.2023.05.10.07.43.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 07:43:23 -0700 (PDT) From: Alexander Mikhalitsyn To: nhorman@tuxdriver.com Cc: davem@davemloft.net, Alexander Mikhalitsyn , Daniel Borkmann , Christian Brauner , Stanislav Fomichev , Marcelo Ricardo Leitner , Xin Long , linux-sctp@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH net-next v2] sctp: add bpf_bypass_getsockopt proto callback Date: Wed, 10 May 2023 16:42:58 +0200 Message-Id: <20230510144258.1343471-1-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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 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 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 --- net/sctp/socket.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index cda8c2874691..fed6057beb60 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -8281,6 +8281,29 @@ static int sctp_getsockopt(struct sock *sk, int level, int optname, return retval; } +static 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 */ @@ -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, @@ -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, -- 2.34.1