1. 程式人生 > >linux特殊許可權SUID,SGID和SBIT的介紹

linux特殊許可權SUID,SGID和SBIT的介紹

先來看看兩個特殊的檔案與目錄
[[email protected] ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 26968 Jan 29  2010 /usr/bin/passwd
[[email protected] ~]# ls -l /usr/bin/wall
-r-xr-sr-x. 1 root tty 10932 Apr 27  2010 /usr/bin/wall
[[email protected] ~]# ls -ld /tmp/
drwxrwxrwt. 7 root root 4096 Jan 20 11:00 /tmp/
這一個passwd命令在所有者的地方多了一個s,第二個wall命令在使用者組的位置多了一個s,第三個tmp目錄,多了一個t。這是為什麼呢?下面我們就來具體看看,這些特殊的許可權是什麼意思?如何設定?

特殊許可權的介紹
Set UID

當s這個標誌出現在檔案所有者的x許可權上時,如/usr/bin/passwd這個檔案的許可權狀態:“-rwsr-xr-x.”,此時就被稱為Set UID,簡稱為SUID。那麼這個特殊許可權的特殊性的作用是什麼呢?
1、SUID許可權僅對二進位制程式(binary program)有效;
2、執行者對於該程式需要具有x的可執行許可權;
3、本許可權僅在執行該程式的過程中有效(run-time);
4、執行者將具有該程式擁有者(owner)的許可權。
SUID的目的就是:讓本來沒有相應許可權的使用者執行這個程式時,可以訪問他沒有許可權訪問的資源。passwd就是一個很鮮明的例子,下面我們就來了解一下這相passwd執行的過程。

我們知道,系統中的使用者密碼是儲存在/etc/shadow中的,而這個檔案的許可權是———-其實有沒有r許可權不重要,因為我們的root使用者是擁有最高的許可權,什麼都能幹了。關鍵是要把密碼寫入到/etc/shadow中。我們知道,除了root使用者能修改密碼外,使用者自己同樣也能修改密碼,為什麼沒有寫入許可權,還能修改密碼,就是因為這個SUID功能。
下面就是passwd這個命令的執行過程
1、因為/usr/bin/passwd的許可權對任何的使用者都是可以執行的,所以系統中每個使用者都可以執行此命令。
2、而/usr/bin/passwd這個檔案的許可權是屬於root的。
3、當某個使用者執行/usr/bin/passwd命令的時候,就擁有了root的許可權了。
4、於是某個使用者就可以藉助root使用者的權力,來修改了/etc/shadow檔案了。
5、最後,把密碼修改成功。

注:這個SUID只能執行在二進位制的程式上(系統中的一些命令),不能用在指令碼上(script),因為指令碼還是把很多的程式集合到一起來執行,而不是指令碼自身在執行。同樣,這個SUID也不能放到目錄上,放上也是無效的。

Set GID
我們前面講過,當s這個標誌出現在檔案所有者的x許可權上時,則就被稱為Set UID。那麼把這個s放到檔案的所屬使用者組x位置上的話,就是SGID。如開頭的/usr/bin/wall命令。
那麼SGID的功能是什麼呢?和SUID一樣,只是SGID是獲得該程式所屬使用者組的許可權。
這相SGID有幾點需要我們注意:
1、SGID對二進位制程式有用;
2、程式執行者對於該程式來說,需具備x的許可權;
3、SGID主要用在目錄上;
理解了SUID,我想SGID也很容易理解。如果使用者在此目錄下具有w許可權的話,若使用者在此目錄下建立新檔案,則新檔案的群組與此目錄的群組相同。

Sticky Bit
這個就是針對others來設定的了,和上面兩個一樣,只是功能不同而已。
SBIT(Sticky Bit)目前只針對目錄有效,對於目錄的作用是:當用戶在該目錄下建立檔案或目錄時,僅有自己與 root才有權力刪除。
最具有代表的就是/tmp目錄,任何人都可以在/tmp內增加、修改檔案(因為許可權全是rwx),但僅有該檔案/目錄建立者與 root能夠刪除自己的目錄或檔案。
注:這個SBIT對檔案不起作用。
SUID/SGID/SBIT許可權設定
和我們前面說的rwx差不多,也有兩種方式,一種是以字元,一種是以數字。
4 為 SUID = u+s
2 為 SGID = g+s
1 為 SBIT = o+t

下面我們就來看看如何設定,並看看達到的效果。

先看SUID的作用及設定
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# cp /usr/bin/passwd ./
[[email protected] tmp]# mkdir testdir
上面兩步是在/tmp目錄下建立passwd檔案和testdir目錄
下面看看這兩個的許可權
[[email protected] tmp]# ls -l passwd ; ls -ld testdir/
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
drwxr-xr-x. 2 root root 4096 Jan 20 19:25 testdir/
我們切換到ldz使用者,然後修改自己的密碼
[[email protected] tmp]# su ldz
[[email protected] tmp]$ ./passwd
Changing password for user ldz.
Changing password for ldz.
(current) UNIX password:
New password:            
Retype new password:
passwd: Authentication token manipulation error
發現上面的ldz改不了自己的密碼,為什麼呢?就是因為沒有許可權把密碼寫入到/etc/shadow中。想讓普通使用者能修改/etc/shadow的話,那就需要用到SUID了。
[[email protected] tmp]$ su root
Password:
[[email protected] tmp]# chmod u+s passwd
[[email protected] tmp]# ls -l passwd
-rwsr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
看到有SUID許可權了,下面再來修改ldz自己的密碼
[[email protected] tmp]$ ./passwd
Changing password for user ldz.
Changing password for ldz.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
我們發現已經成功了。
我想這一下,你對SUID的作用已經瞭解了吧。
如果想把這個改回來(就是把SUID的許可權去掉),我們用數字方式來設定
[[email protected] tmp]# chmod 0755 passwd
[[email protected] tmp]# ls -l passwd
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
OK這樣就改過來了,這個數字的原理和我們前面講的rwx是一樣的,只是在最前面設定相應的數字而已。
注:在普通使用者修改自己的密碼是,密碼要設定的複雜點,否則的話,通過不了認證,普通使用者和root使用者的許可權是不同的。
再看SGID的作用及設定
我們以前面建立的/tmp/testdir為例子
[[email protected] tmp]# ls -ld testdir/
[[email protected] tmp]# chmod 757 testdir/
[[email protected] tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 20 19:25 testdir/
這時候,任何使用者對此目錄都有寫入許可權,那麼我們就在這個目錄裡面建立檔案與目錄,並看看他們的許可權如何
[[email protected] tmp]# su ldz
[[email protected] tmp]$ touch testdir/file1
[[email protected] tmp]$ mkdir testdir/dir1
[[email protected] tmp]$ ls -l testdir
total 0
drw-rw-r–. 1 ldz ldz 0 Jan 21 10:33 dir1
-rw-rw-r–. 1 ldz ldz 0 Jan 21 10:33 file1
這時候的檔案與目錄許可權都是建立者的本身
下面我們就來看看,把這個目錄加上SGID許可權後,再建立檔案與目錄,會是什麼樣的效果
[[email protected] tmp]$ su root
Password:
[[email protected] tmp]# chmod g+s testdir/
[[email protected] tmp]# ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:33 testdir/
[[email protected] tmp]# su ldz
[[email protected] tmp]$ touch testdir/file2
[[email protected] tmp]$ mkdir testdir/dir2
[[email protected] tmp]$ ls -l testdir/
total 0
drw-rw-r–. 1 ldz ldz 0 Jan 21 10:33 dir1
drw-rw-r–. 1 ldz root  0 Jan 21 10:36 dir2
-rw-rw-r–. 1 ldz ldz 0 Jan 21 10:33 file1
-rw-rw-r–. 1 ldz root  0 Jan 21 10:35 file2
[[email protected] tmp]$ ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:36 testdir/
這時候我們就發現,file2和dir2的使用者組變成了root了,也就是他們上層目錄testdir這個目錄的所屬使用者組。
這個應用,應用在一個專案的共同開發上,是很方便的。
[[email protected] tmp]$ su root
Password:
[[email protected] tmp]# chmod g-s testdir/
[[email protected] tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 10:36 testdir/
這樣就還原了
最後我們來看SBIT的作用及設定
[[email protected] tmp]# rm -fr testdir/*
[[email protected] tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:42 testdir/
清空/tmp/testdir/目錄裡面的全部內容。
我們切換成普通使用者,然後再裡面建立檔案,至少需要兩個普通使用者來測試這個,如果沒有的話,就自己建立。
[[email protected] tmp]# su ldz
[[email protected] tmp]$ touch testdir/ldz_file
[[email protected] tmp]$ ls -l testdir/
total 0
-rw-rw-r– 1 ldz ldz 0 Jan 21 11:45 ldz_file
這時候我們建立了一個檔案,我們換成另外一個使用者
[[email protected] tmp]$ su opsers
Password:
[[email protected] tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:45 testdir/
我們看到,雖然其他使用者對ldz_file只有只讀許可權,但由於ldz_file所在的目錄,對其他人是全部的許可權,所以,我們換其他使用者還是可以刪除這個檔案的,看操作
[[email protected] tmp]$ rm -f testdir/ldz_file
[[email protected] tmp]$ ls testdir/
發現我們已經刪除了這個不屬於我們的許可權。
下面我們就給這個目錄加上SBIT許可權,再來看看效果
[[email protected] tmp]$ su root
Password:
[[email protected] tmp]# chmod o+t testdir
[[email protected] tmp]# ls -ld testdir/
drwxr-xrwt. 2 root root 4096 Jan 21 11:49 testdir/
再一次切換普通使用者,建立檔案
[[email protected] tmp]# su ldz
[[email protected] tmp]$ touch testdir/ldz_file
[[email protected] tmp]$ ls -l testdir/ldz_file
-rw-rw-r– 1 ldz ldz 0 Jan 21 11:51 testdir/ldz_file
這個檔案的許可權還是和第一次建立的時候是一樣的,我們再換成其他的使用者,看看能不能再次刪除這個檔案
[[email protected] tmp]$ su opsers
Password:
[[email protected] tmp]$ rm -f testdir/ldz_file
rm: cannot remove `testdir/ldz_file’: Operation not permitted
看到提示,說許可權不夠了,只能由這個檔案的建立者或root使用者才能刪除。這個我們就不演示了。
如果要還原許可權的話,
[[email protected] tmp]$ su root
Password:
[[email protected] tmp]# chmod o-t testdir
[[email protected] tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:51 testdir/
兩個需要注意的問題
OK,關於SUID/SGID/SBIT這些特殊許可權的應用和作用我們已經講完了。但如果你仔細一點的話,會發現,我並沒有用數字方式來更改這個特殊的許可權,為什麼呢?且看下面的分析。

問題1:用數字改變目錄的特殊許可權,不起作用。
我們把/tmp/下面,我們自己建立的實驗檔案刪除
[[email protected] tmp]# rm -fr testdir/
[[email protected] tmp]# rm -fr passwd
然後再重新建立一個檔案和目錄,
[[email protected] tmp]# cp /usr/bin/passwd ./
[[email protected] tmp]# mkdir testdir
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-xr-x 2 root root 4096 Jan 21 12:00 testdir/
下面我們就來用數字方式來更改這三個特殊的許可權,看看會有什麼樣的結果
[[email protected] tmp]# chmod 4755 passwd
[[email protected] tmp]# chmod 3755 testdir/
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rwsr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
發現用這種方式增加這三個特殊許可權沒有問題,那麼我們再把許可權改回去看看
[[email protected] tmp]# chmod 0755 passwd
[[email protected] tmp]# chmod 0755 testdir/
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
我們發現,對檔案,許可權是改回去了,而對於目錄,只改回去了SBIT的許可權,對SUID和SGID改不回去。這是RHEL6上的實驗結果,可能是出於安全性的考慮嗎?這個我就不清楚了,也找不到相關的資料。如果各位網友,有知道什麼原因的,歡迎與我聯絡。在此先謝過了。
所以說,建議大家還是用最明瞭的方式,直接用+-來更改,無論方法如何,最終能得到結果就OK了。哈哈……

問題2:為什麼會有大寫的S和T。
還是用上面的檔案和目錄
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
我們把passwd和testdir的x許可權去掉
[[email protected] tmp]# chmod u-x passwd
[[email protected] tmp]# chmod o-x testdir/
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rw-r-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr– 2 root root 4096 Jan 21 12:00 testdir/
再給他們加上SUID和SBIT許可權
[[email protected] tmp]# chmod u+s passwd
[[email protected] tmp]# chmod o+t testdir/
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rwSr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-T 2 root root 4096 Jan 21 12:00 testdir/
我們看到,這時候的小s和小t已經變成了大S和大T了,為什麼呢?因為他們這個位置沒有了x許可權,如果沒有了x許可權,根據我們上面講的內容,其實,這個特殊的許可權就相當於一個空的許可權,沒有意義。也就是說,如果你看到特殊許可權位置上變成了大寫的了,那麼,就說明,這裡有問題,需要排除。