mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] mm: memfd_luo: pick up seals added after preserve at freeze
@ 2026-11-28  7:24 Long Wei
  2026-11-28  7:24 ` [PATCH 1/2] " Long Wei
  2026-11-28  7:24 ` [PATCH 2/2] selftests/liveupdate: add seal-after-preserve kexec test Long Wei
  0 siblings, 2 replies; 3+ messages in thread
From: Long Wei @ 2026-11-28  7:24 UTC (permalink / raw)
  To: rppt, pasha.tatashin, pratyush
  Cc: shuah, akpm, linux-mm, kexec, linux-kernel, linux-kselftest,
	hewenliang4, Long Wei

A seal added to a memfd after the memfd was preserved with a liveupdate
session is accepted without any error, but the seal is silently gone once
the kexec handover is over: the retrieved memfd no longer has it.

memfd_luo_preserve() snapshots the file seals, but userspace may keep
operating on the memfd until the handover, so the preserve-time snapshot
is naturally incomplete.  memfd_luo_freeze() never re-read the seals and
memfd_luo_retrieve() replays the stale snapshot, so seals added in the
window between preserve() and kexec were dropped without any notice.

Patch 1 re-reads the seals at freeze time, mirroring the file position
handling: freeze is the last snapshot point before kexec and the only
place where the final value can still be sampled and, when it diverged
from the preserve-time snapshot, validated while the live update can
still be aborted.

Patch 2 adds a kexec round-trip selftest that adds F_SEAL_GROW after
preserve and asserts that the seal survives the handover.

Tested on arm64 and x86_64 with two consecutive kexec handovers each:
with the fix the seal survives and dmesg stays free of new warnings.

This series applies on the current next branch of liveupdate.git,
where the file is still named memfd_luo.c. I am aware of the
pending "mm: Rename memfd_luo to shmem_luo" series. If that series
lands first, a trivial s/memfd_luo/shmem_luo/ rebase will be needed,
which I am happy to provide.


Long Wei (2):
  mm: memfd_luo: pick up seals added after preserve at freeze
  selftests/liveupdate: add seal-after-preserve kexec test

 mm/memfd_luo.c                                |  27 ++-
 tools/testing/selftests/liveupdate/Makefile   |   1 +
 .../liveupdate/luo_seal_after_preserve.c      | 170 ++++++++++++++++++
 .../selftests/liveupdate/run-vmtests.sh       |   1 +
 4 files changed, 197 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/liveupdate/luo_seal_after_preserve.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] mm: memfd_luo: pick up seals added after preserve at freeze
  2026-11-28  7:24 [PATCH 0/2] mm: memfd_luo: pick up seals added after preserve at freeze Long Wei
@ 2026-11-28  7:24 ` Long Wei
  2026-11-28  7:24 ` [PATCH 2/2] selftests/liveupdate: add seal-after-preserve kexec test Long Wei
  1 sibling, 0 replies; 3+ messages in thread
From: Long Wei @ 2026-11-28  7:24 UTC (permalink / raw)
  To: rppt, pasha.tatashin, pratyush
  Cc: shuah, akpm, linux-mm, kexec, linux-kernel, linux-kselftest,
	hewenliang4, Long Wei

A seal added to a memfd after the memfd was preserved with a
liveupdate session is accepted without any error, but once the kexec
handover is over the retrieved memfd no longer has it: the change
silently disappears instead of being rejected.

memfd_luo_preserve() snapshots the file seals into the serialized
state, but userspace may keep operating on the memfd until the
handover, by design, so the preserve-time snapshot is naturally
incomplete: a seal can be added at any point in the window between
preserve() and kexec.  memfd_luo_freeze() only re-read the file
position and retrieve() replays the frozen snapshot, so those window
changes were never picked up.  Freeze is the last point before kexec
where they can still be sampled; there is no second chance after it.

Re-read the seals at freeze time, mirroring the file position
handling: freeze is the final snapshot point and changes made after
it are not preserved.  The new seal set is only revalidated when it
actually diverged from the preserve-time snapshot, and the validation
must fail here rather than at retrieve(): while the old kernel is
still in control the live update can still be aborted, but after the
handover a seal that memfd_luo_retrieve() cannot replay would already
be past the point of no return.

Fixes: 8a552d68a86e ("mm: memfd_luo: preserve file seals")
Signed-off-by: Long Wei <longwei27@huawei.com>
---
 mm/memfd_luo.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index 6b68220275eb..08524125475e 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -54,6 +54,9 @@
  *   File seals set on the memfd are preserved and re-applied on restore.
  *   Only seals known to this LUO version (see ``MEMFD_LUO_ALL_SEALS``) may
  *   be present; preservation fails with ``-EOPNOTSUPP`` otherwise.
+ *   Seals added after preserve are picked up when the file is frozen for
+ *   kexec. Freeze is the final snapshot point: like the file position,
+ *   changes made after it are not preserved.
  *
  * Restrictions
  * ============
@@ -322,7 +325,9 @@ static int memfd_luo_preserve(struct liveupdate_file_op_args *args)
 
 static int memfd_luo_freeze(struct liveupdate_file_op_args *args)
 {
+	struct inode *inode = file_inode(args->file);
 	struct memfd_luo_ser *ser;
+	int seals;
 
 	if (WARN_ON_ONCE(!args->serialized_data))
 		return -EINVAL;
@@ -330,10 +335,28 @@ static int memfd_luo_freeze(struct liveupdate_file_op_args *args)
 	ser = phys_to_virt(args->serialized_data);
 
 	/*
-	 * The pos might have changed since prepare. Everything else stays the
-	 * same.
+	 * F_ADD_SEALS takes inode_lock, so sample the seals under the same
+	 * lock to get a consistent final value.  f_pos is sampled here under
+	 * the same inode_lock as preserve did; freeze is the last snapshot
+	 * point for both.
 	 */
+	inode_lock(inode);
+	seals = memfd_get_seals(args->file);
+	if (seals < 0) {
+		inode_unlock(inode);
+		return seals;
+	}
+
+	if (seals != ser->seals) {
+		if (seals & ~MEMFD_LUO_ALL_SEALS) {
+			inode_unlock(inode);
+			return -EOPNOTSUPP;
+		}
+		ser->seals = seals;
+	}
+
 	ser->pos = args->file->f_pos;
+	inode_unlock(inode);
 
 	return 0;
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] selftests/liveupdate: add seal-after-preserve kexec test
  2026-11-28  7:24 [PATCH 0/2] mm: memfd_luo: pick up seals added after preserve at freeze Long Wei
  2026-11-28  7:24 ` [PATCH 1/2] " Long Wei
@ 2026-11-28  7:24 ` Long Wei
  1 sibling, 0 replies; 3+ messages in thread
From: Long Wei @ 2026-11-28  7:24 UTC (permalink / raw)
  To: rppt, pasha.tatashin, pratyush
  Cc: shuah, akpm, linux-mm, kexec, linux-kernel, linux-kselftest,
	hewenliang4, Long Wei

Add luo_seal_after_preserve, a kexec test that preserves an unsealed
memfd, adds F_SEAL_GROW *after* the preserve (which used to be silently
lost across the handover), and verifies after the kexec reboot that
both the data and the seal survived.

The test creates its own memfd instead of using the
create_and_preserve_memfd() helper because the helper creates the memfd
without MFD_ALLOW_SEALING, which makes it unsealable forever.  It is
registered in TEST_GEN_PROGS_EXTENDED and TEST_NAMES so it runs in the
vmtest CI.

Signed-off-by: Long Wei <longwei27@huawei.com>
---
 tools/testing/selftests/liveupdate/Makefile   |   1 +
 .../liveupdate/luo_seal_after_preserve.c      | 170 ++++++++++++++++++
 .../selftests/liveupdate/run-vmtests.sh       |   1 +
 3 files changed, 172 insertions(+)
 create mode 100644 tools/testing/selftests/liveupdate/luo_seal_after_preserve.c

diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index 634211c66652..03bb09bb9867 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -6,6 +6,7 @@ TEST_GEN_PROGS_EXTENDED += luo_kexec_simple
 TEST_GEN_PROGS_EXTENDED += luo_multi_session
 TEST_GEN_PROGS_EXTENDED += luo_stress_sessions
 TEST_GEN_PROGS_EXTENDED += luo_stress_files
+TEST_GEN_PROGS_EXTENDED += luo_seal_after_preserve
 
 TEST_FILES += do_kexec.sh
 
diff --git a/tools/testing/selftests/liveupdate/luo_seal_after_preserve.c b/tools/testing/selftests/liveupdate/luo_seal_after_preserve.c
new file mode 100644
index 000000000000..12e6731c9235
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/luo_seal_after_preserve.c
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (c) 2026, Huawei.
+ * Long Wei <longwei27@huawei.com>
+ *
+ * A selftest to validate that a seal added to a memfd with F_ADD_SEALS
+ * after the memfd is preserved is transferred across a kexec reboot.
+ *
+ * Note: the create_and_preserve_memfd() helper cannot be used here because
+ * it creates the memfd without MFD_ALLOW_SEALING, which makes the memfd
+ * unsealable forever.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <libliveupdate.h>
+
+#ifndef F_ADD_SEALS
+#define F_ADD_SEALS 1033
+#endif
+#ifndef F_GET_SEALS
+#define F_GET_SEALS 1034
+#endif
+#ifndef F_SEAL_GROW
+#define F_SEAL_GROW 0x0004
+#endif
+#ifndef MFD_ALLOW_SEALING
+#define MFD_ALLOW_SEALING 0x0002U
+#endif
+
+/*
+ * Prefer SYS_fcntl when the libc defines it (glibc, musl); fall back to
+ * __NR_fcntl which is the only name nolibc provides.
+ */
+#ifdef SYS_fcntl
+#define LUO_FCNTL_NR SYS_fcntl
+#else
+#define LUO_FCNTL_NR __NR_fcntl
+#endif
+
+#define STATE_SESSION_NAME "seal_after_preserve_state"
+#define STATE_MEMFD_TOKEN  0x1000
+
+#define TEST_SESSION_NAME  "seal_after_preserve_session"
+#define TEST_MEMFD_DATA    "seal-after-preserve"
+#define TEST_MEMFD_TOKEN   0x1001
+
+/* Unsealed, non-empty memfd open for sealing. */
+static int create_sealable_memfd(void)
+{
+	long page_size = getpagesize();
+	void *map = MAP_FAILED;
+	int mfd = -1, ret = -1;
+
+	mfd = memfd_create("seal_test", MFD_ALLOW_SEALING);
+	if (mfd < 0)
+		return -errno;
+
+	if (ftruncate(mfd, page_size)) {
+		ret = -errno;
+		close(mfd);
+		return ret;
+	}
+
+	map = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, mfd, 0);
+	if (map == MAP_FAILED) {
+		ret = -errno;
+		close(mfd);
+		return ret;
+	}
+	snprintf(map, page_size, "%s", TEST_MEMFD_DATA);
+	munmap(map, page_size);
+
+	return mfd;
+}
+
+/* Stage 1: Executed before the kexec reboot. */
+static void run_stage_1(int luo_fd)
+{
+	int session_fd, mfd, seals;
+
+	ksft_print_msg("[STAGE 1] Creating state file for next stage (2)...\n");
+	create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2);
+
+	ksft_print_msg("[STAGE 1] Creating test session '%s'...\n",
+		       TEST_SESSION_NAME);
+	session_fd = luo_create_session(luo_fd, TEST_SESSION_NAME);
+	if (session_fd < 0)
+		fail_exit("luo_create_session");
+
+	ksft_print_msg("[STAGE 1] Preserving unsealed test memfd...\n");
+	mfd = create_sealable_memfd();
+	if (mfd < 0)
+		fail_exit("create unsealed sealable memfd");
+
+	if (luo_session_preserve_fd(session_fd, mfd, TEST_MEMFD_TOKEN))
+		fail_exit("preserve unsealed memfd");
+
+	if (syscall(LUO_FCNTL_NR, mfd, F_ADD_SEALS, F_SEAL_GROW) < 0)
+		fail_exit("F_ADD_SEALS(F_SEAL_GROW) after preserve");
+
+	seals = syscall(LUO_FCNTL_NR, mfd, F_GET_SEALS, 0);
+	if (seals < 0)
+		fail_exit("F_GET_SEALS");
+	if (!(seals & F_SEAL_GROW))
+		fail_exit("F_SEAL_GROW missing (F_GET_SEALS=0x%x)", seals);
+
+	ksft_print_msg("[STAGE 1] F_SEAL_GROW added after preserve (seals=0x%x).\n", seals);
+
+	close(luo_fd);
+	daemonize_and_wait();
+}
+
+/* Stage 2: Executed after the kexec reboot. */
+static void run_stage_2(int luo_fd, int state_session_fd)
+{
+	int session_fd, mfd, seals, stage;
+
+	ksft_print_msg("[STAGE 2] Starting post-kexec verification...\n");
+
+	restore_and_read_stage(state_session_fd, STATE_MEMFD_TOKEN, &stage);
+	if (stage != 2)
+		fail_exit("Expected stage 2, but state file contains %d", stage);
+
+	session_fd = luo_retrieve_session(luo_fd, TEST_SESSION_NAME);
+	if (session_fd < 0)
+		fail_exit("luo_retrieve_session");
+
+	ksft_print_msg("[STAGE 2] Restoring and verifying test memfd (token=%d)...\n",
+		       TEST_MEMFD_TOKEN);
+	mfd = restore_and_verify_memfd(session_fd, TEST_MEMFD_TOKEN,
+				       TEST_MEMFD_DATA);
+	if (mfd < 0)
+		fail_exit("restore_and_verify_memfd for test memfd");
+
+	seals = syscall(LUO_FCNTL_NR, mfd, F_GET_SEALS, 0);
+	if (seals < 0)
+		fail_exit("F_GET_SEALS");
+	if (!(seals & F_SEAL_GROW))
+		fail_exit("F_SEAL_GROW silently lost across kexec (seals=0x%x, expected 0x%x)",
+			  seals, F_SEAL_GROW);
+
+	ksft_print_msg("[STAGE 2] F_SEAL_GROW survived the kexec (seals=0x%x).\n", seals);
+
+	close(mfd);
+
+	if (luo_session_finish(session_fd) < 0)
+		fail_exit("luo_session_finish for test session");
+	close(session_fd);
+
+	ksft_print_msg("[STAGE 2] Finalizing state session...\n");
+	if (luo_session_finish(state_session_fd) < 0)
+		fail_exit("luo_session_finish for state session");
+	close(state_session_fd);
+
+	ksft_print_msg("\n--- SEAL-AFTER-PRESERVE KEXEC TEST PASSED ---\n");
+}
+
+int main(int argc, char *argv[])
+{
+	return luo_test(argc, argv, STATE_SESSION_NAME,
+			run_stage_1, run_stage_2);
+}
diff --git a/tools/testing/selftests/liveupdate/run-vmtests.sh b/tools/testing/selftests/liveupdate/run-vmtests.sh
index c3d828cfd63b..fbc853d981db 100755
--- a/tools/testing/selftests/liveupdate/run-vmtests.sh
+++ b/tools/testing/selftests/liveupdate/run-vmtests.sh
@@ -20,6 +20,7 @@ TEST_NAMES=(
 	"luo_multi_session"
 	"luo_stress_files"
 	"luo_stress_sessions"
+	"luo_seal_after_preserve"
 )
 
 function usage() {
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-28  7:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28  7:24 [PATCH 0/2] mm: memfd_luo: pick up seals added after preserve at freeze Long Wei
2026-11-28  7:24 ` [PATCH 1/2] " Long Wei
2026-11-28  7:24 ` [PATCH 2/2] selftests/liveupdate: add seal-after-preserve kexec test Long Wei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®