Chapter 23. Safe Mode

Table of Contents
Functions restricted/disabled by safe mode

The PHP safe mode is an attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP level, but since the alternatives at the web server and OS levels aren't very realistic, many people, especially ISP's, use safe mode for now.

The configuration directives that control safe mode are:
safe_mode = Off 
safe_mode_gid = 0
safe_mode_include_dir =
safe_mode_exec_dir = 
open_basedir = 
safe_mode_allowed_env_vars = PHP_ 
safe_mode_protected_env_vars = LD_LIBRARY_PATH 
disable_functions =

When safe_mode is on, PHP checks to see if the owner of the current script matches the owner of the file to be operated on by a file function. For example:
-rw-rw-r--    1 rasmus   rasmus       33 Jul  1 19:20 script.php 
-rw-r--r--    1 root     root       1116 May 26 18:01 /etc/passwd
Running this script.php
<?php
 readfile('/etc/passwd'); 
?>
results in this error when safe mode is enabled:
Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not 
allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2

If instead of safe_mode, you set an open_basedir directory then all file operations will be limited to files under the specified directory For example (Apache httpd.conf example):
<Directory /docroot>
  php_admin_value open_basedir /docroot 
</Directory>
If you run the same script.php with this open_basedir setting then this is the result:
Warning: open_basedir restriction in effect. File is in wrong directory in 
/docroot/script.php on line 2

You can also disable individual functions. Note that the disable_functions directive can not be used outside of the php.ini file which means that you cannot disable functions on a per-virtualhost or per-directory basis in your httpd.conf file. If we add this to our php.ini file:
disable_functions readfile,system
Then we get this output:
Warning: readfile() has been disabled for security reasons in 
/docroot/script.php on line 2

Functions restricted/disabled by safe mode

This is a still probably incomplete and possibly incorrect listing of the functions limited by safe mode.

Table 23-1. Safe mode limited functions

FunctionLimitations
dbmopen()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
dbase_open()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
filepro()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
filepro_rowcount()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
filepro_retrieve()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
ifx_*()sql_safe_mode restrictions, (!= safe mode)
ingres_*()sql_safe_mode restrictions, (!= safe mode)
mysql_*()sql_safe_mode restrictions, (!= safe mode)
pg_loimport()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
posix_mkfifo()Checks whether the directory in which you are about to operate has the same UID as the script that is being executed.
putenv()Obeys the safe_mode_protected_env_vars and safe_mode_allowed_env_vars ini-directives. See also the documentation on putenv()
move_uploaded_file()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
chdir()Checks whether the directory in which you are about to operate has the same UID as the script that is being executed.
dl()This function is disabled in safe mode.
backtick operatorThis function is disabled in safe mode.
shell_exec() (functional equivalent of backticks)This function is disabled in safe mode.
exec()You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have .. components in the path to the executable.
system()You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have .. components in the path to the executable.
passthru()You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have .. components in the path to the executable.
popen()You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have .. components in the path to the executable.
mkdir()Checks whether the directory in which you are about to operate has the same UID as the script that is being executed.
rmdir()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
rename()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. Checks whether the directory in which you are about to operate has the same UID as the script that is being executed.
unlink()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. Checks whether the directory in which you are about to operate has the same UID as the script that is being executed.
copy()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. Checks whether the directory in which you are about to operate has the same UID as the script that is being executed. (on source and target)
chgrp()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
chown()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed.
chmod()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits
touch()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. Checks whether the directory in which you are about to operate has the same UID as the script that is being executed.
symlink()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. Checks whether the directory in which you are about to operate has the same UID as the script that is being executed. (note: only the target is checked)
link()Checks whether the file(s)/directories you are about to operate on have the same UID as the script that is being executed. Checks whether the directory in which you are about to operate has the same UID as the script that is being executed. (note: only the target is checked)
getallheaders()In safe mode, headers beginning with 'authorization' (case-insensitive) will not be returned. Warning: this is broken with the aol-server implementation of getallheaders()!
Any function that uses php4/main/fopen_wrappers.c ??