Score:0

How can I monitor deletion of /dev/null

mx flag

Usually I use inotfiy_add_watch to monitor deletion of files, it works for regular files or even most device files(/dev/zero,etc...), but fails for /dev/null. It seems that any operations to it will not trigger inotify events. Why? Is there any other way to monitor rm /dev/null? thx!

djdomi avatar
za flag
why would you do it? i mean after root/sudo can do that
mx flag
cause I am working on a shared server that many modules or program with root permission are running on and recently /dev/null was deleted a couple of times. I am trying to find out who did this.
us flag
You should follow the principle of running programs with least required privileges.Your current setup seems way too fragile.
djdomi avatar
za flag
i am unsure if `chattr +i /dev/null` might work but you should remember that it may be a dangerous situation
djdomi avatar
za flag
Basically, this question has already some solutions like questions [266717] or [899956]
Score:1
cn flag

On Linux, inotify is limited in that it is file system specific. Documentation says that "various pseudo-filesystems such as /proc, /sys, and /dev/pts are not monitorable with inotify"

Linux audit system is capable of logging arbitrary system calls. An example file monitoring watch you might put in /etc/audit/rules.d/specialdev.rules could be:

-a always,exit -F arch=b64 -S unlink,unlinkat,rename,renameat -F success=1 -F path=/dev/null -k specialdev
-a always,exit -F arch=b32 -S unlink,unlinkat,rename,renameat -F success=1 -F path=/dev/null -k specialdev

Note the filtering to specific system calls that actually delete the file, not just write to it. Query recent entries with something like:

ausearch --start yesterday --end now  --key specialdev

You can tell from these log entries when it happened, the process ID and comm, various flavors of uid, and selinux context. Good clues, but you still have to do a bit of looking for the broken shell script or whatever.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.