2

According to this, fanotify has “the possibility to read or modify files before access by other applications”. However, I can't find any example showing how this can be done. From what I understand, fanotify is used for event monitoring and can only modify fanotify mark.

So my question is can fanotify modify files? If yes, how can it be done?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Rexalderan
  • 23
  • 4
  • This is what I understood too, but I don't see how you could do this except by basically overwriting the file, using the open fd that is passed to you from the API, which is not that useful. – meuh Sep 06 '17 at 10:27

1 Answers1

1

The user of fanotify can modify the file before an application accesses it. But this doesn't happen through fanotify. You access and modify the actual file, you aren't presenting a different view to the application.

When you're getting permission events through fanotify, the file access operation remains blocked until you reply to the fanotify interface. While the application is blocked, you can do whatever you want, including reading and modifying the file that the application wanted to access. Once you've sent back the response to the fanotify permission event, assuming that it's a positive response, the application will access the file and find it in the state that you may have modified.

Note that only open (and access) trigger permission events, not read and write. So with fanotify, you can intercept file opening, but you can only monitor subsequent access to the file data.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • So to clarify, when I'm using `open`, it will trigger permission events. While I haven't sent back the event response, I have the fd and can modify the file (directly). When I return the event response (i.e. allow access), it will open the file in the modified state. However, functions like `write` doesn't trigger permission event and thus I can't modify files prior to writing. Do I get this right? – Rexalderan Sep 07 '17 at 01:48