<HTML ><HEAD ><TITLE >Safe Mode</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Features" HREF="features.html"><LINK REL="PREVIOUS" TITLE="Persistent Database Connections" HREF="features.persistent-connections.html"><LINK REL="NEXT" TITLE="Functions restricted/disabled by safe mode" HREF="features.safe-mode.functions.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="features.persistent-connections.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="features.safe-mode.functions.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="features.safe-mode" >Chapter 22. Safe Mode</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="features.safe-mode.html#ini.sect.safe-mode" >Security and Safe Mode</A ></DT ><DT ><A HREF="features.safe-mode.functions.html" >Functions restricted/disabled by safe mode</A ></DT ></DL ></DIV ><P > 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. </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="ini.sect.safe-mode" ></A >Security and Safe Mode</H1 ><P > <DIV CLASS="table" ><A NAME="AEN6100" ></A ><P ><B >Table 22-1. Security and Safe Mode Configuration Directives</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Name</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Default</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Changeable</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >safe_mode</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >safe_mode_gid</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >safe_mode_include_dir</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >safe_mode_exec_dir</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >""</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >safe_mode_allowed_env_vars</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >safe_mode_protected_env_vars</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >LD_LIBRARY_PATH</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >open_basedir</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >disable_functions</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >""</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ></TBODY ></TABLE ></DIV > For further details and definition of the PHP_INI_* constants see <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >. </P ><P > Here is a short explanation of the configuration directives. <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><A NAME="ini.safe-mode" ></A ><TT CLASS="parameter" ><I >safe_mode</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Whether to enable PHP's safe mode. Read the <A HREF="security.html" >Security</A > and chapter for more information. </P ></DD ><DT ><A NAME="ini.safe-mode-gid" ></A ><TT CLASS="parameter" ><I >safe_mode_gid</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > By default, Safe Mode does a UID compare check when opening files. If you want to relax this to a GID compare, then turn on safe_mode_gid. Whether to use <TT CLASS="literal" >UID</TT > (<TT CLASS="constant" ><B >FALSE</B ></TT >) or <TT CLASS="literal" >GID</TT > (<TT CLASS="constant" ><B >TRUE</B ></TT >) checking upon file access. </P ></DD ><DT ><A NAME="ini.safe-mode-include-dir" ></A ><TT CLASS="parameter" ><I >safe_mode_include_dir</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >UID</TT >/<TT CLASS="literal" >GID</TT > checks are bypassed when including files from this directory and its subdirectories (directory must also be in <A HREF="configuration.directives.html#ini.include-path" >include_path</A > or full path must including). </P ><P > As of PHP 4.2.0, this directive can take a semi-colon separated path in a similar fashion to the <A HREF="configuration.directives.html#ini.include-path" >include_path</A > directive, rather than just a single directory. </P ><P > The restriction specified is actually a prefix, not a directory name. This means that "safe_mode_include_dir = /dir/incl" also allows access to "/dir/include" and "/dir/incls" if they exist. When you want to restrict access to only the specified directory, end with a slash. For example: "safe_mode_include_dir = /dir/incl/" </P ></DD ><DT ><A NAME="ini.safe-mode-exec-dir" ></A ><TT CLASS="parameter" ><I >safe_mode_exec_dir</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > If PHP is used in safe mode, <A HREF="function.system.html" ><B CLASS="function" >system()</B ></A > and the other <A HREF="ref.exec.html" >functions executing system programs</A > refuse to start programs that are not in this directory. </P ></DD ><DT ><A NAME="ini.safe-mode-allowed-env-vars" ></A ><TT CLASS="parameter" ><I >safe_mode_allowed_env_vars</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > Setting certain environment variables may be a potential security breach. This directive contains a comma-delimited list of prefixes. In Safe Mode, the user may only alter environment variables whose names begin with the prefixes supplied here. By default, users will only be able to set environment variables that begin with PHP_ (e.g. PHP_FOO=BAR). </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > If this directive is empty, PHP will let the user modify ANY environment variable! </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.safe-mode-protected-env-vars" ></A ><TT CLASS="parameter" ><I >safe_mode_protected_env_vars</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > This directive contains a comma-delimited list of environment variables that the end user won't be able to change using <A HREF="function.putenv.html" ><B CLASS="function" >putenv()</B ></A >. These variables will be protected even if safe_mode_allowed_env_vars is set to allow to change them. </P ></DD ><DT ><A NAME="ini.open-basedir" ></A ><TT CLASS="parameter" ><I >open_basedir</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > Limit the files that can be opened by PHP to the specified directory-tree, including the file itself. This directive is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >NOT</I ></SPAN > affected by whether Safe Mode is turned On or Off. </P ><P > When a script tries to open a file with, for example, fopen or gzopen, the location of the file is checked. When the file is outside the specified directory-tree, PHP will refuse to open it. All symbolic links are resolved, so it's not possible to avoid this restriction with a symlink. </P ><P > The special value <SPAN CLASS="systemitem" >.</SPAN > indicates that the directory in which the script is stored will be used as base-directory. </P ><P > Under Windows, separate the directories with a semicolon. On all other systems, separate the directories with a colon. As an Apache module, open_basedir paths from parent directories are now automatically inherited. </P ><P > The restriction specified with open_basedir is actually a prefix, not a directory name. This means that "open_basedir = /dir/incl" also allows access to "/dir/include" and "/dir/incls" if they exist. When you want to restrict access to only the specified directory, end with a slash. For example: "open_basedir = /dir/incl/" </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Support for multiple directories was added in 3.0.7. </P ></BLOCKQUOTE ></DIV ><P > The default is to allow all files to be opened. </P ></DD ><DT ><A NAME="ini.disable-functions" ></A ><TT CLASS="parameter" ><I >disable_functions</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > This directive allows you to disable certain functions for <A HREF="security.html" >security</A > reasons. It takes on a comma-dilimited list of function names. disable_functions is not affected by <A HREF="features.safe-mode.html#ini.safe-mode" >Safe Mode</A >. </P ><P > This directive must be set in <TT CLASS="filename" >php.ini</TT > For example, you cannot set this in <TT CLASS="filename" >httpd.conf</TT >. </P ></DD ></DL ></DIV > </P ><P > See also: <A HREF="configuration.directives.html#ini.register-globals" >register_globals</A >, <A HREF="ref.errorfunc.html#ini.display-errors" >display_errors</A >, and <A HREF="ref.errorfunc.html#ini.log-errors" >log_errors</A > </P ><P > When <A HREF="features.safe-mode.html#ini.safe-mode" >safe_mode</A > 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: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ls" >-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</PRE ></TD ></TR ></TABLE > Running this script.php <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php readfile('/etc/passwd'); ?></PRE ></TD ></TR ></TABLE > results in this error when safe mode is enabled: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >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</PRE ></TD ></TR ></TABLE > </P ><P > However, there may be environments where a strict <TT CLASS="literal" >UID</TT > check is not appropriate and a relaxed <TT CLASS="literal" >GID</TT > check is sufficient. This is supported by means of the <A HREF="features.safe-mode.html#ini.safe-mode-gid" >safe_mode_gid</A > switch. Setting it to <TT CLASS="literal" >On</TT > performs the relaxed <TT CLASS="literal" >GID</TT > checking, setting it to <TT CLASS="literal" >Off</TT > (the default) performs <TT CLASS="literal" >UID</TT > checking. </P ><P > If instead of <A HREF="features.safe-mode.html#ini.safe-mode" >safe_mode</A >, you set an <A HREF="features.safe-mode.html#ini.open-basedir" >open_basedir</A > directory then all file operations will be limited to files under the specified directory For example (Apache httpd.conf example): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" ><Directory /docroot> php_admin_value open_basedir /docroot </Directory></PRE ></TD ></TR ></TABLE > If you run the same script.php with this <A HREF="features.safe-mode.html#ini.open-basedir" >open_basedir</A > setting then this is the result: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Warning: open_basedir restriction in effect. File is in wrong directory in /docroot/script.php on line 2</PRE ></TD ></TR ></TABLE > </P ><P > You can also disable individual functions. Note that the disable_functions directive can not be used outside of the <TT CLASS="filename" >php.ini</TT > 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 <TT CLASS="filename" >php.ini</TT > file: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >disable_functions readfile,system</PRE ></TD ></TR ></TABLE > Then we get this output: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Warning: readfile() has been disabled for security reasons in /docroot/script.php on line 2</PRE ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="features.persistent-connections.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="features.safe-mode.functions.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Persistent Database Connections</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="features.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Functions restricted/disabled by safe mode</TD ></TR ></TABLE ></DIV ></BODY ></HTML >