更改内核中的文件权限

标签 c linux file-permissions kernel-module

我正在编写内核模块(Linux 中的 C),我想更改其中其他文件的权限。 任何解决方案? 因为我在内核中,所以我不能使用 chmod 系统调用和... 谢谢你的帮助

这是我的生成文件:

> obj-m += ca.o
> 
>     all:
>       make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
>     
>     clean:
>       make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

这是我的代码:

> #include <linux/string.h>
> #include <linux/mm.h>
> /* Snip, tons of includes (all of them :))*/
> #include <linux/delay.h> .... int procfile_write(struct file *file,
> const char *buffer, unsigned long
> count,
>          void *data) { ...    sys_chmod(path, per); ... } ...

Making 时给出警告:

WARNING: "sys_chmod" [file] undefiened

并且在使用“sudo insmod”加载模块时出现此错误:

Unknown sybol in module

似乎这个错误特别发生在内核模块中。任何想法?再次感谢!

最佳答案

欢迎使用 stackoverflow! IIRC 你想要 sys_chmod()

From the Linux Kernel Mailing List

On Thu, Feb 20, 2003 at 11:10:27PM +0100, Andrea Arcangeli wrote: On Thu, Feb 20, 2003 at 12:40:43PM -0500, Jeff Garzik wrote:

On Thu, Feb 20, 2003 at 11:04:37PM +0530, Prasad wrote:

Is there a way using which i could invoke a syscall in the kernel space? The syscall is to be run disguised as another process. The actual

调用 sys_whatever()。查看内核代码以获取示例。

内核已经在 各个地方。系统读,系统写, open_filp、sys_close 和其他 从内核调用函数是安全的 代码——尽管不鼓励这样做。 init/do_mounts.c 是一个特别 烦人的情况,这是一个很大的原因 klibc 需要合并。系统调用 应该从用户空间,而不是 内核。

人们开始担心,因为这不是您可以在内核中做的事情(除非您知道自己在做什么)。如果你只想更改某个事件的权限,请使用 inotify 从用户空间执行此操作或类似的。

免责声明:

这是我在另一个内核模块中找到的一些代码,它使用了 sys_* 调用:

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/syscalls.h>
/* Snip */

int openflags = O_WRONLY|O_CREAT;
if (ml != 1)
        openflags |= O_TRUNC;
wfd = sys_open(collected, openflags, mode);

if (wfd >= 0) {
    sys_fchown(wfd, uid, gid);
    sys_fchmod(wfd, mode);
    state = CopyFile;
}

还发现:

asmlinkage long sys_rename(const char __user *oldname, const char __user *newname);
asmlinkage long sys_chmod(const char __user *filename, mode_t mode);
asmlinkage long sys_fchmod(unsigned int fd, mode_t mode);

include/linux/syscalls.h

请注意,我已经有一段时间没有做任何内核方面的工作了。 检查这是否是用于 chmod 内容的适当接口(interface),并且您没有使用任何其他可能实现安全 Hook 的调用的快捷方式。

此外, This link contains information on syscalls 及其符号。还有 Here 是用户空间 API 系统调用及其在内核中实现位置的快速引用。

关于更改内核中的文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1348358/

相关文章:

php - 除了 docker 容器中的两个用户之外,如何为我的桌面 Ubuntu 机器的用户授予对 docker 卷的完全权限?

c++ - 在有效时间内将正整数写为 2 的幂和的方法总数

c - GtkSourceView/GtkSourceBuffer - 如何用红色标记线条并显示图标

c语言字符初始化

Java .jar 文件在存在时找不到系统文件并且遵循正确的路径

linux - 使用 linux 命令 sed 选择多行

c - 是否可以连接两个管道/ socket ?

linux - 无法将tensorflow升级到1.3.0

node.js - Node Express Unix 域套接字权限

python-3.x - 在Python中写入远程服务器上的文件