CVE-2022-24800: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') (CWE-362)
value: October/System is the system module for October CMS, a self-hosted CMS platform based on the Laravel PHP Framework. Prior to versions 1.0.476, 1.1.12, and 2.2.15, when the developer allows the user to specify their own filename in the `fromData` method, an unauthenticated user can perform remote code execution (RCE) by exploiting a race condition in the temporary storage directory. This vulnerability affects plugins that expose the `October\Rain\Database\Attach\File::fromData` as a public interface and does not affect vanilla installations of October CMS since this method is not exposed or used by the system internally or externally. The issue has been patched in Build 476 (v1.0.476), v1.1.12, and v2.2.15. Those who are unable to upgrade may apply with patch to their installation manually as a workaround.
CWE description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
CVSS v3 base score: 8.1
Published: 2022-07-12T20:15Z
Repository: https://github.com/octobercms/library
Language: PHP
Files changed: src/Database/Attach/File.php
Commit date: 2022-03-29 12:51:19 +1100
Commit message: Refactor

Diff: diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php
index 221be9a6..92f67341 100644
--- a/src/Database/Attach/File.php
+++ b/src/Database/Attach/File.php
@@ -122,18 +122,18 @@ class File extends Model
     }
 
     /**
      * fromFile creates a file object from a file on the disk
      */
-    public function fromFile($filePath)
+    public function fromFile($filePath, $filename = null)
     {
         if ($filePath === null) {
             return;
         }
 
         $file = new FileObj($filePath);
-        $this->file_name = $file->getFilename();
+        $this->file_name = empty($filename) ? $file->getFilename() : $filename;
         $this->file_size = $file->getSize();
         $this->content_type = $file->getMimeType();
         $this->disk_name = $this->getDiskName();
 
         $this->putFile($file->getRealPath(), $this->disk_name);
@@ -150,14 +150,15 @@ class File extends Model
     {
         if ($data === null) {
             return;
         }
 
-        $tempPath = temp_path(basename($filename));
+        $tempName = str_replace('.', '', uniqid('', true)) . '.tmp';
+        $tempPath = temp_path($tempName);
         FileHelper::put($tempPath, $data);
 
-        $file = $this->fromFile($tempPath);
+        $file = $this->fromFile($tempPath, basename($filename));
         FileHelper::delete($tempPath);
 
         return $file;
     }
 

Vulnerable code:     public function fromFile($filePath)
        $this->file_name = $file->getFilename();
        $tempPath = temp_path(basename($filename));
        $file = $this->fromFile($tempPath);
Fixed code:     public function fromFile($filePath, $filename = null)
        $this->file_name = empty($filename) ? $file->getFilename() : $filename;
        $tempName = str_replace('.', '', uniqid('', true)) . '.tmp';
        $tempPath = temp_path($tempName);
        $file = $this->fromFile($tempPath, basename($filename));