Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3804817imw; Mon, 18 Jul 2022 15:01:19 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uiE/kV50asvHNWWoMLvtCYtgpoTNEHSdI7x2NWljPxQkXFS3tatVcryk0ZmN0zukKNLVNQ X-Received: by 2002:a17:90a:e4ce:b0:1ef:87dc:e377 with SMTP id e14-20020a17090ae4ce00b001ef87dce377mr39735195pju.25.1658181678962; Mon, 18 Jul 2022 15:01:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658181678; cv=none; d=google.com; s=arc-20160816; b=K5dCRQYb1MdhnAhR00Jfe5Z15zfwWtd9pZJMb4P47wa3xHuP6AR/FrdlbjnngyiUfh JXboidVfTRYmxVzr+hHxapnWf7aYCHPrwQv0hjFJBWDjsbt4KujE83L4xQl1pScCWR60 DdPsBgVDK35iGx4NtBcQzwKOyC+3pRvExm4UrKlr2n7++BwFbOw5mN+quqN+pWM4Jl6I hF4i+R0jwSzRpCo9K7o+ozYDvC5vDgUfIQZyIqvqdg08nOOKv/nD8y4lAwkmvjcrmpJd A7ey251cvt6fh/DmJt0JwcUfO//2koAIMVSKH0CiIc2pNaHPj2Su9f78p/8BxBQqC9Xi Cpgg== 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=GM8+8p1/J6+pvSGth1ereNERHh7ix8LfwtKYNnc1Yxg=; b=Gm2VCaDAdWbbAF0n0gl5kHql2RIBXNSANcwAUhC/buXZT6x7ZhC/GrFQdStz/VxDqr uRuSItL60Q/HYkqgvF6Dkphyiafq0fY2AsNeoXA45nQb20s/fak3+u0ie7J46i4XJzuu LuzL1uhvN5oQ9MiAfis9u7dp7KftH7hvbTfaumknsyyryNQuGM2iSS4iK7vDWI6H65xf Mel3uoj0eNOxixw/ifzXL5ZAD3NS3OgDBWkzjj+B0ZZ5lNrcQOXdRlEIU5q6eLhX18ad hzMRBde/8/KJM2VflFcadLc32SpmwNQeOkAaR5Er/oRuguZwXYU0vAf5zRjBwIw1WW0B EmcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jgcYn+YC; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ik16-20020a170902ab1000b00163852a9881si3537982plb.343.2022.07.18.15.01.03; Mon, 18 Jul 2022 15:01:18 -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=@gmail.com header.s=20210112 header.b=jgcYn+YC; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236266AbiGRVqs (ORCPT + 99 others); Mon, 18 Jul 2022 17:46:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233858AbiGRVqq (ORCPT ); Mon, 18 Jul 2022 17:46:46 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 073182125D; Mon, 18 Jul 2022 14:46:46 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id g4-20020a17090a290400b001f1f2b7379dso633957pjd.0; Mon, 18 Jul 2022 14:46:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GM8+8p1/J6+pvSGth1ereNERHh7ix8LfwtKYNnc1Yxg=; b=jgcYn+YC6i33CFfygb7e9U5GlpP/bJ0AV/VZXeqqL3Jc02Ng+sA+DrgA0/1OK4EpMo WEccNc2FoNGfHrFYx1dwY03rglWXab6oKFJ1hJGn5s56bBTyy+Pqu2qHdcgohPhvJVFh D5lP5fk5rlAKMoPSrIRPnq/XuCNppfdKD9nrGha2CsejmYvHFx70qtxR9FeqR7+f5dVt LZvG3iyj2ltfq3LxlRpi9//zrWKnGSAaBOhpmr74ITjke3uo/D5jQqq8lAAyI2xPoomY tAF9VaQK1KuB9EsxiVk9lQG4+UzynlHTgw6NYgpQ8SxS2pAsqAS/9/F2wlQH0X4Mo3C2 X0OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GM8+8p1/J6+pvSGth1ereNERHh7ix8LfwtKYNnc1Yxg=; b=sUF/IwOZC5zRRNvvNJpJCuUvfCOnuKv0ecbt3cqjbJMakkhdzJN9K1DqHZcAmrBbDi XCbJy7QrOVFODQ8oj+idl9/+EziZpoYTcvMPSy7SM2CbymfanLDd/cVeYDi6a4ne0O0T guHsM3S9GHLTwqts5Dt7l0sYPLMTwrSpdESkuEP2fgOu1CKYx5mWNjD/by6D38ricIkY b5li40j6N2lQ/w/0Y/ftksBZGJggEFf7BchD9dvSn5U/ukHnm7upxDvSSE7fd80f/XNa H07Qe6g67DsmgGTZyv2fTNcEcrKBfcPE5GdRtN5LFII1yDSIapcI/TZt8WpgLCycOgWG 2dhQ== X-Gm-Message-State: AJIora+UDWWYPzt7LKNBKsNIBli6WdlBmfjnjdvnCS8ZnmwTi0yjAsRF 7NzmUJyQZUl7XNQ9lYHL1A== X-Received: by 2002:a17:90a:f0c8:b0:1f0:671b:f594 with SMTP id fa8-20020a17090af0c800b001f0671bf594mr39853106pjb.238.1658180805505; Mon, 18 Jul 2022 14:46:45 -0700 (PDT) Received: from jevburton3.c.googlers.com.com (223.103.125.34.bc.googleusercontent.com. [34.125.103.223]) by smtp.gmail.com with ESMTPSA id u18-20020a17090341d200b0016cb98ab5b4sm9289657ple.153.2022.07.18.14.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 14:46:43 -0700 (PDT) From: Joe Burton To: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Joe Burton Subject: [PATCH] [PATCH bpf-next] libbpf: Add bpf_obj_get_opts() Date: Mon, 18 Jul 2022 21:46:33 +0000 Message-Id: <20220718214633.3951533-1-jevburton.kernel@gmail.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 From: Joe Burton Add an extensible variant of bpf_obj_get() capable of setting the `file_flags` parameter. This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall. Signed-off-by: Joe Burton --- tools/lib/bpf/bpf.c | 10 ++++++++++ tools/lib/bpf/bpf.h | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 5eb0df90eb2b..5acb0e8bd13c 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname) } int bpf_obj_get(const char *pathname) +{ + LIBBPF_OPTS(bpf_obj_get_opts, opts); + return bpf_obj_get_opts(pathname, &opts); +} + +int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts) { union bpf_attr attr; int fd; + if (!OPTS_VALID(opts, bpf_obj_get_opts)) + return libbpf_err(-EINVAL); + memset(&attr, 0, sizeof(attr)); attr.pathname = ptr_to_u64((void *)pathname); + attr.file_flags = OPTS_GET(opts, file_flags, 0); fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr)); return libbpf_err_errno(fd); diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 88a7cc4bd76f..f31b493b5f9a 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values __u32 *count, const struct bpf_map_batch_opts *opts); +struct bpf_obj_get_opts { + size_t sz; /* size of this struct for forward/backward compatibility */ + + __u32 file_flags; +}; +#define bpf_obj_get_opts__last_field file_flags + LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); LIBBPF_API int bpf_obj_get(const char *pathname); +LIBBPF_API int bpf_obj_get_opts(const char *pathname, + const struct bpf_obj_get_opts *opts); struct bpf_prog_attach_opts { size_t sz; /* size of this struct for forward/backward compatibility */ -- 2.37.0.170.g444d1eabd0-goog