The example of setting open_basdir to docroot has the effect of protecting files outside the web site but does NOT PROTECT files INSIDE the website, and could be construed as a suggested use. There is no reason to use open_basedir at all except to protect from misbehaving scripts. If there is a misbehaving script, one would not like it to delete, modify or upload files to the core website either. An example of a safer setting for open_basedir would be /docroot/safedir (or as is used in doc page "Description of core php.ini directives",open_basedir Note, to tighten it from /www/ to /www/tmp).
セキュリティとセーフモード
| 名前 | デフォルト | 変更可能 | 変更履歴 |
|---|---|---|---|
| safe_mode | "0" | PHP_INI_SYSTEM | PHP 5.4.0 で削除されました。 |
| safe_mode_gid | "0" | PHP_INI_SYSTEM | PHP 4.1.0 から利用可能。PHP 5.4.0 で削除されました。 |
| safe_mode_include_dir | NULL | PHP_INI_SYSTEM | PHP 4.1.0 から利用可能。PHP 5.4.0 で削除されました。 |
| safe_mode_exec_dir | "" | PHP_INI_SYSTEM | PHP 5.4.0 で削除されました。 |
| safe_mode_allowed_env_vars | "PHP_" | PHP_INI_SYSTEM | PHP 5.4.0 で削除されました。 |
| safe_mode_protected_env_vars | "LD_LIBRARY_PATH" | PHP_INI_SYSTEM | PHP 5.4.0 で削除されました。 |
以下に設定ディレクティブに関する 簡単な説明を示します。
セーフモードの設定ディレクティブの簡単な説明を以下に示します。
- safe_mode boolean
-
セーフモードを有効にするか否か。 PHP が --enable-safe-mode でコンパイルされている場合のデフォルトは On、そうでないときのデフォルトは Off です。
警告この機能は PHP 5.3.0 で 非推奨となりました。 この機能を使用しないことを強く推奨します。
- safe_mode_gid boolean
-
デフォルトでは、セーフモードはオープンしようとするファイルの UIDの比較チェックを行います。GIDの比較にすることでこのチェックを 緩やかなものにしたい場合、safe_mode_gidをオンにしてください。 ファイルにアクセスする際にUID (FALSE)を使用するか GID (TRUE)を使用するか制御できます。
- safe_mode_include_dir string
-
このディレクトリ(そのサブディレクトリも含む)の配下のファイルが インクルードされる場合、UID/GID のチェックはバイパスされます。(ディレクトリは include_pathの配下であるか あるいはフルパスで記述される必要があります)
PHP 4.2.0以降、このディレクティブは include_pathと同様に コロン(Windowsではセミコロン)で分けた形式で複数のパスを書くことができます。 ここで指定される制限は実はプレフィックスでありディレクトリ名ではありません。 つまり、"safe_mode_include_dir = /dir/incl" と書くと "/dir/include" と "/dir/incls" の両方へのアクセスが許可されます(もしディレクトリが存在すれば)。 指定したディレクトリのみを許可したい場合には、最後にスラッシュを追加してください。 例:"safe_mode_include_dir = /dir/incl/" PHP 4.2.3 と PHP 4.3.3 以降では、ディレクティブの値が空の場合、 異なる UID/GID を持つファイルを インクルードすることはできません。 以前のバージョンでは、全てのファイルをインクルード可能でした。 - safe_mode_exec_dir string
-
PHPがセーフモードで動作する場合、system()や その他のプログラム実行関数を、 このディレクトリ以外で起動することは拒否されます。 Windowsを含む全ての環境において ディレクトリのセパレータとして/を使用する必要があります。
- safe_mode_allowed_env_vars string
-
ある種の環境変数の設定はセキュリティ上の潜在的な欠陥となりえます。 このディレクティブにはプレフィックスをカンマで区切って書くことができます。 セーフモードでは、ここに書かれたプレフィックスで始まる環境変数だけを ユーザーが変更できるようになります。デフォルトでは、ユーザーは PHP_ で始まる名前の環境変数 (PHP_FOO=BAR など) だけをセットすることができます。
注意:
このディレクティブが空の場合、PHPは全ての環境変数について ユーザーが変更することを許可してしまいます。
- safe_mode_protected_env_vars string
-
putenv()を使ってエンドユーザーが変更するのを 防ぎたい環境変数をカンマ区切りで記述します。ここで設定された環境変数は もしもsafe_mode_allowed_env_varsでは許可されているものであっても 保護されます。
open_basedir, disable_functions, disable_classes, register_globals, display_errors, log_errorsも参照してください。
セーフモードがonの場合、PHPは、 現在のスクリプトの所有者がファイル関数により処理されているファイルまたはディレクトリ の所有者に一致するかどうかを調べます。例えば、
-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
<?php
readfile('/etc/passwd');
?>
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
UID checking. しかし、多くの環境において、厳密なUIDチェックは 適切ではなく、より緩やかなGIDチェックで十分です。 これはsafe_mode_gidスイッチで サポートされます。これをOnにすると制限の緩い GIDチェックに、Off(デフォルト) にするとUIDチェックになります。
safe_modeの代わりに、 open_basedirディレクトリを セットすると、全てのファイル操作は特定のディレクトリ配下のみに制限されます。 例えば(Apacheのhttpd.confの例):
<Directory /docroot> php_admin_value open_basedir /docroot </Directory>
Warning: open_basedir restriction in effect. File is in wrong directory in /docroot/script.php on line 2
特定の関数を無効にすることもできます。 disable_functionsディレクティブは php.ini以外では使用できないことに注意してください。 つまり、httpd.conf上のバーチャルホスト毎あるいはディレクトリ毎に 関数を無効にすることはできない、ということになります。 もしphp.iniファイルに以下を追加した場合:
disable_functions = readfile,system
Warning: readfile() has been disabled for security reasons in /docroot/script.php on line 2
もちろん、これらの PHP の制限はバイナリを実行した場合は有効になりません。
Theres a failure with open_basedir and per-host configuration
in apache as described in bug #42836: http://bugs.php.net/bug.php?id=42836
I got the same errors on my development windows system and apache 2.2.4 with php 5.3.beta1.
This error (or similar) is shown:
Warning: Unknown: open_basedir restriction in effect. File(...)
is not within the allowed path(s): (� �� �@5�,�)
Fix:
- try slashes at the end of the folder name
or
- put "php_admin_value open_basedir ..." at first of all in the configuration
