intercepting file system system calls

后端 未结 4 584
轮回少年
轮回少年 2021-02-08 09:58

I am writing an application for which I need to intercept some filesystem system calls eg. unlink. I would like to save some file say abc. If user deletes the file then I need t

相关标签:
4条回答
  • 2021-02-08 10:37

    One suggestion could be Filesystems in Userspace (FUSE.) That is, write a FUSE module (which is, granted, in userspace) which intercepts filesystem-related syscalls, performs whatever tasks you want, and possibly calls the "default" syscall afterwards.

    You could then mount certain directories with your FUSE filesystem and, for most of your cases, it seems like the default syscall behavior would not need to be overridden.

    0 讨论(0)
  • 2021-02-08 10:43

    If you want to handle deletions only, you could keep a "shadow" directory of hardlinks (created via link) to the files being watched (via inotify, as suggested by Graham Lee).

    If the original is now unlinked, you still have the shadow file to handle as you want to, without using a kernel module.

    0 讨论(0)
  • 2021-02-08 10:55

    As far as hooking into the kernel and intercepting system calls go, this is something I do in a security module I wrote:

    https://github.com/cormander/tpe-lkm

    Look at hijacks.c and symbols.c for the code; how they're used is in the hijack_syscalls function inside security.c. I haven't tried this on linux > 3.0 yet, but the same basic concept should still work.

    It's a bit tricky, and you may have to write a good deal of kernel code to do the file copy before the unlink, but it's possible here.

    0 讨论(0)
  • 2021-02-08 11:02

    You can watch unlink events with inotify, though this might happen too late for your purposes (I don't know because I don't know your purposes, and you should experiment to find out). The in-kernel alternatives based on LSM (by which I mean SMACK, TOMOYO and friends) are really for Mandatory Access Control so may not be suitable for your purposes.

    0 讨论(0)
提交回复
热议问题