SetUID位在Ubuntu中不起作用?

iBug

我想设置了SetUID位的可执行文件应以其所有者身份运行,但我无法真正复制它。我尝试了以下方法。

$ cat prepare.sh 
cp / bin / bash。
chown root.root bash 
chmod 4770 bash#已验证
$ sudo sh 
prepare.sh 
$ ./bash $ id -u 
1000 
$ exit 
$
$ 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 test#已验证
$ sudo chown root.root test 
$ ./test 
1000,1000 
$#为什么???

然而

$ su 
#./bash 
#id -u 
0 
#./test 
0,0 
#退出
#退出
$

注意:挂载点没有nosuid也没有noexec设置。
谁能解释为什么它无法在Ubuntu 16.04 LTS上运行?

muru

对于已编译的可执行文件,来自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.

反转chownchmod顺序对我有效:

$ 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章