我进行了一些试验并注意到一些奇怪的事情:在位于 bash 的副本上设置 setuid 位/usr/bin/bash-test
似乎没有效果。当我运行一个实例时bash-test
,我的主目录没有设置为/root
,当我whoami
从运行命令时bash-test
,我的用户名没有被报告为是root
,这表明它bash-test
没有以 root 身份运行。但是,如果我将 setuid 位设置为 on whoami
,则正如预期的那样,我被报告为任何 shell 中的 root 用户。
我也尝试设置 setuid 位/usr/bin/bash
并观察到相同的行为。
当我在其上设置 setuid 位时,为什么 bash 没有以 root 身份运行?selinux 可能与此有关吗?
我知道在脚本上启用 setuid 存在安全问题,因此默认情况下处于非活动状态,但希望它适用于可执行文件。我创建了一个可执行文件,它按照这篇文章中描述的说明显示 uid 作为输出:Allow setuid on shell scripts
但它在运行之前和之后返回相同的 uid (1000) sudo chmod +s ./setuid-test
。我认为这意味着 setuid 对我的可执行文件没有任何影响,为什么以及如何解决?
源代码:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
printf("%d", geteuid());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
构建和运行
$ gcc -o setuid-test setuid-test.c
$ ./setuid-test
1000
$ sudo chown nobody ./setuid-test; sudo chmod +s ./setuid-test
$ ./setuid-test
1000
Run Code Online (Sandbox Code Playgroud)
运行时ls -la
,这就是我得到的:
me@me:~$ ls -la setuid-test
-rwsrwsr-x 1 nobody me 8572 Aug 19 16:39 setuid-test
Run Code Online (Sandbox Code Playgroud) 我想设置了 SetUID 位的可执行文件应该以其所有者身份运行,但我无法真正重现它。我尝试了以下方法。
$ cat prepare.sh cp /bin/bash 。 chown root.root bash chmod 4770 bash # 已验证 $ sudo sh prepare.sh $ ./bash $ id -u 1000 $退出 $
$ cat test.c #include<stdio.h> #include<unistd.h> int main(){ printf("%d,%d\n", getuid(), geteuid()); 返回0; } $ gcc -o test test.c $ chmod 4770 测试 # 验证 $ sudo chown root.root 测试 $ ./测试 1000,1000 $# 为什么???
然而
$苏 # ./bash # id -u 0 # 。/测试 0,0 # 出口 # 出口 $
注意:挂载点没有nosuid …
我试图了解如何setuid
工作。
所以我做了一个虚拟程序,它只打印当前用户:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout << system("id -a") << "\n";
cout << system("whoami") << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我my-binary
在用户下编译并创建了可执行文件anmol
:
-rwxrwxr-x 1 anmol anmol 9972 Feb 1 16:54 my-binary
Run Code Online (Sandbox Code Playgroud)
然后,我setuid
使用chmod +s
以下选项设置选项:
-rwsrwsr-x 1 anmol anmol 9972 Feb 1 16:54 my-binary
Run Code Online (Sandbox Code Playgroud)
如果我正常执行它,我会得到以下输出:
uid=1000(anmol) gid=1000(anmol) groups=1000(anmol),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),122(sambashare)
anmol
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用 更改为另一个用户su user2
,然后执行它,我会得到:
uid=1001(user2) gid=1001(user2) groups=1001(user2)
user2
Run Code Online (Sandbox Code Playgroud)
当我使用 执行它时sudo ./my-binary
,我得到:
uid=1001(root) gid=1001(root) groups=1001(root)
root
Run Code Online (Sandbox Code Playgroud)
据我了解,无论我如何运行它,我不应该每次都得到第一个输出吗?
我在这里检查了其他类似的问题,有些人建议我检查文件系统是否使用nosuid
选项挂载,所以我执行mount | …