php multiuser system – the www-data problem
March 19, 2009 Leave a comment
On a lot of multi-user systems, like the one at the school where we have 300+ users all with usermod enabled, we also happen to have other web services running. It’s inconvenient and in insecure for everyone to be running their dynamic web stuff as the same user. I understand this is nearly impossible to do with good security, but this is a university and the point of this server is to let students learn, which means being able to host code.
One security problem in particular is php. suexec was built for cgi-bin stuff – but php is a whole other beast. That’s what I’m talking about here – getting php to run as the user who owns it. More specifically, this will show how /home/user/public_html/myphp.php will run as “user”, but stuff in /var/www will still run as www-data.
One good article I found describing this is here: http://alain.knaff.lu/howto/PhpSuexec/
First things first – mod_php needs to be disabled. This can be done globally, but it’s better to just disable it for public_html dirs. This can be done by adding the following to /etc/apache2/apache2.conf.
<Directory /home> php_admin_flag engine off </Directory>
Now, to enable suphp.
First install php-cgi. and the apache2 prefork which has some things we’ll need later on.
apt-get install php-cgi apache2-prefork-dev
Do not install libapache2-mod-suphp – at least not on 8.04. This older version lacks some of the things most people need… like having more than one directory.
Download the latest suphp module from http://www.suphp.org/Home.html. Compile this like:
tar xfzv suphp-SNAPSHOT-2008-03-31.tar.gz cd suphp-SNAPSHOT-2008-03-31 ./configure --with-apxs=/usr/bin/apxs2 --with-setid-mode=owner make make install
Modify apache’s config
LoadModule suphp_module /usr/lib/apache2/modules/mod_suphp.so <Directory /home> AddHandler application/x-httpd-php .php .php3 .php4 .php5 .phtml suPHP_AddHandler application/x-httpd-php suPHP_Engine on </Directory>
Now in /usr/local/etc/suphp.conf
[global] webserver_user=www-data docroot=${HOME}/public_html check_vhost_docroot=false [handlers] ;Handler for php-scripts application/x-httpd-php="php:/usr/bin/php-cgi"
Restart apache. To debug, check /var/log/apache2/errors.log. To test create scripts in public_html directories and in /var/www that exec(‘whoami’) and make sure they’re called with the correct permissions.
It’s a start, but then there’s always stuff like XSS, etc.