From: "Isaac J. Manjarres" <isaacmanjarres@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Jeff Layton <jlayton@kernel.org>,
Chuck Lever <chuck.lever@oracle.com>,
Alexander Aring <alex.aring@gmail.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Vlastimil Babka <vbabka@suse.cz>, Jann Horn <jannh@google.com>,
Shuah Khan <shuah@kernel.org>
Cc: "Isaac J. Manjarres" <isaacmanjarres@google.com>,
kernel-team@android.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
Suren Baghdasaryan <surenb@google.com>,
Kalesh Singh <kaleshsingh@google.com>,
John Stultz <jstultz@google.com>
Subject: [RFC PATCH v1 2/2] selftests/memfd: Add tests for F_SEAL_FUTURE_EXEC
Date: Thu, 5 Dec 2024 17:09:23 -0800 [thread overview]
Message-ID: <20241206010930.3871336-3-isaacmanjarres@google.com> (raw)
In-Reply-To: <20241206010930.3871336-1-isaacmanjarres@google.com>
Add tests to ensure that F_SEAL_FUTURE_EXEC behaves as expected.
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: John Stultz <jstultz@google.com>
Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com>
---
tools/testing/selftests/memfd/memfd_test.c | 79 ++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index 46027c889e74..12c82af406b3 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -30,6 +30,7 @@
#define STACK_SIZE 65536
#define F_SEAL_EXEC 0x0020
+#define F_SEAL_FUTURE_EXEC 0x0040
#define F_WX_SEALS (F_SEAL_SHRINK | \
F_SEAL_GROW | \
@@ -317,6 +318,37 @@ static void *mfd_assert_mmap_private(int fd)
return p;
}
+static void *mfd_fail_mmap_exec(int fd)
+{
+ void *p;
+
+ p = mmap(NULL,
+ mfd_def_size,
+ PROT_EXEC,
+ MAP_SHARED,
+ fd,
+ 0);
+ if (p != MAP_FAILED) {
+ printf("mmap() didn't fail as expected\n");
+ abort();
+ }
+
+ return p;
+}
+
+static void mfd_fail_mprotect_exec(void *p)
+{
+ int ret;
+
+ ret = mprotect(p,
+ mfd_def_size,
+ PROT_EXEC);
+ if (!ret) {
+ printf("mprotect didn't fail as expected\n");
+ abort();
+ }
+}
+
static int mfd_assert_open(int fd, int flags, mode_t mode)
{
char buf[512];
@@ -997,6 +1029,52 @@ static void test_seal_future_write(void)
close(fd);
}
+/*
+ * Test SEAL_FUTURE_EXEC_MAPPING
+ * Test whether SEAL_FUTURE_EXEC_MAPPING actually prevents executable mappings.
+ */
+static void test_seal_future_exec_mapping(void)
+{
+ int fd;
+ void *p;
+
+
+ printf("%s SEAL-FUTURE-EXEC-MAPPING\n", memfd_str);
+
+ fd = mfd_assert_new("kern_memfd_seal_future_exec_mapping",
+ mfd_def_size,
+ MFD_CLOEXEC | MFD_ALLOW_SEALING);
+
+ /*
+ * PROT_READ | PROT_WRITE mappings create VMAs with VM_MAYEXEC set.
+ * However, F_SEAL_FUTURE_EXEC applies to subsequent mappings,
+ * so it should still succeed even if this mapping is active when the
+ * seal is applied.
+ */
+ p = mfd_assert_mmap_shared(fd);
+
+ mfd_assert_has_seals(fd, 0);
+
+ mfd_assert_add_seals(fd, F_SEAL_FUTURE_EXEC);
+ mfd_assert_has_seals(fd, F_SEAL_FUTURE_EXEC);
+
+ mfd_fail_mmap_exec(fd);
+
+ munmap(p, mfd_def_size);
+
+ /* Ensure that new mappings without PROT_EXEC work. */
+ p = mfd_assert_mmap_shared(fd);
+
+ /*
+ * Ensure that mappings created after the seal was applied cannot be
+ * made executable via mprotect().
+ */
+ mfd_fail_mprotect_exec(p);
+
+ munmap(p, mfd_def_size);
+ close(fd);
+}
+
static void test_seal_write_map_read_shared(void)
{
int fd;
@@ -1633,6 +1711,7 @@ int main(int argc, char **argv)
test_seal_shrink();
test_seal_grow();
test_seal_resize();
+ test_seal_future_exec_mapping();
test_sysctl_simple();
test_sysctl_nested();
--
2.47.0.338.g60cca15819-goog
prev parent reply other threads:[~2024-12-06 1:09 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-06 1:09 [RFC PATCH v1 0/2] Add file seal to prevent future exec mappings Isaac J. Manjarres
2024-12-06 1:09 ` [RFC PATCH v1 1/2] mm/memfd: Add support for F_SEAL_FUTURE_EXEC to memfd Isaac J. Manjarres
2024-12-06 17:49 ` Kalesh Singh
2024-12-06 20:50 ` Isaac Manjarres
2024-12-06 18:19 ` Lorenzo Stoakes
2024-12-06 20:48 ` Isaac Manjarres
2024-12-06 21:14 ` Lorenzo Stoakes
2024-12-11 20:56 ` Isaac Manjarres
2025-01-03 15:13 ` Jann Horn
2025-01-06 18:26 ` Jeff Xu
2025-01-07 0:44 ` Kees Cook
2025-01-08 19:06 ` Lorenzo Stoakes
2025-01-08 22:07 ` Kees Cook
2025-01-09 23:30 ` Jeff Xu
2025-01-14 20:02 ` Isaac Manjarres
2025-01-14 21:29 ` Kees Cook
2025-01-14 22:42 ` Isaac Manjarres
2025-01-14 23:41 ` Jeff Xu
2025-01-14 23:56 ` Jeff Xu
2024-12-06 1:09 ` Isaac J. Manjarres [this message]
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=20241206010930.3871336-3-isaacmanjarres@google.com \
--to=isaacmanjarres@google.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=alex.aring@gmail.com \
--cc=chuck.lever@oracle.com \
--cc=jannh@google.com \
--cc=jlayton@kernel.org \
--cc=jstultz@google.com \
--cc=kaleshsingh@google.com \
--cc=kernel-team@android.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@suse.cz \
/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