PHP Velho Oeste 2024

セキュリティとセーフモード

セキュリティとセーフモード設定ディレクティブ
名前 デフォルト 変更可能 変更履歴
safe_mode "0" PHP_INI_SYSTEM PHP 5.4.0 で削除されました。
safe_mode_gid "0" PHP_INI_SYSTEM PHP 5.4.0 で削除されました。
safe_mode_include_dir NULL PHP_INI_SYSTEM 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 で削除されました。
PHP_INI_* モードの詳細および定義については どこで設定を行うのか を参照してください。

以下に設定ディレクティブに関する 簡単な説明を示します。

セーフモードの設定ディレクティブの簡単な説明を以下に示します。

safe_mode boolean

セーフモードを有効にするか否か。 PHP が --enable-safe-mode でコンパイルされている場合のデフォルトは On、そうでないときのデフォルトは Off です。

警告

この機能は PHP 5.3.0 で 非推奨となり、 PHP 5.4.0 で削除されました。

safe_mode_gid boolean

デフォルトでは、セーフモードはオープンしようとするファイルの UIDの比較チェックを行います。GIDの比較にすることでこのチェックを 緩やかなものにしたい場合、safe_mode_gidをオンにしてください。 ファイルにアクセスする際にUID (FALSE)を使用するか GID (TRUE)を使用するか制御できます。

safe_mode_include_dir string

このディレクトリ(そのサブディレクトリも含む)の配下のファイルが インクルードされる場合、UID/GID のチェックはバイパスされます。(ディレクトリは include_pathの配下であるか あるいはフルパスで記述される必要があります)

このディレクティブは include_pathと同様に コロン(Windowsではセミコロン)で分けた形式で複数のパスを書くことができます。 ここで指定される制限は実はプレフィックスでありディレクトリ名ではありません。 つまり、"safe_mode_include_dir = /dir/incl" と書くと "/dir/include" と "/dir/incls" の両方へのアクセスが許可されます(もしディレクトリが存在すれば)。 指定したディレクトリのみを許可したい場合には、最後にスラッシュを追加してください。 例:"safe_mode_include_dir = /dir/incl/" ディレクティブの値が空の場合、 異なる 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
script.php を実行すると、
<?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>
open_basedirでセットしたのと 同じ script.php を実行すると、以下のような結果になります:
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 の制限はバイナリを実行した場合は有効になりません。

add a note add a note

User Contributed Notes 3 notes

up
6
rayro at gmx dot de
15 years ago
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
up
-3
Anonymous
6 years ago
bad/missing config values for the Plesk server running the whole thing. I just followed the directions here: https://vb.3dlat.com/

You can configure PHP to have a separate error log file for each VirtualHost definition. The trick is knowing exactly how to set it up, because you can’t touch the configuration directly without breaking Plesk. Every domain name on your (dv) has its own directory in /var/www/vhosts. A typical directory has the following top level directories:

cgi-bin/
conf/
error_docs/
httpdocs/
httpsdocs/
...and so on

You’ll want to create a vhost.conf file in the domain directory’s conf/ folder with the following lines:

php_value error_log /path/to/error_log
php_flag display_errors off
php_value error_reporting 6143
php_flag log_errors on

Change the first value to match your actual installation (I used /tmp/phperrors.log). After you’re done editing the vhost.conf file, test the configuration from the console with:

apachectl configtest
…or if you don’t have apachectl (as Plesk 8.6 doesn’t seem to)…

/etc/init.d/httpd configtest

And finally tell Plesk that you’ve made this change.

/usr/local/psa/admin/bin/websrvmng -a
up
-13
Anonymous
6 years ago
Whether to enable PHP's safe mode. If PHP is compiled with --enable-safe-mode then defaults to On, otherwise Off. http://www.wikibanat.com
To Top