[bpf,2/2] selftests/bpf: validate frozen map contents stays frozen

Message ID 20200410000425.2597887-2-andriin@fb.com
State New
Headers show
Series
  • Untitled series #42402
Related show

Commit Message

Andrii Nakryiko April 10, 2020, 12:04 a.m.
Test that frozen and mmap()'ed BPF map can't be mprotect()'ed as writable or
executable memory.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/mmap.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/mmap.c b/tools/testing/selftests/bpf/prog_tests/mmap.c
index 16a814eb4d64..1cdb738346a5 100644
--- a/tools/testing/selftests/bpf/prog_tests/mmap.c
+++ b/tools/testing/selftests/bpf/prog_tests/mmap.c
@@ -140,6 +140,22 @@  void test_mmap(void)
 		goto cleanup;
 	}
 
+	map_mmaped = mmap(NULL, map_sz, PROT_READ, MAP_SHARED, data_map_fd, 0);
+	if (CHECK(map_mmaped == MAP_FAILED, "data_mmap",
+		  "data_map R/O mmap failed: %d\n", errno)) {
+		map_mmaped = NULL;
+		goto cleanup;
+	}
+	err = mprotect(map_mmaped, map_sz, PROT_WRITE);
+	if (CHECK(!err, "mprotect_wr", "mprotect() succeeded unexpectedly!\n"))
+		goto cleanup;
+	err = mprotect(map_mmaped, map_sz, PROT_EXEC);
+	if (CHECK(!err, "mprotect_ex", "mprotect() succeeded unexpectedly!\n"))
+		goto cleanup;
+	err = munmap(map_mmaped, map_sz);
+	CHECK_FAIL(err);
+	map_mmaped = NULL;
+
 	bss_data->in_val = 321;
 	usleep(1);
 	CHECK_FAIL(bss_data->in_val != 321);