简体   繁体   English

在Linux中使用setuid位

[英]Using the setuid bit in Linux

I have this C file: 我有这个C文件:

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

int main(void)
{
    printf("%s\n", getlogin());
    printf("%i\n", getuid());
}

I compile it, set the UID and GID both to root and set the setuid bit, so that it looks like this: 我编译它,将UID和GID都设置为root并设置setuid位,使其看起来像这样:

-rwsrwsr-x 1 root    root    8735 Apr  8 19:51 a.out

However when I call $ ./a.out I still get: 但是,当我打电话给$ ./a.out我仍然得到:

user
1000

What am I doing wrong? 我究竟做错了什么?

The real user ID is still the user that called the program, but the effective user ID is root. 实际用户ID仍然是调用程序的用户,但是有效用户ID是root。 In a setuid program, they are not the same. 在setuid程序中,它们是不同的。

To get the effective user ID, call geteuid() . 要获取有效的用户ID,请调用geteuid() You can also use cuserid() to get the name associated with the effective user ID. 您还可以使用cuserid()获取与有效用户ID关联的名称。

Your program has got only the permission to change its uid. 您的程序仅具有更改其uid的权限。 To actually switch to root you have to call setuid(0) in it. 要实际切换到root用户,必须在其中调用setuid(0) Please have a look here 在这里看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM