加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – seteuid / geteuid:程序始终具有root id

发布时间:2020-12-16 09:33:52 所属栏目:百科 来源:网络整理
导读:我目前正试图弄清楚SUID位和相应的函数seteuid和geteuid是如何工作的.所以我写了这个小程序: #include unistd.h#include stdlib.h#include stdio.h#include errno.hint main(int argc,char **argv) { printf("oldid %dn",geteuid()); if(seteuid(0) == -1)
我目前正试图弄清楚SUID位和相应的函数seteuid和geteuid是如何工作的.所以我写了这个小程序:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main(int argc,char **argv) {

    printf("oldid %dn",geteuid());
    if(seteuid(0) == -1)
        perror("seteuid faied");
    printf("newid %dn",geteuid());

    return 0;

}

编译它,将其所有者更改为root,并将文件所有者的s位更改为:

[chris@myhost Test]$ls -l test
-rwsr-xr-x 1 root root 4830 Apr  5 07:56 test

但是产生的输出看起来像这样:

[chris@myhost Test]$./test
oldid 0
newid 0

这是我不明白的事情.根据我的发现,geteuid的第一次调用实际上应该返回该程序的调用者的用户ID(即chris – 我的ID将是1000),但程序将root显示为有效用户id.任何人都可以解释为什么会这样吗?

解决方法

从我Mac上的geteuid()的手册页(OS X 10.6.7):

The real user ID is that of the user who has invoked the program. As the effective user ID gives the process additional permissions during execution of “set-user-ID” mode processes,getuid() is used to determine the real-user-id of the calling process.

由于您已设置suid位,因此程序的有效用户ID是从执行开始的文件所有者(root).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读