From patchwork Wed May 10 13:15:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Mikhalitsyn X-Patchwork-Id: 92082 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3616004vqo; Wed, 10 May 2023 06:17:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6hPAuhY9JFKPlUeVLRsnGkNF7UNWY/lbM1HbOWrMJXWUEEGI8PndzmuUDrXkePgitltEhB X-Received: by 2002:a05:6a00:2194:b0:643:b653:3aa with SMTP id h20-20020a056a00219400b00643b65303aamr20779829pfi.32.1683724669618; Wed, 10 May 2023 06:17:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683724669; cv=none; d=google.com; s=arc-20160816; b=w8N4Axois7AHuM6EwtfniemmL+MbaZvz17+WSRDTY7mHFS+T1n/HWAPbK5vlMzTdyb hpAvnXWuvPDJHTyImthwucxMxq+xZZ9k6xKs4oII1oM2auhT/Cs54d+CjgvKrZWHs1vi 1yv+VP1VeJeUtOjFYTBLyCx0s6HpW+P1wfD7CL6Swbc6krtEIlDXfgD6Lrh97sfVsEmB cx782AP6smPbM5KEaHSEH0nJMF4rAq5In3zv1pBIOlzgnLrdyin7xnUqlP1h6A74AOit GoZVHboVtwmOxN7R3WVnvIXnVuv1oeIhSF9/5AbcSdHzxkF6CXg1iGsBWor2/13zN3Xo jHLg== 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=a4+m+7dc0UGwNdAzW/ds7TFtd8vcV7ga23RII0fZY0k=; b=H8KlKxxNBIex2w5ViqNRoZZKukX+4HvfJw2l8U7ezVixlQgIrrBkSGJiJDH56ot/6j /qgOoNaSHqDn6LNzQAp0wmu99nW4ruXBlwtIcQsOHdnjhwtu3WuR4Ne1zRMyVjmkU85u 0GLohRWw/VgcPGpyzQ+5Iu6tEpbajVlQQ79nldwLO/iUzYNo18S5Ie7MgB7aKpvB+4pb eTxo2/DaroRhAnNJlPaPUIsOoyfM6V0JIac5KEJ+WW/iB8y+/3fPyf+GlVJDrpHIV06w VZy6q37l1v6dPE66DWbeEozqakw69xGYSPXt6sxZUBODpIukhZnE0mU7ZZe8Vu3JZSHi iQVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=QnDwxcwb; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q24-20020aa79618000000b00628217e3ea6si4821086pfg.316.2023.05.10.06.17.36; Wed, 10 May 2023 06:17:49 -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=QnDwxcwb; 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 S236672AbjEJNPv (ORCPT + 99 others); Wed, 10 May 2023 09:15:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229740AbjEJNPt (ORCPT ); Wed, 10 May 2023 09:15:49 -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 DC09612A for ; Wed, 10 May 2023 06:15:46 -0700 (PDT) Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.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 1CE2C3F4D6 for ; Wed, 10 May 2023 13:15:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1683724545; bh=a4+m+7dc0UGwNdAzW/ds7TFtd8vcV7ga23RII0fZY0k=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=QnDwxcwbqo0qcvmbuAkflNeP4iBu/+uu14fg1m+OR/wxY7VXnStVXHlHnR5f+u7gS 19TeDZuXTdBxaZWrQvvUcN8KNaj6cJ76l/aRFlncLW5sqI8Nl7qzd61XLhqHzMIfFX DPvnXQ21AJdTMwgyJxD5PNOtqa9VIo90yVsal+Gi8YboeSxJOo8SgFo5ZU+faE6jHW r8B1+LGY3HX5eBvDlbAqXVRX3hiQlgb0r+nWnTDbjo2jUfOHFzQA24SzMn+1dbSa3c AnFBtdrrs9OaSMH8A75J0PnzaRfLVv2eo0rHk1jSFg7p4/P5cpR+HLr7gEyVdNDoLJ fEQm44H3iMv9w== Received: by mail-ed1-f69.google.com with SMTP id 4fb4d7f45d1cf-50a16ab50e6so6777348a12.0 for ; Wed, 10 May 2023 06:15:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683724544; x=1686316544; 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=a4+m+7dc0UGwNdAzW/ds7TFtd8vcV7ga23RII0fZY0k=; b=RLewV8CyO/AxCtZSnWFQhjS630QJyUttB1ZpfOBxUtOdfcHlC50jFZXzR1h6QUMyhh bhX2G6Q/IZJYc0WkP8GRpJPzzloKnddo8ju2GcL9SXiAQQnicdTj+AzJTK8VfMeTVJUf JDbkwpCZB6tnKK/xUNey2QJtvQS3d6bWeDq+bWN3RaZtvPflFXfBXW+nHDh3rU8sVVnl z7hSgb228ueneJF/jXOsqkUk63Jr1ydrPin09cBnrUjT4OBJadH78NjMZLV2XigCkHeA 503iuw1JUcFUy6oJz6c8te2KNXsJ6PIOJo1zZEaItqHJQxokfJRUPXlwYGHzIfOJGb6z ITdA== X-Gm-Message-State: AC+VfDyu27OURfpnk4whVK1MjSPn48KeLlr67rQwYcRW9hInh5KSDSiY 2n6xM9UEVYQvY8GE6m4lc4EZxPhbCL1r+6HpmoB/DJgZbkNSxPqLw/XH4sUtiPS5i08uK/RRk97 UecSoqTEiIIo3utHkbr465yr04zTZRDxlCf2ViqSg4lTkBmWO0Q== X-Received: by 2002:a05:6402:10cc:b0:502:2494:b8fc with SMTP id p12-20020a05640210cc00b005022494b8fcmr12892380edu.7.1683724544609; Wed, 10 May 2023 06:15:44 -0700 (PDT) X-Received: by 2002:a05:6402:10cc:b0:502:2494:b8fc with SMTP id p12-20020a05640210cc00b005022494b8fcmr12892359edu.7.1683724544287; Wed, 10 May 2023 06:15:44 -0700 (PDT) Received: from amikhalitsyn.. (ip5f5bf3d5.dynamic.kabel-deutschland.de. [95.91.243.213]) by smtp.gmail.com with ESMTPSA id e2-20020a50fb82000000b00509e3053b66sm1824750edq.90.2023.05.10.06.15.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 06:15:43 -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] sctp: add bpf_bypass_getsockopt proto callback Date: Wed, 10 May 2023 15:15:27 +0200 Message-Id: <20230510131527.1244929-1-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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=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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765513279488311025?= X-GMAIL-MSGID: =?utf-8?q?1765513279488311025?= 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 Signed-off-by: Alexander Mikhalitsyn Acked-by: Stanislav Fomichev --- net/sctp/socket.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index cda8c2874691..a9a0ababea90 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; } +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,