为什么有效的UID还是不为0?

Why is effective UID still not 0?

所以我从 here 那里得到了下面的代码。但是,当我按照命令操作时:

sudo chown root:root a.out
sudo chmod u+s a.out

它仍然不会 运行 有效 uid 为 0。

这是代码:

#define _POSIX_C_SOURCE 200112L // Needed with glibc (e.g., linux).
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void report (uid_t real) {
    printf ("Real UID: %d Effective UID: %d\n", real, geteuid());
}

int main (void) {
    uid_t real = getuid();
    report(real);
    seteuid(real);
    report(real);
    return 0;
}

输出为:

Real UID: 1000 Effective UID: 1000
Real UID: 1000 Effective UID: 1000

我认为它应该在的地方:

Real UID: 1000 Effective UID: 0
Real UID: 1000 Effective UID: 1000

getuid 函数returns 仅真实用户id。用户进程的真实用户id还是1000,1000存放在real变量中

因此,在调用 seteuid 函数时,您再次将 1000 设置为有效用户 id.But 实际上,您必须在该位置设置 0。所以只有有效用户id没有改变。

尝试下面的方法,它会正常工作。

#define _POSIX_C_SOURCE 200112L // Needed with glibc (e.g., linux).
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void report (uid_t real) {
    printf ("Real UID: %d Effective UID: %d\n", real, geteuid());
}

int main (void) {
    uid_t real = getuid();
    report(real);
    seteuid(0);
    report(real);
    return 0;
}

显然正如@MarkPlotnick 所说。

cd to the directory where that a.out is. Run df . to get the mount point. Run mount | grep themountpoint and see what the filesystem type and mount options are.

如果说 nosuid 那么您需要重新挂载文件系统的那部分而不使用 nosuid 标志。

这个网站 here 很好地解释了如何做到这一点。