简体   繁体   English

setuid和getuid似乎不起作用

[英]setuid and getuid seems not working

I'm writing an Qt application accessing Linux raw socket, so I need root privilege to run the program. 我正在编写访问Linux原始套接字的Qt应用程序,因此我需要root特权才能运行该程序。 So I did this: 所以我这样做:

In my program, I have this at the beginning: 在我的程序中,我一开始就有这个:

if (getuid() != 0)
{
    fprintf(stderr, "You must be root to run this program. UID = %i\n", getuid());
    exit(-1);
}

Then, I did "chmod 6777 myProgram" as root. 然后,我以root用户身份执行了“ chmod 6777 myProgram”。

But, when I try to run it as a normal user, it says: "You must be root to run this program. UID = 1002", where 1002 is the user ID I'm currently using. 但是,当我尝试以普通用户身份运行它时,它说:“您必须是root用户才能运行此程序。UID= 1002”,其中1002是我当前正在使用的用户ID。

Can any one post a clue? 任何人都可以发布线索吗?

Thanks 谢谢

You're mixing up getuid() and geteuid() . 您正在混合getuid()geteuid() From the man page of getuid() : getuid()的手册页中:

The getuid() function returns the real user ID of the calling process. getuid()函数返回调用过程的真实用户ID。 The geteuid() function returns the effective user ID of the calling process. geteuid()函数返回调用过程的有效用户ID。

The real user ID is that of the user who has invoked the program. 真实的用户ID是调用程序的用户的ID。 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. 由于有效的用户ID在执行设置用户ID模式的过程期间为该过程提供了额外的权限,因此getuid()用于确定调用过程的实际用户ID。

Linux's man page is even more succinct (that previous one was from Mac OS X): Linux的手册页更加简洁(以前的手册来自Mac OS X):

When a normal program is executed, the effective and real user ID of the process are set to the ID of the user executing the file. 当执行普通程序时,该过程的有效用户ID和实际用户ID设置为执行文件的用户的ID。 When a set ID program is executed the real user ID is set to the calling user and the effective user ID corresponds to the set ID bit on the file being executed. 当执行设置ID程序时,实际用户ID被设置为主叫用户,而有效用户ID对应于正在执行的文件上的设置ID位。

For setuid programs, the file needs to be owned by the userid that you want to setuid to, which is in most cases root. 对于setuid程序,该文件必须由您要设置为setuid的用户标识(在大多数情况下为root)拥有。

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

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