Skip to content

Commit c915d8f

Browse files
committed
inotify: Avoid reporting event with invalid wd
When inotify_freeing_mark() races with inotify_handle_inode_event() it can happen that inotify_handle_inode_event() sees that i_mark->wd got already reset to -1 and reports this value to userspace which can confuse the inotify listener. Avoid the problem by validating that wd is sensible (and pretend the mark got removed before the event got generated otherwise). CC: [email protected] Fixes: 7e790dd ("inotify: fix error paths in inotify_update_watch") Message-Id: <[email protected]> Reported-by: [email protected] Reviewed-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent 173ea74 commit c915d8f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

fs/notify/inotify/inotify_fsnotify.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
6565
struct fsnotify_event *fsn_event;
6666
struct fsnotify_group *group = inode_mark->group;
6767
int ret;
68-
int len = 0;
68+
int len = 0, wd;
6969
int alloc_len = sizeof(struct inotify_event_info);
7070
struct mem_cgroup *old_memcg;
7171

@@ -80,6 +80,13 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
8080
i_mark = container_of(inode_mark, struct inotify_inode_mark,
8181
fsn_mark);
8282

83+
/*
84+
* We can be racing with mark being detached. Don't report event with
85+
* invalid wd.
86+
*/
87+
wd = READ_ONCE(i_mark->wd);
88+
if (wd == -1)
89+
return 0;
8390
/*
8491
* Whoever is interested in the event, pays for the allocation. Do not
8592
* trigger OOM killer in the target monitoring memcg as it may have
@@ -110,7 +117,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
110117
fsn_event = &event->fse;
111118
fsnotify_init_event(fsn_event);
112119
event->mask = mask;
113-
event->wd = i_mark->wd;
120+
event->wd = wd;
114121
event->sync_cookie = cookie;
115122
event->name_len = len;
116123
if (len)

0 commit comments

Comments
 (0)