[LTP] [PATCH 2/4] fanotify03: Add test cases for permission events on children

Amir Goldstein amir73il@gmail.com
Mon Feb 10 16:13:14 CET 2025


Verify that permission events are delivered iff parent is watching
children.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 .../kernel/syscalls/fanotify/fanotify03.c     | 47 ++++++++++++++++---
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/testcases/kernel/syscalls/fanotify/fanotify03.c b/testcases/kernel/syscalls/fanotify/fanotify03.c
index 64c933c19..4b2f6e45e 100644
--- a/testcases/kernel/syscalls/fanotify/fanotify03.c
+++ b/testcases/kernel/syscalls/fanotify/fanotify03.c
@@ -121,12 +121,29 @@ static struct tcase {
 			{FAN_OPEN_EXEC_PERM, FAN_DENY}
 		}
 	},
+	{
+		"parent watching children, FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM events",
+		INIT_FANOTIFY_MARK_TYPE(PARENT),
+		FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM | FAN_EVENT_ON_CHILD, 2,
+		{
+			{FAN_ACCESS_PERM, FAN_DENY},
+			{FAN_OPEN_EXEC_PERM, FAN_DENY}
+		}
+	},
+	{
+		"parent not watching children, FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM events",
+		INIT_FANOTIFY_MARK_TYPE(PARENT),
+		FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM, 0,
+		{
+		}
+	},
 };
 
-static void generate_events(void)
+static void generate_events(struct tcase *tc)
 {
 	int fd;
 	char *const argv[] = {FILE_EXEC_PATH, NULL};
+	int exp_ret, exp_errno = tc->event_count ? EPERM : 0;
 
 	/*
 	 * Generate sequence of events
@@ -136,13 +153,25 @@ static void generate_events(void)
 	SAFE_WRITE(SAFE_WRITE_ANY, fd, fname, 1);
 	SAFE_LSEEK(fd, 0, SEEK_SET);
 
-	if (read(fd, buf, BUF_SIZE) != -1)
+	exp_ret = exp_errno ? -1 : 1;
+	errno = 0;
+	if (read(fd, buf, BUF_SIZE) != exp_ret || errno != exp_errno) {
+		tst_res(TFAIL, "read() got errno %d (expected %d)", errno, exp_errno);
 		exit(3);
+	} else if (errno == exp_errno) {
+		tst_res(TINFO, "read() got errno %d as expected", errno);
+	}
 
 	SAFE_CLOSE(fd);
 
-	if (execve(FILE_EXEC_PATH, argv, environ) != -1)
+	exp_ret = exp_errno ? -1 : 0;
+	errno = 0;
+	if (execve(FILE_EXEC_PATH, argv, environ) != exp_ret || errno != exp_errno) {
+		tst_res(TFAIL, "execve() got errno %d (expected %d)", errno, exp_errno);
 		exit(5);
+	} else if (errno == exp_errno) {
+		tst_res(TINFO, "execve() got errno %d as expected", errno);
+	}
 }
 
 static void child_handler(int tmp)
@@ -156,7 +185,7 @@ static void child_handler(int tmp)
 	fd_notify = -1;
 }
 
-static void run_child(void)
+static void run_child(struct tcase *tc)
 {
 	struct sigaction child_action;
 
@@ -174,7 +203,7 @@ static void run_child(void)
 	if (child_pid == 0) {
 		/* Child will generate events now */
 		SAFE_CLOSE(fd_notify);
-		generate_events();
+		generate_events(tc);
 		exit(0);
 	}
 }
@@ -220,6 +249,12 @@ static int setup_mark(unsigned int n)
 
 	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY);
 
+	if (mark->flag == FAN_MARK_PARENT) {
+		SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag,
+				   tc->mask, AT_FDCWD, MOUNT_PATH);
+		return 0;
+	}
+
 	for (; i < ARRAY_SIZE(files); i++) {
 		SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag,
 				  tc->mask, AT_FDCWD, files[i]);
@@ -237,7 +272,7 @@ static void test_fanotify(unsigned int n)
 	if (setup_mark(n) != 0)
 		return;
 
-	run_child();
+	run_child(tc);
 
 	/*
 	 * Process events
-- 
2.34.1



More information about the ltp mailing list