Skip to contentSkip to conversation

Fanotify Directory exclusion not working when using FAN_MARK_MOUNT

DISCUSSION Jan Kara 2 participants 19 messages Mar 14, 2022 – Apr 28, 2022 4 messages not in the archive VFS

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Jan Kara

author reviewer
#2 lore raw
Quoted 13 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

Quoted 6 lines

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.
In retrospect, FAN_EVENT_ON_CHILD and FAN_ONDIR would have been more clear as FAN_MARK_ flags, but that's too late.

Thanks,
Amir.

author reviewer
#4 lore raw
Quoted 45 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

Quoted 7 lines

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.

Oh, right. I forgot about the need for FAN_EVENT_ON_CHILD in the mark->mask. It seems we can set FAN_EVENT_ON_CHILD in the ignored_mask as well but it just gets ignored currently. So we would need to propagate it even from ignore_mask to inode->i_fsnotify_mask. But send_to_group() would also need to be more careful now with ignore masks and apply them from parent only if the particular mark has FAN_EVENT_ON_CHILD in the ignore mask. Interestingly fanotify_group_event_mask() does explicitely apply ignore_mask from the parent regardless of FAN_EVENT_ON_CHILD flags. So there is some inconsistency there and it would need some tweaking...

Overall I guess the functionality makes sense to me (in fact it is somewhat surprising it is not working like that from the beginning), API-wise it is not outright horrible, and technically it seems doable. What do you think?

Quoted 2 lines

In retrospect, FAN_EVENT_ON_CHILD and FAN_ONDIR would have been more clear as FAN_MARK_ flags, but that's too late.

Yeah, it is like this since fanotify has been created so no real chance to change that.

Honza

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 6 lines · Srinivas

Thanks Amir/Jan.
Apart from the directory exclusion i called out, i think from a anti-virus agent side, what would be good to have is the ability to add a mark for FAN_MARK_MOUNT but then additionally ignore an entire subtree of directories under the mount point. Basically express interest in everything but then ignore (trust) some root only writeable directories etc. Can this be done today?
My experiments again show that it is not. Thoughts?

You can do something similar with volatile exclude marks [1] You can grab my branch for testing from [2]

What you do is create the exclude marks lazily -
you can an event, see that it is under the trusted subtree and you call:

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_CREATE | FAN_MARK_VOLATILE,

                       FAN_EVENT_ON_CHILD,
                       AT_FDCWD, "/trusted/foo/");

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/trusted/foo/");

Then you won't get further events until /trusted/foo is not accessed for a while and memory pressure evicts its inode, then on next access to /trusted/foo you will get an event, set the volatile ignored mask again.

Let me know if you think that is useful for AV agents and if not, please let me know why not.

Thanks,
Amir.

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://github.com/amir73il/linux/commits/fsnotify-volatile

#7 lore raw
In reply to Jan Kara
Quoted 55 lines · Jan Kara
Quoted 45 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

Quoted 7 lines

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.

Quoted 10 lines

Oh, right. I forgot about the need for FAN_EVENT_ON_CHILD in the mark->mask. It seems we can set FAN_EVENT_ON_CHILD in the ignored_mask as well but it just gets ignored currently. So we would need to propagate it even from ignore_mask to inode->i_fsnotify_mask. But send_to_group() would also need to be more careful now with ignore masks and apply them from parent only if the particular mark has FAN_EVENT_ON_CHILD in the ignore mask. Interestingly fanotify_group_event_mask() does explicitely apply ignore_mask from the parent regardless of FAN_EVENT_ON_CHILD flags. So there is some inconsistency there and it would need some tweaking...

I am thinking why do we need the duplicate and unaligned ignore mask logic in send_to_group() at all?

With fanotify the only backend using the ->handle_event() multi mark flavor, maybe we should keep it simple and let fanotify do all the specific mark ignore logic internally?

Quoted 4 lines

Overall I guess the functionality makes sense to me (in fact it is somewhat surprising it is not working like that from the beginning), API-wise it is not outright horrible, and technically it seems doable. What do you think?

I think that having ONDIR and ON_CHILD in ignored mask is source for confusion.
Imagine a mount mark with FAN_ONDIR and ignored mark (on dir inode) without FAN_ONDIR.
What should the outcome be?
Don't ignore the events on dir because ignore mask does not have ONDIR? That is not the obvious behavior that people will expect.

ON_CHILD may be a different case, but I also prefer not to deviate it from ONDIR.

The only thing I can think of to add clarification is FAN_MARK_PARENT.

man page already says:
"The flag has no effect when marking mounts and filesystems." It can also say:
"The flag has no effect when set in the ignored mask..." "The flag is implied for both mask and ignored mask when marking
directories with FAN_MARK_PARENT".

Implementation wise, this would be very simple, because we already force set FAN_EVENT_ON_CHILD for FAN_MARK_MOUNT
and FAN_MARK_FILESYSTEM with REPORT_DIR_FID, se we can also force set it for FAN_MARK_PARENT.

But maybe it's just me that thinks this would be more clear??

Thanks,
Amir.

author reviewer
#8 lore raw
Quoted 67 lines · Amir Goldstein
Quoted 55 lines · Jan Kara
Quoted 45 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

Quoted 7 lines

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.

Quoted 10 lines

Oh, right. I forgot about the need for FAN_EVENT_ON_CHILD in the mark->mask. It seems we can set FAN_EVENT_ON_CHILD in the ignored_mask as well but it just gets ignored currently. So we would need to propagate it even from ignore_mask to inode->i_fsnotify_mask. But send_to_group() would also need to be more careful now with ignore masks and apply them from parent only if the particular mark has FAN_EVENT_ON_CHILD in the ignore mask. Interestingly fanotify_group_event_mask() does explicitely apply ignore_mask from the parent regardless of FAN_EVENT_ON_CHILD flags. So there is some inconsistency there and it would need some tweaking...

Quoted 2 lines

I am thinking why do we need the duplicate and unaligned ignore mask logic in send_to_group() at all?

I agree the duplication is not good.

Quoted 3 lines

With fanotify the only backend using the ->handle_event() multi mark flavor, maybe we should keep it simple and let fanotify do all the specific mark ignore logic internally?

Well, if we leave handling of ignore marks completely to ->handle_event() then we pay the cost of indirect call even for ignored events. Not sure how much will it be visible in the overall costs, it's probably worth benchmarking and we'll see.

Quoted 22 lines
Quoted 3 lines

Overall I guess the functionality makes sense to me (in fact it is somewhat surprising it is not working like that from the beginning), API-wise it is not outright horrible, and technically it seems doable. What do you think?

I think that having ONDIR and ON_CHILD in ignored mask is source for confusion. Imagine a mount mark with FAN_ONDIR and ignored mark (on dir inode) without FAN_ONDIR. What should the outcome be?
Don't ignore the events on dir because ignore mask does not have ONDIR? That is not the obvious behavior that people will expect.

ON_CHILD may be a different case, but I also prefer not to deviate it from ONDIR.

The only thing I can think of to add clarification is FAN_MARK_PARENT.

man page already says:
"The flag has no effect when marking mounts and filesystems." It can also say:
"The flag has no effect when set in the ignored mask..." "The flag is implied for both mask and ignored mask when marking
directories with FAN_MARK_PARENT".

Quoted 6 lines

Implementation wise, this would be very simple, because we already force set FAN_EVENT_ON_CHILD for FAN_MARK_MOUNT
and FAN_MARK_FILESYSTEM with REPORT_DIR_FID, se we can also force set it for FAN_MARK_PARENT.

But maybe it's just me that thinks this would be more clear??

Yeah, I'm not sure if adding another flag that iteracts with ON_CHILD or ONDIR adds any clarity to this mess. In my opinion defining that ON_CHILD flag in the ignore mask means "apply this ignore mask to events from immediate children" has an intuitive meaning as it is exactly matching the semantics of ON_CHILD in the normal mark mask.

With ONDIR I agree things are not as obvious. Historically we have applied ignore mask even for events coming from directories regardless of ONDIR flag in the ignore mask. So ignore mask without any special flag has the implicit meaning of "apply to all events regardless of type of originating inode". I don't think we can change that meaning at this point. We could define meaning of ONDIR in ignore mask to either "ignore only events from directories" or to "ignore only events from ordinary files". But neither seems particularly natural or useful.

Another concern is that currently fanotify_mark() ignores both ONDIR and ON_CHILD flags in the ignore mask. So there is a chance someone happens to set them by accident. For the ON_CHILD flag I would be willing to take a chance and assign the meaning to this flag because I think chances for real world breakage are really low. But for ONDIR, given it is unclear whether ignore masks should apply for directory events without that flag, I think the chances someone sets it "just to be sure" are high enough that I would be reluctant to take that chance. So for ONDIR I think we are more or less stuck with keeping it unused in the ignore mask.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#9 lore raw
Quoted 27 lines
Quoted 27 lines
Quoted 3 lines

Overall I guess the functionality makes sense to me (in fact it is somewhat surprising it is not working like that from the beginning), API-wise it is not outright horrible, and technically it seems doable. What do you think?

I think that having ONDIR and ON_CHILD in ignored mask is source for confusion. Imagine a mount mark with FAN_ONDIR and ignored mark (on dir inode) without FAN_ONDIR. What should the outcome be?
Don't ignore the events on dir because ignore mask does not have ONDIR? That is not the obvious behavior that people will expect.

ON_CHILD may be a different case, but I also prefer not to deviate it from ONDIR.

The only thing I can think of to add clarification is FAN_MARK_PARENT.

man page already says:
"The flag has no effect when marking mounts and filesystems." It can also say:
"The flag has no effect when set in the ignored mask..." "The flag is implied for both mask and ignored mask when marking
directories with FAN_MARK_PARENT".

Implementation wise, this would be very simple, because we already force set FAN_EVENT_ON_CHILD for FAN_MARK_MOUNT
and FAN_MARK_FILESYSTEM with REPORT_DIR_FID, se we can also force set it for FAN_MARK_PARENT.

Quoted 8 lines
Quoted 1 line

But maybe it's just me that thinks this would be more clear??

Yeah, I'm not sure if adding another flag that iteracts with ON_CHILD or ONDIR adds any clarity to this mess. In my opinion defining that ON_CHILD flag in the ignore mask means "apply this ignore mask to events from immediate children" has an intuitive meaning as it is exactly matching the semantics of ON_CHILD in the normal mark mask.

Ok.

The only thing is, as you wrote to Srinivas, there is really no practical way to make ignore mask with ON_CHILD work on old kernels, so what can users do if they want to write a portable program? Add this mark and hope for the best?
If users had FAN_MARK_PARENT, the outcome at least would
have been predictable.
Maybe FAN_MARK_PARENT is an overkill.
Maybe what we need is FAN_MARK_IGNORED_ON_CHILD.
It's not very pretty, but it is clear.

Quoted 9 lines

With ONDIR I agree things are not as obvious. Historically we have applied ignore mask even for events coming from directories regardless of ONDIR flag in the ignore mask. So ignore mask without any special flag has the implicit meaning of "apply to all events regardless of type of originating inode". I don't think we can change that meaning at this point. We could define meaning of ONDIR in ignore mask to either "ignore only events from directories" or to "ignore only events from ordinary files". But neither seems particularly natural or useful.

TBH, I always found it annoying that fanotify cannot be used to specify a filter to get only mkdirs, which is a pretty common thing to want to be notified of (i.e. for recursive watch).
But I have no intention to propose API changes to fix that.

Quoted 10 lines

Another concern is that currently fanotify_mark() ignores both ONDIR and ON_CHILD flags in the ignore mask. So there is a chance someone happens to set them by accident. For the ON_CHILD flag I would be willing to take a chance and assign the meaning to this flag because I think chances for real world breakage are really low. But for ONDIR, given it is unclear whether ignore masks should apply for directory events without that flag, I think the chances someone sets it "just to be sure" are high enough that I would be reluctant to take that chance. So for ONDIR I think we are more or less stuck with keeping it unused in the ignore mask.

I agree with your risk assessment, so no hard objection to ON_CHILD on ignored mask.
If we go with FAN_MARK_IGNORED_ON_CHILD it will not be a concern at all.

I should be able to find some time to fix this one way or the other when we decide on the design.

Thanks,
Amir.

author reviewer
#10 lore raw
Quoted 36 lines · Amir Goldstein
Quoted 27 lines
Quoted 27 lines
Quoted 3 lines

Overall I guess the functionality makes sense to me (in fact it is somewhat surprising it is not working like that from the beginning), API-wise it is not outright horrible, and technically it seems doable. What do you think?

I think that having ONDIR and ON_CHILD in ignored mask is source for confusion. Imagine a mount mark with FAN_ONDIR and ignored mark (on dir inode) without FAN_ONDIR. What should the outcome be?
Don't ignore the events on dir because ignore mask does not have ONDIR? That is not the obvious behavior that people will expect.

ON_CHILD may be a different case, but I also prefer not to deviate it from ONDIR.

The only thing I can think of to add clarification is FAN_MARK_PARENT.

man page already says:
"The flag has no effect when marking mounts and filesystems." It can also say:
"The flag has no effect when set in the ignored mask..." "The flag is implied for both mask and ignored mask when marking
directories with FAN_MARK_PARENT".

Implementation wise, this would be very simple, because we already force set FAN_EVENT_ON_CHILD for FAN_MARK_MOUNT
and FAN_MARK_FILESYSTEM with REPORT_DIR_FID, se we can also force set it for FAN_MARK_PARENT.

Quoted 8 lines
Quoted 1 line

But maybe it's just me that thinks this would be more clear??

Yeah, I'm not sure if adding another flag that iteracts with ON_CHILD or ONDIR adds any clarity to this mess. In my opinion defining that ON_CHILD flag in the ignore mask means "apply this ignore mask to events from immediate children" has an intuitive meaning as it is exactly matching the semantics of ON_CHILD in the normal mark mask.

Quoted 6 lines

Ok.

The only thing is, as you wrote to Srinivas, there is really no practical way to make ignore mask with ON_CHILD work on old kernels, so what can users do if they want to write a portable program? Add this mark and hope for the best?

OK, this objection probably tipped the balace towards a new flag for me :)

Quoted 5 lines

If users had FAN_MARK_PARENT, the outcome at least would have been predictable.
Maybe FAN_MARK_PARENT is an overkill.
Maybe what we need is FAN_MARK_IGNORED_ON_CHILD. It's not very pretty, but it is clear.

Or how about FAN_MARK_IGNORED_MASK_CHECKED which would properly check for supported bits in the ignore mask and then we can use ON_CHILD as I wanted and we would regain ONDIR bit for future use as well?

Quoted 10 lines
Quoted 9 lines

With ONDIR I agree things are not as obvious. Historically we have applied ignore mask even for events coming from directories regardless of ONDIR flag in the ignore mask. So ignore mask without any special flag has the implicit meaning of "apply to all events regardless of type of originating inode". I don't think we can change that meaning at this point. We could define meaning of ONDIR in ignore mask to either "ignore only events from directories" or to "ignore only events from ordinary files". But neither seems particularly natural or useful.

Quoted 4 lines

TBH, I always found it annoying that fanotify cannot be used to specify a filter to get only mkdirs, which is a pretty common thing to want to be notified of (i.e. for recursive watch).
But I have no intention to propose API changes to fix that.

I see, so we could repurpose ONDIR bit in ignore mask for EVENT_IGNORE_NONDIR feature or something like that. But as you say, no pressing need...

Quoted 11 lines
Quoted 10 lines

Another concern is that currently fanotify_mark() ignores both ONDIR and ON_CHILD flags in the ignore mask. So there is a chance someone happens to set them by accident. For the ON_CHILD flag I would be willing to take a chance and assign the meaning to this flag because I think chances for real world breakage are really low. But for ONDIR, given it is unclear whether ignore masks should apply for directory events without that flag, I think the chances someone sets it "just to be sure" are high enough that I would be reluctant to take that chance. So for ONDIR I think we are more or less stuck with keeping it unused in the ignore mask.

Quoted 7 lines

I agree with your risk assessment, so no hard objection to ON_CHILD on ignored mask.
If we go with FAN_MARK_IGNORED_ON_CHILD it will not be a concern at all.

I should be able to find some time to fix this one way or the other when we decide on the design.

Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#11 lore raw
Quoted 13 lines
Quoted 4 lines

The only thing is, as you wrote to Srinivas, there is really no practical way to make ignore mask with ON_CHILD work on old kernels, so what can users do if they want to write a portable program? Add this mark and hope for the best?

OK, this objection probably tipped the balace towards a new flag for me :)

Quoted 5 lines

If users had FAN_MARK_PARENT, the outcome at least would have been predictable.
Maybe FAN_MARK_PARENT is an overkill.
Maybe what we need is FAN_MARK_IGNORED_ON_CHILD. It's not very pretty, but it is clear.

Quoted 4 lines

Or how about FAN_MARK_IGNORED_MASK_CHECKED which would properly check for supported bits in the ignore mask and then we can use ON_CHILD as I wanted and we would regain ONDIR bit for future use as well?

I don't follow the reasoning behind the name MASK_CHECKED. If anything, I would rather introduce FAN_IGNORE_MARK. The reasoning is that users may think of this "ignore mark" as a separate mark from the "inode mark", so on this "mark" the meaning of ON_CHILD flags would be pretty clear.

The fact that they are implemented as a single mark with two masks and that each mask also has some flags is an implementation detail.

If we go for FAN_IGNORE_MARK, we would disallow the combination
fanotify_mark(FAN_IGNORE_MARK, FAN_MARK_IGNORED_MASK, ...
and I am also in favor of disallowing FAN_MARK_IGNORED_SURV_MODIFY. I find it completely useless for watching children and if people still need the ignored mask that does not survive modify, they can use the old API.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

With ONDIR I agree things are not as obvious. Historically we have applied ignore mask even for events coming from directories regardless of ONDIR flag in the ignore mask. So ignore mask without any special flag has the implicit meaning of "apply to all events regardless of type of originating inode". I don't think we can change that meaning at this point. We could define meaning of ONDIR in ignore mask to either "ignore only events from directories" or to "ignore only events from ordinary files". But neither seems particularly natural or useful.

Quoted 8 lines
Quoted 4 lines

TBH, I always found it annoying that fanotify cannot be used to specify a filter to get only mkdirs, which is a pretty common thing to want to be notified of (i.e. for recursive watch).
But I have no intention to propose API changes to fix that.

I see, so we could repurpose ONDIR bit in ignore mask for EVENT_IGNORE_NONDIR feature or something like that. But as you say, no pressing need...

Alas, that does not fit nicely with the FAN_IGNORE_MARK abstraction. The inverse does:
The "mark" with ONDIR captures all the create events and the "ignore mark" without ONDIR filters out the non-mkdir.

Thanks,
Amir.

author reviewer
#12 lore raw
Quoted 13 lines · Amir Goldstein
Quoted 13 lines
Quoted 4 lines

The only thing is, as you wrote to Srinivas, there is really no practical way to make ignore mask with ON_CHILD work on old kernels, so what can users do if they want to write a portable program? Add this mark and hope for the best?

OK, this objection probably tipped the balace towards a new flag for me :)

Quoted 5 lines

If users had FAN_MARK_PARENT, the outcome at least would have been predictable.
Maybe FAN_MARK_PARENT is an overkill.
Maybe what we need is FAN_MARK_IGNORED_ON_CHILD. It's not very pretty, but it is clear.

Quoted 6 lines
Quoted 4 lines

Or how about FAN_MARK_IGNORED_MASK_CHECKED which would properly check for supported bits in the ignore mask and then we can use ON_CHILD as I wanted and we would regain ONDIR bit for future use as well?

I don't follow the reasoning behind the name MASK_CHECKED.

My idea was that with FAN_IGNORED_MASK we did the mistake that passed arguments (mask in particular) are not properly checked because we let userspace set bits which are not really used in the ignore mask. So let's fix this by properly checking the arguments and to do that safely we need a new variant of FAN_IGNORED_MASK, hence FAN_IGNORED_MASK_CHECKED...

Quoted 4 lines

If anything, I would rather introduce FAN_IGNORE_MARK. The reasoning is that users may think of this "ignore mark" as a separate mark from the "inode mark", so on this "mark" the meaning of ON_CHILD flags would be pretty clear.

Well, yes, you are speaking about effectively the same flag just under a different name :) I agree my name is poor so I'm happy if we pick another one. The only small reservation I have against the name FAN_IGNORE_MARK is that we would now have to explain in the manpage a new concept of ignore mark and tell this is just a new name for ignore mask which looks a bit silly and perhaps confusing to developers used to the old naming.

Quoted 5 lines

The fact that they are implemented as a single mark with two masks and that each mask also has some flags is an implementation detail.

If we go for FAN_IGNORE_MARK, we would disallow the combination
fanotify_mark(FAN_IGNORE_MARK, FAN_MARK_IGNORED_MASK, ...

Certainly.

Quoted 3 lines

and I am also in favor of disallowing FAN_MARK_IGNORED_SURV_MODIFY. I find it completely useless for watching children and if people still need the ignored mask that does not survive modify, they can use the old API.

Well, but FAN_IGNORE_MARK is not just for watching children. You can still ignore a single file with FAN_IGNORE_MARK. As much as I understand that FAN_MARK_IGNORED_SURV_MODIFY complicates our life, I don't think we'll ever be able to get rid of it completely so I don't see any value in disallowing it with FAN_IGNORE_MARK.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 9 lines

With ONDIR I agree things are not as obvious. Historically we have applied ignore mask even for events coming from directories regardless of ONDIR flag in the ignore mask. So ignore mask without any special flag has the implicit meaning of "apply to all events regardless of type of originating inode". I don't think we can change that meaning at this point. We could define meaning of ONDIR in ignore mask to either "ignore only events from directories" or to "ignore only events from ordinary files". But neither seems particularly natural or useful.

TBH, I always found it annoying that fanotify cannot be used to specify a filter to get only mkdirs, which is a pretty common thing to want to be notified of (i.e. for recursive watch).
But I have no intention to propose API changes to fix that.

Quoted 8 lines
Quoted 3 lines

I see, so we could repurpose ONDIR bit in ignore mask for EVENT_IGNORE_NONDIR feature or something like that. But as you say, no pressing need...

Alas, that does not fit nicely with the FAN_IGNORE_MARK abstraction. The inverse does:
The "mark" with ONDIR captures all the create events and the "ignore mark" without ONDIR filters out the non-mkdir.

Yeah, I actually like the consistency here - the ordinary mask causes the event to be generated IFF the same ignore mask/mark causes the event to be discarded. It would mean that without ONDIR events from directories are not ignored so that's a difference to current meaning of ignore masks but we can do that modification with the new flag.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw
Quoted 11 lines
Quoted 4 lines

If anything, I would rather introduce FAN_IGNORE_MARK. The reasoning is that users may think of this "ignore mark" as a separate mark from the "inode mark", so on this "mark" the meaning of ON_CHILD flags would be pretty clear.

Well, yes, you are speaking about effectively the same flag just under a different name :) I agree my name is poor so I'm happy if we pick another one. The only small reservation I have against the name FAN_IGNORE_MARK is that we would now have to explain in the manpage a new concept of ignore mark and tell this is just a new name for ignore mask which looks a bit silly and perhaps confusing to developers used to the old naming.

Right. here is a first go at that (along with a name change):

"FAN_MARK_IGNORE - This flag has a similar effect as setting the
FAN_MARK_IGNORED_MASK flag - the events in mask shall be added to or removed from the ignore mask.
Unlike the FAN_MARK_IGNORED_MASK flag, this flag also has the effect that the FAN_EVENT_ON_CHILD and FAN_ONDIR flags take effect on the ignored mask, because with FAN_MARK_IGNORED_MASK, those flags have no effect. Note that unlike the FAN_MARK_IGNORED_MASK flag, unless FAN_ONDIR flag is set with FAN_MARK_IGNORE, events on directories will not be ignored."

What I like about this name is that the command
fanotify_mark(FAN_MARK_ADD | FAN_MARK_IGNORE,

                       FAN_MARK_OPEN | FAN_EVENT_ON_CHILD, ...

sounds like spoken English ("add a rule to ignore open events (also) on children").

Please let me know if you agree with that flag name.

Apropos man page, after I am done with that, I will try to shake the dust from all the man page update patches sitting in my queue and re-submit them.

Thanks,
Amir.

author reviewer
#14 lore raw
Quoted 24 lines · Amir Goldstein
Quoted 11 lines
Quoted 4 lines

If anything, I would rather introduce FAN_IGNORE_MARK. The reasoning is that users may think of this "ignore mark" as a separate mark from the "inode mark", so on this "mark" the meaning of ON_CHILD flags would be pretty clear.

Well, yes, you are speaking about effectively the same flag just under a different name :) I agree my name is poor so I'm happy if we pick another one. The only small reservation I have against the name FAN_IGNORE_MARK is that we would now have to explain in the manpage a new concept of ignore mark and tell this is just a new name for ignore mask which looks a bit silly and perhaps confusing to developers used to the old naming.

Right. here is a first go at that (along with a name change):

"FAN_MARK_IGNORE - This flag has a similar effect as setting the
FAN_MARK_IGNORED_MASK flag - the events in mask shall be added to or removed from the ignore mask.
Unlike the FAN_MARK_IGNORED_MASK flag, this flag also has the effect that the FAN_EVENT_ON_CHILD and FAN_ONDIR flags take effect on the ignored mask, because with FAN_MARK_IGNORED_MASK, those flags have no effect. Note that unlike the FAN_MARK_IGNORED_MASK flag, unless FAN_ONDIR flag is set with FAN_MARK_IGNORE, events on directories will not be ignored."

Quoted 7 lines

What I like about this name is that the command
fanotify_mark(FAN_MARK_ADD | FAN_MARK_IGNORE,

                       FAN_MARK_OPEN | FAN_EVENT_ON_CHILD, ...

sounds like spoken English ("add a rule to ignore open events (also) on children").

Please let me know if you agree with that flag name.

Yes, with this explanation I like the new name. Thanks for the effort!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#15 lore raw
In reply to Jan Kara
Quoted 55 lines · Jan Kara
Quoted 45 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

Quoted 7 lines

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.

Quoted 10 lines

Oh, right. I forgot about the need for FAN_EVENT_ON_CHILD in the mark->mask. It seems we can set FAN_EVENT_ON_CHILD in the ignored_mask as well but it just gets ignored currently. So we would need to propagate it even from ignore_mask to inode->i_fsnotify_mask. But send_to_group() would also need to be more careful now with ignore masks and apply them from parent only if the particular mark has FAN_EVENT_ON_CHILD in the ignore mask. Interestingly fanotify_group_event_mask() does explicitely apply ignore_mask from the parent regardless of FAN_EVENT_ON_CHILD flags. So there is some inconsistency there and it would need some tweaking...

Jan,

Just a heads up - you were right about this inconsistency and I have both patches to fix it [1] and LTP test to reproduce the issue [2] and started work on the new FAN_MARK_IGNORE API.
The new API has no tests yet, but it has a man page draft [3].

The description of the bugs as I wrote them in the fix commit message:

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignored mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignored mask of group A, but before this change it does

And if group A ignored mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events

on child and on dir")

Thanks,
Amir.

[1] https://github.com/amir73il/linux/commits/fan_mark_ignore
[2] https://github.com/amir73il/ltp/commits/fan_mark_ignore
[3] https://github.com/amir73il/man-pages/commits/fan_mark_ignore

author reviewer
#16 lore raw
Quoted 87 lines · Amir Goldstein
Quoted 55 lines · Jan Kara
Quoted 45 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

Quoted 7 lines

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.

Quoted 10 lines

Oh, right. I forgot about the need for FAN_EVENT_ON_CHILD in the mark->mask. It seems we can set FAN_EVENT_ON_CHILD in the ignored_mask as well but it just gets ignored currently. So we would need to propagate it even from ignore_mask to inode->i_fsnotify_mask. But send_to_group() would also need to be more careful now with ignore masks and apply them from parent only if the particular mark has FAN_EVENT_ON_CHILD in the ignore mask. Interestingly fanotify_group_event_mask() does explicitely apply ignore_mask from the parent regardless of FAN_EVENT_ON_CHILD flags. So there is some inconsistency there and it would need some tweaking...

Jan,

Just a heads up - you were right about this inconsistency and I have both patches to fix it [1] and LTP test to reproduce the issue [2] and started work on the new FAN_MARK_IGNORE API.
The new API has no tests yet, but it has a man page draft [3].

The description of the bugs as I wrote them in the fix commit message:

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignored mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignored mask of group A, but before this change it does

Quoted 6 lines

And if group A ignored mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events

on child and on dir")

Thanks for looking into this! Yeah, the change in behavior looks OK to me.

Honza

Quoted 3 lines

[1] https://github.com/amir73il/linux/commits/fan_mark_ignore
[2] https://github.com/amir73il/ltp/commits/fan_mark_ignore
[3] https://github.com/amir73il/man-pages/commits/fan_mark_ignore

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#17 lore raw
Quoted 20 lines
Quoted 20 lines

Jan,

Just a heads up - you were right about this inconsistency and I have both patches to fix it [1] and LTP test to reproduce the issue [2] and started work on the new FAN_MARK_IGNORE API.
The new API has no tests yet, but it has a man page draft [3].

The description of the bugs as I wrote them in the fix commit message:

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignored mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignored mask of group A, but before this change it does

Quoted 9 lines
Quoted 6 lines

And if group A ignored mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events

on child and on dir")

Thanks for looking into this! Yeah, the change in behavior looks OK to me.

And I got sufficiently annoyed by our mixed terminology of "ignored mask" and "ignore mask". Man pages only use the latter and also most of the comments in code and many of the commit messages but not all of them and variable name is of course the former, so I decided to take action:

commit 6c6f07348c0c587e2bdcdb997caa30f852e818ef

Author: Amir Goldstein <[email protected]>
Date:   Tue Apr 12 13:25:34 2022 +0300

fanotify: prepare for setting event flags in ignore mask

[...]

To emphasize the change in terminology, also rename ignored_mask mark member to ignore_mask and use accessor to get only ignored events or events and flags.

This change in terminology finally aligns with the "ignore mark" language in man pages and in most of the comments.

I hope I didn't take it too far...

Thanks,
Amir.

Quoted 4 lines
Quoted 4 lines

[1] https://github.com/amir73il/linux/commits/fan_mark_ignore
[2] https://github.com/amir73il/ltp/commits/fan_mark_ignore
[3] https://github.com/amir73il/man-pages/commits/fan_mark_ignore

author reviewer
#18 lore raw
Quoted 43 lines · Amir Goldstein
Quoted 20 lines
Quoted 20 lines

Jan,

Just a heads up - you were right about this inconsistency and I have both patches to fix it [1] and LTP test to reproduce the issue [2] and started work on the new FAN_MARK_IGNORE API.
The new API has no tests yet, but it has a man page draft [3].

The description of the bugs as I wrote them in the fix commit message:

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignored mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignored mask of group A, but before this change it does

Quoted 9 lines
Quoted 6 lines

And if group A ignored mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events

on child and on dir")

Thanks for looking into this! Yeah, the change in behavior looks OK to me.

And I got sufficiently annoyed by our mixed terminology of "ignored mask" and "ignore mask". Man pages only use the latter and also most of the comments in code and many of the commit messages but not all of them and variable name is of course the former, so I decided to take action:

commit 6c6f07348c0c587e2bdcdb997caa30f852e818ef

Author: Amir Goldstein <[email protected]>
Date:   Tue Apr 12 13:25:34 2022 +0300

fanotify: prepare for setting event flags in ignore mask

[...]

Quoted 8 lines

To emphasize the change in terminology, also rename ignored_mask mark member to ignore_mask and use accessor to get only ignored events or events and flags.

This change in terminology finally aligns with the "ignore mark" language in man pages and in most of the comments.

I hope I didn't take it too far...

Forgot to respond to this one but it makes sense to me.

Honza

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#20 lore raw
Quoted 7 lines · Srinivas
Quoted 4 lines
Quoted 4 lines

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |

                      FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

Quoted 1 line
Quoted 1 line

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |

Quoted 8 lines

                      FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

This works perfectly fine on the newer kernels but does not on the older kernels.  Is there any way we could get this working too on the old 3.x and 4.x kernels? (without the need for patching etc.)

No, I don't think there is a way. Combining ignore marks on directories with mount / superblock marks was made possible only by fsnotify changes that went into 5.9 kernel (commit 497b0c5a7c06 ("fsnotify: send event to parent and child with single callback") in particular). Before that the notification core did not have information from the parent directory available when generating event for a mount mark and so ignore mask could not be applied. So only ignore marks on individual files worked until that moment.

Honza

Quoted 59 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a  process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc");  ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD | FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/"); fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

The first call tells fanotify that the inode mark on "/tmp/foo" is interested in events on children (and not only on self). The second call sets the ignored mark for open/close events.

The fix only removed the need to include the events in the first call.

Should we also interpret FAN_EVENT_ON_CHILD correctly in a call to fanotify_mark() to set an ignored mask? Possibly. But that has not been done yet.
I can look into that if there is interest.
In retrospect, FAN_EVENT_ON_CHILD and FAN_ONDIR would have been more clear as FAN_MARK_ flags, but that's too late.

Thanks,
Amir.

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#21 lore raw
In reply to Amir Goldstein
Quoted 33 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

Quoted 7 lines

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

Thinking about this again, it could also be considered a bug (although convenient at times ;), that in current upstream kernel this combination of marks results in ignoring the OPEN_PERM & CLOSE_WRITE events on children of /tmp/fio, couldn't it? We probably should not consider the parent's ignore mask for events on children to maintain compatibility with older kernels? In theory it could bite someone unexpectedly...

And to enable this convenient functionality we should rather introduce the new bit we've discussed. What do you think?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#22 lore raw
Quoted 50 lines · Jan Kara
Quoted 33 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Srinivas

If a process calls fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM, 0, "/mountpoint") no other directory exclusions can be applied.

However a path (file) exclusion can still be applied using

fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/abc"); ===> path exclusion that works.

I think the directory exclusion not working is a bug as otherwise AV solutions cant exclude directories when using FAN_MARK_MOUNT.

I believe the change should be simple since we are already supporting path exclusions. So we should be able to add the same for the directory inode.

Quoted 7 lines
Quoted 2 lines

215676 – fanotify Ignoring/Excluding a Directory not working with FAN_MARK_MOUNT (kernel.org)

Thanks for report! So I believe this should be fixed by commit 4f0b903ded ("fsnotify: fix merge with parent's ignored mask") which is currently sitting in my tree and will go to Linus during the merge (opening in a week).

Actually, in a closer look, that fix alone is not enough.

With the current upstream kernel this should work to exclude events in a directory:

Quoted 7 lines

fanotify_mark(fd, FAN_MARK_ADD, FAN_EVENT_ON_CHILD |
FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK |
FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, "/tmp/fio/");

Thinking about this again, it could also be considered a bug (although convenient at times ;), that in current upstream kernel this combination of marks results in ignoring the OPEN_PERM & CLOSE_WRITE events on children of /tmp/fio, couldn't it? We probably should not consider the parent's ignore mask for events on children to maintain compatibility with older kernels? In theory it could bite someone unexpectedly...

Quoted 3 lines

And to enable this convenient functionality we should rather introduce the new bit we've discussed. What do you think?

I kind of made this "convenient bug" public on several occasions. Considering how useful this bug is, I think it is more likely that someone will be bit by "fixing" the "bug" in a stable kernel that is it likely for someone to be bit by the "bug".

Also, if taking away the "convenient bug" from stable kernels without providing the new API is not very fair. and making sure that the "bug fix" is not backported to any stable kernel is not sustainable.

IOW, I do not see good coming from changing behavior now. We can document that behavior of ON_CHILD/ONDIR in ignored mask is "undefined" and then make the behavior defined with a new FAN_MARK_IGNORED_MASK_* flag.

Thanks,
Amir.