Skip to content

Commit

Permalink
Added function_usable() to common functions
Browse files Browse the repository at this point in the history
It is now used to check whether dangerous functions like eval() and exec() are available.
It appears that the Suhosin extension (which is becoming popular) terminates script
execution instead of returning e.g. FALSE when it has a function blacklisted.
function_exists() checks are insufficient and our only option is to check the ini
settings here.

Filed an issue here: sektioneins/suhosin#18
... hopefully we'll be able to deal with this in a more elegant way in the future.

(this commit supersedes PR bcit-ci#1809)
  • Loading branch information
narfbg committed Nov 7, 2012
1 parent 17e11cd commit e9d2dc8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 12 deletions.
47 changes: 47 additions & 0 deletions system/core/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,5 +651,52 @@ function _stringify_attributes($attributes, $js = FALSE)
}
}

// ------------------------------------------------------------------------

if ( ! function_exists('function_usable'))
{
/**
* Function usable
*
* Executes a function_exists() check, and if the Suhosin PHP
* extension is loaded - checks whether the function that is
* checked might be disabled in there as well.
*
* This is useful as function_exists() will return FALSE for
* functions disabled via the *disable_functions* php.ini
* setting, but not for *suhosin.executor.func.blacklist* and
* *suhosin.executor.disable_eval*. These settings will just
* terminate script execution if a disabled function is executed.
*
* @link http://www.hardened-php.net/suhosin/
* @param string $function_name Function to check for
* @return bool TRUE if the function exists and is safe to call,
* FALSE otherwise.
*/
function function_usable($function_name)
{
static $_suhosin_func_blacklist;

if (function_exists($function_name))
{
if ( ! isset($_suhosin_func_blacklist))
{
$_suhosin_func_blacklist = extension_loaded('suhosin')
? array()
: explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));

if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
{
$_suhosin_func_blacklist[] = 'eval';
}
}

return in_array($function_name, $_suhosin_func_blacklist, TRUE);
}

return FALSE;
}
}

/* End of file Common.php */
/* Location: ./system/core/Common.php */
4 changes: 3 additions & 1 deletion system/core/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,9 @@ protected function _ci_load($_ci_data)
// If the PHP installation does not support short tags we'll
// do a little string replacement, changing the short tags
// to standard PHP echo statements.
if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE && config_item('rewrite_short_tags') === TRUE)
if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
&& config_item('rewrite_short_tags') === TRUE && function_usable('eval')
)
{
echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
}
Expand Down
11 changes: 7 additions & 4 deletions system/libraries/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -1732,11 +1732,14 @@ protected function _send_with_mail()
*/
protected function _send_with_sendmail()
{
$fp = @popen($this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'.' -r '.$this->clean_email($this->_headers['Return-Path']), 'w');

if ($fp === FALSE OR $fp === NULL)
// is popen() enabled?
if ( ! function_usable('popen')
OR FALSE === ($fp = @popen(
$this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From'])
.' -t -r '.$this->clean_email($this->_headers['Return-Path'])
, 'w'))
) // server probably has popen disabled, so nothing we can do to get a verbose error.
{
// server probably has popen disabled, so nothing we can do to get a verbose error.
return FALSE;
}

Expand Down
14 changes: 11 additions & 3 deletions system/libraries/Image_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,11 @@ public function image_process_imagemagick($action = 'resize')
}

$retval = 1;
@exec($cmd, $output, $retval);
// exec() might be disabled
if (function_usable('exec'))
{
@exec($cmd, $output, $retval);
}

// Did it work?
if ($retval > 0)
Expand Down Expand Up @@ -947,7 +951,11 @@ public function image_process_netpbm($action = 'resize')
$cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';

$retval = 1;
@exec($cmd, $output, $retval);
// exec() might be disabled
if (function_usable('exec'))
{
@exec($cmd, $output, $retval);
}

// Did it work?
if ($retval > 0)
Expand All @@ -959,7 +967,7 @@ public function image_process_netpbm($action = 'resize')
// With NetPBM we have to create a temporary image.
// If you try manipulating the original it fails so
// we have to rename the temp file.
copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
unlink($this->dest_folder.'netpbm.tmp');
@chmod($this->full_dst_path, FILE_WRITE_MODE);

Expand Down
6 changes: 3 additions & 3 deletions system/libraries/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ protected function _file_mime_type($file)
? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
: 'file --brief --mime '.$file['tmp_name'].' 2>&1';

if (function_exists('exec'))
if (function_usable('exec'))
{
/* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
* However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
Expand All @@ -1223,7 +1223,7 @@ protected function _file_mime_type($file)
}
}

if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec'))
if ( (bool) @ini_get('safe_mode') === FALSE && function_usable('shell_exec'))
{
$mime = @shell_exec($cmd);
if (strlen($mime) > 0)
Expand All @@ -1237,7 +1237,7 @@ protected function _file_mime_type($file)
}
}

if (function_exists('popen'))
if (function_usable('popen'))
{
$proc = @popen($cmd, 'r');
if (is_resource($proc))
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Release Date: Not Released
- Changed environment defaults to report all errors in *development* and only fatal ones in *testing*, *production* but only display them in *development*.
- Updated *ip_address* database field lengths from 16 to 45 for supporting IPv6 address on :doc:`Trackback Library <libraries/trackback>` and :doc:`Captcha Helper <helpers/captcha_helper>`.
- Removed *cheatsheets* and *quick_reference* PDFs from the documentation.
- Added availability checks where usage of dangerous functions like ``eval()`` and ``exec()`` is required.

- Helpers

Expand Down Expand Up @@ -270,6 +271,7 @@ Release Date: Not Released
- Removed redundant conditional to determine HTTP server protocol in ``set_status_header()``.
- Changed ``_exception_handler()`` to respect php.ini *display_errors* setting.
- Added function ``is_https()`` to check if a secure connection is used.
- Added function ``function_usable()`` to check if a function exists and is not disabled by `Suhosin <http://www.hardened-php.net/suhosin/>`.
- Added support for HTTP-Only cookies with new config option *cookie_httponly* (default FALSE).
- Renamed method ``_call_hook()`` to ``call_hook()`` in the :doc:`Hooks Library <general/hooks>`.
- :doc:`Output Library <libraries/output>` changes include:
Expand Down
15 changes: 14 additions & 1 deletion user_guide_src/source/general/common_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@ is_https()
==========

Returns TRUE if a secure (HTTPS) connection is used and FALSE
in any other case (including non-HTTP requests).
in any other case (including non-HTTP requests).

function_usable($function_name)
===============================

Returns TRUE if a function exists and is usable, FALSE otherwise.

This function runs a ``function_exists()`` check and if the
`Suhosin extension <http://www.hardened-php.net/suhosin/>` is loaded,
checks if it doesn't disable the function being checked.

It is useful if you want to check for the availability of functions
such as ``eval()`` and ``exec()``, which are dangerous and might be
disabled on servers with highly restrictive security policies.

0 comments on commit e9d2dc8

Please sign in to comment.