繁体   English   中英

程序仍然能够在setuid和setgid之后仅打开root用户文件

[英]Program still able to open root only files after setuid and setgid

我正在编写一个程序,该程序将基于身份验证以不同的用户身份运行。 该程序是setuid root,并在删除特权之前使用PAM身份验证。

我正在使用setuid()setgid()在身份验证后放弃特权。 但是显然这还不够,因为在调用这些程序后,我的程序似乎仍然可以访问仅open()根文件。

有什么建议么?

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

// Code to drop Priv
int u = 1000, g = 1000;

printf("Starting User %d Group %d\n", (int) getuid(), (int) getgid());
printf("Setting User %d Group %d\n", u, g);
if (setgid(g) || setuid(u)) {
    printf("Could not set uid or gid %d", errno);
    return 0;
}
printf("Have set User %d Group %d\n", (int) getuid(), (int) getgid());

其输出为:

Starting User 0 Group 0
Setting User 1000 Group 1000
Have set User 1000 Group 1000

在调用此代码后,我的程序仍可以打开仅具有root权限的文件:

-rw-r----- 1 root   root    505 May  5  2015 rootFile

打开的代码非常简单:

// Later
int fd = open("rootFile", O_RDONLY);
if (fd == -1) {
    // Never happens
} else {
    // Happens
}

看一下这篇文章: https : //www.securecoding.cert.org/confluence/display/c/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges

看起来您可能对补充团体有“问题”。 在设置gid和uid之前

setgroups(0, NULL);

并且您的代码应该可以正常工作。

暂无
暂无

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

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