Received: by 2002:a05:7412:bb8d:b0:d7:7d3a:4fe2 with SMTP id js13csp2284831rdb; Thu, 17 Aug 2023 17:07:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH63XyQ7+TEK/7AKt5QtIvjno0pzjDeTsjwFgLHy2HX9BOOxxlkk/wUSzn8gpD4lzJFaQRY X-Received: by 2002:ac2:5e6c:0:b0:4fd:fc3e:722c with SMTP id a12-20020ac25e6c000000b004fdfc3e722cmr412941lfr.58.1692317273008; Thu, 17 Aug 2023 17:07:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692317272; cv=none; d=google.com; s=arc-20160816; b=c86f1BNEekRwWTuQRugN9fTniMOlFwjcqOSyqyUA1P1KYq7qEl9lElDHjDML4O/ZxR iqWp6gQ7zACz7iJHnNj+Vl83GPEhq2ef6ZplY2EcHXSwl9TTesqD8ZGrLgj3jPrHZ9bZ aSo2+U0224aY0AeyFg1r5/ZbJDcKi08P2hiQKVisVbVn4lCt80myhH8o3IQ0zO934OB5 uuD584Q4/oS6KHKfAHw7P6rjDNLQWEO5CKH2Ds7ECI8gkn+TxaALz1LX8G6G+iigXD4o b+yOFZ37QYl52R2Tv3JlHu8P0Gfy1RnZR25frwwip+JxmZ5+EBpwIVTpcomxZXBk34Bm 2rqw== 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=WwuNwSQO4JjwcfqL+k1N+kwu+e1Kuo7mf/uUCHCBGDE=; fh=D3vveYC+apzUgrVv3Xddj4daXaIcbwg6Ey++n3AzlRE=; b=Q62VCyah7JLziKyXYiZRDS+wwwfKAIK5m5tJQaWjF36k5Mr32tBF+p3khxbXwbvcy7 SY8N0xeCiAyL1wq7xOAlfIr4wEulVqd9N+j7so4KcAvLedpHK8LvDywXQ67Xh2fJGxdZ Ri1Fmll8sOKytJXtesYK2cirrQzb459hxfVWHJWWVJQ6Ot7e5p4GiMKMx92j+yAAS8fe v4boZEb5haQZSr5CGv7vrv0W8TLUdKAsSffRcaOyERA8aR8lYd6Dybr2gG2vM64lVgeB Gu1kDSfX884GCQfFUQLMBC0Sum5O6VAtjsLqKklNlfoqamkZWqZbKPQXZLlIw5KtcrWP HMiQ== 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 m21-20020aa7c495000000b00521ce86bb68si452191edq.473.2023.08.17.17.07.27; Thu, 17 Aug 2023 17:07:52 -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 S1344182AbjHQO5A (ORCPT + 99 others); Thu, 17 Aug 2023 10:57:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352497AbjHQO42 (ORCPT ); Thu, 17 Aug 2023 10:56:28 -0400 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F75F358E; Thu, 17 Aug 2023 07:56:10 -0700 (PDT) Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-522bd411679so10206592a12.0; Thu, 17 Aug 2023 07:56:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692284169; x=1692888969; 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=WwuNwSQO4JjwcfqL+k1N+kwu+e1Kuo7mf/uUCHCBGDE=; b=CXvhM0Vhbi6roe+5kIgCd6luWv9/ZadHYeG/ZJMBj8+9SrWkSBZYiVH/bDrmrMrUBW Z9j6Y8AZIPtJ61SFdx4Je9sn+P9ZN5fbpgFxQU8QToa7DtXPlxcGjHmMLPHKXEvY7dHE 7BZ9wwJEgFbaDvwVcJcpvXm9zhGoTbXkK7XDGCHZiAoatcOx1F7cYHjbP3a4ukXFqrZM 2MYkGxhAWXSHAdkLmjBopQvYZ5cpBhgJy1wUHXGOmwdeEq4OnesXkl9nKJj6uURvfsiJ rVPqX3bdkpVl0QkEggZ3yXNwijzyoSlYcLtrp1HSj++2GmNavEnsPGq2MY+G5sPpcoub iEgg== X-Gm-Message-State: AOJu0YxnDqgjyYD89mALM4g0RvV7OMLkeOkHDeLwPHnvF7oMGMyqUBcb InrLD5Wm7XXrh4X7/CfZXI0= X-Received: by 2002:aa7:d6c2:0:b0:528:89b0:ec50 with SMTP id x2-20020aa7d6c2000000b0052889b0ec50mr465165edr.21.1692284168871; Thu, 17 Aug 2023 07:56:08 -0700 (PDT) Received: from localhost (fwdproxy-cln-017.fbsv.net. [2a03:2880:31ff:11::face:b00c]) by smtp.gmail.com with ESMTPSA id w4-20020a50fa84000000b00523463540e0sm9825049edr.85.2023.08.17.07.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 07:56:08 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, martin.lau@linux.dev, 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, krisman@suse.de Subject: [PATCH v3 1/9] bpf: Leverage sockptr_t in BPF getsockopt hook Date: Thu, 17 Aug 2023 07:55:46 -0700 Message-Id: <20230817145554.892543-2-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230817145554.892543-1-leitao@debian.org> References: <20230817145554.892543-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 Leverage sockptr_t structure to have an argument that is either an userspace pointer, or, a kernel pointer. This makes this function flexible, so, we can mix and match user and kernel space pointers. The main motivation for this change is to use it in the io_uring {g,s}etsockopt(), which will use a userspace pointer for *optval, but, a kernel value for optlen. Signed-off-by: Breno Leitao --- include/linux/bpf-cgroup.h | 5 +++-- kernel/bpf/cgroup.c | 20 +++++++++++--------- net/socket.c | 5 +++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index 57e9e109257e..d16cb99fd4f1 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -139,9 +139,10 @@ int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head, int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level, int *optname, char __user *optval, int *optlen, char **kernel_optval); + int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, - int optname, char __user *optval, - int __user *optlen, int max_optlen, + int optname, sockptr_t optval, + sockptr_t optlen, int max_optlen, int retval); int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level, diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 5b2741aa0d9b..ebc8c58f7e46 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -1875,8 +1875,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, } int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, - int optname, char __user *optval, - int __user *optlen, int max_optlen, + int optname, sockptr_t optval, + sockptr_t optlen, int max_optlen, int retval) { struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); @@ -1903,8 +1903,8 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, * one that kernel returned as well to let * BPF programs inspect the value. */ - - if (get_user(ctx.optlen, optlen)) { + if (copy_from_sockptr(&ctx.optlen, optlen, + sizeof(ctx.optlen))) { ret = -EFAULT; goto out; } @@ -1915,8 +1915,8 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, } orig_optlen = ctx.optlen; - if (copy_from_user(ctx.optval, optval, - min(ctx.optlen, max_optlen)) != 0) { + if (copy_from_sockptr(ctx.optval, optval, + min(ctx.optlen, max_optlen))) { ret = -EFAULT; goto out; } @@ -1930,7 +1930,8 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, if (ret < 0) goto out; - if (optval && (ctx.optlen > max_optlen || ctx.optlen < 0)) { + if (!sockptr_is_null(optval) && + (ctx.optlen > max_optlen || ctx.optlen < 0)) { if (orig_optlen > PAGE_SIZE && ctx.optlen >= 0) { pr_info_once("bpf getsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n", ctx.optlen, max_optlen); @@ -1942,11 +1943,12 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, } if (ctx.optlen != 0) { - if (optval && copy_to_user(optval, ctx.optval, ctx.optlen)) { + if (!sockptr_is_null(optval) && + copy_to_sockptr(optval, ctx.optval, ctx.optlen)) { ret = -EFAULT; goto out; } - if (put_user(ctx.optlen, optlen)) { + if (copy_to_sockptr(optlen, &ctx.optlen, sizeof(ctx.optlen))) { ret = -EFAULT; goto out; } diff --git a/net/socket.c b/net/socket.c index 1dc23f5298ba..33ea5eb91ade 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2311,8 +2311,9 @@ int __sys_getsockopt(int fd, int level, int optname, char __user *optval, if (!in_compat_syscall()) err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname, - optval, optlen, max_optlen, - err); + USER_SOCKPTR(optval), + USER_SOCKPTR(optlen), + max_optlen, err); out_put: fput_light(sock->file, fput_needed); return err; -- 2.34.1