views:

964

answers:

3

Hi,

I'd like to build a simple web app, which manages some directory on a server. I want to give people the option to use chown and chmod.

What is the safest way to give PHP this permission? The quickest thing is just running Apache and PHP as root, but that doesn't seem to be a smart idea.

One other thing I thought of, was creating a separate script which has setuid root..

Thanks!

+2  A: 

Well, it certainly sounds like a dangerous idea to begin with and I'd prefer sitting down and thinking through the whole strategy of what is trying to be achieved.

The danger is privilege escalation of an executable script which a remote user could modify or upload, of course. Full chown/chmod in a web app is equivalent to just pasting your root password on the page.

What is it exactly which needs to happen?

If the chown needs to happen for some reason but not to root (we hope) then the functionality should be wrapped. I would take the user requests and queue them, then have a separate process (could be shell, php, perl, anything) running as root by cron check this queue, check to see if the request fit the allowed parameters, and make the changes.

Devin Ceartas
Hi Devin, I'd like to make a web interface, allowing people to manage their public_html and homedirectory by other means than FTP or SSH. I do realize the risks, but I'm also curious from a hypothetical point of view what the most appropriate method would be.
Evert
And with 'appropriate' I actually mean the most secure solution :). Another use-case would be allowing a web-app to change it's own configuration. In most cases people just make the config world-writable or owned by www-data, but that doesn't seem like the best idea either.
Evert
A: 

One way would be to set up sudo on your machine (assuming it's a Linux box). Sudo allows you to run commands elevated, governed by restrictions set forth in the sudoers.conf file. Use tight rules to limit its use to the required commands in a specific directory for the user your web service is running under (like www-data), and then call the command shell from your PHP script something like tis:

shell_exec("sudo chmod 777 dirname");

Do make sure that your sudo config is tight, to ensure that breaking out will be next to impossible.

kathmann
I know sudo can be used to limit what commands can be issued, but can you limit the parameters? I.e. can you allow chmod 666 file.txt but not chmod 777 hack.jpg.php using sudo?
Devin Ceartas
You could intercept illegal masks in your PHP code, or alternatively the sudoers file does allow wildcards and in- and exclusion ranges (not REGEX but usable) in all hostnames, pathnames and command line arguments.
kathmann
I would strongly advise against using shellexec for what the user is trying to do. The php commands chmod and chgrp where made so you dont need to use shellexec
ae
I agree, but what Devin is asking apparently requires root or equivalent access, i.e.: www-data is not the owner of the containing directory. PHP's chmod, chown, chgrp and fileperms cannot elevate.
kathmann
I'm not convinced sudo will work if there's no TTY shell, also this seems like a sledgehammer approach compared to the setuid bit.
Evert
A: 

Perhaps you should look at the php commands: chmod, chown, chgrp and fileperms

chmod

chmod("/somedir/somefile", 0600);
ae