From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41C2D25103 for ; Fri, 3 Nov 2023 19:06:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=none Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BF27D51 for ; Fri, 3 Nov 2023 12:06:07 -0700 (PDT) Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3A3IwfDs026399 for ; Fri, 3 Nov 2023 12:06:06 -0700 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3u56kxg2qf-12 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 03 Nov 2023 12:06:06 -0700 Received: from twshared44805.48.prn1.facebook.com (2620:10d:c0a8:1b::2d) by mail.thefacebook.com (2620:10d:c0a8:83::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 3 Nov 2023 12:06:04 -0700 Received: by devbig019.vll3.facebook.com (Postfix, from userid 137359) id EB1183AE3848C; Fri, 3 Nov 2023 12:05:53 -0700 (PDT) From: Andrii Nakryiko To: , , , CC: , , , , Subject: [PATCH v9 bpf-next 14/17] libbpf: add BPF token support to bpf_btf_load() API Date: Fri, 3 Nov 2023 12:05:20 -0700 Message-ID: <20231103190523.6353-15-andrii@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231103190523.6353-1-andrii@kernel.org> References: <20231103190523.6353-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-ORIG-GUID: 8OLViaaGYgSF170pwFZ3yBLGBiQhm3pG X-Proofpoint-GUID: 8OLViaaGYgSF170pwFZ3yBLGBiQhm3pG X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-03_18,2023-11-02_03,2023-05-22_02 Allow user to specify token_fd for bpf_btf_load() API that wraps kernel's BPF_BTF_LOAD command. This allows loading BTF from unprivileged process as long as it has BPF token allowing BPF_BTF_LOAD command, which can be created and delegated by privileged process. Signed-off-by: Andrii Nakryiko --- tools/lib/bpf/bpf.c | 4 +++- tools/lib/bpf/bpf.h | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 91d45af3e9cd..4b15d20347ac 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -1184,7 +1184,7 @@ int bpf_raw_tracepoint_open(const char *name, int p= rog_fd) =20 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_l= oad_opts *opts) { - const size_t attr_sz =3D offsetofend(union bpf_attr, btf_log_true_size)= ; + const size_t attr_sz =3D offsetofend(union bpf_attr, btf_token_fd); union bpf_attr attr; char *log_buf; size_t log_size; @@ -1209,6 +1209,8 @@ int bpf_btf_load(const void *btf_data, size_t btf_s= ize, struct bpf_btf_load_opts =20 attr.btf =3D ptr_to_u64(btf_data); attr.btf_size =3D btf_size; + attr.btf_token_fd =3D OPTS_GET(opts, token_fd, 0); + /* log_level =3D=3D 0 and log_buf !=3D NULL means "try loading without * log_buf, but retry with log_buf and log_level=3D1 on error", which i= s * consistent across low-level and high-level BTF and program loading diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 26018aab839c..323bc04d8082 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -133,9 +133,10 @@ struct bpf_btf_load_opts { * If kernel doesn't support this feature, log_size is left unchanged. */ __u32 log_true_size; + __u32 token_fd; size_t :0; }; -#define bpf_btf_load_opts__last_field log_true_size +#define bpf_btf_load_opts__last_field token_fd =20 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts); --=20 2.34.1