3

I am writing a kernel module that hijacks several system calls. Several of the system calls take paths as parameters. For my purposes, I need to work with full paths, but since these parameters are passed as strings, it could be anything like "documents", "/home/main/../bob", or "../lib". I need to get the real paths of these references, but I don't know which function to use or which information I need (e.g., the current working directory, etc.).

The kernel obviously has some way of making paths normalized so it can perform operations necessary for SELinux, IMA, and a bunch of other things. How can I do that?

Melab
  • 2,594
  • 7
  • 30
  • 51

1 Answers1

0
char *get_full_path(int dfd, const char *__user fname) 
{    
struct path tmp_path;
unsigned int lookup_flags = LOOKUP_EMPTY;

#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
    int error = user_path_at(dfd, fname, lookup_flags, &tmp_path);
#else
    int empty = 0;
    int error = user_path_at_empty(dfd, fname, lookup_flags, &tmp_path, &empty);
#endif
if (0 == error) 
{
    char tpath[512] = { 0 };
    char *p_full_path = d_path(&tmp_path, tpath, 512);      
    return p_full_path;
}   
return "";
}
ytfrdfiw
  • 29
  • 5