Skip to contentSkip to conversation

inotify: do not coalesce rename events

PATCH In review Hungyu Lin 3 participants 3 messages Oct 27 – Nov 3

Subsystems (4)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v2 1/1 inotify: do not coalesce rename events

inotify compares the mask, watch descriptor and name when coalescing adjacent unread events, but ignores their rename cookies. Distinct IN_MOVED_FROM events can therefore be coalesced, losing the cookie needed to match a later IN_MOVED_TO event.

For example, watch two directories with IN_MOVE. Move an entry from the first directory into an unwatched directory, recreate it under the same name, then move it from the first directory into the second watched directory before reading the events. The second IN_MOVED_FROM is merged with the first, while the IN_MOVED_TO retains the second rename's cookie. Userspace can no longer pair the second rename's events.

The original inotify implementation compared cookies before coalescing. The fsnotify conversion lost this comparison. Do not coalesce IN_MOVED_FROM or IN_MOVED_TO events, which carry the identity of their respective renames. Leave coalescing of other event types unchanged.

Fixes 63c882a05416 ("inotify: reimplement inotify using fsnotify")
---
 fs/notify/inotify/inotify_fsnotify.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/notify/inotify/inotify_fsnotify.c +1 -1
index 7c326ec2e8a8..6e3af6452dcc 100644
@@ -36,7 +36,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
 
 	old = INOTIFY_E(old_fsn);
 	new = INOTIFY_E(new_fsn);
-	if (old->mask & FS_IN_IGNORED)
+	if (old->mask & (FS_IN_IGNORED | FS_MOVE))
 		return false;
 	if ((old->mask == new->mask) &&
 	    (old->wd == new->wd) &&
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 18 lines · Hungyu Lin

inotify compares the mask, watch descriptor and name when coalescing adjacent unread events, but ignores their rename cookies. Distinct IN_MOVED_FROM events can therefore be coalesced, losing the cookie needed to match a later IN_MOVED_TO event.

For example, watch two directories with IN_MOVE. Move an entry from the first directory into an unwatched directory, recreate it under the same name, then move it from the first directory into the second watched directory before reading the events. The second IN_MOVED_FROM is merged with the first, while the IN_MOVED_TO retains the second rename's cookie. Userspace can no longer pair the second rename's events.

The original inotify implementation compared cookies before coalescing. The fsnotify conversion lost this comparison. Do not coalesce IN_MOVED_FROM or IN_MOVED_TO events, which carry the identity of their respective renames. Leave coalescing of other event types unchanged.

Quoted 4 lines
Fixes 63c882a05416 ("inotify: reimplement inotify using fsnotify")
Quoted 20 lines
---
 fs/notify/inotify/inotify_fsnotify.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/notify/inotify/inotify_fsnotify.c +1 -2
index 7c326ec2e8a8..6e3af6452dcc 100644
@@ -36,7 +36,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
 
        old = INOTIFY_E(old_fsn);
        new = INOTIFY_E(new_fsn);
-       if (old->mask & FS_IN_IGNORED)
+       if (old->mask & (FS_IN_IGNORED | FS_MOVE))
                return false;
        if ((old->mask == new->mask) &&
            (old->wd == new->wd) &&
--

2.43.0

maintainer
#2 lore raw
Quoted 19 lines · Amir Goldstein
Quoted 18 lines · Hungyu Lin

inotify compares the mask, watch descriptor and name when coalescing adjacent unread events, but ignores their rename cookies. Distinct IN_MOVED_FROM events can therefore be coalesced, losing the cookie needed to match a later IN_MOVED_TO event.

For example, watch two directories with IN_MOVE. Move an entry from the first directory into an unwatched directory, recreate it under the same name, then move it from the first directory into the second watched directory before reading the events. The second IN_MOVED_FROM is merged with the first, while the IN_MOVED_TO retains the second rename's cookie. Userspace can no longer pair the second rename's events.

The original inotify implementation compared cookies before coalescing. The fsnotify conversion lost this comparison. Do not coalesce IN_MOVED_FROM or IN_MOVED_TO events, which carry the identity of their respective renames. Leave coalescing of other event types unchanged.

Quoted 5 lines
Quoted 4 lines
Fixes 63c882a05416 ("inotify: reimplement inotify using fsnotify")

Thanks Amir and ignore my comment on v1. I've added this to my tree.

Honza

Quoted 21 lines
Quoted 21 lines
---
 fs/notify/inotify/inotify_fsnotify.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/notify/inotify/inotify_fsnotify.c +1 -2
index 7c326ec2e8a8..6e3af6452dcc 100644
@@ -36,7 +36,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
 
        old = INOTIFY_E(old_fsn);
        new = INOTIFY_E(new_fsn);
-       if (old->mask & FS_IN_IGNORED)
+       if (old->mask & (FS_IN_IGNORED | FS_MOVE))
                return false;
        if ((old->mask == new->mask) &&
            (old->wd == new->wd) &&
--

2.43.0

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR