Manuale PHP
Indietro |  | Avanti

# Capitolo 42. Modalità sicura (Safe mode)

La modalità Safe Mode è un tentativo di risolvere il problema di sicurezza derivante dalla condivisione del server. Dal punto di vista architetturale non è corretto cercare di risolvere questo problema al livello del PHP, ma poiché le alternative al livello del web server e del SO (Sistema Operativo) non sono realistiche, in molti, specialmente ISP (Internet Service Provider), utilizzano la modalità sicura.

# Security and Safe Mode

Tabella 42-1. Direttive di configurazione della Modalità sicura e Sdella icurezza

Nome | Default | Changeable | Changelog
--- | --- | --- | ---
safe_mode | "0" | PHP_INI_SYSTEM |
safe_mode_gid | "0" | PHP_INI_SYSTEM | Disponibile dal PHP 4.1.0.
safe_mode_include_dir | NULL | PHP_INI_SYSTEM | Disponibile dal PHP 4.1.0.
safe_mode_exec_dir | "" | PHP_INI_SYSTEM |
safe_mode_allowed_env_vars | "PHP_" | PHP_INI_SYSTEM |
safe_mode_protected_env_vars | "LD_LIBRARY_PATH" | PHP_INI_SYSTEM |
open_basedir | NULL | PHP_INI_SYSTEM |
disable_functions | "" | `php.ini` only | Disponibile dal PHP 4.0.1.
disable_classes | "" | `php.ini` only | Disponibile dal PHP 4.3.2.

Per ulteriori dettagli e definizione delle costanti PHP_INI_* vedere ini_set().

Breve descrizione dei parametri di configurazione.

safe_mode boolean

  Abilita o disabilita la modalità sicura di PHP's. Leggere il capitolo Sicurezza per ulteriori informazioni.

safe_mode_gid boolean

  Di 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 UID (`FALSE`) or GID (`TRUE`) checking upon file access.

safe_mode_include_dir string

  UID/GID checks are bypassed when including files from this directory and its subdirectories (directory must also be in include_path or full path must including).

  As of PHP 4.2.0, this directive can take a colon (semi-colon on Windows) separated path in a fashion similar to the include_path directive, rather than just a single directory.

  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/"

safe_mode_exec_dir string

  If PHP is used in safe mode, system() and the other functions executing system programs refuse to start programs that are not in this directory. You have to use / as directory separator on all environments including Windows.

safe_mode_allowed_env_vars string

  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).

  > Nota: If this directive is empty, PHP will let the user modify ANY environment variable!

safe_mode_protected_env_vars string

  This directive contains a comma-delimited list of environment variables that the end user won't be able to change using putenv(). These variables will be protected even if safe_mode_allowed_env_vars is set to allow to change them.

open_basedir string

  Limit the files that can be opened by PHP to the specified directory-tree, including the file itself. This directive is NOT affected by whether Safe Mode is turned On or Off.

  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.

  The special value . indicates that the working directory of the script will be used as the base-directory. This is, however, a little dangerous as the working directory of the script can easily be changed with chdir().

  In `httpd.conf`, open_basedir can be turned off (e.g. for some virtual hosts) the same way as any other configuration directive with "php_admin_value open_basedir none".

  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.

  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/"

  > Nota: Support for multiple directories was added in 3.0.7.

  The default is to allow all files to be opened.

disable_functions string

  This directive allows you to disable certain functions for security reasons. It takes on a comma-delimited list of function names. disable_functions is not affected by Safe Mode.

  This directive must be set in `php.ini` For example, you cannot set this in `httpd.conf`.

disable_classes string

  This directive allows you to disable certain classes for security reasons. It takes on a comma-delimited list of class names. disable_classes is not affected by Safe Mode.

  This directive must be set in `php.ini` For example, you cannot set this in `httpd.conf`.

  > Availability note: This directive became available in PHP 4.3.2

See also: register_globals, display_errors, and log_errors

Le direttive di configurazione che controllano la modalità sicura sono:

    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 =

Quando safe_mode è attiva (on), il PHP verifica se il proprietario dello script in esecuzione e il proprietario del file su cui si sta operando con una funzione sui file, coincidono. Per esempio:

    -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

Eseguendo questo 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

Se, invece di safe_mode, viene definita una directory open_basedir allora tutte le operazioni sui file saranno limitate ai file sottostanti la directory specificata. Per esempio (nel file httpd.conf di Apache):

    <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

È possibile inoltre disabilitare le singole funzioni. Se si aggiunge la seguente riga al file php.ini:

    disable_functions readfile,system

Then we get this output:

    Warning: readfile() has been disabled for security reasons in
    /docroot/script.php on line 2

Indietro | Partenza | Avanti
Connessioni Persistenti ai Database | Risali | Funzioni limitate/disabilitate dalla modalità sicura (safe-mode)