Kyberdigi Labs

PHP patch exec_dir

C. McCohy <mccohy@kyberdigi.cz>
Actual version: 5.2.12
Limit executing of external commands in PHP language cz en
Kyberdigi Labs
Projects
Exec_dir
Download
Installation
Configuration
Other
Bugs
History

Access counter
Counter

PHP patch exec_dir

Progamming language PHP allows one to limit executing of external commands via configuration directive safe_mode. This directive should contain full path to a directory conatining programs which PHP script can run. If the script tries to execute a command not located in this directory, the command is not executed. This configuration directive is active only if safe mode is enabled, which means more and sometimes unwanted restrictions to users.

PHP has no known possibility to limit executing of external commands with disabled safe mode. Teherefore, here is a patch adding special directive exec_dir straightly into PHP. This directive is very similar to safe_mode_exec_dir, but safe mode has not to be enabled.

This patch limits executing commands via functions from the exec family, i. e. exec(), system() or popen().


Download

You can download this patch for concrete PHP version you are using:

Version of PHP Patch exec_dir Size MD5
5.2.12 php-exec-dir.5.2.12.patch.gz 1662 B 5aaff8163b1d6a2bc3ca729b31cf840d
5.2.11 php-exec-dir.5.2.11.patch.gz 1679 B f3baff3c6c0c6b9f3f97c1fc975120de
5.2.10 php-exec-dir.5.2.10.patch.gz 1631 B ef906a1781ba401225767291767774c6
5.2.9 php-exec-dir.5.2.9.patch.gz 1610 B f6c10955043df74519420dd7a7921785
5.2.8 php-exec-dir.5.2.8.patch.gz 1644 B 06d0c00224958c9a3ca4a827fa054f93
5.2.7 php-exec-dir.5.2.7.patch.gz 1675 B fa349a9c3c005a6b9cec9149ffe26ef1
5.2.6 php-exec-dir.5.2.6.patch.gz 1655 B fdf7b9089317b768899edaed47edb4a6
5.2.5 php-exec-dir.5.2.5.patch.gz 1655 B bb9860a6b6abea4df5ecfba0770571dc
5.2.4 php-exec-dir.5.2.4.patch.gz 1642 B e97f87622c5056b880f096547c1521d2
5.2.3 php-exec-dir.5.2.3.patch.gz 1645 B 83a12c2a380d1597cdd017da4b9f2ac4
5.2.2 php-exec-dir.5.2.2.patch.gz 1647 B d5565bb4af91afc41bcd961d254207da
5.2.1 php-exec-dir.5.2.1.patch.gz 1644 B 3a311800ee45b3ed8e28c94c148190bf
5.2.0 php-exec-dir.5.2.0.patch.gz 1596 B 0edc3f5607a9211d4e1d5987b41c1cff
5.1.6 php-exec-dir.5.1.6.patch.gz 1643 B 38cf24d5675e9a2489c48ad4db1669da
5.1.5 php-exec-dir.5.1.5.patch.gz 1643 B 4a8cb3f2f84b03fa86193ab0f845fbe0a
5.1.4 php-exec-dir.5.1.4.patch.gz 1588 B b3e7bb57b806db4c70f45b1fda263c8d
5.1.3 php-exec-dir.5.1.3.patch.gz 1611 B 509acfd8fb4b060731f917b855ea7a0d
5.1.2 php-exec-dir.5.1.2.patch.gz 1585 B b42dcbde9d6ad540fafad795038721f6
5.1.1 php-exec-dir.5.1.1.patch.gz 1637 B 2590c0def73955c0f534b815fc3dbfe3
5.1.0 php-exec-dir.5.1.0.patch.gz 1665 B 91c628fcf29d2908ae7dae3a60120254
5.0.5 php-exec-dir.5.0.5.patch.gz 1673 B 5c2193621313c88ff8267b352b71302e
5.0.4 php-exec-dir.5.0.4.patch.gz 1654 B 29bb4bb4a9de2600839c673978e0b7d6
5.0.3 php-exec-dir.5.0.3.patch.gz 1686 B e4deac1a1675459441b01af6695c74fe
5.0.2 php-exec-dir.5.0.2.patch.gz 1628 B 795c4d02244cd54900ba5b33cb7b7633
5.0.1 php-exec-dir.5.0.1.patch.gz 1679 B b553a4d66f305b709a9639ed6e42e61a
5.0.0 php-exec-dir.5.0.0.patch.gz 1675 B 244b20e45aab59d858eaf470286cddd8
4.4.9 php-exec-dir.4.4.9.patch.gz 1898 B 3b0d5b757f9ecf921c92ce79f6141d18
4.4.8 php-exec-dir.4.4.8.patch.gz 1924 B 117bc6e13139f8b8543f5a7cd6a0abc6
4.4.7 php-exec-dir.4.4.7.patch.gz 1904 B b8a95e2f2c2793b2acb5dfd44cfc7514
4.4.6 php-exec-dir.4.4.6.patch.gz 1900 B eb38ff40ec2d31d19c89102589136a71
4.4.5 php-exec-dir.4.4.5.patch.gz 1898 B fd3c4213fd1195bcd5a4c1ab0ead610f
4.4.4 php-exec-dir.4.4.4.patch.gz 1931 B 386e78c0f7d6b6f85559d18c94def4f2
4.4.3 php-exec-dir.4.4.3.patch.gz 1932 B aff9215b65245df2d89860e991a59a38
4.4.2 php-exec-dir.4.4.2.patch.gz 1872 B c16133f2a3ef55b5918495a48bc59d69
4.4.1 php-exec-dir.4.4.1.patch.gz 1963 B 5972e35781848cd40d448829c55670c2
4.4.0 php-exec-dir.4.4.0.patch.gz 1974 B 1c207f9733ecbb2f65a40adf723e5bba
4.3.11 php-exec-dir.4.3.11.patch.gz 2013 B 288cf48d25631021ef4875b5454b204b
4.3.10 php-exec-dir.4.3.10.patch.gz 2066 B 7a127d0038a20ba036fdec4807b01b76
4.3.9 php-exec-dir.4.3.9.patch.gz 2002 B f2cc540a01548ddef3f3a28482171fe6
4.3.8 php-exec-dir.4.3.8.patch.gz 1992 B 6130360a0f05a4f93278377dac864819
4.3.7 php-exec-dir.4.3.7.patch.gz 1897 B fe7f67e15fb5cc35b7bb284afed265df
4.3.6 php-exec-dir.4.3.6.patch.gz 1918 B f5b0b55383a0ba617eeb142a854c64e9
4.3.5 php-exec-dir.4.3.5.patch.gz 1928 B 8ee3d449efafc24f9a733fec0a2ae679
4.3.4 php-exec-dir.4.3.4.patch.gz 1969 B 1518afb2f6ba3e9b86638caee6b2cb32
4.3.3 php-exec-dir.4.3.3.patch.gz 1996 B a00fcbc5e281b7f1423034717247df0c
4.3.2 php-exec-dir.4.3.2.patch.gz 1930 B c11d5e3e97d5e0a87b878bcc9ddb3995


Installation

This patch modifies PHP source, so first of all you need the PHP source, download it from official PHP site. After unpacking the archive, step into the directory created:
$ cd /path/to/directory/with/php-x.y.z
Now apply the patch:
$ zcat /path/to/file/php-exec-dir.x.y.z.patch.gz | patch -p1
If everything went OK, you should see output similar to this one:
patching file ext/standard/exec.c
patching file ext/standard/file.c
patching file main/main.c
patching file main/php_globals.h
patching file php.ini-dist
patching file php.ini-recommended
Now you have to compile and install the PHP.

Note: patch is compressed with gzip, so you have to use zcat to decompress it for the patch prgram or gunzip to decompress the patch as such.


Configuration

All you have to do is set the directove exec_dir. You can do this in php.ini or in configuration files of http server Apache. Beacause of some security reasons, this variable cannot be changed while running PHP script.

The directive should contain full path to an existing directory which contains files or symlinks to files which can be executed.

For example, let's imagine a server with running http server Apache, which serves 2 virtuals: virtual1.tld and virtual2.tld, each of these being maintained by other person. Maintainer of virtual1.tld wants to execute cp, rm and mv, maintainer of virtual2.tld wants to execute mysqldump.

The most clean solution now is to create one directory for virtual1.tld, i.e. /usr/local/php/bin/virtual1.tld and place binaries cp, rm and mv into it (or create symlinks to them). Similary we should create /usr/local/php/bin/virtual2.tld with mysqldump in it. Of cource it is better to create symlinks than to copy raw files, because of upgrades. Now we have to configure Apache:

  <VirtualHost virtual1.tld>
    php_admin_value exec_dir /usr/local/php/bin/virtual1.tld
  </VirtualHost>

  <VirtualHost virtual2.tld>
    php_admin_value exec_dir /usr/local/php/bin/virtual2.tld
  </VirtualHost>
A bit more simple is to create one directory for whole server containing trusted commands. In our example we can create directory /usr/local/php and place (or create symlinks) cp, mv, rm and mysqldump in it. Now it is enough to edit the php.ini file, where should be such line:
exec_dir = /usr/local/php
Note: after editing php.ini or Apache configuration files, the Apache server has to be restarted.


Other

Now we have to describe how the executing works. The name of the command to be executed will be modified, first it will be shorted: only the last part (only characters behind last occurence of '/') of the command will rest. Before this new command name, the contents of exec_dir will be placed.

For example, if we call /bin/cp and directive exec_dir conatins /usr/local/php, the command /bin/cp will be modified to cp and then to /usr/local/bin/cp. This command will be executed.

If the exec_dir directive is not set or is set to an empty string, functions exec(), popen() etc. are running without any restrictions.

If safe mode is enabled and safe_mode_exec_dir is set, exec_dir is ignored.

If exec_dir is set, the called command cannot contain strings ".." and ";", similar functionality offers safe_mode_exec_dir.


Bugs

Possible memory leak has been found by Manuel Mausz. He also sent a patch for exec_dir patch solving the issue (thanks!). All versions of the exec_dir patch bellow 5.2.11 include the leak, so please upgrade to 5.2.11, patch for this version should be secured.

A minimalistic bug about disabling exec_dir has been discovered by myself. If you want to disable the exec_dir in (i.e.) httpd.conf, you have to add such line somewhere:
php_admin_value exec_dir none
Although you set it so, it does not disable the usability of exec_dir, but it tries to run binaries as if they were in the root directory of the filesystem (/ls, /df etc.). If you are in trouble with this, please download new patch for your version of PHP. All patches listed in section download are correct ones, so check the MD5 of the patch you have to those in the list. Vulnerable versions: 4.3.2 - 4.3.8 (inclusive), 5.0.0 and 5.0.1. This is not a really dramatical bug you should really worry about.

VeNoMouS reported that you can execute commands out of specified directories if you prepend a ';', '|', '&' or may other ugly character to the beginning of the command and try to execute it with the backtick operator. In original safe_mode_exec_dir the backtick operator is turned off, in this patch it is not. Therefore, all the patches listed here were updated with a simple fix that escapes these and other characters the same way as other commands (as exec() or popen()) do. You are strongly encouraged to download new patch for your version of PHP. The patches listed in section download are correct ones, so check the MD5 of the patch you have to those in the list. All version from 4.3.2 to 4.3.7 (inclusive) were vulnerable.

There are no other known bugs at this moment. If you have exec_dir set to some directory and you will execute command out of it, please let me know at mccohy@kyberdigi.cz.

Note: the patch has not been tested on Windows platform yet. If you test it and it will work, let me know.


History

The patch was created for purposes of limit execution of external commands of users on a multidomain apache server, first for PHP version 4.2.1. The patch was sent to PHP developers so it could be a part of PHP (see the archive of mailinglist php-dev), but no one of PHP developers was interested in. On the other side, some PHP users wanted this patch, therefore this site was created.

Valid HTML 4.01 Kyberdigi Labs