PHP: Bezpe�n� re�im - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<Persistentn� datab�zov� spojen�Pou�it� PHP z p��kazov� ��dky>
view the version of this page
Last updated: Thu, 15 Jul 2004

Kapitola 24. Bezpe�n� re�im

Bezpe�n� re�im PHP je pokus o �e�en� bezpe�nosti sd�len�ch server�. Je architektur�ln� nekorektn� pokou�et se �e�it tento probl�m na �rovni PHP, ale proto�e �e�en� na �rovni webovsk�ho serveru a opera�n�ho syst�mu nejsou p��li� realistick�, mnoho lid�, zvla�t� ISP, pou��v� nyn� bezpe�n� re�im.

Konfigura�n� direktivy, kter� ovl�daj� bezpe�n� re�im:
safe_mode = Off 
open_basedir = 
safe_mode_exec_dir = 
safe_mode_allowed_env_vars = PHP_ 
safe_mode_protected_env_vars = LD_LIBRARY_PATH 
disable_functions =

Pokud je safe_mode zapnut�, PHP kontroluje, je-li vlastn�k b��c�ho skriptu vlastn�kem souboru, s n�m� se m� manipulovat. Nap��klad:
-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
Spu�t�n� skriptu script.php
<?php
 readfile
('/etc/passwd');
?>
bude m�t v bezpe�n�m re�imu za v�sledek tuto chybu:
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

Pokud nam�sto safe_mode nastav�te adres�� open_basedir, potom v�echny operace se soubory budou omezeny pod specifikovan� adres��. Nap��klad (p��klad Apache httpd.conf):
<Directory /docroot>
  php_admin_value open_basedir /docroot 
</Directory>
Kdy� spust�te tent�� soubor script.php s timto nastaven�m open_basedir, dostanete tento v�sledek:
Warning: open_basedir restriction in effect. File is in wrong directory in 
/docroot/script.php on line 2

M��ete tak� vypnout jednotliv� funkce. Pokud p�id�me toto do souboru php.ini:
disable_functions readfile,system
z�sk�me takov�to v�stup:
Warning: readfile() has been disabled for security reasons in 
/docroot/script.php on line 2

Funkce omezen�/deaktivovan� v bezpe�n�m re�imu

Toto je pravd�podobn� ne�pln� a mo�n� nespr�vn� p�ehled funkc� omezen�ch v bezpe�n�m re�imu.

Tabulka 24-1. Funkce omezen� v bezpe�n�m re�imu

FunkceOmezen�
dbmopen()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
dbase_open()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
filepro()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
filepro_rowcount()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
filepro_retrieve()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
ifx_*()sql_safe_mode restrictions, (!= safe mode)
ingres_*()sql_safe_mode restrictions, (!= safe mode)
mysql_*()sql_safe_mode restrictions, (!= safe mode)
pg_loimport()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
posix_mkfifo()Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript.
putenv()Obeys the safe_mode_protected_env_vars and safe_mode_allowed_env_vars ini-directives. Viz tak� dokumentaci putenv()
move_uploaded_file()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
chdir()Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript.
dl()Tyto funkce jsou v bezpe�n�m re�imu (safe-mode) deaktivov�ny.
backtick oper�torTyto funkce jsou v bezpe�n�m re�imu (safe-mode) deaktivov�ny.
shell_exec() (funk�n� ekvivalent backticks)Tyto funkce jsou v bezpe�n�m re�imu (safe-mode) deaktivov�ny.
exec()M��ete spou�t�t programy pouze uvnit� adres��e safe_mode_exec_dir. Z praktick�ch d�vod� nen� moment�ln� mo�n� m�t v cest� k souboru s programem komponenty ...
system()M��ete spou�t�t programy pouze uvnit� adres��e safe_mode_exec_dir. Z praktick�ch d�vod� nen� moment�ln� mo�n� m�t v cest� k souboru s programem komponenty ...
passthru()M��ete spou�t�t programy pouze uvnit� adres��e safe_mode_exec_dir. Z praktick�ch d�vod� nen� moment�ln� mo�n� m�t v cest� k souboru s programem komponenty ...
popen()M��ete spou�t�t programy pouze uvnit� adres��e safe_mode_exec_dir. Z praktick�ch d�vod� nen� moment�ln� mo�n� m�t v cest� k souboru s programem komponenty ...
mkdir()Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript.
rmdir()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
rename()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript.
unlink()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript.
copy()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript. (pro source a target)
chgrp()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
chown()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript.
chmod()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Nav�c nem��ete nastavovat SUID, SGID a sticky bit
touch()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript.
symlink()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript. (pozn.: testov�n je pouze c�l)
link()Kontroluje, zda soubory/adres��e, se kter�mi pracujete, maj� stejn� UID jako spu�t�n� skript. Kontroluje, zda adres��, ve kter�m pracujete, m� stejn� UID jako spu�t�n� skript. (pozn.: testov�n je pouze c�l)
getallheaders()V bezpe�n�m re�imu nebudou hlavi�ky za��naj�c� 'authorization' (bez ohledu na velikost p�smen) vraceny. Varov�n�: toto nefunguje s aol-server implementac� getallheaders()!
Jak�koli funkce kter� pou��vaj� php4/main/fopen_wrappers.c ??



add a note add a note User Contributed Notes
Bezpe�n� re�im
bertrand dot gorge at epistema dot com
23-Sep-2004 12:58
Beware that when in safe mode, mkdir creates folders with apache's UID, though the files you create in them are of your script's UID (usually the same as your FTP uid).

What this means is that if you create a folder, you won't be able to remove it, nor to remove any of the files you've created in it (because those operations require the UID to be the same).

Ideally mkdir should create the folder with the script's UID, but this has already been discussed and will not be fixed in the near future.

In the meantime, I would advice NOT to user mkdir in safe mode, as you may end up with folders you can't remove/use.
Russ
31-Aug-2004 03:52
Sometimes you're stuck on a system you don't run and you can't control the setting of safe mode.  If your script needs to run on different hosts (some with safe mode, some without it), you can code around it with something like:

<?php
// Check for safe mode
if( ini_get('safe_mode') ){
  
// Do it the safe mode way
}else{
  
// Do it the regular way
}

?>
daniel dot gaddis at tlc dot state dot tx dot us
17-Mar-2004 07:03
on windows if multiple directories are wanted for safe_mode_exec_dir and open_basedir be sure to separate the paths with a semi colon and double quote the whole path string. For example:

safe_mode = On
safe_mode_exec_dir = "F:\WWW\HTML;F:\batfiles\batch"
open_basedir = "F:\WWW\HTML;F:\batfiles\batch"
uuganbat at datacom dot mn
26-Feb-2004 09:32
users executing shell scripts by filename.php, and I take the server in safe mode but some things are not working on web server, and disabled functions by php.ini but the simple commands in Unix are executing how  disable to execute shell scripts via *.php file.

example
<?php
echo `ls -l /etc/`; echo `more /etc/passwd`;
?>
matthias at remove-this dot hrz dot uni-kassel dot de
12-Nov-2003 05:38
Beware: Safe mode will not permit you to create new files in directories which have different owner than the owner of the script. This typically applies to /tmp, so contrary to Unix intuition, you will not be able to create new files there (even if the /tmp rights are set correctly).

If you need to write into files in /tmp (for example to put logfiles of your PHP application there) create them first on the command line by doing a

touch /tmp/whatever.log

as the same user who owns the PHP script. Then, provided the rest is configured correctly, the PHP script will be able to write into that file.
info at phpcoding dot net
08-Mar-2003 02:44
readfile() seems not always to take care of the safe_mode setting.
When the file you want to read with readfile() resides in the same directory as the script itself, it doesn`t matter who the owner of the file is, readfile() will work.
gtg782a at mail dot gatech dot edu
26-Jan-2003 05:14
zebz: The user would not be able to create a directory outside the namespace where he/she would be able to modify its contents. One can't create a directory that becomes apache-owned unless one owns the parent directory.

Another security risk: since files created by apache are owned by apache, a user could call the fputs function and output PHP code to a newly-created file with a .php extension, thus creating an apache-owned PHP script on the server. Executing that apache-owned script would allow the script to work with files in the apache user's namespace, such as logs. A solution would be to force PHP-created files to be owned by the same owner/group as the script that created them. Using open_basedir would be a likely workaround to prevent ascension into uncontrolled areas.
dizzy at roedu dot net
16-Jan-2003 07:25
For people using linux there is a very nice solution to the shared server problem. It's called vserver/ctx. Here is the URL:
10-Sep-2002 10:30
Some paranoid web managers also restrict the set_user_abort() function.

This constitutes a security issue for hosted web sites, because the hosted script cannot guarantee safe atomic operations on files in a reasonnable time (the time limit may still be in effect):

If set_user_abort() is disabled by the web admin, a user can corrupt the files of a hosted web site by simply sending many requests to the PHP script, and aborting it fast. In some cases that can be easily reproduced after a dozen of attempts, the script will be interrupted in the middle of a file or database update!

The only way for the hosted web site to protect itself in this case is to use a sleep() with a random but not null short time before performing atomic operations.

Web admins should then definitely NOT disable the set_user_abort() function which is vital to ensure atomic operations on hosted critical files or databases.

Instead they should only disable the set_time_limit() function, and set a constant but reasonnable time for any script to complete their atomic operations.
10-Sep-2002 10:19
disk_free_space($directory) is also restricted by the safe_mode ! It can also be completely forbidden as this would allow a script to test the available space in order to fill it with a giant file, preventing other scripts to use that space (notably in "/tmp").
disk_free_space() is then informational, and this does not prevent system quotas to limit the size of your files to a value lower than the available free space!
Most web server admins that propose PHP hosting, will implement quotas for your hosting account, but also on any shared resources such as temporary folders.
tom at tom420 dot com
19-Jul-2002 08:33
open_basedir only restricts file operations to files and directories under a specified directory, but you can still user system ("vi /home/somedir/somefile"), so safe_mode still has a place here as it is much more restrictive then open_basedir.

Also, to reply to someone who said that 'above' and 'below' was a matter of perspective, sure it is. Of course, a file is not under another one, etc, it just pointed by some inode. But in the common language we consider the root (/) to be above everything else, and /home is below root, and /home/myfile is below /home. There is no written standard, but most people (those I know anyway) agree on that syntax.
zebz at ihaveenoughspam_hotmail dot com
28-Apr-2002 03:42
All the filesystem-related functions (unlink, fopen, unlink, etc) seems to be restricted the same way in safe mode, at least on PHP 4.2. If the file UID is different *but* the directory (where the file is located) UID is the same, it will work.

So creating a directory in safe mode is usually a bad idea since the UID will be different from the script (it will be the apache UID) so it won't be possible to do anything with the files created on this directory.
devik at cdi dot cz
25-Jan-2002 12:45
Just to note, I created patch which allows VirtualHost to set User under which all (PHP too) runs. It is more secure than safe_mode. See luxik.cdi.cz/~devik/apache/ if you are interested
jedi at tstonramp dot com
08-Sep-2001 02:17
Many filesystem-related functions are not appropriately restricted when Safe Mode is activated on an NT server it seems.  I would assume that this is due to the filesystem not making use of UID.

In all of my scripts, no matter WHO owns the script (file Ownership-wise) or WHO owns the directory/file in question; both UIDs display

(getmyuid() and fileowner()) as UID = 0

This has the rather nasty side effect of Safe Mode allowing multiple filesystem operations because it believes the script owner and file/dir owner are one and the same.

While this can be worked around by the judicious application of proper filesystem privileges, it's still a "dud" that many of Safe Mode's securities are simply not there with an NT implementation.

<Persistentn� datab�zov� spojen�Pou�it� PHP z p��kazov� ��dky>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page