Skip to contentSkip to conversation

mm: memfd_luo: pick up seals added after preserve at freeze

PATCH New Long Wei 2 participants 4 messages Nov 30

Subsystems (4)
lore
Share & tools
lore

Long Wei

author
#0 lore raw

v2 cover mm: memfd_luo: pick up seals added after preserve at freeze

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.

Changes in v2:
- Add an __NR_fcntl64 fallback to LUO_FCNTL_NR in the selftest so it also builds on 32-bit architectures using the generic syscall table (riscv32, csky, nios2), where __NR_fcntl does not exist.

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
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 mm: memfd_luo: pick up seals added after preserve at freeze

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")
---
 mm/memfd_luo.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
mm/memfd_luo.c +25 -2
index 6b68220275eb..08524125475e 100644
@@ -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;
 }
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/2 selftests/liveupdate: add seal-after-preserve kexec test

In reply to 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.

---
 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
tools/testing/selftests/liveupdate/Makefile +1 -0
index 634211c66652..03bb09bb9867 100644
@@ -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
 
tools/testing/selftests/liveupdate/luo_seal_after_preserve.c new file +170 -0

170 lines not on this page · raw message

tools/testing/selftests/liveupdate/run-vmtests.sh +1 -0
index c3d828cfd63b..fbc853d981db 100755
@@ -20,6 +20,7 @@ TEST_NAMES=(
 	"luo_multi_session"
 	"luo_stress_files"
 	"luo_stress_sessions"
+	"luo_seal_after_preserve"
 )
 
 function usage() {
Signature · 1 line
2.43.0