gpt4 book ai didi

c++ - setuid 和 getuid 似乎不起作用

转载 作者:太空狗 更新时间:2023-10-29 11:45:12 25 4
gpt4 key购买 nike

我正在编写一个访问 Linux 原始套接字的 Qt 应用程序,因此我需要 root 权限才能运行该程序。所以我这样做了:

在我的程序中,开头是这样的:

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

然后,我以 root 身份执行“chmod 6777 myProgram”。

但是,当我尝试以普通用户身份运行它时,它说:“你必须是 root 才能运行这个程序。UID = 1002”,其中 1002 是我当前使用的用户 ID。

任何人都可以发布线索吗?

谢谢

最佳答案

您混淆了 getuid()geteuid()。来自 getuid() 的手册页:

The getuid() function returns the real user ID of the calling process. The geteuid() function returns the effective user ID of the calling process.

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.

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. 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.

对于 setuid 程序,该文件需要由您要为其设置 setuid 的用户标识所有,在大多数情况下为 root。

关于c++ - setuid 和 getuid 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com