先来看问题
问题来了:
echo的行为返回是对的还是vi是对的?
现场分析:
-
vi 和 echo 都被设置为SGID权限。
-
abc文件所在的父目录权限是777
-
echo无法写abc文件,而vi是可以的
当时被问到这个问题时,我也是一征,怎么会这样。加之SUID,SGID在企业应用很少,如此这么久早忘了一干二净,只是赶紧去补课~,但经过30MIN后依然没有找到问题所在,不名有些尴尬。后来在群里和朋友们一起讨论下后发现了问题所在。我们这里一一道来。
一道:
因为该父目录权限是777
,所以该目录是任何人可写,又因为该目录只是普通的777权限目录,没有ST保护,所以该目录下的文件是任何人可写可删可改的。正做简单演示:
-
-
先增加两个普通用户
stanley
和tom
[root@app1 ~]# id stanley uid=501(stanley) gid=501(stanley) groups=501(stanley) [root@app1 ~]# id tom uid=502(tom) gid=502(tom) groups=502(tom)
-
-
创建/root_test_dir 目录并赋权为777
[root@app1 ~]# mkdir /root_test_dir [root@app1 ~]# chmod 777 /root_test_dir [root@app1 ~]# ll /root_test_dir/
-
-
分别登录
stanley
和tom
用户,并在/root_test_dir目录下创建以自己姓名命名的文件stanley.txt 和 tom.txt,并分别编辑内容
###stanley用户 [stanley@app1 ~]$ cd /root_test_dir/ [stanley@app1 root_test_dir]$ touch stanley.txt [stanley@app1 root_test_dir]$ echo -e "###Notice:\nThis is stanley's file" > stanley.txt ###tom用户 [tom@app1 ~]$ cd /root_test_dir/ [tom@app1 root_test_dir]$ touch tom.txt [tom@app1 root_test_dir]$ echo -e "###Notice:\nThis is tom's file" > tom.txt ###root用户 [root@app1 ~]# ll /root_test_dir/ [root@app1 ~]# cat /root_test_dir/{stanley.txt,tom.txt}
为方便实现,分别使用对应用户各自复制一份文件出来
###stanley用户 [stanley@app1 root_test_dir]$ cp stanley.txt stanley1.txt ###tom用户 [tom@app1 root_test_dir]$ cp tom.txt tom1.txt
/tmp目录之所以不会出现这样的问题是因为/tmp目录添加了ST粘贴位,所以不会出现这样的问题
[stanley@app1 root_test_dir]$ echo 'stanley need tom1.txt file too!' >> tom1.txt
执行vi命令:
vi tom1.txt文件后强制编辑后wq!
强制保存。
大家可以看到问题确认是这样的。这是为什么呢?
最开始的时候,个人也纳闷了很久,折腾了30来分钟也没有搞定。但丝毫没有怀疑是Linux系统的问题。所以问题肯定还是出在权限的问题上。
几经折腾后,最终忽略了一个问题,同时对SUID,SGID的概念没有深入理解。
-
问题1: vi是的工作原理是先写.swap临时文件再覆盖原文件的。因为父目录是777,所以任何用户都可以覆盖其它人的文件
SGID的权限赋予的是vi 和 echo两个命令,而且这两个命令也确实是被正常执行了。只是/root_test_dir目录下的文件没有权限被重写而已。
大家试下在/root_test_dir目录下,mv是可以被覆盖其它人的文件的。
所以,这SUID,SGID和目录777权限的问题,大家应该清楚了吧。
原创文章,作者:sjfbjs,如若转载,请注明出处:http://www.178linux.com/30602