从内核空间中的文件指针获取fd [英] get fd from file pointer in kernel space

查看:186
本文介绍了从内核空间中的文件指针获取fd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个结构文件,是否有可能在Linux内核空间中获取相关的文件描述符?我正在尝试使用sys_chmod或sys_fchmod更改权限.一个使用文件描述符,另一个则期望来自用户空间的文件名.我可以弄清楚如何获取文件名,但是如何将其转换为用户空间指针?

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?

谢谢

推荐答案

您追求的功能是chmod_common:

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

使用path和要设置的模式.不幸的是,正如您所注意到的,它是静态的,并且显然没有导出.因此,您可以采取多种方式:

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:

  • 复制自己的功能中的所有操作
  • struct file获取文件描述符"(丑陋)
  • 找到一种调用sys_chmod
  • 的方法
  • 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

现在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);

所有这些都非常符合您在内核中不应该做的事情" .

这篇关于从内核空间中的文件指针获取fd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆