Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 特殊權限完整解析:SUID、SGID 與 Sticky Bit

前言:rwx 之外的三個特殊權限

多數人對 Linux 權限的印象停留在 rwx 三組九個字元上,但系統其實還保留了三位特殊權限:SUID、SGID 與 Sticky Bit。它們分別疊在「擁有者」「所屬群組」「其他人」的執行位(x)之上,用來處理一般權限模型解決不了的問題。

Set UID(SUID,數值 4)

當 s 出現在檔案擁有者的 x 位置,就稱為 SUID。最經典的例子是 /usr/bin/passwd,權限長這樣:-rwsr-xr-x。它要真正生效,必須同時滿足四個條件:

  • 只對二進位可執行程式(binary program)有效;
  • 執行者本身必須對該程式具備 x 權限;
  • 提權僅在程式執行期間(run-time)有效;
  • 執行者在此期間會取得程式擁有者的身分與權限。

所以 SUID 的用意是:讓原本沒有某項權限的使用者,在執行這個程式時,能順利存取他平常碰不到的資源。passwd 正是最鮮明的示範。

我們知道使用者密碼存放在 /etc/shadow,而這個檔案的權限是 ----------(早期 RHEL 版本為 -r--------)。有沒有 r 權限其實不重要,因為 root 擁有最高權限,什麼都能做;關鍵在於普通使用者也得能把新密碼寫進 /etc/shadow。他明明沒有寫入權限,卻能改密碼,靠的就是 SUID。

passwd 的執行流程如下:

  1. /usr/bin/passwd 對所有使用者都開放執行,因此人人都能呼叫;
  2. 該檔案的擁有者與所屬群組都是 root;
  3. 使用者執行它時,在程式執行期間取得了 root 權限;
  4. 於是得以借 root 之權修改 /etc/shadow;
  5. 密碼修改完成。

特別提醒:SUID 只能加在二進位程式(系統命令)上,不能加在 shell 腳本上,因為腳本實際上是把一堆程式串起來執行,並非腳本自己在跑;同樣地,SUID 加在目錄上也是無效的。

Set GID(SGID,數值 2)

把 s 放到「所屬群組」的 x 位置,就是 SGID,例如 /usr/bin/wall。它的作用與 SUID 類似,只不過取得的是該程式所屬群組的權限。使用時同樣有幾點要注意:

  • SGID 對二進位程式有效;
  • 執行者需對該程式具備 x 權限;
  • SGID 最常被用在目錄上。

理解 SUID 之後,SGID 就不難懂:若使用者對某目錄具備 w 權限,他在該目錄下建立新檔案時,新檔案的所屬群組會直接繼承該目錄的群組,而不是建立者自己的群組。

Sticky Bit(SBIT,數值 1)

Sticky Bit 針對「其他人」設定,目前只對目錄有效。它的規則很簡單:使用者在此目錄下建立檔案或子目錄後,只有建立者本人與 root 能刪除它。

最具代表性的就是 /tmp:任何人都能在裡面新增、修改檔案(權限是 rwxrwxrwx),但每個人只能刪掉自己的東西。要注意 SBIT 對一般檔案不起作用。

權限設定方式

與 rwx 一樣,特殊權限也有字元與數字兩種寫法:

4  SUID = u+s
2  SGID = g+s
1  SBIT = o+t

動手做:SUID

[root@japie ~]# cd /tmp/
[root@japie tmp]# cp /usr/bin/passwd ./
[root@japie tmp]# mkdir testdir
[root@japie 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/

切換成一般使用者 yufei 並嘗試改密碼:

[root@japie tmp]# su yufei
[yufei@japie tmp]$ ./passwd
Changing password for user yufei.
(current) UNIX password:
New password:
Retype new password:
passwd: Authentication token manipulation error

失敗的原因正是他沒有權限把密碼寫入 /etc/shadow。加上 SUID 後再試一次:

[root@japie tmp]# chmod u+s passwd
[root@japie tmp]# ls -l passwd
-rwsr-xr-x. 1 root root 26968 Jan 20 23:27 passwd

[yufei@japie tmp]$ ./passwd
passwd: all authentication tokens updated successfully.

用數字方式移除也很直觀:

[root@japie tmp]# chmod 0755 passwd
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd

原理與 rwx 的數字寫法完全相同,只是在最前面多一位數字。順帶一提,一般使用者設定密碼時請弄得複雜一些,否則會通過不了認證——普通使用者與 root 的密碼強度規則並不相同。

動手做:SGID

[root@japie tmp]# chmod 757 testdir/
drwxr-xrwx. 2 root root 4096 Jan 20 19:25 testdir/

[yufei@japie tmp]$ touch testdir/file1
[yufei@japie tmp]$ mkdir testdir/dir1
[yufei@japie tmp]$ ls -l testdir
drw-rw-r--. 1 japie japie 0 Jan 21 10:33 dir1
-rw-rw-r--. 1 japie japie 0 Jan 21 10:33 file1

此時檔案與目錄的群組都是建立者本身。加上 SGID 之後:

[root@japie tmp]# chmod g+s testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:33 testdir/

[yufei@japie tmp]$ touch testdir/file2
[yufei@japie tmp]$ mkdir testdir/dir2
[yufei@japie tmp]$ ls -l testdir/
drw-rw-r--. 1 japie japie 0 Jan 21 10:33 dir1
drw-rw-r--. 1 japie root  0 Jan 21 10:36 dir2
-rw-rw-r--. 1 japie japie 0 Jan 21 10:33 file1
-rw-rw-r--. 1 japie root  0 Jan 21 10:35 file2

file2 與 dir2 的所屬群組變成了 root,也就是上層目錄 testdir 的群組。這個特性在多人群組共同開發專案時非常好用。還原用 chmod g-s testdir/ 即可。

動手做:Sticky Bit

[root@japie tmp]# rm -fr testdir/*
[yufei@japie tmp]$ touch testdir/japie_file
[yufei@japie tmp]$ su opsers
[opsers@japie tmp]$ rm -f testdir/japie_file
[opsers@japie tmp]$ ls testdir/

雖然 opsers 對 japie_file 只有讀權限,但因為目錄對其他人開放全部權限,他仍然刪得掉。加上 SBIT 後:

[root@japie tmp]# chmod o+t testdir
drwxr-xrwt. 2 root root 4096 Jan 21 11:49 testdir/

[opsers@japie tmp]$ rm -f testdir/japie_file
rm: cannot remove `testdir/japie_file': Operation not permitted

權限不足,只有建立者或 root 能刪除。還原用 chmod o-t testdir。

兩個容易踩到的眉角

一、用數字改目錄的特殊權限,未必改得回來

[root@japie tmp]# chmod 4755 passwd
[root@japie tmp]# chmod 3755 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/

[root@japie tmp]# chmod 0755 passwd
[root@japie tmp]# chmod 0755 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/

在 RHEL 6 的實驗中,檔案能改回來,目錄卻只移除了 SBIT,SUID 與 SGID 卡住不動。是否出於安全考量,筆者手邊沒有足夠資料佐證。實務上的建議很簡單:直接用 + / - 的字元寫法,最直白也最不容易出錯。

二、為什麼會出現大寫的 S 和 T

[root@japie tmp]# chmod u-x passwd
[root@japie tmp]# chmod o-x 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/

[root@japie tmp]# chmod u+s passwd
[root@japie tmp]# chmod o+t 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,特殊權限就形同虛設、沒有意義。所以看到大寫字母,等於系統在跟你說:這裡的設定有問題,快去排查。

補充:數字與字元寫法對照

# 為 file 加上 SUID
chmod 4755 file

# 為 test 目錄加上 SGID
chmod 2755 test/

# 為 test 目錄加上 SBIT
chmod 1755 test/

# 同時設定且完全拿掉 x(7666)
chmod 7666 file
-rwSrwSrwT 1 root root 0 Feb 6 21:49 file

看到 S、T 就代表那個位置沒有執行權限,除了 root 之外誰也動不了。字元寫法同樣可行:

chmod u=rwxs,o=rx file      # file 加 SUID
chmod g+s,o=wrx test/       # test 加 SGID
chmod o=rwxt test/          # test 加 SBIT
Copyright © Linux教程網 All Rights Reserved