對特權位的理解

 特權位就相當於尚方寶劍,對可執行程序加上特權位以後,其它用戶執行該可執行文件,留下的印跡就是可執行文件的所有者和所有組的印跡,而非執行用戶的印跡。

 

拿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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章