get fd from file pointer in kernel space

眉间皱痕 提交于 2019-12-06 14:10:33

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".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!