Get the path that fd points to in the linux kernel module

is currently completing a simple kernel module coding job that simulates the rootkit hidden process. According to the observation of strace ps , the ps program traverses the directory through open / proc opening fd, and getdents < fd > .

The function prototype of

sys_getdents is as follows:

// linux/syscalls.h
asmlinkage long (*sys_getdents) (unsigned int fd, struct linux_dirent __user *dirent, unsigned int count);
The structure of

struct linux_dirent is as follows:

// linux/fs/readdir.c
struct linux_dirent {
    unsigned long   d_ino;
    unsigned long   d_off;
    unsigned short  d_reclen;
    char            d_name[1];
};

because you don"t want to pollute your access to other directories, you want to determine the path that fd points to when you add hooks to sys_getdents .

when programming in user mode, we can use readlink / proc/self/fd/ < fd > to get it, but is there any good way to do this in kernel state? I hope you will not hesitate to give me your advice, thank you!

Mar.02,2021

idea: get the path through fd-> struct file-> struct path-> path

  1. fget (fd) get struct file
  2. d_path (file.path) get the path

= the above is the original text of the answer, and the following is the main supplement =

uses current- > files instead of fget,. The code is as follows:

struct files_struct *files;
struct file *file;

int buflen = 256;
char buf[buflen];
char *path;

files = current->files;
spin_lock(&files->file_lock);
file = fcheck_files(files, fd);
path = d_path(&file->f_path, buf, buflen);
spin_unlock(&files->file_lock);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4bce1-44e7e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4bce1-44e7e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?