Join Up!
96654 members and counting!

 
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousPersistent Database ConnectionsUsing PHP from the command linenext
Last updated: Tue, 28 May 2002
view the printer friendly version or the printer friendly version with notes or change language to Czech | German | Spanish

Luku 23. 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.

Taulu 23-1. Configuration directives controlling safe mode are:

DirectiveDefault value
safe_mode Off
safe_mode_gid 0
safe_mode_include_dir ""
safe_mode_exec_dir 1
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

However, there may be environments where a strict UID check is not appropriate and a relaxed GID check is sufficient. This is supported by means of the safe_mode_gid switch. Setting it to On performs the relaxed GID checking, setting it to Off (the default) performs UID checking.

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.

Taulu 23-2. Safe mode limited functions

FunctionLimitations
dbmopen()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
dbase_open()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
filepro()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
filepro_rowcount()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
filepro_retrieve()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
ifx_*()sql_safe_mode restrictions, (!= safe mode)
ingres_*()sql_safe_mode restrictions, (!= safe mode)
mysql_*()sql_safe_mode restrictions, (!= safe mode)
pg_loimport()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
posix_mkfifo()Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä.
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()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
chdir()Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä.
dl()Tämä funktio ei ole käytössä jos safe mode on kytketty päälle.
backtick operatorTämä funktio ei ole käytössä jos safe mode on kytketty päälle.
shell_exec() (functional equivalent of backticks)Tämä funktio ei ole käytössä jos safe mode on kytketty päälle.
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()Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä.
rmdir()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
rename()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä.
unlink()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä.
copy()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä. (on source and target)
chgrp()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
chown()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä.
chmod()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. In addition, you cannot set the SUID, SGID and sticky bits
touch()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä.
symlink()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä. (note: only the target is checked)
link()Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä. (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()!
header()In safe mode, the uid of the script is added to the realm part of the WWW-Authenticate header if you set this header (used for HTTP Authentication).
highlight_file(), show_source() Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä. (note: only affected since PHP 4.2.1)
parse_ini_file() Tarkistaa onko käsiteltävillä tiedostoilla/hakemistoilla sama UID kuin ajettavalla skriptillä. Tarkistaa onko hakemistolla, jossa operoidaan, sama UID kuin ajettavalla skriptillä. (note: only affected since PHP 4.2.1)
Any function that uses php4/main/fopen_wrappers.c ??

User Contributed Notes
Safe Mode
add a note about notes
There are no user contributed notes for this page.
previousPersistent Database ConnectionsUsing PHP from the command linenext
Last updated: Tue, 28 May 2002
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: http://phpbuilder.com/
Last updated: Thu Oct 31 18:34:28 2002 EST