Skip to contentSkip to conversation

syscalls/fanotify26: Add test for nested permission events

PATCH Superseded Edvard Conrad via ltp 2 participants 2 messages Oct 27

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/fanotify26: Add test for nested permission events

The kernel creates the file descriptor it reports with a permission event by an open that must not generate further events. When the reported path is on a stacked filesystem, that open opens the real file on the backing filesystem, and since commit ebe559609d78 ("fs: get rid of __FMODE_NONOTIFY kludge") the backing file no longer inherits FMODE_NONOTIFY. A listener that also watches the backing filesystem is then sent a second permission event from inside its own read(2), which only the listener itself can answer, so a single reader never returns.

Verify that a listener returns from read(2) and answers an open permission event for a file on an overlayfs mount. The first test case marks the overlay only and passes on any kernel, the second one also marks the backing filesystem, which is the shape an anti-malware scanner uses on an overlayfs root.

The second case fails on a kernel without the pending fix "fs: let backing files inherit FMODE_NONOTIFY from the user file", so the test is added to runtest/staging for now.

---
 runtest/staging                               |   1 +
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify26.c     | 185 ++++++++++++++++++
 3 files changed, 187 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify26.c
runtest/staging +1 -0
index ef1cdea15..2ce76b119 100644
@@ -1 +1,2 @@
 # Tests for features that are not yet in the stable kernel ABI
+fanotify26 fanotify26
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index d6d0599f1..c13d9b33c 100644
@@ -23,4 +23,5 @@
 /fanotify23
 /fanotify24
 /fanotify25
+/fanotify26
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify26.c new file +185 -0

185 lines not on this page · raw message

Signature · 5 lines
2.56.0


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

Hi Edvard,

testcases/kernel/syscalls/fanotify/fanotify26.c line 3

Quoted 8 lines · Edvard Conrad

syscalls/fanotify26: Add test for nested permission events

testcases/kernel/syscalls/fanotify/fanotify26.c new file +3 -0
@@ -0,0 +1,185 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*\
+ * Check that a permission event on a stacked filesystem does not deadlock the

A copyright header block ("/* Copyright (c) 2026 Edvard Conrad <[email protected]> */") is missing after the SPDX license identifier.

testcases/kernel/syscalls/fanotify/fanotify26.c line 97

Quoted 9 lines
testcases/kernel/syscalls/fanotify/fanotify26.c new file +9 -0
+	if (tc->mark_backing && filesystem_mark_unsupported) {
+		tst_res(TCONF, "FAN_MARK_FILESYSTEM not supported");
+		return;
+	}
+
+	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY);
+
+	SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | FAN_MARK_FILESYSTEM,
+			   FAN_OPEN_PERM, AT_FDCWD, OVL_MNT);

Both test cases use FAN_MARK_FILESYSTEM on OVL_MNT, so gating filesystem_mark_unsupported on tc->mark_backing causes the first test case to fail with TBROK when FAN_MARK_FILESYSTEM is unsupported on overlayfs or in the kernel. Use REQUIRE_MARK_TYPE_SUPPORTED_ON_FS(FAN_MARK_FILESYSTEM, OVL_MNT) and REQUIRE_MARK_TYPE_SUPPORTED_ON_FS(FAN_MARK_FILESYSTEM, BACKING_MNT) in setup() and drop filesystem_mark_unsupported.

testcases/kernel/syscalls/fanotify/fanotify26.c line 126

Quoted 11 lines
testcases/kernel/syscalls/fanotify/fanotify26.c new file +11 -0
+		fd = open(OVL_FILE, O_RDONLY);
+		if (fd >= 0)
+			SAFE_CLOSE(fd);
+		exit(0);
+	}
+
+	/* The opener sleeps in the kernel once its event is queued. */
+	if (TST_PROCESS_STATE_WAIT(opener, 'D', WAIT_MSEC)) {
+		SAFE_CLOSE(fd_notify);
+		tst_brk(TBROK, "Open generated no permission event");
+	}

Use "if (fd != -1)" to check file descriptor validity in opener. Also, SAFE_CLOSE(fd_notify) before tst_brk() is redundant because cleanup() already closes fd_notify.

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify26.c new file +5 -0
+	reader = SAFE_FORK();
+	if (!reader) {
+		answer_one_event();
+		exit(0);
+	}

testcases/kernel/syscalls/fanotify/fanotify26.c line 142

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify26.c new file +10 -0
+
+	if (TST_PROCESS_STATE_WAIT(reader, 'Z', WAIT_MSEC)) {
+		tst_res(TFAIL,
+			"Listener blocked in read() by a nested event");
+		SAFE_KILL(reader, SIGKILL);
+	} else {
+		tst_res(TPASS, "Listener answered the event");
+	}
+
+	SAFE_WAITPID(reader, NULL, 0);

Inferring TPASS from the child reaching 'Z' state and reaping it with SAFE_WAITPID(reader, NULL, 0) hides abnormal child termination. Call tst_res(TPASS, "Listener answered the event") at the end of answer_one_event() in the child, and only call SAFE_WAITPID(reader, NULL, 0) after SAFE_KILL(reader, SIGKILL), letting the library reap the children on the normal path.

testcases/kernel/syscalls/fanotify/fanotify26.c line 146

Quoted 3 lines
testcases/kernel/syscalls/fanotify/fanotify26.c new file +3 -0
+	/* Releases whatever is still queued, so the opener can finish. */
+	SAFE_CLOSE(fd_notify);
+	fd_notify = -1;

Setting fd_notify = -1 is redundant because SAFE_CLOSE() already resets its argument to -1.

testcases/kernel/syscalls/fanotify/fanotify26.c line 169

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify26.c new file +5 -0
+static void cleanup(void)
+{
+	if (fd_notify >= 0)
+		SAFE_CLOSE(fd_notify);
+}

Use "if (fd_notify != -1)" instead of "if (fd_notify >= 0)".

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