RubySec

Providing security resources for the Ruby community

CVE-2024-46986 (camaleon_cms): Camaleon CMS affected by arbitrary file write to RCE (GHSL-2024-182)

ADVISORIES

GEM

camaleon_cms

SEVERITY

CVSS v3.x: 9.9 (Critical)

UNAFFECTED VERSIONS

  • < 2.8.0

PATCHED VERSIONS

  • >= 2.8.1

DESCRIPTION

An arbitrary file write vulnerability accessible via the upload method of the MediaController allows authenticated users to write arbitrary files to any location on the web server Camaleon CMS is running on (depending on the permissions of the underlying filesystem). E.g. This can lead to a delayed remote code execution in case an attacker is able to write a Ruby file into the config/initializers/ subfolder of the Ruby on Rails application.

Once a user upload is started via the upload method, the file_upload and the folder parameter.

def upload(settings = {})
  params[:dimension] = nil if params[:skip_auto_crop].present?
  f = { error: &apos;File not found.&apos; }
  if params[:file_upload].present?
    f = upload_file(params[:file_upload],
                    { folder: params[:folder], dimension: params[&apos;dimension&apos;], formats: params[:formats], versions: params[:versions],
                      thumb_size: params[:thumb_size] }.merge(settings))
  end
  [..]
end

are passed to the upload_file method. Inside that method the given settings are merged with some presets. The file format is checked against the formats settings we can override with the formats parameters.

# formats validations
  return { error: &quot;#{ct(&apos;file_format_error&apos;)} (#{settings[:formats]})&quot; } unless cama_uploader.class.validate_file_format(
    uploaded_io.path, settings[:formats]
 )

Our given folder is then passed unchecked to the Cama_uploader:

key = File.join(settings[:folder], settings[:filename]).to_s.cama_fix_slash
res = cama_uploader.add_file(settings[:uploaded_io], key, { same_name: settings[:same_name] })

In the add_file method of CamaleonCmsLocalUploader this key argument containing the unchecked path is then used to write the file to the file system:

def add_file(uploaded_io_or_file_path, key, args = {})
  [..]
  upload_io = uploaded_io_or_file_path.is_a?(String) ? File.open(uploaded_io_or_file_path) : uploaded_io_or_file_path
  File.open(File.join(@root_folder, key), &apos;wb&apos;) { |file| file.write(upload_io.read) }
  [..]
end

Impact

This issue may lead up to Remote Code Execution (RCE) via arbitrary file write.

Remediation

Normalize file paths constructed from untrusted user input before using them and check that the resulting path is inside the targeted directory. Additionally, do not allow character sequences such as .. in untrusted input that is used to build paths.

See Also

CodeQL: Uncontrolled data used in path expression OWASP: Path Traversal

RELATED