Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3609092rdb; Wed, 13 Sep 2023 18:26:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5cJmYOC+8G3mpbcfw4l0LvMKNtRHKMhDqCZ78j3I2mjAUeNEvkOKyLAv/QYp2RFymaCvM X-Received: by 2002:a05:6808:398a:b0:3ab:8ae5:e7c6 with SMTP id gq10-20020a056808398a00b003ab8ae5e7c6mr4842045oib.1.1694654816961; Wed, 13 Sep 2023 18:26:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694654816; cv=none; d=google.com; s=arc-20160816; b=TkFgglqN6DqQAAsNXUF/HLe4FrHnRaG0BtX3UVkAS95B9aCv+1g7xVXKX9b1SsURjs xtXZM08nlsRRZ5KwNF0DpffeyC79JuGjFDlvxYCPFJgiXlqJpXhkVxlCXM9jXbVTUq1E 8HiMFBEOx7WK6OUOH+9NYYKswN3yoajrLsz2tuOYzyJhFC5p9dvHaNGOA5ucb3KSKC+H W6UWB10z3Yf53teZY1DUA5RXdQ1EGXJRXVEHqkstf8bAW+7NiS1oqkQ19WOilLb2NBTg 4cHz79Z7jKQNdquqbTqsmpSKYU6qc2okDxhFZXuoFWVg2SEJ5l8xD50Ccb7AgIDMwdbe QHdw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=HNDakB63GeZKBPdnx2EtHPN64oGZn+t0qvDI93lexvQ=; fh=HmFj3TSCWJbXSlIbjcHElL/SJMYYS7knVWpkGPW/7+c=; b=yxTtKhPzoKIbh0Fl6NkCksKBPu7xW8iRRUIkgErnjKyqJG8YK0OjDn7KkIpz6ZW8lH bAj6TxcAV5iFP0tCDJgXYqxm8NFcQOunLNV04iO+gpSPiGB6r3yDIueOUAGhV1tg67N2 NjN5TbI952W2Y8GI2ZZL4YrHhi/XOiBWbrio5sD+BWEvyVXdEGKeUVMVBZkY4etpE7Y7 g+uABmlhNERIaMfIBaW3QBx9K8RwTPbn9Ccb7iiNgavcec4TZ6oSWqbFZbAUkIQHWq7Z cUuaE1HDi4upmqAO5+Li3e9F3REIAAonz0uYdvc6ZFqkvtssz+RSLj0yRYP8U+QlGeor r0ig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id h7-20020a056a00170700b0068e2566615esi476448pfc.379.2023.09.13.18.26.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 18:26:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A388B80D7E4B; Wed, 13 Sep 2023 08:28:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229516AbjIMP2c (ORCPT + 99 others); Wed, 13 Sep 2023 11:28:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229511AbjIMP20 (ORCPT ); Wed, 13 Sep 2023 11:28:26 -0400 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12B811BC3; Wed, 13 Sep 2023 08:28:22 -0700 (PDT) Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-502e0b7875dso925656e87.0; Wed, 13 Sep 2023 08:28:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618900; x=1695223700; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HNDakB63GeZKBPdnx2EtHPN64oGZn+t0qvDI93lexvQ=; b=fvCHy6ZcoUIYrtFltO0XIziKj5/zzrXcoANIOXCNMIGvhE7dW4y/2RfYPpDblDZlIb hBTxL++nfoMUvP8wKbluFXmMSzDb+7+iZIEaKTrXbhlETCFMIQ7WwGRoB8YJtIq+PCq+ n+mP0F7cLlfiqwQ/xnMatPavlzjUiFZ0nF4nRwWDNt4ucOS6ukt/OEI/qTFlAocX0ZSP T65d74W3ZvXaf8sH8ngpAmWEoxiuXPPZ7Av5lLDVbNTxSfJ4D7lsWgr/QStFVlrJAZWh So65SFqEpkqeNTLn1wHgTv0T1mqSK8FVhe/+droqTT5lt2pcw67D+SMlHZSjZjGrB35G H5xw== X-Gm-Message-State: AOJu0YyD4hrFwSBwkpebL58O4cV7D9N1lzD9A5kbo4eVTnU+xLVf3CMi +T5eCPdAx5+vkGGBjwa5b0E= X-Received: by 2002:ac2:53a9:0:b0:500:b64a:ad15 with SMTP id j9-20020ac253a9000000b00500b64aad15mr2104579lfh.52.1694618900057; Wed, 13 Sep 2023 08:28:20 -0700 (PDT) Received: from localhost (fwdproxy-cln-016.fbsv.net. [2a03:2880:31ff:10::face:b00c]) by smtp.gmail.com with ESMTPSA id n13-20020a170906b30d00b0099cc36c4681sm8596578ejz.157.2023.09.13.08.28.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:19 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, martin.lau@linux.dev, krisman@suse.de, "David S. Miller" , Eric Dumazet Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org Subject: [PATCH v6 2/8] net/socket: Break down __sys_getsockopt Date: Wed, 13 Sep 2023 08:27:38 -0700 Message-Id: <20230913152744.2333228-3-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:28:38 -0700 (PDT) Split __sys_getsockopt() into two functions by removing the core logic into a sub-function (do_sock_getsockopt()). This will avoid code duplication when executing the same operation in other callers, for instance. do_sock_getsockopt() will be called by io_uring getsockopt() command operation in the following patch. Suggested-by: Martin KaFai Lau Signed-off-by: Breno Leitao --- include/net/sock.h | 3 +++ net/socket.c | 48 +++++++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index aa8fb54ad0af..fbd568a43d28 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1863,6 +1863,9 @@ int sock_setsockopt(struct socket *sock, int level, int op, sockptr_t optval, unsigned int optlen); int do_sock_setsockopt(struct socket *sock, bool compat, int level, int optname, char __user *user_optval, int optlen); +int do_sock_getsockopt(struct socket *sock, bool compat, int level, + int optname, char __user *user_optval, + int __user *user_optlen); int sk_getsockopt(struct sock *sk, int level, int optname, sockptr_t optval, sockptr_t optlen); diff --git a/net/socket.c b/net/socket.c index 360332e098d4..fb943602186e 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2333,28 +2333,17 @@ SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level, int optname)); -/* - * Get a socket option. Because we don't know the option lengths we have - * to pass a user mode parameter for the protocols to sort out. - */ -int __sys_getsockopt(int fd, int level, int optname, char __user *optval, - int __user *optlen) +int do_sock_getsockopt(struct socket *sock, bool compat, int level, + int optname, char __user *optval, + int __user *optlen) { int max_optlen __maybe_unused; const struct proto_ops *ops; - int err, fput_needed; - struct socket *sock; - - sock = sockfd_lookup_light(fd, &err, &fput_needed); - if (!sock) - return err; + int err; err = security_socket_getsockopt(sock, level, optname); if (err) - goto out_put; - - if (!in_compat_syscall()) - max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen); + return err; ops = READ_ONCE(sock->ops); if (level == SOL_SOCKET) @@ -2365,11 +2354,34 @@ int __sys_getsockopt(int fd, int level, int optname, char __user *optval, err = ops->getsockopt(sock, level, optname, optval, optlen); - if (!in_compat_syscall()) + if (!compat) { + max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen); err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname, optval, optlen, max_optlen, err); -out_put: + } + + return err; +} +EXPORT_SYMBOL(do_sock_getsockopt); + +/* Get a socket option. Because we don't know the option lengths we have + * to pass a user mode parameter for the protocols to sort out. + */ +int __sys_getsockopt(int fd, int level, int optname, char __user *optval, + int __user *optlen) +{ + bool compat = in_compat_syscall(); + int err, fput_needed; + struct socket *sock; + + sock = sockfd_lookup_light(fd, &err, &fput_needed); + if (!sock) + return err; + + err = do_sock_getsockopt(sock, compat, level, optname, optval, + optlen); + fput_light(sock->file, fput_needed); return err; } -- 2.34.1