Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i get absolute path in kernel space from file descriptor

Tags:

c

linux-kernel

I am trying to hook unlinkat.my hooking function.

but i get only file name instead of absolute path.so i want absolute path to compare string.when i try rm -r than i get only file name if i get absolute path then it works.so please tell me how i get absolute path.
my code is

long mw_sys_unlink(int dfd, const char *filename ,int flag)
{
        long ret;
        if( strstr(filename,"/tmp/a/"))
        {
                printk(KERN_INFO "file %s has not been deleted by kernel module\n", filename);
                return -1;
        }
        else
        {
                ret = orig_sys_unlink(dfd ,filename,flag);
                printk(KERN_INFO "file %s has been deleted", filename);
                return ret;
        }
}
like image 646
vikas_saini Avatar asked Aug 24 '15 06:08

vikas_saini


1 Answers

Try the following:

    char *tmp = (char*)__get_free_page(GFP_TEMPORARY);

    file *file = fget(dfd);
    if (!file) {
        goto out
    }

    char *path = d_path(&file->f_path, tmp, PAGE_SIZE);
    if (IS_ERR(path)) {
        printk("error: %d\n", (int)path);
        goto out;
    }

    printk("path: %s\n", path);
out:
    free_page((unsigned long)tmp);
like image 154
Arthur Avatar answered Nov 03 '22 02:11

Arthur