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 323E41C769 for ; Wed, 21 Jun 2023 23:41:13 +0000 (UTC) Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF7521721 for ; Wed, 21 Jun 2023 16:41:11 -0700 (PDT) Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35LKHCb9019329 for ; Wed, 21 Jun 2023 16:41:11 -0700 Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3rc04w5r7m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 21 Jun 2023 16:41:11 -0700 Received: from twshared58712.02.prn6.facebook.com (2620:10d:c085:208::11) by mail.thefacebook.com (2620:10d:c085:11d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Wed, 21 Jun 2023 16:41:10 -0700 Received: by devbig019.vll3.facebook.com (Postfix, from userid 137359) id 4AE7B333E88A8; Wed, 21 Jun 2023 16:38:22 -0700 (PDT) From: Andrii Nakryiko To: CC: , , , , , , , Subject: [PATCH v3 bpf-next 06/14] selftests/bpf: add BPF token-enabled test for BPF_MAP_CREATE command Date: Wed, 21 Jun 2023 16:38:01 -0700 Message-ID: <20230621233809.1941811-7-andrii@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230621233809.1941811-1-andrii@kernel.org> References: <20230621233809.1941811-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: bpf@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-GUID: 4pOaFbLWLpY77K8GKoAQl7ilKPyk7HxI X-Proofpoint-ORIG-GUID: 4pOaFbLWLpY77K8GKoAQl7ilKPyk7HxI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-06-21_13,2023-06-16_01,2023-05-22_02 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Add test for creating BPF token with support for BPF_MAP_CREATE delegation. And validate that its allowed_map_types filter works as expected and allows to create privileged BPF maps through delegated token, as long as they are allowed by privileged creator of a token. Signed-off-by: Andrii Nakryiko --- .../testing/selftests/bpf/prog_tests/token.c | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testi= ng/selftests/bpf/prog_tests/token.c index 153c4e26ef6b..0f832f9178a2 100644 --- a/tools/testing/selftests/bpf/prog_tests/token.c +++ b/tools/testing/selftests/bpf/prog_tests/token.c @@ -89,8 +89,63 @@ static void subtest_token_create(void) ASSERT_OK(restore_priv_caps(old_caps), "restore_caps"); } =20 +static void subtest_map_token(void) +{ + LIBBPF_OPTS(bpf_token_create_opts, token_opts); + LIBBPF_OPTS(bpf_map_create_opts, map_opts); + int token_fd =3D 0, map_fd =3D 0, err; + __u64 old_caps =3D 0; + + /* check that it's ok to allow any map type */ + token_opts.allowed_map_types =3D ~0ULL; /* any current and future map t= ypes is allowed */ + err =3D bpf_token_create(-EBADF, TOKEN_PATH, &token_opts); + if (!ASSERT_OK(err, "token_create_future_proof")) + return; + unlink(TOKEN_PATH); + + /* create BPF token allowing STACK, but not QUEUE map */ + token_opts.allowed_cmds =3D 1ULL << BPF_MAP_CREATE; + token_opts.allowed_map_types =3D 1ULL << BPF_MAP_TYPE_STACK; /* but not= QUEUE */ + err =3D bpf_token_create(-EBADF, TOKEN_PATH, &token_opts); + if (!ASSERT_OK(err, "token_create")) + return; + + /* drop privileges to test token_fd passing */ + if (!ASSERT_OK(drop_priv_caps(&old_caps), "drop_caps")) + goto cleanup; + + token_fd =3D bpf_obj_get(TOKEN_PATH); + if (!ASSERT_GT(token_fd, 0, "token_get")) + goto cleanup; + + /* BPF_MAP_TYPE_STACK is privileged, but with given token_fd should suc= ceed */ + map_opts.token_fd =3D token_fd; + map_fd =3D bpf_map_create(BPF_MAP_TYPE_STACK, "token_stack", 0, 8, 1, &= map_opts); + if (!ASSERT_GT(map_fd, 0, "stack_map_fd")) + goto cleanup; + close(map_fd); + map_fd =3D 0; + + /* BPF_MAP_TYPE_QUEUE is privileged, and token doesn't allow it, so sho= uld fail */ + map_opts.token_fd =3D token_fd; + map_fd =3D bpf_map_create(BPF_MAP_TYPE_QUEUE, "token_queue", 0, 8, 1, &= map_opts); + if (!ASSERT_EQ(map_fd, -EPERM, "queue_map_fd")) + goto cleanup; + +cleanup: + if (map_fd > 0) + close(map_fd); + if (token_fd) + close(token_fd); + unlink(TOKEN_PATH); + if (old_caps) + ASSERT_OK(restore_priv_caps(old_caps), "restore_caps"); +} + void test_token(void) { if (test__start_subtest("token_create")) subtest_token_create(); + if (test__start_subtest("map_token")) + subtest_map_token(); } --=20 2.34.1