From: Joey Gouly <joey.gouly@arm.com>
To: "Catalin Marinas" <catalin.marinas@arm.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Lennart Poettering" <lennart@poettering.net>,
"Zbigniew Jędrzejewski-Szmek" <zbyszek@in.waw.pl>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
Kees Cook <keescook@chromium.org>,
Szabolcs Nagy <szabolcs.nagy@arm.com>,
Mark Brown <broonie@kernel.org>,
Jeremy Linton <jeremy.linton@arm.com>,
Topi Miettinen <toiwoton@gmail.com>, <linux-mm@kvack.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-kernel@vger.kernel.org>,
<linux-abi-devel@lists.sourceforge.net>, <nd@arm.com>,
<joey.gouly@arm.com>, <shuah@kernel.org>
Subject: [PATCH v1 2/2] kselftest: vm: add tests for memory-deny-write-execute
Date: Wed, 26 Oct 2022 16:04:57 +0100 [thread overview]
Message-ID: <20221026150457.36957-3-joey.gouly@arm.com> (raw)
In-Reply-To: <20221026150457.36957-1-joey.gouly@arm.com>
Add some tests to cover the new PR_SET_MDWE prctl.
Signed-off-by: Joey Gouly <joey.gouly@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
---
tools/testing/selftests/vm/mdwe_test.c | 194 +++++++++++++++++++++++++
1 file changed, 194 insertions(+)
create mode 100644 tools/testing/selftests/vm/mdwe_test.c
diff --git a/tools/testing/selftests/vm/mdwe_test.c b/tools/testing/selftests/vm/mdwe_test.c
new file mode 100644
index 000000000000..67f3fc06d069
--- /dev/null
+++ b/tools/testing/selftests/vm/mdwe_test.c
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <asm/hwcap.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/auxv.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include <linux/prctl.h>
+
+#include "../kselftest.h"
+
+#define PR_SET_MDWE 65
+# define PR_MDWE_FLAG_MMAP 1
+
+#define PR_GET_MDWE 66
+
+#ifdef __aarch64__
+#define PROT_BTI 0x10 /* BTI guarded page */
+#endif
+
+#define TEST1 "mmap(PROT_WRITE | PROT_EXEC)\n"
+#define TEST2 "mmap(PROT_WRITE); mprotect(PROT_EXEC)\n"
+#define TEST3 "mmap(PROT_EXEC); mprotect(PROT_EXEC | PROT_READ)\n"
+#define TEST4 "mmap(PROT_EXEC); mprotect(PROT_EXEC | PROT_BTI)\n"
+
+int fork_test(int (*func)(int))
+{
+ pid_t pid;
+ int status;
+
+ pid = fork();
+ if (pid < 0) {
+ printf("fork failed\n");
+ return KSFT_FAIL;
+ }
+
+ if (pid == 0)
+ exit(func(1));
+
+ waitpid(pid, &status, 0);
+
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+
+ return 0;
+}
+
+static inline void test_result(int err, const char *msg)
+{
+ switch (err) {
+ case KSFT_PASS:
+ ksft_test_result_pass(msg);
+ break;
+ case KSFT_FAIL:
+ ksft_test_result_fail(msg);
+ break;
+ case KSFT_SKIP:
+ ksft_test_result_skip(msg);
+ break;
+ default:
+ ksft_test_result_error("Unknown return code %d from %s",
+ err, msg);
+ break;
+ }
+}
+
+int test1(int mdwe_enabled)
+{
+ void *p;
+
+ int size = getpagesize();
+ int mmap_flags = MAP_SHARED | MAP_ANONYMOUS;
+
+ p = mmap(0, size, PROT_WRITE | PROT_EXEC, mmap_flags, 0, 0);
+
+ if (mdwe_enabled)
+ return p == MAP_FAILED ? KSFT_PASS : KSFT_FAIL;
+ else
+ return p != MAP_FAILED ? KSFT_PASS : KSFT_FAIL;
+}
+
+int test2(int mdwe_enabled)
+{
+ void *p;
+ int ret;
+
+ int size = getpagesize();
+ int mmap_flags = MAP_SHARED | MAP_ANONYMOUS;
+
+ p = mmap(0, size, PROT_WRITE, mmap_flags, 0, 0);
+ if (p == MAP_FAILED)
+ return 0;
+ ret = mprotect(p, size, PROT_EXEC);
+
+ if (mdwe_enabled)
+ return ret < 0 ? KSFT_PASS : KSFT_FAIL;
+ else
+ return ret == 0 ? KSFT_PASS : KSFT_FAIL;
+}
+
+int test3(int mdwe_enabled)
+{
+ void *p;
+ int ret;
+
+ int size = getpagesize();
+ int mmap_flags = MAP_SHARED | MAP_ANONYMOUS;
+
+ p = mmap(0, size, PROT_EXEC, mmap_flags, 0, 0);
+ if (p == MAP_FAILED)
+ return 0;
+
+ ret = mprotect(p, size, PROT_EXEC | PROT_READ);
+
+ return ret == 0 ? KSFT_PASS : KSFT_FAIL;
+}
+
+#ifdef __aarch64__
+int test4(int mdwe_enabled)
+{
+ void *p;
+ int ret;
+
+ int size = getpagesize();
+ int mmap_flags = MAP_SHARED | MAP_ANONYMOUS;
+
+ if (!(getauxval(AT_HWCAP2) & HWCAP2_BTI))
+ return KSFT_SKIP;
+
+ p = mmap(0, size, PROT_EXEC, mmap_flags, 0, 0);
+ if (p == MAP_FAILED)
+ return KSFT_FAIL;
+
+ ret = mprotect(p, size, PROT_EXEC | PROT_BTI);
+
+ return ret == 0 ? KSFT_PASS : KSFT_FAIL;
+}
+#endif
+
+int main(void)
+{
+ int ret;
+
+ ksft_print_header();
+#ifdef __aarch64__
+ ksft_set_plan(12);
+#else
+ ksft_set_plan(9);
+#endif
+
+ // First run the tests without MDWE
+ test_result(test1(0), TEST1);
+ test_result(test2(0), TEST2);
+ test_result(test3(0), TEST3);
+#ifdef __aarch64__
+ test_result(test4(0), TEST4);
+#endif
+
+ // Enable MDWE and then run the tests again.
+ ret = prctl(PR_SET_MDWE, PR_MDWE_FLAG_MMAP, 0, 0, 0);
+ if (ret < 0) {
+ ksft_print_msg("PR_SET_MDWE failed or unsupported!\n");
+ goto exit;
+ }
+
+ ret = prctl(PR_GET_MDWE, PR_MDWE_FLAG_MMAP, 0, 0, 0);
+ if (ret == 0)
+ ksft_exit_fail_msg("PR_GET_MDWE failed!");
+
+ test_result(test1(1), "MDWE: " TEST1);
+ test_result(test2(1), "MDWE: " TEST2);
+ test_result(test3(1), "MDWE: " TEST3);
+#ifdef __aarch64__
+ test_result(test4(1), "MDWE: " TEST4);
+#endif
+
+ // Verify the MDWE setting is transferred when fork()ing
+ test_result(fork_test(test1), "MDWE+fork: " TEST1);
+ test_result(fork_test(test2), "MDWE+fork: " TEST2);
+ test_result(fork_test(test3), "MDWE+fork: " TEST3);
+#ifdef __aarch64__
+ test_result(fork_test(test4), "MDWE+fork: " TEST4);
+#endif
+
+exit:
+ ksft_finished();
+
+ return 0;
+}
+
--
2.17.1
next prev parent reply other threads:[~2022-10-26 15:08 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-26 15:04 [PATCH v1 0/2] mm: In-kernel support for memory-deny-write-execute (MDWE) Joey Gouly
2022-10-26 15:04 ` [PATCH v1 1/2] mm: Implement memory-deny-write-execute as a prctl Joey Gouly
2022-10-28 18:51 ` Kees Cook
2022-11-10 11:27 ` Joey Gouly
2022-11-10 12:03 ` Catalin Marinas
2022-11-12 6:11 ` Topi Miettinen
2022-11-15 15:35 ` Catalin Marinas
2022-11-15 19:31 ` Topi Miettinen
2022-10-26 15:04 ` Joey Gouly [this message]
2022-10-28 17:03 ` [PATCH v1 2/2] kselftest: vm: add tests for memory-deny-write-execute Mark Brown
2022-11-08 17:33 ` Joey Gouly
2022-11-09 13:33 ` Mark Brown
2022-10-28 17:45 ` Kees Cook
2022-10-28 20:16 ` Kees Cook
2022-11-07 12:23 ` Szabolcs Nagy
2022-10-28 20:19 ` Kees Cook
2022-11-06 19:42 ` [PATCH v1 0/2] mm: In-kernel support for memory-deny-write-execute (MDWE) Topi Miettinen
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=20221026150457.36957-3-joey.gouly@arm.com \
--to=joey.gouly@arm.com \
--cc=akpm@linux-foundation.org \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=jeremy.linton@arm.com \
--cc=keescook@chromium.org \
--cc=lennart@poettering.net \
--cc=linux-abi-devel@lists.sourceforge.net \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nd@arm.com \
--cc=shuah@kernel.org \
--cc=szabolcs.nagy@arm.com \
--cc=toiwoton@gmail.com \
--cc=viro@zeniv.linux.org.uk \
--cc=zbyszek@in.waw.pl \
/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: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).