[Devel] [PATCH hci-8.1 4/7] selftests/mm: check that MADV_DOEXEC refuses an exec from a vfork() child

Konstantin Khorenko khorenko at virtuozzo.com
Wed Oct 14 20:15:02 MSK 2026


A vfork() parent shares the mm with the child and would be woken in
exec_mmap() before the preserved ranges are copied, so the exec is
refused with EBUSY. Check the errno in the child and that the parent's
marked memory is intact afterwards.

https://virtuozzo.atlassian.net/browse/VSTOR-150234

Feature: oracle/mm: MADV_DOEXEC madvise() flag
Assisted-by: LLM
Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 tools/testing/selftests/mm/doexec.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/tools/testing/selftests/mm/doexec.c b/tools/testing/selftests/mm/doexec.c
index ab1d461de5517..01e786a4cb75c 100644
--- a/tools/testing/selftests/mm/doexec.c
+++ b/tools/testing/selftests/mm/doexec.c
@@ -1839,6 +1839,33 @@ struct more_case {
 	int (*fn)(void);
 };
 
+/* A vfork() parent shares the mm and survives the exec: refused. */
+static int vfork_exec(void)
+{
+	size_t size = sysconf(_SC_PAGESIZE);
+	char *ram = preserved_ram(size);
+	char addr_arg[32], size_arg[32];
+	char *args[] = { "doexec", "--check", addr_arg, size_arg, "1", NULL };
+	enum { REFUSED = 10 };
+	pid_t pid;
+
+	if (!ram)
+		return unsupported();
+	snprintf(addr_arg, sizeof(addr_arg), "%llu",
+		 (unsigned long long)(uintptr_t)ram);
+	snprintf(size_arg, sizeof(size_arg), "%zu", size);
+	pid = vfork();
+	if (!pid) {
+		execv("/proc/self/exe", args);
+		_exit(errno == EBUSY ? REFUSED : KSFT_FAIL);
+	}
+	if (pid < 0)
+		return KSFT_FAIL;
+	if (wait_child(pid) != REFUSED)
+		return fail("preserving exec from a vfork() child not refused");
+	return check_bytes(ram, size, 0x31) ? KSFT_FAIL : KSFT_PASS;
+}
+
 static const struct more_case more_cases[] = {
 	{ "madv_doexec_flag sysctl matches MADV_DOEXEC", sysctl_flag },
 	{ "shared, file, executable and stack mappings refused", refused_mappings },
@@ -1858,6 +1885,7 @@ static const struct more_case more_cases[] = {
 	{ "1 GB sparse range: preserved", sparse_exec },
 	{ "swapped out pages: preserved", swapped_exec },
 	{ "half of a THP: preserved", thp_half_exec },
+	{ "vfork() child: preserving exec refused", vfork_exec },
 };
 
 int main(int argc, char **argv)

-- 
2.43.0



More information about the Devel mailing list