c - 在内核模块中为根用户提供权限

标签 c linux permissions linux-kernel linux-device-driver

Linux 内核 5.0.0-37

我正在编写一个函数来管理权限,作为指向 struct inode_operations 的函数指针提供.这是一个简化的 stub 实现:

#include <linux/cred.h>

int pfsw_permission(struct inode *ino, int op){
    if(uid_eq(INIT_USER->uid, get_current_user()->uid)){
        printk(KERN_INFO "Current user is root\n");
    } else {
        printk(KERN_INFO "Current user is not root\n");
    }
    if(op & (MAY_READ | MAY_WRITE)){
        return 0;
    }

    return -EACCES;
}

当编译包含此函数的内核模块并尝试加载它时 dmesg 显示以下错误:

Unknown symbol root_user (err -2)

我认为这是由于 INIT_USER 宏来自 include/linux/sched/user.h定义为

extern struct user_struct root_user;
#define INIT_USER (&root_user)

问题: 为什么符号 root_user 已声明,但未定义?如何正确使用INIT_USER

最佳答案

root_user 不是 seem使用 EXPORT_SYMBOL 导出通过 linux 内核;因此您不能从模块中使用它。

查看its definition我们可以看到 uid 值设置为 GLOBAL_ROOT_UID。这是在 include/linux/uidgid.h 中定义的一个宏,基本上只是一个从 0 到 kuid_t 的类型转换,所以如果你需要的话,你可以只使用那个宏UID。

所以...

Why is the symbol root_user declared, but not defined?

符号已定义。虽然它没有导出,因此您不能从模块中使用它。

How to use INIT_USER correctly?

“不是”。你不能使用它。

关于c - 在内核模块中为根用户提供权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475335/

相关文章:

无法在简单的 C 代码片段中使用 2 个 getchar() 函数

计算 C 中的熵

c - 在 makefile 的帮助下编译后权限被拒绝

regex - 如何过滤 Bash 正则表达式(Linux)中除一个之外的所有值?

powershell - 在 Windows Server 2008 R2 上将 HKCR\CLSID\* key 的所有者更改为管理员

c - 从字符串中删除多余的空格(就地)

c++ - 指针可以带参数吗?这3种指针有什么区别

c - 用于简单 GUI 的编程语言或库?

PHP: move_uploaded_file(): 无法移动 '/

angular - Firebase消息传递: Can I check if the user already has a token without invoking the browser prompt?