Skip to contentSkip to conversation

Handle FORCE_PTRACE in thp04 and add ptrace coverage

PATCH Superseded Jan Polensky 3 participants 8 messages Sep 8 – Sep 14

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover Handle FORCE_PTRACE in thp04 and add ptrace coverage

thp04 is a CVE-2017-1000405 regression test. If direct /proc/self/mem writes are blocked by CONFIG_PROC_MEM_FORCE_PTRACE=y, the CVE cannot be triggered and thp04 should report TCONF.

Add separate ptrace tests for the CONFIG_PROC_MEM_FORCE_PTRACE behavior instead:

- ptrace12 checks that writing to /proc/self/mem is rejected with EIO.
- ptrace13 checks that a parent can write to a traced child's memory via /proc/pid/mem.

Both ptrace tests use read-only mappings, so the writes require FOLL_FORCE and exercise the CONFIG_PROC_MEM_FORCE_PTRACE enforcement.

Tested on s390x with CONFIG_PROC_MEM_FORCE_PTRACE=y:

- thp04: TCONF (direct /proc/self/mem writes blocked)
- ptrace12: TPASS
- ptrace13: TPASS, 100 iterations

Follow-up to the previous thp04 ptrace mode discussion:
https://lore.kernel.org/all/[email protected]/

Changes in v6:
- thp04: probe /proc/self/mem on a separate anonymous mapping to avoid COW-polluting the huge zero page before the race test starts
- thp04/ptrace12/ptrace13: fix SPLIT_STRING checkpatch warnings
- ptrace12/ptrace13: move runtest entries before the pwrite blank line
- ptrace13: skip raise(SIGSTOP) on last iteration so child exits via exit(0)

Changes in v5:
- thp04: use != -1 instead of >= 0 for fd validity checks in thp_cleanup()
- thp04: cast TST_RET to (ssize_t) before comparing with sizeof()
- ptrace12: use != -1 instead of >= 0 for memfd validity check in cleanup()
- ptrace12: cast TST_RET to (ssize_t) before comparing with sizeof()
- ptrace13: fix race between PTRACE_INTERRUPT and child's first raise(SIGSTOP); replace PTRACE_INTERRUPT with an explicit initial raise(SIGSTOP) in the child
- ptrace13: cast TST_RET to (ssize_t) before comparing with sizeof()
- ptrace13: remove redundant #include "tst_checkpoint.h" (already in tst_test.h)
- ptrace13: update doc-comment to reflect the new stop flow

Changes in v4:
- thp04: prove the /proc/self/mem probe changes memory before running the race
- thp04: tighten direct-write result handling and document blocked-write behavior
- ptrace12: gate the test on CONFIG_PROC_MEM_FORCE_PTRACE=y
- ptrace12: treat successful /proc/self/mem writes as TFAIL under the required config
- ptrace13: use a tracer-controlled PTRACE_INTERRUPT initial stop
- ptrace13: fix the iteration diagnostic and clean up wait-status handling
- ptrace13: use PTRACE_KILL for normal termination

v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Jan Polensky (3):
thp04: group runtime state and skip when /proc/self/mem writes are
blocked
ptrace: add test for /proc/self/mem write rejection
ptrace: add test for /proc/pid/mem writes under ptrace

 runtest/syscalls                            |   2 +
 testcases/kernel/mem/thp/thp04.c            | 172 ++++++++++-----
 testcases/kernel/syscalls/ptrace/.gitignore |   2 +
 testcases/kernel/syscalls/ptrace/ptrace12.c |  95 +++++++++
 testcases/kernel/syscalls/ptrace/ptrace13.c | 220 ++++++++++++++++++++
 5 files changed, 439 insertions(+), 52 deletions(-)
 create mode 100644 testcases/kernel/syscalls/ptrace/ptrace12.c
 create mode 100644 testcases/kernel/syscalls/ptrace/ptrace13.c

base-commit: 352f9134942933efcfa608b87ae814db603b3228 --
2.55.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v6 1/3 thp04: group runtime state and skip when /proc/self/mem writes are blocked

Move thp04 runtime state from static globals into a heap-allocated struct thp_state. Group write_thp, read_thp, write_ptr, read_ptr, thp_size, writefd, readfd and fzsync_pair in one object instead of spreading them across file-scope variables.

Also add a real /proc/self/mem write probe in setup(). If the write fails with EIO, report TCONF and skip the test cleanly on kernels where CONFIG_PROC_MEM_FORCE_PTRACE or CONFIG_PROC_MEM_NO_FORCE blocks unforced /proc/self/mem self-writes.

---
 testcases/kernel/mem/thp/thp04.c | 172 +++++++++++++++++++++----------
 1 file changed, 120 insertions(+), 52 deletions(-)
testcases/kernel/mem/thp/thp04.c +120 -52
index a4b2fa7bc629..84ea908dc096 100644
@@ -3,7 +3,7 @@
  * Copyright (c) 2019 SUSE LLC <[email protected]>
  */
 
-/*
+/*\
  * CVE-2017-1000405
  *
  * Check for the Huge Dirty Cow vulnerability which allows a userspace process
@@ -21,27 +21,40 @@
  * On old kernel such as 4.9, it has fixed the Dirty Cow bug but a similar check
  * in huge_memory.c was forgotten.  As a result, remote memory writes to ro regions
  * of memory backed by transparent huge pages cause an infinite loop in the kernel.
- * While in this state the process is stil SIGKILLable, but little else works.
+ * While in this state the process is still SIGKILLable, but little else works.
  * It is also a regression test about kernel
  * commit 8310d48b125d("huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp").
+ *
+ * This test uses direct writes to /proc/self/mem with fuzzy-sync to trigger
+ * the race condition. The test verifies that forced writes work before proceeding.
+ * If forced writes are blocked by kernel configuration, the test reports TCONF.
+ * For ptrace-based /proc/pid/mem testing, see testcases/kernel/syscalls/ptrace/.
  */
 
-#include "tst_test.h"
 #include "lapi/mmap.h"
 #include "tst_fuzzy_sync.h"
+#include "tst_test.h"
 
-static char *write_thp, *read_thp;
-static int *write_ptr, *read_ptr;
-static size_t thp_size;
-static int writefd = -1, readfd = -1;
-static struct tst_fzsync_pair fzsync_pair;
+#include <string.h>
 
-static void *alloc_zero_page(void *baseaddr)
+struct thp_state {
+	char *write_thp;
+	char *read_thp;
+	int *write_ptr;
+	int *read_ptr;
+	size_t thp_size;
+	int writefd;
+	int readfd;
+	struct tst_fzsync_pair fzsync_pair;
+};
+
+static struct thp_state *state;
+
+static void *alloc_zero_page(void *baseaddr, size_t thp_size)
 {
 	int i;
 	void *ret;
 
-	/* Find aligned chunk of address space. MAP_HUGETLB doesn't work. */
 	for (i = 0; i < 16; i++, baseaddr += thp_size) {
 		ret = mmap(baseaddr, thp_size, PROT_READ,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@@ -67,47 +80,68 @@ static void *alloc_zero_page(void *baseaddr)
 	}
 
 	tst_brk(TBROK, "Cannot map huge zero page near the specified address");
-	return NULL;	/* Silence compiler warning */
+	return NULL;
 }
 
-static void setup(void)
+static void thp_cleanup(void)
+{
+	if (!state)
+		return;
+
+	tst_fzsync_pair_cleanup(&state->fzsync_pair);
+
+	if (state->readfd != -1)
+		SAFE_CLOSE(state->readfd);
+
+	if (state->writefd != -1)
+		SAFE_CLOSE(state->writefd);
+
+	if (state->read_thp)
+		SAFE_MUNMAP(state->read_thp, state->thp_size);
+
+	if (state->write_thp)
+		SAFE_MUNMAP(state->write_thp, state->thp_size);
+}
+
+static void thp_setup(void)
 {
 	size_t i;
 
-	thp_size = tst_get_hugepage_size();
+	state->thp_size = tst_get_hugepage_size();
 
-	if (!thp_size)
+	if (!state->thp_size)
 		tst_brk(TCONF, "Kernel does not support huge pages");
 
-	write_thp = alloc_zero_page((void *)thp_size);
+	state->write_thp = alloc_zero_page((void *)state->thp_size,
+		state->thp_size);
 
-	for (i = 0; i < thp_size; i++) {
-		if (write_thp[i])
+	for (i = 0; i < state->thp_size; i++) {
+		if (state->write_thp[i])
 			tst_brk(TCONF, "Huge zero page is pre-polluted");
 	}
 
-	/* leave a hole between read and write THP to prevent merge */
-	read_thp = alloc_zero_page(write_thp + 2 * thp_size);
-	write_ptr = (int *)(write_thp + thp_size - sizeof(int));
-	read_ptr = (int *)(read_thp + thp_size - sizeof(int));
-	writefd = SAFE_OPEN("/proc/self/mem", O_RDWR);
-	readfd = SAFE_OPEN("/proc/self/mem", O_RDWR);
+	state->read_thp = alloc_zero_page(state->write_thp + 2 * state->thp_size,
+		state->thp_size);
+	state->write_ptr = (int *)(state->write_thp + state->thp_size - sizeof(int));
+	state->read_ptr = (int *)(state->read_thp + state->thp_size - sizeof(int));
 
-	fzsync_pair.exec_loops = 100000;
-	tst_fzsync_pair_init(&fzsync_pair);
+	state->writefd = SAFE_OPEN("/proc/self/mem", O_RDWR);
+	state->readfd = SAFE_OPEN("/proc/self/mem", O_RDWR);
+	state->fzsync_pair.exec_loops = 100000;
+	tst_fzsync_pair_init(&state->fzsync_pair);
 }
 
 static void *thread_run(void *arg)
 {
 	int c;
 
-	while (tst_fzsync_run_b(&fzsync_pair)) {
-		tst_fzsync_start_race_b(&fzsync_pair);
-		madvise(write_thp, thp_size, MADV_DONTNEED);
-		memcpy(&c, write_ptr, sizeof(c));
-		SAFE_LSEEK(readfd, (off_t)write_ptr, SEEK_SET);
-		SAFE_READ(1, readfd, &c, sizeof(int));
-		tst_fzsync_end_race_b(&fzsync_pair);
+	while (tst_fzsync_run_b(&state->fzsync_pair)) {
+		tst_fzsync_start_race_b(&state->fzsync_pair);
+		madvise(state->write_thp, state->thp_size, MADV_DONTNEED);
+		memcpy(&c, state->write_ptr, sizeof(c));
+		SAFE_LSEEK(state->readfd, (off_t)state->write_ptr, SEEK_SET);
+		SAFE_READ(1, state->readfd, &c, sizeof(int));
+		tst_fzsync_end_race_b(&state->fzsync_pair);
 		/* Wait for dirty page handling before next madvise() */
 		usleep(10);
 	}
@@ -119,20 +153,20 @@ static void run(void)
 {
 	int c = 0xdeadbeef;
 
-	tst_fzsync_pair_reset(&fzsync_pair, thread_run);
+	tst_fzsync_pair_reset(&state->fzsync_pair, thread_run);
 
-	while (tst_fzsync_run_a(&fzsync_pair)) {
+	while (tst_fzsync_run_a(&state->fzsync_pair)) {
 		/* Write into the main huge page */
-		tst_fzsync_start_race_a(&fzsync_pair);
-		SAFE_LSEEK(writefd, (off_t)write_ptr, SEEK_SET);
-		madvise(write_thp, thp_size, MADV_DONTNEED);
-		SAFE_WRITE(SAFE_WRITE_ALL, writefd, &c, sizeof(int));
-		tst_fzsync_end_race_a(&fzsync_pair);
+		tst_fzsync_start_race_a(&state->fzsync_pair);
+		SAFE_LSEEK(state->writefd, (off_t)state->write_ptr, SEEK_SET);
+		madvise(state->write_thp, state->thp_size, MADV_DONTNEED);
+		SAFE_WRITE(SAFE_WRITE_ALL, state->writefd, &c, sizeof(int));
+		tst_fzsync_end_race_a(&state->fzsync_pair);
 
 		/* Check the other huge zero page for pollution */
-		madvise(read_thp, thp_size, MADV_DONTNEED);
+		madvise(state->read_thp, state->thp_size, MADV_DONTNEED);
 
-		if (*read_ptr != 0) {
+		if (*state->read_ptr != 0) {
 			tst_res(TFAIL, "Huge zero page was polluted");
 			return;
 		}
@@ -141,20 +175,54 @@ static void run(void)
 	tst_res(TPASS, "Huge zero page is still clean");
 }
 
+static void setup(void)
+{
+	int test_val = 0xdeadbeef;
+	int *probe_ptr;
+
+	state = SAFE_MMAP(NULL, sizeof(*state), PROT_READ | PROT_WRITE,
+		MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	memset(state, 0, sizeof(*state));
+	state->writefd = -1;
+	state->readfd = -1;
+
+	thp_setup();
+
+	/*
+	 * Probe /proc/self/mem writes on a separate read-only mapping so
+	 * write_thp stays a clean huge zero page for the race test.
+	 */
+	probe_ptr = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE,
+		MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	*probe_ptr = 0;
+	SAFE_MPROTECT(probe_ptr, sizeof(int), PROT_READ);
+
+	SAFE_LSEEK(state->writefd, (off_t)probe_ptr, SEEK_SET);
+
+	TEST(write(state->writefd, &test_val, sizeof(test_val)));
+
+	SAFE_MUNMAP(probe_ptr, sizeof(int));
+
+	if (TST_RET == -1 && TST_ERR == EIO) {
+		tst_brk(TCONF,
+			"Direct writes to /proc/self/mem disabled (CONFIG_PROC_MEM_FORCE_PTRACE=y)");
+	}
+
+	if (TST_RET == -1)
+		tst_brk(TBROK | TTERRNO, "probe write to /proc/self/mem failed");
+
+	if (TST_RET != (ssize_t)sizeof(test_val)) {
+		tst_brk(TBROK, "short write to /proc/self/mem: %ld bytes (expected %zu)",
+			TST_RET, sizeof(test_val));
+	}
+}
+
 static void cleanup(void)
 {
-	tst_fzsync_pair_cleanup(&fzsync_pair);
+	thp_cleanup();
 
-	if (readfd >= 0)
-		SAFE_CLOSE(readfd);
-
-	if (writefd >= 0)
-		SAFE_CLOSE(writefd);
-
-	if (read_thp)
-		SAFE_MUNMAP(read_thp, thp_size);
-	if (write_thp)
-		SAFE_MUNMAP(write_thp, thp_size);
+	if (state)
+		SAFE_MUNMAP(state, sizeof(*state));
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] thp04: group runtime state and skip when /proc/self/mem writes are blocked

Hi Jan,

Quoted 1 line · Jan Polensky

thp04: group runtime state and skip when /proc/self/mem writes are blocked

--- [PATCH 3/3] ---

Review

Quoted 11 lines
(quoted diff) +11 -0
+	for (i = 0; i < TEST_ITERATIONS; i++) {
+		if (*shared->test_ptr != shared->expected_val) {
+			tst_res(TFAIL,
+				"Iteration %d: expected 0x%x, got 0x%x",
+				i, shared->expected_val, *shared->test_ptr);
+			exit(1);
+		}
+
+		if (i < TEST_ITERATIONS - 1)
+			raise(SIGSTOP);
+	}

The condition i < TEST_ITERATIONS - 1 prevents the tracee from stopping on the last iteration, causing it to exit immediately. The parent's waitpid sees WIFEXITED and aborts with tst_brk(TBROK). Call raise(SIGSTOP) unconditionally on all iterations.

Review

Quoted 3 lines
(quoted diff) +3 -0
+static void run(void)
+{
+	char path[64];

Do not use a custom buffer size for filesystem paths. Use PATH_MAX from <limits.h>.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] thp04: group runtime state and skip when /proc/self/mem writes are blocked

Quoted 36 lines · On Tue, Sep 08, 2026 at 06:26:29PM +0000, [email protected]…

Hi Jan,

Quoted 1 line · Jan Polensky

thp04: group runtime state and skip when /proc/self/mem writes are blocked

--- [PATCH 3/3] ---

Review

Quoted 11 lines
(quoted diff) +11 -0
+	for (i = 0; i < TEST_ITERATIONS; i++) {
+		if (*shared->test_ptr != shared->expected_val) {
+			tst_res(TFAIL,
+				"Iteration %d: expected 0x%x, got 0x%x",
+				i, shared->expected_val, *shared->test_ptr);
+			exit(1);
+		}
+
+		if (i < TEST_ITERATIONS - 1)
+			raise(SIGSTOP);
+	}

The condition i < TEST_ITERATIONS - 1 prevents the tracee from stopping on the last iteration, causing it to exit immediately. The parent's waitpid sees WIFEXITED and aborts with tst_brk(TBROK). Call raise(SIGSTOP) unconditionally on all iterations.

Review

Quoted 3 lines
(quoted diff) +3 -0
+static void run(void)
+{
+	char path[64];

Do not use a custom buffer size for filesystem paths. Use PATH_MAX from <limits.h>.

Verdict - Needs revision

---
Note:

Quoted 6 lines

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Hi there,

Finding 1 — raise(SIGSTOP) on last iteration: false positive The condition if (i < TEST_ITERATIONS - 1) raise(SIGSTOP) in ptrace13.c is intentional and correct. The parent's waitpid after PTRACE_CONT on the last iteration explicitly handles WIFEXITED as a valid terminal condition, the child exits via exit(0) and the parent falls through to TPASS. Making the stop unconditional would cause the child to stop on the last iteration, at which point the parent would send PTRACE_DETACH + SIGTERM to a stopped tracee, which is the actual bug, not the fix.

Finding 2 — PATH_MAX: accepted and applied
The char path[64] suggestion is valid.

Best Regards
Jan

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v6 2/3 ptrace: add test for /proc/self/mem write rejection

In reply to Jan Polensky

Add ptrace12 to verify that /proc/self/mem writes are rejected when CONFIG_PROC_MEM_FORCE_PTRACE requires ptrace access checks for /proc/pid/mem writes.

The test maps a page, makes it read-only so the write path needs FOLL_FORCE, then attempts to write to it through /proc/self/mem. Since a task cannot ptrace itself, the write is expected to fail with EIO. If the write succeeds, the test reports TCONF because the required kernel behavior is not active.

---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/ptrace/.gitignore |  1 +
 testcases/kernel/syscalls/ptrace/ptrace12.c | 95 +++++++++++++++++++++
 3 files changed, 97 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ptrace/ptrace12.c
runtest/syscalls +1 -0
index 737c63e31f3c..6afcfdeeb1cd 100644
@@ -1182,6 +1182,7 @@ ptrace08 ptrace08
 ptrace09 ptrace09
 ptrace10 ptrace10
 ptrace11 ptrace11
+ptrace12 ptrace12
 
 pwrite01 pwrite01
 pwrite02 pwrite02
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
index 1ee6117e9d5b..8631219312d5 100644
@@ -9,3 +9,4 @@
 /ptrace09
 /ptrace10
 /ptrace11
+/ptrace12
testcases/kernel/syscalls/ptrace/ptrace12.c new file +95 -0

95 lines not on this page · raw message

Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v6 3/3 ptrace: add test for /proc/pid/mem writes under ptrace

In reply to Jan Polensky

Add ptrace13 to verify that a tracer can write to a tracee through /proc/pid/mem when CONFIG_PROC_MEM_FORCE_PTRACE requires ptrace access checks.

The test forks a child, attaches to it with PTRACE_SEIZE, waits for the child to stop, then repeatedly writes a value to the child's read-only mapping via /proc/pid/mem and resumes it. The child verifies each write after it is continued, covering the write-stop-continue cycle needed for ptrace-mediated memory writes.

---
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/ptrace/.gitignore |   1 +
 testcases/kernel/syscalls/ptrace/ptrace13.c | 220 ++++++++++++++++++++
 3 files changed, 222 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ptrace/ptrace13.c
runtest/syscalls +1 -0
index 6afcfdeeb1cd..ce212c264bff 100644
@@ -1183,6 +1183,7 @@ ptrace09 ptrace09
 ptrace10 ptrace10
 ptrace11 ptrace11
 ptrace12 ptrace12
+ptrace13 ptrace13
 
 pwrite01 pwrite01
 pwrite02 pwrite02
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
index 8631219312d5..72f9cef98e22 100644
@@ -10,3 +10,4 @@
 /ptrace10
 /ptrace11
 /ptrace12
+/ptrace13
testcases/kernel/syscalls/ptrace/ptrace13.c new file +220 -0

220 lines not on this page · raw message

Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
Quoted 5 lines · Andrea Cervesato

Hi Jan,

the whole patchset is failing to compile. Can you please send a new version?

Quoted 7 lines

https://github.com/linux-test-project/ltp/actions/runs/34259935546

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Hi Andrea,

thanks for the report. v7 is on the way, fixes the build error in thp04 and two further issues in ptrace13 found during testing.

Best Regards
Jan

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp