繁体   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