6

我正在尝试构建一个平台来启动一些脚本。此脚本放置在每个用户的主文件夹中。每次启动都应该使用每个用户 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

顺便说一句,启动这个脚本的用户在 root 组中(在 GNU/linux 操作系统上),它拥有所有的 root 权限。

如果我尝试使用 root 用户启动相同的代码,我会得到一个不同的错误:

OSError: [Errno 13] Permission denied

如果有人可以帮助我了解正在发生的事情,请...

4

5 回答 5

5

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

于 2011-09-23T12:58:56.900 回答
2

只有超级用户可以随时更改 uid,仅将用户添加到 root 组是不够的。

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 进程通信。虽然这是一个更高级的设置...

于 2011-09-23T12:59:39.763 回答
0

OSError: [Errno 1] Operation not permitted表示启动脚本的用户权限不足。在根组中是不够的,它实际上需要CAP_SETUID能力。

OSError: [Errno 13] Permission denied可能是一个不相关的错误。你应该看看它的堆栈跟踪。

于 2011-09-23T12:59:49.983 回答
0

线

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

以多种方式使我感到困惑。

  1. shlex.split()似乎是多余的,因为没有什么可拆分的。
  2. 最好把Popen()'s 参数放在一个列表中。
  3. 如果user_script.py没有执行权限,即使是 root 也不能​​这样做。
于 2011-09-23T13:00:07.500 回答
-1

您还使用 setuid 权限。那就是给,

       chmod 4755 script.py

现在,即使是普通用户,如果您执行程序,它也会切换为特定用途。你不会得到任何权限问题。

于 2014-07-23T12:55:44.530 回答