From 961323a05f5cfbba80594aff3fe7b954510fc22e Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Fri, 27 Jan 2017 17:26:24 +0000 Subject: [PATCH 1/2] MDL-57801 core_filestorage: Verify hash of temp file before committing This addresses an edge scenario on NFS filesystems with no space remaining, where subsequent uploads fail silently while zero byte files are saved to the pool (and for some reason the filesize check passes). --- lib/filestorage/file_system_filedir.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/filestorage/file_system_filedir.php b/lib/filestorage/file_system_filedir.php index b307a5dc758..0993dd8314d 100644 --- a/lib/filestorage/file_system_filedir.php +++ b/lib/filestorage/file_system_filedir.php @@ -424,9 +424,9 @@ class file_system_filedir extends file_system { ignore_user_abort($prev); throw new file_exception('storedfilecannotcreatefile'); } - if (filesize($hashfile.'.tmp') !== $filesize) { - // This should not happen. - unlink($hashfile.'.tmp'); + if (sha1_file($hashfile.'.tmp') !== $contenthash) { + // Highly unlikely edge case, but this can happen on an NFS volume with no space remaining. + @unlink($hashfile.'.tmp'); ignore_user_abort($prev); throw new file_exception('storedfilecannotcreatefile'); } From efa7d241680e3201efe6a32ac46e1d9cbfae4acf Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Wed, 8 Feb 2017 14:33:48 +0000 Subject: [PATCH 2/2] MDL-57801 core_filestorage: Delete temp file if disk space runs out --- lib/filestorage/file_system_filedir.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/filestorage/file_system_filedir.php b/lib/filestorage/file_system_filedir.php index 0993dd8314d..ff7a4400d5f 100644 --- a/lib/filestorage/file_system_filedir.php +++ b/lib/filestorage/file_system_filedir.php @@ -421,6 +421,7 @@ class file_system_filedir extends file_system { @unlink($hashfile.'.tmp'); if (!copy($pathname, $hashfile.'.tmp')) { // Borked permissions or out of disk space. + @unlink($hashfile.'.tmp'); ignore_user_abort($prev); throw new file_exception('storedfilecannotcreatefile'); }