使用时真实有效用户的变化subprocess.call()"

2024-10-02 00:38:21 发布

您现在位置:Python中文网/ 问答频道 /正文

先决条件:

  • Python2.6
  • GNU Linux

我有一些关于subprocess.call'行为和保护http服务器。在

以下代码示例在不使用/使用shell时有所不同:

1

sudo python -c "from subprocess import call; from os import setreuid, setregid; setreuid(1000,0); setregid(1000,0); call(['touch','./aaa'])"

生成“root”拥有的文件。在

^{pr2}$

生成用户1000拥有的文件。在

问题:

在第一种情况下,产生一个由有效用户拥有的文件,在第二种情况下生成一个真正用户拥有的文件的原因是什么?在

python2.6(没有“setresuid”)中是否有一种方法可以在python代码中临时(安全地)更改用户?在

使用特权有效用户来临时更改真实用户以提高/降低权限是否安全?在


Tags: 文件代码用户fromgnuimport服务器http
2条回答

你可以使用sudo:

sudo python -c "from subprocess import call; call('sudo -u unprivileged_user touch ./aaa', shell=True)"

当您的/bin/shbash时,可能会发生这种情况,因为bash将有效用户id设置为实际用户id,除非在启动时提供-p选项。在

这在documentation中描述:

If Bash is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS, BASHOPTS, CDPATH, and GLOBIGNORE variables, if they appear in the environment, are ignored, and the effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.

相关问题 更多 >

    热门问题