为什么setuid没有作为所有者运行



我目前正试图了解文件权限中的特殊位的作用,但目前无法理解setuid位的作用。从所有的在线资源中,它说:

通常被称为SUID,用户访问级别的特殊权限只有一个功能:具有SUID的文件总是作为拥有该文件的用户执行,而不管用户传递命令

然而,在一个简单的实验中,这似乎不是真的(除非我误解了,做错了什么?(即

mkdir /tmp/foo
mkdir /tmp/foo/bar
chmod 0700 /tmp/foo/bar                        # Restrict directory so only current user can access
echo content > /tmp/foo/bar/baz.txt            # Create content in the restricted directory
echo "ls /tmp/foo/bar" > /tmp/foo/myscript.sh  # Script to access content of restricted directoy
chmod 4777 /tmp/foo/myscript.sh                # Set setuid bit so the script runs as current user
/tmp/foo/myscript.sh                           # Show it works when run as current user
#> baz.txt
su bob                 # Switch to a new user
/tmp/foo/myscript.sh   # Run script again
#> ls: cannot open directory '/tmp/foo/bar': Permission denied

我的期望是,当设置了setuid位时,脚本应该以原始用户的身份执行,因此应该具有将ls放入受限目录的权限。但我得到了一个拒绝权限的错误,表明脚本不是作为原始用户运行的。

如果能帮助我理解我做错了什么,我们将不胜感激。(示例在zsh/ubuntu 20.04/wsl2上运行(

suid位仅适用于二进制可执行程序,而不适用于shell脚本。你可以在这里找到更多信息:https://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts

相关内容

最新更新