CVE-2022-24796 (published 2022-03-31T23:15Z)

RaspberryMatic is a free and open-source operating system for running a cloud-free smart-home using the homematicIP / HomeMatic hardware line of IoT devices. A Remote Code Execution (RCE) vulnerability in the file upload facility of the WebUI interface of RaspberryMatic exists. Missing input validation/sanitization in the file upload mechanism allows remote, unauthenticated attackers with network access to the WebUI interface to achieve arbitrary operating system command execution via shell metacharacters in the HTTP query string. Injected commands are executed as root, thus leading to a full compromise of the underlying system and all its components. Versions after `2.31.25.20180428` and prior to `3.63.8.20220330` are affected. Users are advised to update to version `3.63.8.20220330` or newer. There are currently no known workarounds to mitigate the security impact and users are advised to update to the latest version available.

Severity: HIGH
CVSS v2 base score: 10.0 (AV:N/AC:L/Au:N/C:C/I:C/A:C)
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-78
cwe_name: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
description: The product constructs all or part of an OS command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended OS command when it is sent to a downstream component.
extended_description: 
		   This weakness can lead to a vulnerability in environments in which the attacker does not have direct access to the operating system, such as in web applications. Alternately, if the weakness occurs in a privileged program, it could allow the attacker to specify commands that normally would not be accessible, or to call alternate commands with privileges that the attacker does not have. The problem is exacerbated if the compromised process does not follow the principle of least privilege, because the attacker-controlled commands may run with special system privileges that increases the amount of damage.
            There are at least two subtypes of OS command injection:
               
				 The application intends to execute a single, fixed program that is under its own control. It intends to use externally-supplied inputs as arguments to that program. For example, the program might use system("nslookup [HOSTNAME]") to run nslookup and allow the user to supply a HOSTNAME, which is used as an argument. Attackers cannot prevent nslookup from executing. However, if the program does not remove command separators from the HOSTNAME argument, attackers could place the separators into the arguments, which allows them to execute their own program after nslookup has finished executing.
				 The application accepts an input that it uses to fully select which program to run, as well as which commands to use. The application simply redirects this entire command to the operating system. For example, the program might use "exec([COMMAND])" to execute the [COMMAND] that was supplied by the user. If the COMMAND is under attacker control, then the attacker can execute arbitrary commands or programs. If the command is being executed using functions like exec() and CreateProcess(), the attacker might not be able to combine multiple commands together in the same line.
               
            From a weakness standpoint, these variants represent distinct programmer errors. In the first variant, the programmer clearly intends that input from untrusted parties will be part of the arguments in the command to be executed. In the second variant, the programmer does not intend for the command to be accessible to any untrusted party, but the programmer probably has not accounted for alternate ways in which malicious attackers can provide input.
         
         

Fix information:
repo_url: https://github.com/jens-maus/RaspberryMatic
commit_details: repo_url: https://github.com/jens-maus/RaspberryMatic
author: Jens Maus
author_date: 2022-03-14 11:36:29+01:00
msg: reworked fileupload.ccc in 0031-WebUI-Fix-FileUpload WebUI patch to
contain several security checks for a valid admin session id and
query string checks as well as omitting the critical use of "eval"
to parse the URL query string altogether. This should significantly
improve the security burden, thus fix a security issue raised by @qx-f7
repository: repo_url: https://github.com/jens-maus/RaspberryMatic
repo_name: jens-maus/RaspberryMatic
description: :house: A feature-rich but lightweight, buildroot-based Linux operating system alternative for your CloudFree CCU3/ELV-Charly 'homematicIP CCU' IoT smarthome central. Running as a pure virtual appliance (ProxmoxVE, Home Assistant, LXC, Docker/OCI, Kubernetes/K8s, etc.) or on a dedicated embedded device (RaspberryPi, Tinkerboard, IntelNUC, etc.)
repo_language: JavaScript
owner: jens-maus
file_changes: filename: 0031-WebUI-Fix-FileUpload.patch
change_type: ModificationType.MODIFY
diff: @@ -365,12 +365,18 @@
  }
 --- occu/WebUI/www/config/fileupload.ccc.orig
 +++ occu/WebUI/www/config/fileupload.ccc
-@@ -0,0 +1,61 @@
+@@ -0,0 +1,109 @@
 +#!/bin/sh
-+# shellcheck shell=dash disable=SC2169,SC2034,SC2154
++# shellcheck shell=dash disable=SC3036,SC3010,SC2034,SC3060,SC2116,SC3045 source=/dev/null
 +
 +echo -ne "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
 +
++# allow only POST requests
++if [[ "${REQUEST_METHOD}" != "POST" ]]; then
++  echo "ERROR: no POST request"
++  exit 1
++fi
++
 +# fake read boundary+disposition, etc.
 +read -r boundary
 +read -r disposition
@@ -387,21 +393,63 @@
 +# 6 + 2 newlines == 10 junk bytes
 +a=$((a*2+b+c+d+10))
 +
-+# extract all params from QUERY_STRING
-+# shellcheck disable=SC2046,SC2116,SC2086
-+eval $(echo ${QUERY_STRING//&/;})
-+
 +# calculate the expected content length using
 +# HTTP_CONTENT_LENGTH or CONTENT_LENGTH
-+if [ -z "${HTTP_CONTENT_LENGTH}" ]; then
++if [[ -z "${HTTP_CONTENT_LENGTH}" ]]; then
 +  HTTP_CONTENT_LENGTH=${CONTENT_LENGTH}
 +fi
 +SIZE=$((HTTP_CONTENT_LENGTH-a))
 +
++# continue only if SIZE > 0
++if [[ "${SIZE}" -le 0 ]]; then
++  echo "ERROR: POST size <= 0"
++  exit 1
++fi
++
++# extract known params from QUERY_STRING only
++while IFS= read -r -d '&' KEYVAL && [[ -n "$KEYVAL" ]]; do
++  case ${KEYVAL%%=*} in
++    url) url=${KEYVAL#*=} ;;
++    sid) sid=${KEYVAL#*=} ;;
++    action) action=${KEYVAL#*=} ;;
++    downloadOnly) downloadOnly=${KEYVAL#*=} ;;
++  esac
++done <<END
++$(echo "${QUERY_STRING}&")
++END
++
++# check for url and action parameter
++if [[ -z "${url}" ]] || [[ -z "${action}" ]]; then
++  echo "ERROR: missing required URL parameters"
++  exit 1
++fi
++
++# check for a valid ADMIN session id
++if [[ "${#sid}" -eq 12 ]]; then
++  # parse the current version
++  [[ -r /VERSION ]] && . /VERSION
++
++  # use CCU.getVersion which is allowed only for Admins
++  RES=$(/usr/bin/curl http://127.0.0.1/api/homematic.cgi \
++                      -H 'Content-Type: application/json' \
++                      -d "{\"method\":\"CCU.getVersion\",\"params\":{\"_session_id_\": \"${sid//@}\"}}")
++
++  # check the curl result contains the current
++  # version number or not
++  if ! echo "${RES}" | grep -q "${VERSION}"; then
++    echo "ERROR: no valid admin session id"
++    exit 1
++  fi
++else
++  echo "ERROR: invalid session id"
++  exit 1
++fi
++
 +# write out the data
 +filename=$(mktemp -p /usr/local/tmp)
 +if ! /usr/bin/head -q -c ${SIZE} >"${filename}"; then
-+  echo "ERROR (head)"
++  echo "ERROR: head failure"
++  exit 1
 +fi
 +
 +echo "<html>"

programming_language: PowerShell
filename: fileupload.ccc
change_type: ModificationType.MODIFY
diff: @@ -1,8 +1,14 @@
 #!/bin/sh
-# shellcheck shell=dash disable=SC2169,SC2034,SC2154
+# shellcheck shell=dash disable=SC3036,SC3010,SC2034,SC3060,SC2116,SC3045 source=/dev/null
 
 echo -ne "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
 
+# allow only POST requests
+if [[ "${REQUEST_METHOD}" != "POST" ]]; then
+  echo "ERROR: no POST request"
+  exit 1
+fi
+
 # fake read boundary+disposition, etc.
 read -r boundary
 read -r disposition
@@ -19,21 +25,63 @@ d=0
 # 6 + 2 newlines == 10 junk bytes
 a=$((a*2+b+c+d+10))
 
-# extract all params from QUERY_STRING
-# shellcheck disable=SC2046,SC2116,SC2086
-eval $(echo ${QUERY_STRING//&/;})
-
 # calculate the expected content length using
 # HTTP_CONTENT_LENGTH or CONTENT_LENGTH
-if [ -z "${HTTP_CONTENT_LENGTH}" ]; then
+if [[ -z "${HTTP_CONTENT_LENGTH}" ]]; then
   HTTP_CONTENT_LENGTH=${CONTENT_LENGTH}
 fi
 SIZE=$((HTTP_CONTENT_LENGTH-a))
 
+# continue only if SIZE > 0
+if [[ "${SIZE}" -le 0 ]]; then
+  echo "ERROR: POST size <= 0"
+  exit 1
+fi
+
+# extract known params from QUERY_STRING only
+while IFS= read -r -d '&' KEYVAL && [[ -n "$KEYVAL" ]]; do
+  case ${KEYVAL%%=*} in
+    url) url=${KEYVAL#*=} ;;
+    sid) sid=${KEYVAL#*=} ;;
+    action) action=${KEYVAL#*=} ;;
+    downloadOnly) downloadOnly=${KEYVAL#*=} ;;
+  esac
+done <<END
+$(echo "${QUERY_STRING}&")
+END
+
+# check for url and action parameter
+if [[ -z "${url}" ]] || [[ -z "${action}" ]]; then
+  echo "ERROR: missing required URL parameters"
+  exit 1
+fi
+
+# check for a valid ADMIN session id
+if [[ "${#sid}" -eq 12 ]]; then
+  # parse the current version
+  [[ -r /VERSION ]] && . /VERSION
+
+  # use CCU.getVersion which is allowed only for Admins
+  RES=$(/usr/bin/curl http://127.0.0.1/api/homematic.cgi \
+                      -H 'Content-Type: application/json' \
+                      -d "{\"method\":\"CCU.getVersion\",\"params\":{\"_session_id_\": \"${sid//@}\"}}")
+
+  # check the curl result contains the current
+  # version number or not
+  if ! echo "${RES}" | grep -q "${VERSION}"; then
+    echo "ERROR: no valid admin session id"
+    exit 1
+  fi
+else
+  echo "ERROR: invalid session id"
+  exit 1
+fi
+
 # write out the data
 filename=$(mktemp -p /usr/local/tmp)
 if ! /usr/bin/head -q -c ${SIZE} >"${filename}"; then
-  echo "ERROR (head)"
+  echo "ERROR: head failure"
+  exit 1
 fi
 
 echo "<html>"

programming_language: Shell