public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: jeffxu@chromium.org
To: skhan@linuxfoundation.org, keescook@chromium.org
Cc: akpm@linux-foundation.org, dmitry.torokhov@gmail.com,
	dverkamp@chromium.org, hughd@google.com, jeffxu@google.com,
	jorgelo@chromium.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
	mnissler@chromium.org, jannh@google.com,
	linux-hardening@vger.kernel.org, Jeff Xu <jeffxu@chromium.org>
Subject: [PATCH v3] mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd
Date: Fri,  2 Dec 2022 01:34:04 +0000	[thread overview]
Message-ID: <20221202013404.163143-7-jeffxu@google.com> (raw)
In-Reply-To: <20221202013404.163143-1-jeffxu@google.com>

From: Jeff Xu <jeffxu@chromium.org>

When apply F_SEAL_EXEC to an executable memfd, add write seals also to
prevent modification of memfd.

Signed-off-by: Jeff Xu <jeffxu@chromium.org>
---
 mm/memfd.c                                 |  3 +++
 tools/testing/selftests/memfd/memfd_test.c | 25 ++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/mm/memfd.c b/mm/memfd.c
index 96dcfbfed09e..3a04c0698957 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -222,6 +222,9 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
 		}
 	}
 
+	if (seals & F_SEAL_EXEC && inode->i_mode & 0111)
+		seals |= F_ALL_SEALS;
+
 	*file_seals |= seals;
 	error = 0;
 
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index 775c9e6c061e..0731e5b3cdce 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -32,6 +32,13 @@
 #define F_SEAL_EXEC	0x0020
 #endif
 
+#define F_ALL_SEALS (F_SEAL_SEAL | \
+		     F_SEAL_SHRINK | \
+		     F_SEAL_GROW | \
+		     F_SEAL_WRITE | \
+		     F_SEAL_FUTURE_WRITE | \
+		     F_SEAL_EXEC)
+
 #ifndef MAX_PATH
 #define MAX_PATH 256
 #endif
@@ -1006,6 +1013,7 @@ static void test_exec_seal(void)
 
 	printf("%s SEAL-EXEC\n", memfd_str);
 
+	printf("%s	Apply SEAL_EXEC\n", memfd_str);
 	fd = mfd_assert_new("kern_memfd_seal_exec",
 			    mfd_def_size,
 			    MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_EXEC);
@@ -1024,7 +1032,24 @@ static void test_exec_seal(void)
 	mfd_fail_chmod(fd, 0700);
 	mfd_fail_chmod(fd, 0100);
 	mfd_assert_chmod(fd, 0666);
+	mfd_assert_write(fd);
+	close(fd);
+
+	printf("%s	Apply ALL_SEALS\n", memfd_str);
+	fd = mfd_assert_new("kern_memfd_seal_exec",
+			    mfd_def_size,
+			    MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_EXEC);
 
+	mfd_assert_mode(fd, 0777);
+	mfd_assert_chmod(fd, 0700);
+
+	mfd_assert_has_seals(fd, 0);
+	mfd_assert_add_seals(fd, F_SEAL_EXEC);
+	mfd_assert_has_seals(fd, F_ALL_SEALS);
+
+	mfd_fail_chmod(fd, 0711);
+	mfd_fail_chmod(fd, 0600);
+	mfd_fail_write(fd);
 	close(fd);
 }
 
-- 
2.39.0.rc0.267.gcb52ba06e7-goog


  parent reply	other threads:[~2022-12-02  1:36 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-02  1:33 [PATCH v3] mm/memfd: MFD_NOEXEC_SEAL and MFD_EXEC jeffxu
2022-12-02  1:33 ` [PATCH v3] mm/memfd: add F_SEAL_EXEC jeffxu
2022-12-02 22:43   ` Kees Cook
2022-12-02 22:45   ` Kees Cook
2022-12-02  1:34 ` [PATCH v3] mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC jeffxu
2022-12-02 11:32   ` kernel test robot
2022-12-02 13:33   ` kernel test robot
2022-12-02 13:43   ` kernel test robot
2022-12-02 22:56   ` Kees Cook
2022-12-02 23:32     ` Jeff Xu
2022-12-02  1:34 ` [PATCH v3] selftests/memfd: add tests for F_SEAL_EXEC jeffxu
2022-12-02  1:34 ` [PATCH v3] selftests/memfd: add tests for MFD_NOEXEC_SEAL MFD_EXEC jeffxu
2022-12-02  1:34 ` [PATCH v3] mm/memfd: security hook for memfd_create jeffxu
2022-12-02 10:11   ` kernel test robot
2022-12-02 12:33   ` kernel test robot
2022-12-02 22:58   ` Kees Cook
2022-12-02 23:23     ` Jeff Xu
2022-12-02  1:34 ` jeffxu [this message]
2022-12-02 23:23   ` [PATCH v3] mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd Daniel Verkamp
2022-12-03  2:29     ` Jeff Xu
2022-12-02 22:41 ` [PATCH v3] mm/memfd: MFD_NOEXEC_SEAL and MFD_EXEC Kees Cook

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=20221202013404.163143-7-jeffxu@google.com \
    --to=jeffxu@chromium.org \
    --cc=akpm@linux-foundation.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=dverkamp@chromium.org \
    --cc=hughd@google.com \
    --cc=jannh@google.com \
    --cc=jeffxu@google.com \
    --cc=jorgelo@chromium.org \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mnissler@chromium.org \
    --cc=skhan@linuxfoundation.org \
    /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