Received: by 2002:a05:6358:51dd:b0:131:369:b2a3 with SMTP id 29csp365376rwl; Wed, 9 Aug 2023 16:08:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzz4KCSm5391F8PP2RgySFPI3cKLJgj8tMdR7qkh/aEprTr3qCmLwZLg0K366C+hRFs7+6 X-Received: by 2002:a05:6a20:1cf:b0:13d:7aa3:aa72 with SMTP id 15-20020a056a2001cf00b0013d7aa3aa72mr525601pzz.5.1691622490888; Wed, 09 Aug 2023 16:08:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691622490; cv=none; d=google.com; s=arc-20160816; b=UE3yZKJ0/dxuV98/hJcps6WNJehxpb9G0UYFlNTT/sSesRPs1h1BqejpvFxpZo6mpp 647Y/2PqjEUin89Kw5cPKN71GIh7XfNmWFQfMgPCyEEjiGeoKyvM+Gzjy6NKVFwl5WzX c03odQ83pCs+aw6VjTXN2L0366gtCyyAlaWZbVfNehturxx6q0N2Su+4f071P3m2RXCr H4CIaM5JeEXTBNDklZ8YIlEOerIR1Me7yUrjq2M+E0/d3V2UYZxM4DHo429ltxdW3HAm OJOv07T0dsPG5DU05RUR8j+voGbTU4ztYe1t/cG8MSblkJDrplMxWtIY40wMlqsNmC/u iXgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:mime-version:date :dkim-signature:message-id; bh=qMJv6ufpqtknR1yGk38bAZzuEUJ6prfve1gllA06oPM=; fh=ZSNOuxC6W0pZKpAncdbjf9oPxDcRSxhX9VJdBjYID/w=; b=wrEAj0RWVqnCnh2It0IbryYyk0gpi4XGjkivCXGw0zYAG2GtXoOJ2FyqosFDUIbDrZ +BGOdo/IZtVhtzM5XSd9bWrUkNFh7vu+pdvfmhjzn93SRrwnNrHL/w1QyVfAGd9Qqn1N FFs9AlmDUzfO5Pdgv9biTiuRe389Byg9CzhTWkeD7slAOn/6gXvT6T5WWSazcnRr/n3v gdLjz4sTPJywT22KBN4k48A8YrRgOYb7hCSDfWcmIc32T1IITxuSn7EzVyqGC3idqEAT RM/4NWn4ThcAa9eMWKVcYhqRiOAn7F9Z6up38XOu4pmvbX61m7sFv9gL/Ls7RCd0iX7x w1bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="T2KggH/E"; 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=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y24-20020a170902b49800b001b89551a392si149439plr.113.2023.08.09.16.07.58; Wed, 09 Aug 2023 16:08:10 -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=@linux.dev header.s=key1 header.b="T2KggH/E"; 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=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229863AbjHIWCs (ORCPT + 99 others); Wed, 9 Aug 2023 18:02:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbjHIWCq (ORCPT ); Wed, 9 Aug 2023 18:02:46 -0400 Received: from out-83.mta1.migadu.com (out-83.mta1.migadu.com [IPv6:2001:41d0:203:375::53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D02211FD2 for ; Wed, 9 Aug 2023 15:02:45 -0700 (PDT) Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1691618564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qMJv6ufpqtknR1yGk38bAZzuEUJ6prfve1gllA06oPM=; b=T2KggH/EwPTITIwBwhHFSVHu4zjoW1l0wILdp1Zu6fK4jEvQccACra3z4KxIbDv3+Ow2Dh IMKV7G5YVbJvmBBCsVFiIRdzD/qGLH8oB/+x/i2fVsfKZ4fMzFbR2BJM7b2vU0Tb/iDjZM dyAfTOoZhxSCpuHuipwPY+XSm1MKepk= Date: Wed, 9 Aug 2023 15:02:31 -0700 MIME-Version: 1.0 Subject: Re: [PATCH v2 8/8] io_uring/cmd: BPF hook for setsockopt cmd Content-Language: en-US To: Breno Leitao Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, kuba@kernel.org, pabeni@redhat.com, sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com References: <20230808134049.1407498-1-leitao@debian.org> <20230808134049.1407498-9-leitao@debian.org> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: <20230808134049.1407498-9-leitao@debian.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,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 On 8/8/23 6:40 AM, Breno Leitao wrote: > Add support for BPF hooks for io_uring setsockopts command. > > This implementation follows a similar approach to what > __sys_setsockopt() does, but, operates only on kernel memory instead of > user memory (which is also possible, but not preferred since the kernel > memory is already available) > > Signed-off-by: Breno Leitao > --- > io_uring/uring_cmd.c | 23 +++++++++++++++++++++-- > 1 file changed, 21 insertions(+), 2 deletions(-) > > diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c > index 3693e5779229..b7b27e4dbddd 100644 > --- a/io_uring/uring_cmd.c > +++ b/io_uring/uring_cmd.c > @@ -205,23 +205,42 @@ static inline int io_uring_cmd_setsockopt(struct socket *sock, > { > void __user *optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval)); > int optname = READ_ONCE(cmd->sqe->optname); > + sockptr_t optval_s = USER_SOCKPTR(optval); > int optlen = READ_ONCE(cmd->sqe->optlen); > int level = READ_ONCE(cmd->sqe->level); > + char *kernel_optval = NULL; > int err; > > err = security_socket_setsockopt(sock, level, optname); > if (err) > return err; > > + if (!in_compat_syscall()) { > + err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, > + &optname, > + USER_SOCKPTR(optval), > + &optlen, > + &kernel_optval); > + if (err < 0) > + return err; > + if (err > 0) > + return 0; > + > + /* Replace optval by the one returned by BPF */ > + if (kernel_optval) > + optval_s = KERNEL_SOCKPTR(kernel_optval); > + } > + > if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock)) > err = sock_setsockopt(sock, level, optname, > - USER_SOCKPTR(optval), optlen); > + optval_s, optlen); > else if (unlikely(!sock->ops->setsockopt)) > err = -EOPNOTSUPP; > else > err = sock->ops->setsockopt(sock, level, optname, > - USER_SOCKPTR(koptval), optlen); > + optval_s, optlen); The bpf side changes make sense. Thanks. With all the bpf pieces in place, __sys_{get,set}sockopt() is looking very similar to io_uring_cmd_{get,set}sockopt(). There are small differences like one takes fd and another already has a sock ptr, and io_uring_cmd_getsockopt() is SOL_SOCKET only. In general, can they be refactored somehow such that future changes don't have to be made in multiple places?