From 80c27aab8c1125483a76f4c004b4bf85c97dde04 Mon Sep 17 00:00:00 2001 From: Charles Fulton Date: Fri, 25 May 2012 08:41:52 -0700 Subject: [PATCH] MDL-27559 opendir: validate directory pointer on open --- backup/bb/restore_bb.php | 4 +++- backup/util/helper/backup_general_helper.class.php | 4 +++- lib/filelib.php | 5 ++++- lib/moodlelib.php | 11 ++++++++--- lib/rsslib.php | 5 ++++- lib/webdavlib.php | 5 ++++- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/backup/bb/restore_bb.php b/backup/bb/restore_bb.php index 3e01adfd859..962c46b7efc 100644 --- a/backup/bb/restore_bb.php +++ b/backup/bb/restore_bb.php @@ -8,7 +8,9 @@ defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.'); require_once($CFG->dirroot.'/backup/bb/xsl_emulate_xslt.inc'); function get_subdirs($directory){ - $opendirectory = opendir( $directory ); + if (!$opendirectory = opendir( $directory )) { + return array(); + } while(false !== ($filename = readdir($opendirectory))) { if (is_dir($directory.$filename) and $filename != ".." and $filename != "."){ $subdirs[] = $filename; diff --git a/backup/util/helper/backup_general_helper.class.php b/backup/util/helper/backup_general_helper.class.php index ff5dc89ddd1..df3c8d910f8 100644 --- a/backup/util/helper/backup_general_helper.class.php +++ b/backup/util/helper/backup_general_helper.class.php @@ -79,7 +79,9 @@ abstract class backup_general_helper extends backup_helper { return array(); } - $dir = opendir($path); + if (!$dir = opendir($path)) { + return array(); + } while (false !== ($file = readdir($dir))) { if ($file == '.' || $file == '..') { // Skip dots continue; diff --git a/lib/filelib.php b/lib/filelib.php index dbc1133f0a1..243d005ce63 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2520,7 +2520,10 @@ function fulldelete($location) { return false; } if (is_dir($location)) { - $currdir = opendir($location); + + if (!$currdir = opendir($location)) { + return false; + } while (false !== ($file = readdir($currdir))) { if ($file <> ".." && $file <> ".") { $fullfile = $location."/".$file; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8b0379b9551..c696edc31c6 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8005,7 +8005,10 @@ function get_list_of_plugins($directory='mod', $exclude='', $basedir='') { } if (file_exists($basedir) && filetype($basedir) == 'dir') { - $dirhandle = opendir($basedir); + if (!$dirhandle = opendir($basedir)) { + debugging("Could not open $basedir"); + return array(); + } while (false !== ($dir = readdir($dirhandle))) { $firstchar = substr($dir, 0, 1); if ($firstchar === '.' or $dir === 'CVS' or $dir === '_vti_cnf' or $dir === 'simpletest' or $dir === 'yui' or $dir === 'phpunit' or $dir === $exclude) { @@ -10201,7 +10204,7 @@ function apd_get_profiling() { } /** - * Delete directory or only it's content + * Delete directory or only its content * * @param string $dir directory path * @param bool $content_only @@ -10212,7 +10215,9 @@ function remove_dir($dir, $content_only=false) { // nothing to do return true; } - $handle = opendir($dir); + if (!$handle = opendir($dir)) { + return false; + } $result = true; while (false!==($item = readdir($handle))) { if($item != '.' && $item != '..') { diff --git a/lib/rsslib.php b/lib/rsslib.php index 01ac161659d..79a90b4498d 100644 --- a/lib/rsslib.php +++ b/lib/rsslib.php @@ -110,7 +110,10 @@ function rss_delete_file($componentname, $instance) { $dirpath = "$CFG->cachedir/rss/$componentname"; if (is_dir($dirpath)) { - $dh = opendir($dirpath); + if (!$dh = opendir($dirpath)) { + error_log("Could not open $dirpath"); + return; + } while (false !== ($filename = readdir($dh))) { if ($filename!='.' && $filename!='..') { if (preg_match("/{$instance->id}_/", $filename)) { diff --git a/lib/webdavlib.php b/lib/webdavlib.php index 70bff78a183..88561c1da41 100644 --- a/lib/webdavlib.php +++ b/lib/webdavlib.php @@ -946,7 +946,10 @@ EOD; if ($result) { // recurse directories if (is_dir($localpath)) { - $dp = opendir($localpath); + if (!$dp = opendir($localpath)) { + error_log("Could not open $localpath"); + return false; + } $fl = array(); while($filename = readdir($dp)) { if ((is_file($localpath."/".$filename) || is_dir($localpath."/".$filename)) && $filename!="." && $filename != "..") {