Skip to contentSkip to conversation

fsnotify: fix sending inotify event with unexpected filename

PATCH In review Amir Goldstein 3 participants 7 messages Nov 11, 2024 – Nov 13, 2024

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author reviewer
#0 lore raw

PATCH 1/1 fsnotify: fix sending inotify event with unexpected filename

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

---

Jan,

I tested this fix with the manual reproducer and it passes the LTS sanity tests.

Did not have time to write fanotify+inotify test yet.

Thanks,
Amir.

 fs/notify/fsnotify.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/notify/fsnotify.c +3 -1
index 82ae8254c068..316eec309299 100644
@@ -333,12 +333,14 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
 	if (!inode_mark)
 		return 0;
 
-	if (mask & FS_EVENT_ON_CHILD) {
+	if (mask & FS_EVENTS_POSS_ON_CHILD) {
 		/*
 		 * Some events can be sent on both parent dir and child marks
 		 * (e.g. FS_ATTRIB).  If both parent dir and child are
 		 * watching, report the event once to parent dir with name (if
 		 * interested) and once to child without name (if interested).
+		 *
+		 * In any case, whether the parent is watching or not watching,
 		 * The child watcher is expecting an event without a file name
 		 * and without the FS_EVENT_ON_CHILD flag.
 		 */
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Amir Goldstein

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

Quoted 7 lines

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

Thanks,

Josef

maintainer
#2 lore raw
In reply to Amir Goldstein
Quoted 36 lines · Amir Goldstein

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

Quoted 7 lines

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

Thanks for analysis, Amir!

fs/notify/fsnotify.c static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, · line 336

Quoted 6 lines
fs/notify/fsnotify.c +1 -1
@@ -333,12 +333,14 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
 	if (!inode_mark)
 		return 0;
 
-	if (mask & FS_EVENT_ON_CHILD) {
+	if (mask & FS_EVENTS_POSS_ON_CHILD) {

So this is going to work but as far as I'm reading the code in fsnotify_handle_event() I would be maybe calmer if we instead wrote the condition as:

if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS))

I.e., if the event on the inode is not expecting name & dir, clear them. Instead of your variant which I understand as: "if we could have added name & dir only for parent, clear it now". The bitwise difference between these two checks is: FS_DELETE_SELF | FS_MOVE_SELF | FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | FS_ERROR, none of which should matter. Maybe I'm paranoid but we already had too many subtle bugs in this code so I'm striving for maximum robustness :). What do you think?

Honza

Quoted 13 lines
fs/notify/fsnotify.c +2 -0
 		/*
 		 * Some events can be sent on both parent dir and child marks
 		 * (e.g. FS_ATTRIB).  If both parent dir and child are
 		 * watching, report the event once to parent dir with name (if
 		 * interested) and once to child without name (if interested).
+		 *
+		 * In any case, whether the parent is watching or not watching,
 		 * The child watcher is expecting an event without a file name
 		 * and without the FS_EVENT_ON_CHILD flag.
 		 */
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#3 lore raw
Quoted 60 lines · Jan Kara
Quoted 36 lines · Amir Goldstein

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

Quoted 7 lines

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

Thanks for analysis, Amir!

fs/notify/fsnotify.c static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, · line 336

Quoted 6 lines
fs/notify/fsnotify.c +1 -1
@@ -333,12 +333,14 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
 	if (!inode_mark)
 		return 0;
 
-	if (mask & FS_EVENT_ON_CHILD) {
+	if (mask & FS_EVENTS_POSS_ON_CHILD) {

So this is going to work but as far as I'm reading the code in fsnotify_handle_event() I would be maybe calmer if we instead wrote the condition as:

if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS))

Quoted 7 lines

I.e., if the event on the inode is not expecting name & dir, clear them. Instead of your variant which I understand as: "if we could have added name & dir only for parent, clear it now". The bitwise difference between these two checks is: FS_DELETE_SELF | FS_MOVE_SELF | FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | FS_ERROR, none of which should matter. Maybe I'm paranoid but we already had too many subtle bugs in this code so I'm striving for maximum robustness :). What do you think?

BTW, I can just massage the patch on commit since you're now busy with HSM stuff but I wanted to check what's your opinion on the change.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#4 lore raw
In reply to Jan Kara
Quoted 52 lines · Jan Kara
Quoted 36 lines · Amir Goldstein

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

Quoted 7 lines

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

Thanks for analysis, Amir!

Quoted 4 lines
fs/notify/fsnotify.c +0 -0
@@ -333,12 +333,14 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
      if (!inode_mark)
              return 0;
 
Quoted 8 lines

fs/notify/fsnotify.c static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, · line 336

Quoted 2 lines
fs/notify/fsnotify.c +1 -1
-     if (mask & FS_EVENT_ON_CHILD) {
+     if (mask & FS_EVENTS_POSS_ON_CHILD) {

So this is going to work but as far as I'm reading the code in fsnotify_handle_event() I would be maybe calmer if we instead wrote the condition as:

if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS))

The problem is that the comment below
"Some events can be sent on both parent dir and child marks..." is relevant in the context of FS_EVENTS_POSS_ON_CHILD and FS_EVENT_ON_CHILD, meaning those are exactly the set of events that could be sent to parent with FS_EVENT_ON_CHILD and to child without it.

The comment makes no sense in the context of the ALL_FSNOTIFY_DIRENT_EVENTS check,
Unless we add a comment saying the dirent events set has zero intersection with events possible on child.

Quoted 8 lines

I.e., if the event on the inode is not expecting name & dir, clear them. Instead of your variant which I understand as: "if we could have added name & dir only for parent, clear it now". The bitwise difference between these two checks is: FS_DELETE_SELF | FS_MOVE_SELF | FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | FS_ERROR, none of which should matter. Maybe I'm paranoid but we already had too many subtle bugs in this code so I'm striving for maximum robustness :). What do you think?

How about a
BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ALL_FSNOTIFY_DIRENT_EVENTS) with a comment to clarify?

Quoted 2 lines

BTW, I can just massage the patch on commit since you're now busy with HSM stuff but I wanted to check what's your opinion on the change.

Sure, no problem.

Thanks,
Amir.

maintainer
#5 lore raw
Quoted 69 lines · Amir Goldstein
Quoted 52 lines · Jan Kara
Quoted 36 lines · Amir Goldstein

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

Quoted 7 lines

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

Thanks for analysis, Amir!

Quoted 4 lines
fs/notify/fsnotify.c +0 -0
@@ -333,12 +333,14 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
      if (!inode_mark)
              return 0;
 
Quoted 8 lines

fs/notify/fsnotify.c static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, · line 336

Quoted 2 lines
fs/notify/fsnotify.c +1 -1
-     if (mask & FS_EVENT_ON_CHILD) {
+     if (mask & FS_EVENTS_POSS_ON_CHILD) {

So this is going to work but as far as I'm reading the code in fsnotify_handle_event() I would be maybe calmer if we instead wrote the condition as:

if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS))

The problem is that the comment below
"Some events can be sent on both parent dir and child marks..." is relevant in the context of FS_EVENTS_POSS_ON_CHILD and FS_EVENT_ON_CHILD, meaning those are exactly the set of events that could be sent to parent with FS_EVENT_ON_CHILD and to child without it.

Quoted 4 lines

The comment makes no sense in the context of the ALL_FSNOTIFY_DIRENT_EVENTS check,
Unless we add a comment saying the dirent events set has zero intersection with events possible on child.

Good point and what I *actually* wanted to do is:

/*
* Some events can be sent on both parent dir and child marks (e.g.
* FS_ATTRIB). If both parent dir and child are watching, report the
* event once to parent dir with name (if interested) and once to child
* without name (if interested).
*
* In any case regardless whether the parent is watching or not, the
* child watcher is expecting an event without the FS_EVENT_ON_CHILD
* flag. The file name is expected if and only if this is a directory
* event.
*/
mask &= ~FS_EVENT_ON_CHILD;
if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS)) {
dir = NULL;
name = NULL;
}

Hmm?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#6 lore raw
Quoted 95 lines · Jan Kara
Quoted 69 lines · Amir Goldstein
Quoted 52 lines · Jan Kara
Quoted 36 lines · Amir Goldstein

We got a report that adding a fanotify filsystem watch prevents tail -f from receiving events.

Reproducer:

1. Create 3 windows / login sessions. Become root in each session.
2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
3. In the first window, run: fsnotifywait -S -m /boot
4. In the second window, run: echo data >> /boot/foo
5. In the third window, run: tail -f /boot/foo
6. Go back to the second window and run: echo more data >> /boot/foo
7. Observe that the tail command doesn't show the new data.
8. In the first window, hit control-C to interrupt fsnotifywait.
9. In the second window, run: echo still more data >> /boot/foo
10. Observe that the tail command in the third window has now printed the missing data.

When stracing tail, we observed that when fanotify filesystem mark is set, tail does get the inotify event, but the event is receieved with the filename:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
50) = 32

This is unexpected, because tail is watching the file itself and not its parent and is inconsistent with the inotify event received by tail when fanotify filesystem mark is not set:

read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16

The inteference between different fsnotify groups was caused by the fact that the mark on the sb requires the filename, so the filename is passed to fsnotify(). Later on, fsnotify_handle_event() tries to take care of not passing the filename to groups (such as inotify) that are interested in the filename only when the parent is watching.

Quoted 7 lines

But the logic was incorrect for the case that no group is watching the parent, some groups are watching the sb and some watching the inode.

Fixes 7372e79c9eb9 ("fanotify: fix logic of reporting name info with watched parent")

Thanks for analysis, Amir!

Quoted 4 lines
fs/notify/fsnotify.c +0 -0
@@ -333,12 +333,14 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
      if (!inode_mark)
              return 0;
 
Quoted 8 lines

fs/notify/fsnotify.c static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, · line 336

Quoted 2 lines
fs/notify/fsnotify.c +1 -1
-     if (mask & FS_EVENT_ON_CHILD) {
+     if (mask & FS_EVENTS_POSS_ON_CHILD) {

So this is going to work but as far as I'm reading the code in fsnotify_handle_event() I would be maybe calmer if we instead wrote the condition as:

if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS))

The problem is that the comment below
"Some events can be sent on both parent dir and child marks..." is relevant in the context of FS_EVENTS_POSS_ON_CHILD and FS_EVENT_ON_CHILD, meaning those are exactly the set of events that could be sent to parent with FS_EVENT_ON_CHILD and to child without it.

Quoted 4 lines

The comment makes no sense in the context of the ALL_FSNOTIFY_DIRENT_EVENTS check,
Unless we add a comment saying the dirent events set has zero intersection with events possible on child.

Good point and what I *actually* wanted to do is:

/*
* Some events can be sent on both parent dir and child marks (e.g.
* FS_ATTRIB). If both parent dir and child are watching, report the
* event once to parent dir with name (if interested) and once to child
* without name (if interested).
*
* In any case regardless whether the parent is watching or not, the
* child watcher is expecting an event without the FS_EVENT_ON_CHILD
* flag. The file name is expected if and only if this is a directory
* event.
*/
mask &= ~FS_EVENT_ON_CHILD;
if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS)) {
dir = NULL;
name = NULL;
}

Quoted 1 line

Hmm?

Ok, that works for me :)

Thanks,
Amir.