我想设置了 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也没有noexec设置。
谁能解释为什么它无法在 Ubuntu 16.04 LTS 上运行?
mur*_*uru 15
对于编译后的可执行文件,来自man 2 chown:
When the owner or group of an executable file are changed by an
unprivileged user the S_ISUID and S_ISGID mode bits are cleared. POSIX
does not specify whether this also should happen when root does the
chown(); the Linux behavior depends on the kernel version.
Run Code Online (Sandbox Code Playgroud)
颠倒chown和chmod顺序对我有用:
$ sudo chmod 4770 foo
$ sudo chown root:root foo
$ stat foo
File: 'foo'
Size: 8712 Blocks: 24 IO Block: 4096 regular file
Device: 801h/2049d Inode: 967977 Links: 1
Access: (0770/-rwxrwx---) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-04-18 15:15:15.074425000 +0900
Modify: 2017-04-18 15:15:15.074425000 +0900
Change: 2017-04-18 15:15:33.683725000 +0900
Birth: -
$ sudo chmod 4777 foo
$ ./foo
1000,0
Run Code Online (Sandbox Code Playgroud)
ilk*_*chu 15
在您的第一种情况下,Bash 不喜欢作为 setuid 运行。
如果 Bash 启动时有效用户(组)id 不等于真实用户(组)id,...,并且有效用户 id 设置为真实用户 id。
请参阅:Bash 的启动文件手册,Setuid 位似乎对 bash 也没有影响。
在第二种情况下,重要的是chmod和的顺序chown,正如穆鲁已经回答的那样。更改所有者会重置 setuid 位。