Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1931341rdb; Tue, 5 Sep 2023 09:07:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFxNnDPofymQMeuNljxfyrzT3x2ANwmJHrpte3gg9kXwuyzv7PdhfSSxLTJkVpKLXsfaSuz X-Received: by 2002:a50:fa84:0:b0:522:57d9:6553 with SMTP id w4-20020a50fa84000000b0052257d96553mr232483edr.1.1693930046282; Tue, 05 Sep 2023 09:07:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693930046; cv=none; d=google.com; s=arc-20160816; b=sc8Q77MNIwY6DQ4j8e95bKztHcPUsN2EDTH81mNIjVGptYaUJHpqn8FihDOakRsfmO liEIjNupUE2r5U1IZYWWqjJs/YDW5Pm2JU7uwTNAnqtXGfSRESTw68yZw5eDXKgr3g3Q ISBbmxMZrNBMDuQuqsyfQGEAdegJz8ppeDpXmBkcIg1idp7rWDpeDq8sGTytxuvnbgQx 7gpX4yU9eYIMVFOxqMHKwlX2HUrScqcuEMj+/cPdUPVTPMfH0vwmo4ocThKdXN5frePr nY77h5CJbyDhXoLBZX7uSNlZU3kM3gxWs19v/jyUY0a/Wc3stmoJ/EfW5EV6gau5Zx5/ DeQQ== 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=IUL6OhPSlmc9b0ojpccZ5UMJYWzfKfwD3kXIKD6Vw5c=; fh=bZ46dV2+WtY78vUxyO3bA9+xUxqpdRmcAyygCP+hLEU=; b=EG0sjqtxDevfXsP99i3ImRLmhLv5P5vyP94ZBC+VQh3Y7SioY2MMea6iRMuaZYO8jy Ga7KDWPRbarKqpEwNRl0nOLeDe+LFVS3BKHa+kWwZajnQMHIbZH6q13pJoahFJiT+Ypo YM5aWps9rrsNSsQdv0ku4OjOoRKYGarnIYFuuuk8j032XfylEdawWdQ+ST4r1/dv9lM8 Usm0+UBeAKRIbDVtcAsi2TjlZAufy7bX+3034B+FfRzBuo1OVcaQBxgVKJBlhupnadfb Du9EngSclBRu/1G5BfrDdQVWBmUhiHg8LcpmqYqHwGzbEJ5hQR04CRZt9aVwzTiqMjN7 G67g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b1-20020aa7c6c1000000b005230e0295d0si7756807eds.126.2023.09.05.09.07.20; Tue, 05 Sep 2023 09:07:26 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244944AbjIDQZb (ORCPT + 21 others); Mon, 4 Sep 2023 12:25:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237311AbjIDQZ1 (ORCPT ); Mon, 4 Sep 2023 12:25:27 -0400 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5324A18B; Mon, 4 Sep 2023 09:25:24 -0700 (PDT) Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-52a4737a08fso2149587a12.3; Mon, 04 Sep 2023 09:25:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693844723; x=1694449523; 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=IUL6OhPSlmc9b0ojpccZ5UMJYWzfKfwD3kXIKD6Vw5c=; b=Ip4ojanxIMbB1us7R3axNK4aY8r2jFrdH3KO1U2WrgTZEST0mBqE9/wL2fkE5VbEBc U4X5dB1B/ErVtAPMeNWcXdRDiTFMVY7npxlVYHlW76zyGfofdtQqhdO94L1cxfBBoI/D Hl5QXnNqg2ovQny1aJS8A1Ff0lJiOfM5/QOjCrAND42qWxjmRwGjDJrCK8THFaoWglvp kbo9c/8vYPRWZOnqmVML/F4Z+kkCQsmB+5fFOTXbl6SMMNGF1m+wjgTtPPvNkaBzb1hu YwXehJuhOGol70f50B16HWbYbo6Il+yXV15tyq/pThVsxeefqC9JptTm27lzbQ4F3Q1I 35mQ== X-Gm-Message-State: AOJu0YyN+m+J/AXwrxwUUn+BR7orhBwtirlX0aC8TTzJoyZL37bykg4p 0h6hTr9rUyTyrCHGjHTGtfo= X-Received: by 2002:a05:6402:2047:b0:523:95e:c2c0 with SMTP id bc7-20020a056402204700b00523095ec2c0mr7062520edb.42.1693844722624; Mon, 04 Sep 2023 09:25:22 -0700 (PDT) Received: from localhost (fwdproxy-cln-006.fbsv.net. [2a03:2880:31ff:6::face:b00c]) by smtp.gmail.com with ESMTPSA id w11-20020aa7d28b000000b0052565298bedsm5987110edq.34.2023.09.04.09.25.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Sep 2023 09:25:22 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, martin.lau@linux.dev, krisman@suse.de, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Song Liu , Yonghong Song , KP Singh , Hao Luo , Jiri Olsa , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org Subject: [PATCH v4 02/10] bpf: Leverage sockptr_t in BPF setsockopt hook Date: Mon, 4 Sep 2023 09:24:55 -0700 Message-Id: <20230904162504.1356068-3-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230904162504.1356068-1-leitao@debian.org> References: <20230904162504.1356068-1-leitao@debian.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 Change BPF setsockopt hook (__cgroup_bpf_run_filter_setsockopt()) to use sockptr instead of user pointers. This brings flexibility to the function, since it could be called with userspace or kernel pointers. This change will allow the creation of a core sock_setsockopt, called do_sock_setsockopt(), which will be called from both the system call path and by io_uring command. This also aligns with the getsockopt() counterpart, which is now using sockptr_t universal pointer. Signed-off-by: Breno Leitao --- include/linux/bpf-cgroup.h | 2 +- kernel/bpf/cgroup.c | 5 +++-- net/socket.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index f5b4fb6ed8c6..cecfe8c99f28 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -137,7 +137,7 @@ int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head, enum cgroup_bpf_attach_type atype); int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level, - int *optname, char __user *optval, + int *optname, sockptr_t optval, int *optlen, char **kernel_optval); int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index ebc8c58f7e46..f0dedd4f7f2e 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -1785,7 +1785,7 @@ static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx, } int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, - int *optname, char __user *optval, + int *optname, sockptr_t optval, int *optlen, char **kernel_optval) { struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); @@ -1808,7 +1808,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, ctx.optlen = *optlen; - if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) { + if (copy_from_sockptr(ctx.optval, optval, + min(*optlen, max_optlen))) { ret = -EFAULT; goto out; } diff --git a/net/socket.c b/net/socket.c index 6fda5d011521..9ec9a8a07c0e 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2287,7 +2287,7 @@ int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, if (!in_compat_syscall()) err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname, - user_optval, &optlen, + optval, &optlen, &kernel_optval); if (err < 0) goto out_put; -- 2.34.1