From: Roberto Sassu <roberto.sassu@huaweicloud.com> To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, shuah@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, jakub@cloudflare.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, houtao1@huawei.com, Roberto Sassu <roberto.sassu@huawei.com> Subject: [PATCH 6/7] selftests/bpf: Ensure fd modes are checked for map iters and destroy links Date: Tue, 6 Sep 2022 19:03:00 +0200 [thread overview] Message-ID: <20220906170301.256206-7-roberto.sassu@huaweicloud.com> (raw) In-Reply-To: <20220906170301.256206-1-roberto.sassu@huaweicloud.com> From: Roberto Sassu <roberto.sassu@huawei.com> Add an additional check in do_read_map_iter_fd(), to ensure that map iterators requiring read-write access to a map cannot be created when they receive as input a read-only fd. Do it for array maps, sk storage maps and sock maps. Allowing that operation could result in a map update operation not authorized by LSMs (since they were asked for read-only access). Finally, destroy the link when it is not supposed to be created. Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com> --- .../selftests/bpf/prog_tests/bpf_iter.c | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index e89685bd587c..b2d067d38f47 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -72,10 +72,38 @@ static void do_read_map_iter_fd(struct bpf_object_skeleton **skel, struct bpf_pr struct bpf_map *map) { DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + struct bpf_map_info info_m = { 0 }; + __u32 info_m_len = sizeof(info_m); union bpf_iter_link_info linfo; struct bpf_link *link; char buf[16] = {}; int iter_fd, len; + int ret, fd; + + DECLARE_LIBBPF_OPTS(bpf_get_fd_opts, fd_opts_rdonly, + .open_flags = BPF_F_RDONLY, + ); + + ret = bpf_obj_get_info_by_fd(bpf_map__fd(map), &info_m, &info_m_len); + if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd")) + return; + + fd = bpf_map_get_fd_by_id_opts(info_m.id, &fd_opts_rdonly); + if (!ASSERT_GE(fd, 0, "bpf_map_get_fd_by_id_opts")) + return; + + memset(&linfo, 0, sizeof(linfo)); + linfo.map.map_fd = fd; + opts.link_info = &linfo; + opts.link_info_len = sizeof(linfo); + link = bpf_program__attach_iter(prog, &opts); + + close(fd); + + if (!ASSERT_ERR_PTR(link, "attach_map_iter")) { + bpf_link__destroy(link); + return; + } memset(&linfo, 0, sizeof(linfo)); linfo.map.map_fd = bpf_map__fd(map); @@ -656,12 +684,12 @@ static void test_bpf_hash_map(void) opts.link_info_len = sizeof(linfo); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); if (!ASSERT_ERR_PTR(link, "attach_iter")) - goto out; + goto free_link; linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); if (!ASSERT_ERR_PTR(link, "attach_iter")) - goto out; + goto free_link; /* hashmap1 should be good, update map values here */ map_fd = bpf_map__fd(skel->maps.hashmap1); @@ -683,7 +711,7 @@ static void test_bpf_hash_map(void) linfo.map.map_fd = map_fd; link = bpf_program__attach_iter(skel->progs.sleepable_dummy_dump, &opts); if (!ASSERT_ERR_PTR(link, "attach_sleepable_prog_to_iter")) - goto out; + goto free_link; linfo.map.map_fd = map_fd; link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); -- 2.25.1
WARNING: multiple messages have this Message-ID (diff)
From: Roberto Sassu <roberto.sassu-XF6JlduFytWkHkcT6e4Xnw@public.gmane.org> To: ast-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, daniel-FeC+5ew28dpmcu3hnIyYJQ@public.gmane.org, andrii-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, martin.lau-fxUVXftIFDnyG1zEObXtfA@public.gmane.org, song-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, yhs-b10kYP2dOMg@public.gmane.org, john.fastabend-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, kpsingh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, sdf-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, haoluo-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, jolsa-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, mykolal-b10kYP2dOMg@public.gmane.org, shuah-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, davem-fT/PcQaiUtIeIZ0/mPfg9Q@public.gmane.org, edumazet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, kuba-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, pabeni-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org, jakub-lDpJ742SOEtZroRs9YW3xA@public.gmane.org Cc: bpf-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, netdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-kselftest-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, houtao1-hv44wF8Li93QT0dZR+AlfA@public.gmane.org, Roberto Sassu <roberto.sassu-hv44wF8Li93QT0dZR+AlfA@public.gmane.org> Subject: [PATCH 6/7] selftests/bpf: Ensure fd modes are checked for map iters and destroy links Date: Tue, 6 Sep 2022 19:03:00 +0200 [thread overview] Message-ID: <20220906170301.256206-7-roberto.sassu@huaweicloud.com> (raw) In-Reply-To: <20220906170301.256206-1-roberto.sassu-XF6JlduFytWkHkcT6e4Xnw@public.gmane.org> From: Roberto Sassu <roberto.sassu-hv44wF8Li93QT0dZR+AlfA@public.gmane.org> Add an additional check in do_read_map_iter_fd(), to ensure that map iterators requiring read-write access to a map cannot be created when they receive as input a read-only fd. Do it for array maps, sk storage maps and sock maps. Allowing that operation could result in a map update operation not authorized by LSMs (since they were asked for read-only access). Finally, destroy the link when it is not supposed to be created. Signed-off-by: Roberto Sassu <roberto.sassu-hv44wF8Li93QT0dZR+AlfA@public.gmane.org> --- .../selftests/bpf/prog_tests/bpf_iter.c | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index e89685bd587c..b2d067d38f47 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -72,10 +72,38 @@ static void do_read_map_iter_fd(struct bpf_object_skeleton **skel, struct bpf_pr struct bpf_map *map) { DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + struct bpf_map_info info_m = { 0 }; + __u32 info_m_len = sizeof(info_m); union bpf_iter_link_info linfo; struct bpf_link *link; char buf[16] = {}; int iter_fd, len; + int ret, fd; + + DECLARE_LIBBPF_OPTS(bpf_get_fd_opts, fd_opts_rdonly, + .open_flags = BPF_F_RDONLY, + ); + + ret = bpf_obj_get_info_by_fd(bpf_map__fd(map), &info_m, &info_m_len); + if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd")) + return; + + fd = bpf_map_get_fd_by_id_opts(info_m.id, &fd_opts_rdonly); + if (!ASSERT_GE(fd, 0, "bpf_map_get_fd_by_id_opts")) + return; + + memset(&linfo, 0, sizeof(linfo)); + linfo.map.map_fd = fd; + opts.link_info = &linfo; + opts.link_info_len = sizeof(linfo); + link = bpf_program__attach_iter(prog, &opts); + + close(fd); + + if (!ASSERT_ERR_PTR(link, "attach_map_iter")) { + bpf_link__destroy(link); + return; + } memset(&linfo, 0, sizeof(linfo)); linfo.map.map_fd = bpf_map__fd(map); @@ -656,12 +684,12 @@ static void test_bpf_hash_map(void) opts.link_info_len = sizeof(linfo); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); if (!ASSERT_ERR_PTR(link, "attach_iter")) - goto out; + goto free_link; linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); if (!ASSERT_ERR_PTR(link, "attach_iter")) - goto out; + goto free_link; /* hashmap1 should be good, update map values here */ map_fd = bpf_map__fd(skel->maps.hashmap1); @@ -683,7 +711,7 @@ static void test_bpf_hash_map(void) linfo.map.map_fd = map_fd; link = bpf_program__attach_iter(skel->progs.sleepable_dummy_dump, &opts); if (!ASSERT_ERR_PTR(link, "attach_sleepable_prog_to_iter")) - goto out; + goto free_link; linfo.map.map_fd = map_fd; link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); -- 2.25.1
next prev parent reply other threads:[~2022-09-06 17:41 UTC|newest] Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-09-06 17:02 [PATCH 0/7] bpf: Add fd modes check for map iter and extend libbpf Roberto Sassu 2022-09-06 17:02 ` [PATCH 1/7] bpf: Add missing fd modes check for map iterators Roberto Sassu 2022-09-06 18:21 ` Alexei Starovoitov 2022-09-06 18:21 ` Alexei Starovoitov 2022-09-07 8:02 ` Roberto Sassu 2022-09-07 8:02 ` Roberto Sassu 2022-09-07 16:02 ` Alexei Starovoitov 2022-09-07 16:02 ` Alexei Starovoitov 2022-09-08 13:58 ` Roberto Sassu 2022-09-08 13:58 ` Roberto Sassu 2022-09-08 15:17 ` Alexei Starovoitov 2022-09-08 15:17 ` Alexei Starovoitov 2022-09-06 17:02 ` [PATCH 2/7] libbpf: Define bpf_get_fd_opts and introduce bpf_map_get_fd_by_id_opts() Roberto Sassu 2022-09-06 17:02 ` Roberto Sassu 2022-09-06 17:02 ` [PATCH 3/7] libbpf: Introduce bpf_prog_get_fd_by_id_opts() Roberto Sassu 2022-09-06 17:02 ` [PATCH 4/7] libbpf: Introduce bpf_btf_get_fd_by_id_opts() Roberto Sassu 2022-09-06 17:02 ` [PATCH 5/7] libbpf: Introduce bpf_link_get_fd_by_id_opts() Roberto Sassu 2022-09-06 17:03 ` Roberto Sassu [this message] 2022-09-06 17:03 ` [PATCH 6/7] selftests/bpf: Ensure fd modes are checked for map iters and destroy links Roberto Sassu 2022-09-06 17:03 ` [PATCH 7/7] selftests/bpf: Add tests for _opts variants of libbpf Roberto Sassu 2022-09-06 17:03 ` Roberto Sassu
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220906170301.256206-7-roberto.sassu@huaweicloud.com \ --to=roberto.sassu@huaweicloud.com \ --cc=andrii@kernel.org \ --cc=ast@kernel.org \ --cc=bpf@vger.kernel.org \ --cc=cgroups@vger.kernel.org \ --cc=daniel@iogearbox.net \ --cc=davem@davemloft.net \ --cc=edumazet@google.com \ --cc=haoluo@google.com \ --cc=houtao1@huawei.com \ --cc=jakub@cloudflare.com \ --cc=john.fastabend@gmail.com \ --cc=jolsa@kernel.org \ --cc=kpsingh@kernel.org \ --cc=kuba@kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-kselftest@vger.kernel.org \ --cc=martin.lau@linux.dev \ --cc=mykolal@fb.com \ --cc=netdev@vger.kernel.org \ --cc=pabeni@redhat.com \ --cc=roberto.sassu@huawei.com \ --cc=sdf@google.com \ --cc=shuah@kernel.org \ --cc=song@kernel.org \ --cc=yhs@fb.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.