简体   繁体   English

难以理解setuid / setgid

[英]Trouble understanding setuid/setgid

I've been trying to wrap my head around how setuid/setgid work... I came up with this contrived example: 我一直在努力思考setuid / setgid的工作原理...我想到了这个人为的示例:

Users: 用户:

  • userA belongs to group A userA属于组A
  • userB belongs to group B userB属于组B

File a.txt is owned by userA with permissions rw-rw---- (660). 文件a.txt由userA拥有,权限为rw-rw---- (660)。 I thought if userA created this file, test.sh: 我以为如果userA创建了这个文件test.sh:

#!/bin/sh
cat a.txt

and ran either chmod g+s test.sh or chmod u+s test.sh that upon execution by userB, it would cat a.txt. 并运行chmod g+s test.shchmod u+s test.sh ,在userB执行后,它将显示a.txt。 Instead, I get Permission Denied . 相反,我得到Permission Denied Just to be clear, the file permissions for test.sh end up looking like this: rwsr-xr-x or rwxr-sr-x . 为了清楚rwsr-xr-x ,test.sh的文件许可权最终看起来像是: rwsr-xr-xrwxr-sr-x I thought the whole point of setuid/setgid was that when other users executed the file, they would assume the user id or group id of the file (test.sh in this case). 我认为setuid / setgid的全部要点是,当其他用户执行该文件时,他们将假定该文件的用户ID或组ID(在本例中为test.sh)。 Am I doing something wrong, or is my understanding off? 我是在做错什么,还是我的理解力不强?

I've been running my test on RedHat in case that matters. 我一直在RedHat上进行测试,以防万一。 I've also tried playing with umask , attempting to run the setuid/setgid after running umask 0002 . 我也尝试过使用umask ,尝试在运行umask 0002之后运行setuid / setgid。 Didn't help... 没帮助...

WARNING: Setuid and setgid shell scripts are a security hole. 警告:Setuid和setgid Shell脚本是一个安全漏洞。 There are lots of ways for someone to cause a setuid/setgid to do things that you (the author) didn't intend. 有人可以通过很多方法使setuid / setgid做您(作者)不想要的事情。

A common trick is to monkey with the environment variables. 一个常见的技巧是利用环境变量。 For example, someone could do this: 例如,有人可以这样做:

ln -s cat /bin/rm
export PATH=.:${PATH}

then use your setuid script to delete the a.txt file. 然后使用您的setuid脚本删除 a.txt文件。


Fortunately, the setuid and setgid bits are ignored for shell scripts on many modern Linux systems; 幸运的是,在许多现代Linux系统上,shell脚本都忽略了setuid和setgid位。 see https://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts . 参见https://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts Some of the answers give workarounds ... 一些答案给出了解决方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM