struct task_struct 成员?

发布于 2025-01-02 07:17:19 字数 688 浏览 1 评论 0原文

我已经编写了在 /proc 文件上读取和写入的模块,并且工作正常,但是当我创建下面所示的权限函数时,想要使用它的权限,它给了我错误(基本上我希望每个人都可以读取该文件,但只有 root 可以写在里面)。

int my_permission(struct inode *inode, int op)
{
if(op == 4||(op == 2 && current->euid = 0))  //euid is not a member of task_struct
return 0;
return -EACCES;
}
const  struct inode_operations my_iops = {
.permission = my_permission,
};

但它给了我如下错误:

/home/karan/practice/procf/testproc1.c: In function ‘my_permission’:
/home/karan/practice/procf/testproc1.c:50:32: error: ‘struct task_struct’ has no member named ‘euid’

我认为 task_struct 中还有其他成员指向 user id 。我对解决方案以及用于的 task_struct 成员字段的描述感兴趣。

问候 卡兰

i have written module which reads and write on /proc file and is working fine but want to use permissions with it when i make the function for permissions shown below its gives me error (basically i want every one could read the file but only root could write in it).

int my_permission(struct inode *inode, int op)
{
if(op == 4||(op == 2 && current->euid = 0))  //euid is not a member of task_struct
return 0;
return -EACCES;
}
const  struct inode_operations my_iops = {
.permission = my_permission,
};

but its giving me error as follow:

/home/karan/practice/procf/testproc1.c: In function ‘my_permission’:
/home/karan/practice/procf/testproc1.c:50:32: error: ‘struct task_struct’ has no member named ‘euid’

I think there is other member in task_struct which points to user id . I am interested in solution as well as description of task_struct members field used for.

regards
karan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2025-01-09 07:17:19

请参阅 include/linux/cred.h:

    #define current_euid()          (current_cred_xxx(euid))  
    #define current_cred_xxx(xxx)                   \
    ({                                              \
            current_cred()->xxx;                    \
    })

因此 current_euid() 依赖于 current_cred():

    /**
     * current_cred - Access the current task's subjective credentials
     *
     * Access the subjective credentials of the current task.  RCU-safe,
     * since nobody else can modify it.
     */
    #define current_cred() \
            rcu_dereference_protected(current->cred, 1)

因此,对于您的问题,要进行有效的 UID 比较,请查看 /fs/exec.c:

    if (current_euid() == current_uid() && current_egid() == current_gid())
    bprm->cred->euid = current_euid();
            bprm->cred->euid = inode->i_uid

与您的程序对比:

    if (current_euid() == 0)

这意味着即使用户不是以UID=0登录的,也会被有效地视为UID==0?听起来很危险

See include/linux/cred.h:

    #define current_euid()          (current_cred_xxx(euid))  
    #define current_cred_xxx(xxx)                   \
    ({                                              \
            current_cred()->xxx;                    \
    })

and so current_euid() falls back on current_cred():

    /**
     * current_cred - Access the current task's subjective credentials
     *
     * Access the subjective credentials of the current task.  RCU-safe,
     * since nobody else can modify it.
     */
    #define current_cred() \
            rcu_dereference_protected(current->cred, 1)

So for you problem, to do effective UID comparison, take a look at /fs/exec.c:

    if (current_euid() == current_uid() && current_egid() == current_gid())
    bprm->cred->euid = current_euid();
            bprm->cred->euid = inode->i_uid

contrast with your program:

    if (current_euid() == 0)

which means even if the user is not logined with UID=0, it will be treated effecively as UID==0? Sounds dangerous

温柔一刀 2025-01-09 07:17:19
if(op == 4||(op == 2 && current->euid = 0))
  1. 您确定要将 0 分配给 current->euid 吗?也许你是想比较。
  2. current 在哪里定义的?我假设它是一个 struct task_struct ,但如果是这样,那么 euid 是一个指针吗?或者current是指向struct task_struct的指针?如果 euidcurrent 都不是指针,只需将 -> 替换为

有点偏离主题,但是:

const  struct inode_operations my_iops = {
.permission = my_permission,
};

这种事情会让维护你代码的程序员想在你睡梦中杀死你。使用 typedef 提高可读性。不要声明全局变量;如果这样做,请不要在函数之后声明它们。当声明具有部分赋值的结构时,在声明之后执行它更具可读性。

我知道它是一个测试,或者是不完整的代码,或者是没有人需要维护的东西。但您可能想在几年后阅读本文,当您这样做时,您将花费两倍的时间来尝试找到您正在寻找的解决方案。你把这个贴在SO上,那里有一些纳粹风格的人,比如我自己,在看到这样的声明后今晚将难以入睡。

if(op == 4||(op == 2 && current->euid = 0))
  1. Are you sure you want to assign 0 to current->euid ? Maybe you meant to compare.
  2. Where is current defined? I'm assuming its a struct task_struct but if so, is euid a pointer? or is current a pointer to a struct task_struct? If neither euid nor current are pointers just replace -> with .

Kind of out of topic, but:

const  struct inode_operations my_iops = {
.permission = my_permission,
};

Is the kind of thing that would make a programmer maintaining your code want to kill you in your sleep. Use typedef for readability. Don't declare global variables; if you do, don't declare them after a function. When declaring a struct with partial assignments its more readable to do it after the declaration.

I know its a test, or an incomplete code, or something no one will need to maintain. But you might want to read this in a couple of years, and when you do you'll spend twice as much trying to find the solution you are looking for. And you are posting this on SO where there are some style nazis, like myself, that will have problems to sleep this night after seeing a declaration like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文