inotify13: check rename cookie pairing after adjacent moves
PATCH 1/1 inotify13: check rename cookie pairing after adjacent moves
Queue an unpaired IN_MOVED_FROM event, followed by an IN_MOVED_FROM and IN_MOVED_TO pair with the same source watch descriptor and name as the first event. Watch only IN_MOVE so creation of the replacement entry does not separate the two IN_MOVED_FROM events in the queue.
Affected kernels coalesce the second IN_MOVED_FROM into the first, leaving the IN_MOVED_TO cookie without a matching IN_MOVED_FROM cookie. Check that all three events are retained, the two moves have distinct cookies, and the second IN_MOVED_FROM matches the IN_MOVED_TO cookie. Cover both regular files and directories.
--- runtest/syscalls | 1 + testcases/kernel/syscalls/inotify/.gitignore | 1 + testcases/kernel/syscalls/inotify/inotify13.c | 147 ++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c
runtest/syscalls +1 -0
@@ -677,6 +677,7 @@ inotify09 inotify09 inotify10 inotify10 inotify11 inotify11 inotify12 inotify12 +inotify13 inotify13 fanotify01 fanotify01 fanotify02 fanotify02
testcases/kernel/syscalls/inotify/.gitignore +1 -0
@@ -10,3 +10,4 @@ /inotify10 /inotify11 /inotify12 +/inotify13
testcases/kernel/syscalls/inotify/inotify13.c new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 12 lines · Hungyu Lin
Queue an unpaired
IN_MOVED_FROMevent, followed by anIN_MOVED_FROMandIN_MOVED_TOpair with the same source watch descriptor and name as the first event. Watch onlyIN_MOVEso creation of the replacement entry does not separate the twoIN_MOVED_FROMevents in the queue.Affected kernels coalesce the second
IN_MOVED_FROMinto the first, leaving theIN_MOVED_TOcookie without a matchingIN_MOVED_FROMcookie. Check that all three events are retained, the two moves have distinct cookies, and the secondIN_MOVED_FROMmatches theIN_MOVED_TOcookie. Cover both regular files and directories.
Quoted 3 lines
Suggested-by Amir Goldstein <[email protected]>Signed-off-by Hungyu Lin <[email protected]>---
Thanks for following up on this
Quoted 100 lines
runtest/syscalls | 1 + testcases/kernel/syscalls/inotify/.gitignore | 1 + testcases/kernel/syscalls/inotify/inotify13.c | 147 ++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 testcases/kernel/syscalls/inotify/inotify13.cruntest/syscalls +1
-0@@ -677,6 +677,7 @@ inotify09 inotify09 inotify10 inotify10 inotify11 inotify11 inotify12 inotify12 +inotify13 inotify13 fanotify01 fanotify01 fanotify02 fanotify02testcases/kernel/syscalls/inotify/.gitignore +1
-0@@ -10,3 +10,4 @@ /inotify10 /inotify11 /inotify12 +/inotify13testcases/kernel/syscalls/inotify/inotify13.c new file +67
-0@@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2026 Hungyu Lin <[email protected]> +*/ + +/*\ +* Verify that coalescing does not lose an IN_MOVED_FROM event needed to +* match a later IN_MOVED_TO event by its rename cookie. +* +* Move an entry from a watched directory to an unwatched directory, recreate +* it under the same name, then move it to another watched directory. Watch +* only move events so the two IN_MOVED_FROM events are adjacent in the queue. +* Their cookies must differ, and the second must match the IN_MOVED_TO cookie. +* Test both regular files and directories without reading between renames. +*/ + +#include "config.h" +#include <fcntl.h> +#include <string.h> +#include "tst_test.h" +#include "inotify.h" + +#ifdef HAVE_SYS_INOTIFY_H +#include <sys/inotify.h> + +#define SOURCE "source/entry" +#define SAVED "unwatched/entry" +#define TARGET "target/entry" + +static int fd_notify =-1 ; + +static void create_entry (int directory) +{ +if (directory) +SAFE_MKDIR (SOURCE ,0700 ); +else +SAFE_TOUCH (SOURCE ,0600 ,NULL ); +} + +static void verify_inotify (unsigned int directory) +{ +union { +struct inotify_event align; +char data[4096 ]; + } buf; +struct inotify_event *event, *events[3 ]; +unsigned int count =0 ; +size_t pos =0 , len; +unsigned int from =IN_MOVED_FROM , to =IN_MOVED_TO ; +int wd_source, wd_target; +const char *type = directory ?"directory" :"file" ; + +if (directory) { + from |=IN_ISDIR ; + to |=IN_ISDIR ; + } + + fd_notify =SAFE_MYINOTIFY_INIT1 (O_NONBLOCK ); + wd_source =SAFE_MYINOTIFY_ADD_WATCH (fd_notify,"source" ,IN_MOVE ); + wd_target =SAFE_MYINOTIFY_ADD_WATCH (fd_notify,"target" ,IN_MOVE ); + +create_entry (directory); +SAFE_RENAME (SOURCE ,SAVED ); +create_entry (directory); +SAFE_RENAME (SOURCE ,TARGET ); + + len =SAFE_READ (SAFE_READ_ANY , fd_notify, buf.data,sizeof (buf.data));
testcases/kernel/syscalls/inotify/inotify13.c line 77
Quoted 10 lines
testcases/kernel/syscalls/inotify/inotify13.c new file +10
-0+SAFE_CLOSE (fd_notify); + fd_notify =-1 ; + +if (directory) { +SAFE_RMDIR (SAVED ); +SAFE_RMDIR (TARGET ); + }else { +SAFE_UNLINK (SAVED ); +SAFE_UNLINK (TARGET ); + }
not strictly needed for tmpdir setup
Quoted 25 lines
testcases/kernel/syscalls/inotify/inotify13.c new file +25
-0+ +while (pos < len) { +if (len - pos <sizeof (*event)) +tst_brk (TBROK ,"Incomplete inotify event" ); + + event = (struct inotify_event *)(buf.data + pos); +if (event->len > len - pos -sizeof (*event)) +tst_brk (TBROK ,"Incomplete inotify event name" ); + +tst_res (TINFO ,"%s: wd=%d mask=%#x cookie=%u name=%.*s" , + type, event->wd, event->mask, event->cookie, + (int )event->len, event->name); +if (event->len <sizeof ("entry" ) || +memcmp (event->name,"entry" ,sizeof ("entry" ))) { +tst_res (TFAIL ,"%s: unexpected rename event name" , type); +return ; + } +if (count <ARRAY_SIZE (events)) + events[count] = event; + count++; + pos +=sizeof (*event) + event->len; + } + +if (count !=ARRAY_SIZE (events)) { + tst_res(TFAIL, "%s: expected two FROM events and one TO, got %u",
testcases/kernel/syscalls/inotify/inotify13.c line 112
Quoted 10 lines
testcases/kernel/syscalls/inotify/inotify13.c new file +10
-0+ type, count); +return ; + } + +if (events[0 ]->wd != wd_source || events[0 ]->mask != from || + events[1 ]->wd != wd_source || events[1 ]->mask != from || + events[2 ]->wd != wd_target || events[2 ]->mask != to) { +tst_res (TFAIL ,"%s: unexpected rename event sequence" , type); +return ; + }
Most tests use the pattern of preparing an expected event_set[] array sometimes interleaved with generating the events.
I find this existing pattern more readable, but if there is a reason not to follow existing practice, I can live with the above pattern.
Existing tests are also more verbose in the specific unexpected detail.
Thanks,
Amir.
Quoted 38 lines
testcases/kernel/syscalls/inotify/inotify13.c new file +35
-1+ +if (events[0 ]->cookie == events[1 ]->cookie || + events[1 ]->cookie != events[2 ]->cookie) { +tst_res (TFAIL ,"%s: rename cookies do not identify the later pair" , + type); +return ; + } + + tst_res(TPASS, "%s: second FROM cookie matches the TO cookie", type); +} + +static void setup (void ) +{ +SAFE_MKDIR ("source" ,0700 ); +SAFE_MKDIR ("unwatched" ,0700 ); +SAFE_MKDIR ("target" ,0700 ); +} + +static void cleanup (void ) +{ +if (fd_notify >=0 ) +SAFE_CLOSE (fd_notify); +} + +static struct tst_test test = { + .test = verify_inotify, + .tcnt =2 , + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir =1 , +}; + +#else +TST_TEST_TCONF ("system doesn't have required inotify support" ); +#endif --2.43.0
Hi Hungyu,
testcases/kernel/syscalls/inotify/inotify13.c line 69
Quoted 5 lines · Hungyu Lin
inotify13: check rename cookie pairing after adjacent moves
testcases/kernel/syscalls/inotify/inotify13.c new file +3
-0+ len =SAFE_READ (SAFE_READ_ANY , fd_notify, buf.data,sizeof (buf.data)); +SAFE_CLOSE (fd_notify); + fd_notify =-1 ;
SAFE_CLOSE() already sets fd_notify to -1, so the manual reset should be removed.
testcases/kernel/syscalls/inotify/inotify13.c line 135
Quoted 5 lines
testcases/kernel/syscalls/inotify/inotify13.c new file +5
-0+static void cleanup (void ) +{ +if (fd_notify >=0 ) +SAFE_CLOSE (fd_notify); +}
Use if (fd_notify != -1) to check file descriptor validity in cleanup().
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