PHP’s Built-In Solutions For Shared Hosting
Posted by Stuart Herbert @ 9:12 AM, Tue 27 Nov 07
Filed under: The Web Platform
8 Comments
In my last article, I covered the fundamental security problem that exists when you have multiple websites owned by different people on the same box. The challenge is to secure the box not just from outside attack (something you have to do anyway, and which I’ll cover later in this series), but also to make sure that code running on one website can’t steal confidential data like MySQL passwords from any of the other websites.
This isn’t a problem caused by, or unique to, PHP. It has been a problem with websites ever since the original NCSA httpd was released back in the early nineties with the ability to run Perl scripts via the CGI interface. Over fifteen years later, NCSA httpd has given way to Apache and Perl has given way to mod_php, but the problem is still the same.
(On Windows, the problem is a little different, because Windows handles process permissions differently. I’ll look at that in a later article).
PHP 4 & 5 ship with two features which were designed to tackle this issue – safe_mode, and open_basedir.
Introducing safe_mode
Safe mode is an optional PHP feature aimed at restricting which files any PHP script can access. It works like this:
When your PHP script is executed, PHP makes a note of which user owns your PHP script. On a shared hosting server, this will be your user account – the account you log into to FTP files up to the server.
Whilst your PHP script is running, if your script wants to access any other files, PHP checks to see who owns those files first. If the file isn’t owned by the same user who owns the running script, PHP refuses to open the file.
Sounds like a great solution to the problem. Your PHP script can open your files – which is what you want – but it cannot open the files of any of the other customers on the box. So how do we switch it on?
Configuring safe_mode
To switch on safe_mode, set “safe_mode=1” in your php.ini file, and restart Apache. Then test your website, and make sure it still works.
If you have PEAR (or a.n.other PHP libraries) installed in a central location on your server and listed in the include_dir setting, you should also add this location to safe_mode_include_dir as well. This tells PHP to skip the owning user test when accessing the PEAR libraries.
There are also other things you can configure, right down to enabling / disabling specific PHP functions and classes. Full details, as always, are in the excellent PHP Manual. I won’t be going into them in any detail in this article.
The Problems With safe_mode
Alas, if it sounds too good to be true, then all too often it is
Any moderately-complicated PHP application will create files on the server. The obvious example is uploaded images to a blog, but it’s just as likely to be cache files for RSS feeds or to reduce database overhead, or a friendly web-based installer like the one that comes with Wordpress. When your script creates these files, the files on disk will be owned by Apache, not by your user account. Remember, Apache doesn’t run with your user account’s privileges; it runs as its own user!
With safe_mode enabled, PHP can’t read any of the files created by Apache. To use safe_mode, your PHP script can never ever write brand new files to disk. (It can write out existing files that you own). You’ll have to store all of your data in the database, which isn’t always convenient or the fastest solution.
It’s also (theoretically) possible to get around safe_mode. safe_mode is a PHP feature, not a security policy enforced by the underlying operating system. A PHP extension (one written in C, not in PHP) could ignore safe_mode and just open any files that it chooses (and that Apache can see). The PHP developers audit the official PHP extensions, to make sure none of them can be abused like this, but when it comes to third-party extensions, you’re on your own.
Sadly, PHP is just the wrong place architecturally to solve this security problem, and as a result safe_mode will not be part of PHP 6. If you currently rely on safe_mode to secure your servers, it’s time to start looking at other ways to secure your shared hosts. I hope you’ll find my next article or two about alternatives both useful and timely
Restricting Access With open_basedir
The second PHP feature that helps is open_basedir. Although it’s documented as part of the safe_mode section of the PHP Manual, to all intents and purposes it is a separate feature that can be switched on and off without requiring safe_mode.
safe_mode doesn’t care where a file on disk is; all it cares about is who owns the file. open_basedir is the orthogonal feature. It doesn’t care who owns a file, only where the file exists on disk. You tell PHP which directory it is allowed to open files from, and PHP makes sure that all attempts to access files outside that directory will fail.
The idea is to setup each website so that PHP is only allowed to open PHP files installed for that website.
Switching On open_basedir
The open_basedir setting can be edited in php.ini, but to be honest that makes little sense on a shared hosting server. You’re much better off putting this configuration into the httpd.conf entry for each individual website:
ServerName www.example.com
DocumentRoot /home/customer1/public_html/www.example.com/
php_admin_flag open_basedir /home/customer1/public_html/www.example.com/
...
There’s one gotcha with open_basedir that you need to pay close attention to. Despite the name, PHP doesn’t expect open_basedir to be the name of a directory; it treats it as a prefix. The check PHP uses is something like this:
function check_open_basedir($file)
{
// resolve any symlink
$file = realpath($file);
$open_basedir = ini_get("open_basedir");
// check to ensure file is inside open_basedir
if (substr($file, 0, strlen($open_basedir)) === $open_basedir)
{
return false;
}
return true;
}
To make sure that PHP treats open_basedir as a real directory, always put a slash at the end of the value for open_basedir.
open_basedir and PHP 6
For the moment at least, open_basedir will continue to be supported in PHP 6. There’s a slight change to how it is configured (with PHP 5, you can set open_basedir in .htaccess files; with PHP 6 you have to put it in httpd.conf or php.ini) but the actual functionality stays the same.
open_basedir is vulnerable to the same theoretical circumvention as safe_mode, so be careful when installing third party PHP extensions onto a shared server.
Where Do We Go From Here?
I’ve looked at two solutions implemented by PHP 4 & 5 to help make a shared hosting server more secure.
safe_mode stops you opening up files owned by other customers, but it has the side effect that your web application cannot create files of its own. This feature has been removed from PHP 6.
open_basedir stops you opening up files outside the specified directory on disk. This feature is still in PHP 6, but can now only be configured from phi.ini and Apache’s httpd.conf.
Both features rely on third party extensions supporting them. It’s perfectly possible for a third party extension to choose to bypass both features, thus re-creating the security hole we’re trying to close.
In terms of our challenge, both features come close to solving it, but neither is 100% guaranteed to do so. Data security isn’t just a legal obligation, it’s also a moral one, and you can’t meet your moral obligation using these features alone.
Fundamentally, PHP is the wrong place to solve this problem. PHP is trying to overcome a security weakness that it has inherited from Apache (and all other web servers; this isn’t a problem specific to Apache), and in turn they are constrained by the security model implemented by UNIX systems themselves.
Moving up the stack, if the problem can’t be fixed in PHP, maybe Apache can offer some help? I’ll take a look at that in the next article.
This article is part of The Web Platform, an on-going series of blog posts about the environment that you need to create and nurture to run your web-based application in. If you have any topics that you’d like to see covered in future articles, please leave them in the comments on this page.
Share This
8 Comments
Web Hosting » Blog Archive » PHP’s Built-In Solutions For Shared Hosting says:
November 27th, 2007 at 9:48 am
[...] Urbino wrote an interesting post today onHere’s a quick excerptOn a shared hosting server, this will be your user account – the account you log into to FTP files up to the server. … The obvious example is uploaded images to a blog, but it’s just as likely to be cache files for RSS feeds or to reduce database overhead, or a friendly web-based i…I’ve looked at two solutions implemented by PHP 4 & 5 to help make a shared hosting server more secure. … safe_mode stops you opening up files owned by other customers, but it has the side effect that your web application cannot create files of its own…. [...]
Jorrit Schippers says:
November 27th, 2007 at 10:47 pm
I’m not entirely sure, but shouldn’t it be php_admin_flag instead of php_flag, because now someone can override it in the .htaccess file?
Looking forward to your next articles by the way.
Stu says:
November 27th, 2007 at 10:51 pm
@Jorrit: You’re absolutely right, it should be php_admin_flag, not php_flag. I’ve updated the article accordingly. Thanks!
(The relevant PHP manual page, if anyone wants to see full details.)
Have Any Other Shared Hosting Solutions For Me To Look At? | Stuart Herbert On PHP says:
January 20th, 2008 at 12:27 pm
[...] safe mode [...]
Stuart Herbert On PHP - » Using mpm-peruser To Secure A Shared Server says:
April 19th, 2008 at 9:13 pm
[...] hosting server is how to secure the website from attack both from the outside and from the inside. PHP has built-in features to help, but ultimately it’s the wrong place to address the problem. Apache has built-in features too, [...]
Stuart Herbert On PHP - » Using mpm-itk To Secure A Shared Server says:
April 19th, 2008 at 9:18 pm
[...] hosting server is how to secure the website from attack both from the outside and from the inside. PHP has built-in features to help, but ultimately it’s the wrong place to address the problem. Apache has built-in features too, [...]
Hosting Companies Review says:
June 14th, 2009 at 3:58 pm
safe mode is very annoying
I prever vps when developing my apps
lwice says:
August 13th, 2009 at 7:53 am
“There’s a slight change to how it is configured (with PHP 5, you can set open_basedir in .htaccess files; with PHP 6 you have to put it in httpd.conf or php.ini) but the actual functionality stays the same.”
i test php5.2.9 and php5.3.0 what you wrote,but it seems wrong,and from the apache2 code php_admin_flag is set to RSRC_CONF|ACCESS_CONF,so you can not set open_basedir in .htaccess.
here is the error log from apache error log:
“.htaccess: php_admin_value not allowed here”
from php5.3 on ,open_basedir can be set using ini_set() function.(attribute changed from INI_SYSTEM to INI_ALL)
i’m messed up by the php security……
Saturday, November 21, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment