CVE-2019-16915 (published 2019-09-26T18:15Z)

An issue was discovered in pfSense through 2.4.4-p3. widgets/widgets/picture.widget.php uses the widgetkey parameter directly without sanitization (e.g., a basename call) for a pathname to file_get_contents or file_put_contents.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/pfsense/pfsense
commit_details: repo_url: https://github.com/pfsense/pfsense
author: jim-p
author_date: 2019-07-01 15:58:16-04:00
msg: Picture widget corrections. Fixes #9610

* Sanitize user input before using as path/filenames
* Use a more accurate method of determining image type on read
* More sanity checks before reading images.
repository: repo_url: https://github.com/pfsense/pfsense
repo_name: pfsense/pfsense
description: Main repository for pfSense
repo_language: PHP
owner: pfsense
file_changes: filename: picture.widget.php
change_type: ModificationType.MODIFY
diff: @@ -25,18 +25,42 @@ require_once("functions.inc");
 
 
 if ($_GET['getpic']=="true") {
-	$pic_type_s = explode(".", $user_settings['widgets'][$_GET['widgetkey']]['picturewidget_filename']);
-	$pic_type = $pic_type_s[1];
+	$wk = basename($_GET['widgetkey']);
+	$image_filename = "/conf/widget_image.{$wk}";
+	if (empty($wk) ||
+	    !isset($user_settings['widgets'][$wk]) ||
+	    !is_array($user_settings['widgets'][$wk]) ||
+	    !file_exists($image_filename)) {
+		echo null;
+		exit;
+	}
+
+	/* Do not rely on filename to determine image type. */
+	$img_info =getimagesize($image_filename);
+	switch ($img_info[2]) {
+		case IMAGETYPE_GIF:
+			$pic_type = "gif";
+			break;
+		case IMAGETYPE_JPEG:
+			$pic_type = "jpg";
+			break;
+		case IMAGETYPE_PNG:
+			$pic_type = "png";
+			break;
+		default:
+			echo null;
+			exit;
+	}
 
-	if ($user_settings['widgets'][$_GET['widgetkey']]['picturewidget']) {
-		if (file_exists("/conf/widget_image." . $_GET['widgetkey'])) {
-			$data = file_get_contents("/conf/widget_image." . $_GET['widgetkey']);
+	if ($user_settings['widgets'][$wk]['picturewidget']) {
+		if (file_exists($image_filename)) {
+			$data = file_get_contents($image_filename);
 		} else {
 			$data = "";
 		}
 	}
 
-	header("Content-Disposition: inline; filename=\"{$user_settings['widgets'][$_GET['widgetkey']]['picturewidget_filename']}\"");
+	header("Content-Disposition: inline; filename=\"" . basename($image_filename) . "\"");
 	header("Content-Type: image/{$pic_type}");
 	header("Content-Length: " . strlen($data));
 	echo $data;
@@ -44,6 +68,7 @@ if ($_GET['getpic']=="true") {
 }
 
 if ($_POST['widgetkey']) {
+	$wk = basename($_POST['widgetkey']);
 	set_customwidgettitle($user_settings);
 	if (is_uploaded_file($_FILES['pictfile']['tmp_name'])) {
 		/* read the file contents */
@@ -66,9 +91,9 @@ if ($_POST['widgetkey']) {
 				die("Not a gif/jpg/png");
 			}
 			$picname = basename($_FILES['uploadedfile']['name']);
-			$user_settings['widgets'][$_POST['widgetkey']]['picturewidget'] = "/conf/widget_image";
-			file_put_contents("/conf/widget_image." . $_POST['widgetkey'], $data);
-			$user_settings['widgets'][$_POST['widgetkey']]['picturewidget_filename'] = $_FILES['pictfile']['name'];
+			$user_settings['widgets'][$wk]['picturewidget'] = "/conf/widget_image";
+			file_put_contents("/conf/widget_image.{$wk}", $data);
+			$user_settings['widgets'][$wk]['picturewidget_filename'] = $_FILES['pictfile']['name'];
 		}
 	}
 

programming_language: PHP