Skip to content

Commit 3e19deb

Browse files
amir73ilpevik
authored andcommitted
fanotify21: Test reporting event with RDWR fd on RO mount
When event_f_flags request to open O_RDWR files for event->fd, the event listener should not get events with fd on a read-only mount. Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Amir Goldstein <[email protected]>
1 parent 5647a1f commit 3e19deb

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

testcases/kernel/syscalls/fanotify/fanotify21.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <ctype.h>
2222
#include <stdlib.h>
2323
#include <string.h>
24+
#include <sys/mount.h>
2425
#include "tst_test.h"
2526
#include "tst_safe_stdio.h"
2627
#include "tst_safe_macros.h"
@@ -45,16 +46,25 @@ static struct test_case_t {
4546
char *name;
4647
int fork;
4748
int want_pidfd_err;
49+
int remount_ro;
4850
} test_cases[] = {
4951
{
5052
"return a valid pidfd for event created by self",
5153
0,
5254
0,
55+
0,
5356
},
5457
{
5558
"return invalid pidfd for event created by terminated child",
5659
1,
5760
FAN_NOPIDFD,
61+
0,
62+
},
63+
{
64+
"fail to open rw fd for event created on read-only mount",
65+
0,
66+
0,
67+
1,
5868
},
5969
};
6070

@@ -122,7 +132,7 @@ static void do_setup(void)
122132
REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
123133
TEST_FILE);
124134

125-
fanotify_fd = SAFE_FANOTIFY_INIT(FAN_REPORT_PIDFD, O_RDONLY);
135+
fanotify_fd = SAFE_FANOTIFY_INIT(FAN_REPORT_PIDFD, O_RDWR);
126136
SAFE_FANOTIFY_MARK(fanotify_fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD,
127137
TEST_FILE);
128138

@@ -143,6 +153,16 @@ static void do_test(unsigned int num)
143153

144154
tst_res(TINFO, "Test #%d: %s", num, tc->name);
145155

156+
if (tc->remount_ro) {
157+
/* SAFE_MOUNT fails to remount FUSE */
158+
if (mount(tst_device->dev, MOUNT_PATH, tst_device->fs_type,
159+
MS_REMOUNT|MS_RDONLY, NULL) != 0) {
160+
tst_brk(TFAIL,
161+
"filesystem %s failed to remount readonly",
162+
tst_device->fs_type);
163+
}
164+
}
165+
146166
/*
147167
* Generate the event in either self or a child process. Event
148168
* generation in a child process is done so that the FAN_NOPIDFD case
@@ -157,7 +177,16 @@ static void do_test(unsigned int num)
157177
* Read all of the queued events into the provided event
158178
* buffer.
159179
*/
160-
len = SAFE_READ(0, fanotify_fd, event_buf, sizeof(event_buf));
180+
len = read(fanotify_fd, event_buf, sizeof(event_buf));
181+
if (len < 0) {
182+
if (tc->remount_ro && errno == EROFS) {
183+
tst_res(TPASS, "cannot read event with rw fd from a ro fs");
184+
return;
185+
}
186+
tst_brk(TBROK | TERRNO, "reading fanotify events failed");
187+
} else if (tc->remount_ro) {
188+
tst_res(TFAIL, "got unexpected event with rw fd from a ro fs");
189+
}
161190
while (i < len) {
162191
struct fanotify_event_metadata *event;
163192
struct fanotify_event_info_pidfd *info;
@@ -297,6 +326,7 @@ static struct tst_test test = {
297326
.cleanup = do_cleanup,
298327
.all_filesystems = 1,
299328
.needs_root = 1,
329+
.mount_device = 1,
300330
.mntpoint = MOUNT_PATH,
301331
.forks_child = 1,
302332
};

0 commit comments

Comments
 (0)