CVE-2023-38997 (published 2023-08-09T19:15Z)

A directory traversal vulnerability in the Captive Portal templates of OPNsense Community Edition before 23.7 and Business Edition before 23.4.2 allows attackers to execute arbitrary system commands as root via a crafted ZIP archive.

CVSS v3 base score: 7.2, HIGH (CVSS:3.1/AV:N/AC:L/PR:H/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/opnsense/core
commit_details: repo_url: https://github.com/opnsense/core
author: Ad Schellevis
author_date: 2023-06-28 17:17:12+02:00
msg: Services: Captive Portal - safeguard template overlay distribution.
repository: repo_url: https://github.com/opnsense/core
repo_name: opnsense/core
description: OPNsense GUI, API and systems backend
repo_language: PHP
owner: opnsense
file_changes: filename: overlay_template.py
change_type: ModificationType.MODIFY
diff: @@ -40,15 +40,15 @@ if len(sys.argv) > 1:
     cnf = Config()
     zoneid = sys.argv[1]
     target_directory = '/var/captiveportal/zone%s/htdocs/' % zoneid
-    template_data = cnf.fetch_template_data(sys.argv[1])
+    template_data = cnf.fetch_template_data(zoneid)
     if template_data is not None and len(template_data) > 20:
         print ('overlay user template package for zone %s' % zoneid)
         zip_content = base64.b64decode(template_data)
         input_data = BytesIO(zip_content)
         with zipfile.ZipFile(input_data, mode='r', compression=zipfile.ZIP_DEFLATED) as zf_in:
             for zf_info in zf_in.infolist():
-                if zf_info.filename[-1] != '/':
-                    target_filename = '%s%s' % (target_directory, zf_info.filename)
+                target_filename = '%s%s' % (target_directory, zf_info.filename)
+                if os.path.realpath(target_filename).startswith(target_directory):
                     file_target_directory = '/'.join(target_filename.split('/')[:-1])
                     if not os.path.isdir(file_target_directory):
                         os.makedirs(file_target_directory)

programming_language: Python