mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Long Wei <longwei27@huawei.com>
To: <rppt@kernel.org>, <pasha.tatashin@soleen.com>, <pratyush@kernel.org>
Cc: <shuah@kernel.org>, <akpm@linux-foundation.org>,
	<linux-mm@kvack.org>, <kexec@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
	<hewenliang4@huawei.com>, Long Wei <longwei27@huawei.com>
Subject: [PATCH 2/2] selftests/liveupdate: add seal-after-preserve kexec test
Date: Sat, 28 Nov 2026 15:24:44 +0800	[thread overview]
Message-ID: <20261128072444.1114964-3-longwei27@huawei.com> (raw)
In-Reply-To: <20261128072444.1114964-1-longwei27@huawei.com>

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


      parent reply	other threads:[~2026-11-28  7:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [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=20261128072444.1114964-3-longwei27@huawei.com \
    --to=longwei27@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=hewenliang4@huawei.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=pratyush@kernel.org \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.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

all inboxes | Powered by JetHome®