實際用戶ID,有效用戶ID與保存設置用戶ID(轉自:http://blog.csdn.net/dlutbrucezhang/article/details/8806015)

real user ID:實際用戶ID,指的是進程執行者是誰

effective user ID:有效用戶ID,指進程執行時對文件的訪問權限

saved set-user-ID:保存設置用戶ID,作爲effective user ID的副本,在執行exec調用時後能重新恢復原來的effectiv user ID.

上面這三個ID是相對於進程而言的.

set-user-ID:設置用戶ID,這是相對於文件來說的.設置了set-user-ID位的可執行程序,執行時,進程的effective user ID與saved set-uesr-ID都爲程序文件所屬用戶的ID,些時real user ID與effective user ID就不一定相等了.這類程序稱之爲SUID程序,這類程序有特殊的用途.典型的例子:passwd程序,ping程序等.

passwd程序是要修改用戶密碼,此時是要修改/etc/passwd或修改/etc/shadow文件(有必要時),然而一般用戶沒有修改這兩個文件的權限.passwd程序設置了set-user-ID位的,並且該文件的所有都是root,所以,一般用戶執行時,也具有了root的權限.

ping程序也如此,因爲ping程序要產生原始套接字(raw),所以需要有root的權限.然而一般用戶之所以能用ping程序,就是因爲ping程序的所有都是root用戶,並且它設置了set-user-ID位.

爲一可執行程序設置set-user-ID位:

pds@FSSR:~> su root
口令:
FSSR:/home/pds # chown root suid
FSSR:/home/pds # ll suid
-rwxr-xr-x 1 root users 7702 2008-08-10 11:28 suid
FSSR:/home/pds # chmod u+s suid
FSSR:/home/pds # ll suid
-rwsr-xr-x 1 root users 7702 2008-08-10 11:28 suid
FSSR:/home/pds # exit
exit

 

相對的,沒有設置set-user-ID位的可執行程序,稱之爲非SUID程序,該程序執行時,real user ID與effective user ID相等.

setuid可以修改real user ID,effective user ID和saved set-user-ID這三個值,但是要用權限.

這是函數原型:

int setuid(uid_t uid)

1.如果用戶(當前調用的用戶)有超級用戶權限,則real user ID,effective user ID和saved set-user-ID都將設置爲參數uid的值.

2.如果用戶沒有超級用戶權限,僅當參數uid等於real user ID或saved set-user-ID時,effective user ID被設置爲參數uid的值,real user ID和saved set-user-ID不變;否則返回錯誤.

這是自己按自己的理解總結的 ^_^

 


Unix中常見的幾個概念,下面做一個解釋.

首先需要明確一點,這幾個概念都是和進程相關的.
real user ID表示的是實際上進程的執行者是誰,effective user ID主要用於校驗該進程在執行時所獲得的文件訪問權限,也就是說當進程訪問

文件時檢查權限時實際上檢查的該進程的"effective user ID",saved set-user-ID 僅在effective user ID發生改變時保存.

一般情況下,real user ID就是進程的effective user ID,但是當要運行的可執行程序設置了"set-user-ID"位之後,進程的effective user ID

變成該文件的屬主用戶id,同時該進程的"saved set-user-ID"變成此時進程的"effective user ID",也就是該可執行程序的屬主用戶ID,該進程

在執行一些與文件訪問權限相關的操作時系統檢查的是進程的effective user ID.

爲什麼需要一個"saved set-user-ID"?因爲當進程沒有超級用戶權限的時候,進程在設置"effective user ID"時需要將需要設置的ID和該進程

的"real user ID"或者"saved set-user-ID"進行比較.

APUE2中進行的解釋是:
1)If the process has superuser privileges, the setuid function sets the real user ID, effective user ID, and saved set-user-

ID to uid.

2)If the process does not have superuser privileges, but uid equals either the real user ID or the saved set-user-ID, setuid

sets only the effective user ID to uid. The real user ID and the saved set-user-ID are not changed.

3)If neither of these two conditions is true, errno is set to EPERM, and 1 is returned
也就是說:
1)當用戶具有超級用戶權限的時候,setuid 函數設置的id對三者都起效.
2)否則,僅當該id爲real user ID 或者saved set-user-ID時,該id對effective user ID起效.
3)否則,setuid函數調用失敗.

也就是說,這個saved set-user-ID更多的作用是在進程切換自己的effective user ID起作用.

需要特別提醒的是:並沒有任何的API可以獲取到進程的saved set-user-ID,它僅僅是系統在調用setuid函數時進行比較而起作用的.
APUE2中關於此事的原話如下:
Note that we can obtain only the current value of the real user ID and the effective user ID with the functions getuid and

geteuid from Section 8.2. We can't obtain the current value of the saved set-user-ID.


舉一個例子說明問題,假設這樣的一種情況,系統中有兩個用戶A,B,還有一個由B創建的可執行程序proc,該可執行程序的set-
user-id位已經進行了設置.

當A用戶執行程序proc時,
程序的real user ID = A的用戶ID,effective user ID = B的用戶ID,  saved set-user-ID=B的用戶ID.

假如在該進程結束了對某些限制只能由用戶B訪問的文件操作後,程序將effective user ID設置回A,也就是說此時:
程序的real user ID = A的用戶ID,effective user ID = A的用戶ID,  saved set-user-ID=B的用戶ID.

這個改動之所以能成功,原因在於上面列舉出的情況2):該ID爲進程的real user ID.

最後,假設由於種種原因進程需要再次切換effective user ID爲B,可是因爲不能通過API獲取進程的saved set-user-ID(該值爲B的用戶ID),所

以只能通過兩種途徑獲得(可能還有別的途徑):
a)在設置effective user ID變回A之前保存effective user ID,它的值爲B的用戶ID.
b)調用函數getpwnam( "B"),在返回的struct passwd *指針中成員pw_uid存放的就是用戶B的ID.
這樣,這個調用setuid(B的用戶ID)就會成功,原因也在於上面說的情況2):該ID與進程的saved set-user-ID相同.

APUE2中關於這幾個值的相關解釋在section4.4和section8.11中都有涉及.

發佈了15 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章