admin管理员组

文章数量:1302328

When using exec() or proc_open() after posix_seteuid() or posix_setuid() I expected the resulting process to run as the UID I set inside the running script. This does not happen.

is this how it's supposed to work? I've worked around this by changing the user on the executed commandline, but it just seems like a security hole to change the UID and have exec() and proc_open() run things as root anyway.

Google searching has turned up nothing on this subject.

<?php
if (false === posix_setgid(1000)) echo "Could not setgid\n";
if (false === posix_setuid(1000)) echo "Could not setuid\n";
echo posix_getpwuid(posix_getuid())['name']."\n";
echo exec('echo $USER')."\n";

Was expecting:
# php test
ron
ron

Got instead:
# php test
ron
root

When using exec() or proc_open() after posix_seteuid() or posix_setuid() I expected the resulting process to run as the UID I set inside the running script. This does not happen.

is this how it's supposed to work? I've worked around this by changing the user on the executed commandline, but it just seems like a security hole to change the UID and have exec() and proc_open() run things as root anyway.

Google searching has turned up nothing on this subject.

<?php
if (false === posix_setgid(1000)) echo "Could not setgid\n";
if (false === posix_setuid(1000)) echo "Could not setuid\n";
echo posix_getpwuid(posix_getuid())['name']."\n";
echo exec('echo $USER')."\n";

Was expecting:
# php test
ron
ron

Got instead:
# php test
ron
root

Share Improve this question edited Feb 11 at 3:54 Ron asked Feb 11 at 3:17 RonRon 131 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You should execute id -u or whoami command to inspect the current user. $USER is an environment variable which is initialized by the login command (see Who sets $USER and $USERNAME environment variables?). setuid or seteuid only changes the uid of the process.

It's because the exec() and proc_open() functions don't respect the changes made to the user ID (UID) or group ID (GID) with posix_setuid() and posix_setgid() within the PHP script.

You'll need to run the PHP script with the desired user privileges from the start, or explicitly specify the user in the command line

OR

You can use another methods like sudo to control the user under which the command runs.

本文标签: posixPHP posixseteuid() and posixsetuid() not reflected in exec() or procopen()Stack Overflow