对特权位的理解

 特权位就相当于尚方宝剑,对可执行程序加上特权位以后,其它用户执行该可执行文件,留下的印迹就是可执行文件的所有者和所有组的印迹,而非执行用户的印迹。

 

拿touch来说,不加特权位的情况下,用户shitou创建了一个文件shitoufile,其所有者是shitou,所有组也是shitou,也就是执行者的身份。

[shitou@ssh ~]$ touch /tmp/shitoufile
[shitou@ssh ~]$ ll /tmp/shitoufile
-rw-rw-r-- 1 shitou shitou 0 11-09 21:00 /tmp/shitoufile

 

加入特权位以后,用户shitou创建了一个文件shitoufile1,其所有者是root,所有组也是root,也就是可执行文件所有者和所有组的身份。

[root@ssh ~]# chmod u+s,g+s /bin/touch

[shitou@ssh ~]$ touch /tmp/shitoufile1
[shitou@ssh ~]$ ll /tmp | grep shitou
-rw-rw-r-- 1 shitou shitou       0 11-09 21:00 shitoufile
-rw-rw-r-- 1 root   root         0 11-09 21:00 shitoufile1

[root@ssh ~]# chmod u-s,g-s /bin/touch   #还原特权位,便于后面实验

 

 

组的特权位也可以应用于目录(文件夹),用户的特权位应用于目录不起任何作用,待会实例证明。

 

我们先在/tmp下创建一个子目录/shitoudir,然后将/shitoudir 的所有者和所有组改成shitou.shitou。

[root@ssh ~]# mkdir /tmp/shitoudir
[root@ssh ~]# chown shitou.shitou /tmp/shitoudir

 

然后root将/tmp/shitoudir加上用户特权位和组特权位,并在下面创建一个文件file3和一个子目录subdir,通过检查file3的所有者

[root@ssh ~]# chmod u+s,g+s /tmp/shitoudir
[root@ssh ~]# touch /tmp/shitoudir/file3
[root@ssh ~]# ll /tmp/shitoudir
-rw-r--r-- 1 root   shitou 0 11-09 21:28 file3

[root@ssh ~]# mkdir /tmp/shitoudir/subdir
[root@ssh ~]# ll /tmp/shitoudir

-rw-r--r-- 1 root   shitou    0 11-09 21:28 file3
drwxr-sr-x 2 root   shitou 4096 11-09 21:32 subdir

发现所有者还是root,所有组改成了shitou,证明了用户特权位对目录没有作用 ,组特权位对目录有作用,会让在下面创建的文件或者目录的gid等于目录的gid。

细心的读者也许会发现,subdir的权限为rwxr-sr-x,也就是所组特权位继承了父目录的。这样,如果我们对某个目录施加了组特权位,这样该目录下创建的文件/子目录/子目录下的文件都继承了该祖宗的所有组。 不信你在subdir下再创建一个文件看看。

 

这里出现了一个疑问?根据文件的uid和gid特权位的讨论,假设我们对touch设定了setuid,setgid,那么touch创建的文件所有者和所有组会等于touch的所有者和所有组。而根据目录gid特权位的讨论,如果对目录设定了特权位,在目录下面创建的文件会继承该目录的所有组。当两者冲突的时候,到底哪个优先呢? 实验论证一下:

上面的/tmp/shitoudir已经加上了组特权位,先在给touch加上用户特权位和组特权位

[root@ssh shitoudir]# chmod u+s,g+s /bin/touch
[root@ssh shitoudir]# ll /bin/touch
-rwsr-sr-x 1 root root 42284 2008-10-31 /bin/touch
[root@ssh shitoudir]# ll -d /tmp/shitoudir
drwxr-sr-x 3 shitou shitou 4096 11-09 21:38 /tmp/shitoudir

 

然后root和shitou各创建一个文件rootfile和shitoufile,如下所示:

[root@ssh shitoudir]# touch rootfile

[shitou@ssh shitoudir]$ touch shitoufile
[shitou@ssh shitoudir]$ ll
总计 4
-rw-r--r-- 1 root shitou    0 11-09 21:28 file3
-rw-r--r-- 1 root shitou    0 11-09 21:39 rootfile
-rw-rw-r-- 1 root shitou    0 11-09 21:40 shitoufile
drwxr-sr-x 2 root shitou 4096 11-09 21:32 subdir

 

发现创建的rootfile和shitoufile 所有者都是root,也就是touch的所有者,但是所有组均为shitou,也就是继承了目录的所有组,并非继承了touch文件的所有组。这说明,组特权位在父目录和可执行文件之间冲突时,优先选择父目录的组特权位。

 

实验完毕,将设置还原:

[root@ssh shitoudir]# chmod u-s,g-s /bin/touch

[root@ssh shitoudir]# cd ../
[root@ssh tmp]# rm -rf /tmp/shitoudir

 

 

 

 

 

 

 

 

发布了44 篇原创文章 · 获赞 2 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章