首页 > 解决方案 > shell_exec() 子进程应该继承euid吗?

问题描述

我正在以 root 身份运行 php-cli 7.3.19(在 Debian 10 Buster,linux 内核 4.19.0-8-amd64 上),并且在使用 posix_seteuid() 更改我的 euid 之后,子进程应该继承我的 euid ?

我认为答案是肯定的,但测试它,

root@devdb:/srv/http/easyad_branches# whoami
root
root@devdb:/srv/http/easyad_branches# id
uid=0(root) gid=0(root) groups=0(root)
root@devdb:/srv/http/easyad_branches# php -r ' \
var_dump(posix_seteuid(posix_getpwnam("www-data")["uid"])); \
var_dump(shell_exec("whoami;id")); \ 
posix_seteuid(0); \
var_dump(posix_setuid(posix_getpwnam("www-data")["uid"])); \ 
var_dump(shell_exec("whoami;id"));'
bool(true)
string(44) "root
uid=0(root) gid=0(root) groups=0(root)
"
bool(true)
string(53) "www-data
uid=33(www-data) gid=0(root) groups=0(root)
"

似乎 whoami 继承了我的uideuid,而不是继承了我的euideuid,这是预期的行为吗?

换一种说法,我得到了bool(true) root bool(true) www-data,但我预料到了bool(true) www-data bool(true) www-data,我的期望是错的,还是发生了其他事情?

标签: phplinuxposixuid

解决方案


似乎whoami继承了我的uid,因为它是euid,而不是继承我的euid,因为它是euid,这是预期的行为吗?

我不是 100% 确定 PHP,但是在直接调用 Linux 系统调用(例如 C 或 C++)的编程语言中,这种行为是正常的。

这种行为的一个众所周知的副作用是将该set-euid位设置为 shell 脚本没有意义(只要使用“普通”shell - 例如 bash - ):

外壳程序(例如/bin/sh)将启动时euid设置为不同的 UID,但外壳程序启动的程序将同时具有euiduid设置为外壳程序的uid值,即启动脚本的用户的 UID...


推荐阅读