2
votes

Given a struct file, is it possible to get the associated file descriptor in linux kernel space? I am trying to change permissions using either sys_chmod or sys_fchmod. One takes a file descriptor the other expects a filename from user space. I can figure out how to get the filename but how would I cast it to a user space pointer?

Thanks

1

1 Answers

2
votes

The function you're after is chmod_common:

static int chmod_common(struct path *path, umode_t mode)

Which takes a path and the mode you want to set. Unfortunately, as you noticed, it's static and obviously not exported. So you could go multiple ways:

  • Replicate whatever it does in a function of your own
  • Get "the file descriptor" from struct file (ugly)
  • Find a way to call sys_chmod

Now sys_chmod expects a user pointer but you're in the kernel. Here's what you could do to trick it:

mm_segment_t oldfs = get_fs();

char __user *userptr;
userptr = (char __user __force *) kernptr;

set_fs(KERNEL_DS);

/* call sys_chmod */

set_fs(oldfs);

All this is very much in line with "things you never should do in the Kernel".