diff mbox series

[bpf-next] bpf: fix segmentation fault of test_progs

Message ID 20200807172016.150952-1-Jianlin.Lv@arm.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series [bpf-next] bpf: fix segmentation fault of test_progs | expand

Commit Message

Jianlin Lv Aug. 7, 2020, 5:20 p.m. UTC
test_progs reports the segmentation fault as below

$ sudo ./test_progs -t mmap --verbose
test_mmap:PASS:skel_open_and_load 0 nsec
......
test_mmap:PASS:adv_mmap1 0 nsec
test_mmap:PASS:adv_mmap2 0 nsec
test_mmap:PASS:adv_mmap3 0 nsec
test_mmap:PASS:adv_mmap4 0 nsec
Segmentation fault

This issue was triggered because mmap() and munmap() used inconsistent
length parameters; mmap() creates a new mapping of 3*page_size, but the
length parameter set in the subsequent re-map and munmap() functions is
4*page_size; this leads to the destruction of the process space.

Another issue is that when unmap the second page fails, the length
parameter to delete tmp1 mappings should be 3*page_size.

Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
---
 tools/testing/selftests/bpf/prog_tests/mmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Andrii Nakryiko Aug. 7, 2020, 8:13 p.m. UTC | #1
On Fri, Aug 7, 2020 at 10:21 AM Jianlin Lv <Jianlin.Lv@arm.com> wrote:
>
> test_progs reports the segmentation fault as below
>
> $ sudo ./test_progs -t mmap --verbose
> test_mmap:PASS:skel_open_and_load 0 nsec
> ......
> test_mmap:PASS:adv_mmap1 0 nsec
> test_mmap:PASS:adv_mmap2 0 nsec
> test_mmap:PASS:adv_mmap3 0 nsec
> test_mmap:PASS:adv_mmap4 0 nsec
> Segmentation fault
>
> This issue was triggered because mmap() and munmap() used inconsistent
> length parameters; mmap() creates a new mapping of 3*page_size, but the
> length parameter set in the subsequent re-map and munmap() functions is
> 4*page_size; this leads to the destruction of the process space.
>
> Another issue is that when unmap the second page fails, the length
> parameter to delete tmp1 mappings should be 3*page_size.
>
> Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/mmap.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/mmap.c b/tools/testing/selftests/bpf/prog_tests/mmap.c
> index 43d0b5578f46..2070cfe19cac 100644
> --- a/tools/testing/selftests/bpf/prog_tests/mmap.c
> +++ b/tools/testing/selftests/bpf/prog_tests/mmap.c
> @@ -192,7 +192,7 @@ void test_mmap(void)
>         /* unmap second page: pages 1, 3 mapped */
>         err = munmap(tmp1 + page_size, page_size);
>         if (CHECK(err, "adv_mmap2", "errno %d\n", errno)) {
> -               munmap(tmp1, map_sz);
> +               munmap(tmp1, 3 * page_size);

this is a good catch, thank you!

>                 goto cleanup;
>         }
>
> @@ -207,8 +207,8 @@ void test_mmap(void)
>         CHECK(tmp1 + page_size != tmp2, "adv_mmap4",
>               "tmp1: %p, tmp2: %p\n", tmp1, tmp2);
>
> -       /* re-map all 4 pages */
> -       tmp2 = mmap(tmp1, 4 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
> +       /* re-map all 3 pages */
> +       tmp2 = mmap(tmp1, 3 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
>                     data_map_fd, 0);

"all 3 pages" is a lie, there are 4. I'd still want to work with all 4
pages. How about we mmap() 4 pages of anonymous memory first, then do
all the mmap() with MAP_FIXED, re-using that memory range. That will
ensure that we are not stepping on any other allocated memory, right?


>         if (CHECK(tmp2 == MAP_FAILED, "adv_mmap5", "errno %d\n", errno)) {
>                 munmap(tmp1, 3 * page_size); /* unmap page 1 */
> @@ -226,7 +226,7 @@ void test_mmap(void)
>         CHECK_FAIL(map_data->val[2] != 321);
>         CHECK_FAIL(map_data->val[far] != 3 * 321);
>
> -       munmap(tmp2, 4 * page_size);
> +       munmap(tmp2, 3 * page_size);
>
>         /* map all 4 pages, but with pg_off=1 page, should fail */
>         tmp1 = mmap(NULL, 4 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
> --
> 2.17.1
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/mmap.c b/tools/testing/selftests/bpf/prog_tests/mmap.c
index 43d0b5578f46..2070cfe19cac 100644
--- a/tools/testing/selftests/bpf/prog_tests/mmap.c
+++ b/tools/testing/selftests/bpf/prog_tests/mmap.c
@@ -192,7 +192,7 @@  void test_mmap(void)
 	/* unmap second page: pages 1, 3 mapped */
 	err = munmap(tmp1 + page_size, page_size);
 	if (CHECK(err, "adv_mmap2", "errno %d\n", errno)) {
-		munmap(tmp1, map_sz);
+		munmap(tmp1, 3 * page_size);
 		goto cleanup;
 	}
 
@@ -207,8 +207,8 @@  void test_mmap(void)
 	CHECK(tmp1 + page_size != tmp2, "adv_mmap4",
 	      "tmp1: %p, tmp2: %p\n", tmp1, tmp2);
 
-	/* re-map all 4 pages */
-	tmp2 = mmap(tmp1, 4 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
+	/* re-map all 3 pages */
+	tmp2 = mmap(tmp1, 3 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
 		    data_map_fd, 0);
 	if (CHECK(tmp2 == MAP_FAILED, "adv_mmap5", "errno %d\n", errno)) {
 		munmap(tmp1, 3 * page_size); /* unmap page 1 */
@@ -226,7 +226,7 @@  void test_mmap(void)
 	CHECK_FAIL(map_data->val[2] != 321);
 	CHECK_FAIL(map_data->val[far] != 3 * 321);
 
-	munmap(tmp2, 4 * page_size);
+	munmap(tmp2, 3 * page_size);
 
 	/* map all 4 pages, but with pg_off=1 page, should fail */
 	tmp1 = mmap(NULL, 4 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,