簡體   English   中英

使用os.setuid()[python]“不允許操作”

[英]“Operation not permitted” on using os.setuid( ) [python]

我正在嘗試構建一個平台來啟動一些腳本。 此腳本放在每個用戶的主文件夾中。 每次啟動都應該使用每個用戶ID,所以,我正在為每個用戶執行以下操作:

user_id = pwd.getpwnam( user )[ 3 ]
user_home = pwd.getpwnam( user )[ 5 ]

os.chdir( user_home )
os.setuid( user_id )

subprocess.Popen( shlex.split( "user_script.py" ) )

但是,當python嘗試執行os.setuid( user_id ) ,會引發此異常:

Traceback (most recent call last):
  File "launcher.py", line XX, in <module>

OSError: [Errno 1] Operation not permitted

順便說一句,啟動此腳本的用戶位於根組(在GNU / linux OS上)並且具有所有root權限。

如果我嘗試使用root用戶啟動相同的代碼,我會得到一個不同的錯誤:

OSError: [Errno 13] Permission denied

如果有人能幫我理解發生了什么,請...

只有root可以做setuid,在root-group中是不夠的。

只有超級用戶可以隨時更改uid,只是將用戶添加到根組是不夠的。

setuid(2)例如提到:

 The setuid() system call is permitted if the specified ID is equal to the
 real user ID or the effective user ID of the process, or if the effective
 user ID is that of the super user.

在Linux上,還有:

   Under Linux, setuid() is implemented like the POSIX version with the 
   _POSIX_SAVED_IDS feature.  This allows a set-user-ID (other than  root)
   program to drop all of its user privileges, do some un-privileged work, and
   then reengage the original effective user ID in a secure manner.

我甚至不知道Python是否直接實現了這一點,但它並不完全是你想要的。

簡而言之就是:以root身份啟動初始流程。

如果您擔心安全性,請啟動兩個進程,一個作為root用戶,一個作為非特權用戶,並讓非特權進程通過套接字與root進程通信。 這是一個更先進的設置,但......

你也使用setuid權限。 那是給的,

       chmod 4755 script.py

現在,即使是普通用戶,如果執行程序,它也將切換為特定用途。 您不會收到任何權限問題。

OSError: [Errno 1] Operation not permitted表示啟動腳本的用戶沒有足夠的權限。 在根組中是不夠的,它實際上需要CAP_SETUID功能。

OSError: [Errno 13] Permission denied可能是一個無關的錯誤。 你應該看一下它的堆棧跟蹤。

這條線

subprocess.Popen( shlex.split( "user_script.py" ) )

我以多種方式迷惑我。

  1. shlex.split()似乎是多余的,因為沒有什么可以拆分。
  2. 最好把Popen()的參數放在一個列表中。
  3. 如果user_script.py沒有執行權限,那么即使root也不能這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM