0

我试图弄清楚我的编辑正在观看哪些文件。我了解到计算 inotify fds 的数量/proc/${PID}/fd可能的,我的问题是:是否可以通过一个进程转储监视的 inode 列表

更新:我已经更新了一个可行的解决方案,并感谢您在此处提供有用的参考。

更新 2:好吧,最近我发现kallsyms_lookup_name(和更多符号) not export since Linux Kernel v5.7,所以如果其他人关心,我决定更新我自己的解决方案。

4

1 回答 1

0

解决了。在khookkprobe中使用的机制的帮助下,我只是简单地挂钩并使用来窃取.__x64_sys_inotify_add_watchuser_path_atdentry

下面列出了代码片段,这里提供了我的工作解决方案。

#define IN_ONLYDIR          0x01000000  /* only watch the path if it is a directory */
#define IN_DONT_FOLLOW      0x02000000  /* don't follow a sym link */


//regs->(di, si, dx, r10), reference: arch/x86/include/asm/syscall_wrapper.h#L125
//SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, u32, mask)
KHOOK_EXT(long, __x64_sys_inotify_add_watch, const struct pt_regs *);
static long khook___x64_sysinotify_add_watch(const struct pt_regs *regs)
{
    int wd;
    struct path path;
    unsigned int flags = 0;
    char buf[PATH_MAX];
    char *pname;
    // decode the registers
    int fd = (int) regs->di;
    const char __user *pathname = (char __user *) regs->si;
    u32 mask = (u32) regs->dx;

    // do the original syscall
    wd = KHOOK_ORIGIN(__x64_sys_inotify_add_watch, regs);
    // get the pathname
    if (!(mask & IN_DONT_FOLLOW))
        flags |= LOOKUP_FOLLOW;
    if (mask & IN_ONLYDIR)
        flags |= LOOKUP_DIRECTORY;
    if ( wd>=0 && (user_path_at(AT_FDCWD, pathname, flags, &path)==0) )
    {
        pname = dentry_path_raw(path.dentry, buf, PATH_MAX); //"pname" points to "buf[PATH_MAX]"
        path_put(&path);
        printk("%s, PID %d add (%d,%d): %s\n", current->comm, task_pid_nr(current), fd, wd, pname);
    }
    return wd;
}
于 2020-02-23T14:40:50.050 回答