Security: Arbitrary shell command injection through PDF import or unpaper preprocessing

Bug #1961528 reported by René Walendy
256
This bug affects 1 person
Affects Status Importance Assigned to Milestone
ocrfeeder (Ubuntu)
Confirmed
Undecided
Unassigned

Bug Description

I have discovered a security vulnerability that allows a malicious actor to inject arbitrary shell commands into a system running `ocrfeeder` with automatic `unpaper` pre-processing enabled.

Proof of concept:
- Create a PDF file with the name `foo; nautilus; bar.pdf`
- Open `ocrfeeder`
- Ensure that settings -> tools -> unpaper images is checked
- Import the PDF file
- `nautilus` is started (If your particular system doesn't have nautilus, you can use any other executable here)

The vulnerability presents itself as follows: When `ocrfeeder` generates the temporary file as an input for `unpaper`, it keeps the name of the originally imported PDF intact (see https://gitlab.gnome.org/GNOME/ocrfeeder/-/blob/master/src/ocrfeeder/util/lib.py#L182). `unpaper` is then called on this temporary file: In https://gitlab.gnome.org/GNOME/ocrfeeder/-/blob/master/src/ocrfeeder/util/lib.py#L189, the file names for input and output are concatenated to a command string. This string is subsequently passed to `os.system()`, i.e., executed in a fully-featured shell. No escaping or quoting of any kind is used, hence, arbitrary control characters can be injected into the command through the file name of the initial PDF. Especially, spaces and semicolons will be parsed, allowing to break out of the file name and execute arbitrary commands on the shell.

Since `ocrfeeder-cli` does not support `unpaper` as far as I understand, exploiting this vulnerability requires the user to explicitly import the malicious file, which leads me to believe that zero-click exploitation is not possible at the moment.

I have also reported this issue to https://gitlab.gnome.org/GNOME/ocrfeeder .

Metadata:
system: Ubuntu 20.04.4 LTS
affected package version: ocrfeeder 0.8.1+git20200128.b945089-1ubuntu1

CVE References

Revision history for this message
René Walendy (0k53-rene-f830) wrote (last edit ):

Digging further, I was able to expand on this. ocrfeeder also uses Python's subprocess.run(cmd, shell=True) when it converts pdf files to images (see https://gitlab.gnome.org/GNOME/ocrfeeder/-/blob/master/src/ocrfeeder/util/lib.py#L104). While quotation marks are used, nothing prevents us from injecting a subshell into the filename.

Proof of Concept:
- keep settings -> tools -> unpaper images turned OFF
- create a PDF file with name `foo$(nautilus).pdf`
- Import the PDF file
- `nautilus` is started

The vulnerability causes the subshell to be started when orcfeeder invokes `gs` to convert the PDF file into individual images. This vector is slightly more effective, since it doesn't require the user to have the unpaper option turned on.

EDIT: This was apparently fixed upstream in https://gitlab.gnome.org/GNOME/ocrfeeder/-/commit/5286120c8bc8b7ba74e0f9b19b5262b509f38cee but the version in the package repository is still affected.

summary: - Security: Arbitrary shell command injection through file import path
+ Security: Arbitrary shell command injection through PDF import or
+ unpaper preprocessing
Revision history for this message
Alex Murray (alexmurray) wrote :

Thanks for reporting this vulnerability to the upstream project and for linking to the fix upstream - perhaps the best way forward might be to either ask the upstream project if they want to request a CVE for this issue OR to do so yourself via https://cveform.mitre.org/ - once a CVE is assigned all distros will then be alerted to the issue and can work on patching it as part of their regular security update process.

If a CVE is assigned please let us know - also since this fix is already public, would you be happy for this bug report to also be made public?

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

To be clear: The fix in 5286120c only applies to the vector described in the addendum. The unpaper method from the initial report is still viable on master (51952ab8 as of 2022-02-21). I am not aware of any fix for this method being published before. My proposed fix has not yet been accepted upstream. Hence, I suggest that the bug be kept private until the fix is published in the upstream repository.

I have reached out to the upstream maintainers regarding CVE assignment and will report back when this process completes.

Revision history for this message
Alex Murray (alexmurray) wrote :

Excellent - thanks for the clarification - we will wait to hear more from you regarding CVE assignment and upstream patching. Thanks.

Revision history for this message
Seth Arnold (seth-arnold) wrote :

Hello René, have you heard anything from upstream on this recently?

Thanks

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

Hi, unfortunately no. Neither any comment about the vulnerability, nor any activity regarding my proposed fix.

Revision history for this message
Seth Arnold (seth-arnold) wrote :

That's unfortunate; it might be worth opening the bugs here and there so the wider community has an opportunity to work on a fix. If you haven't communicated lately, could you give them a poke?

Thanks

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

I dug around a bit and found their private email. I have a reply now – Turns out that the maintainer had gitlab notifications turned off. The general statement is that ocrfeeder "is pretty much unmaintained", but that they will review my proposed patch before Monday. I'll report back as soon as I hear about the review.

ocrfeeder also seems to be looking for a new upstream maintainer (I've been asked, but I'm afraid I will be unable to take on this responsibility with my current workload.)

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

Good news: The maintainer was very quick to review the fix, this is being merged upstream now. Once this is done, I think we can make this bug public.

So far no consensus on CVE assignment yet, I'm on it.

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

https://gitlab.gnome.org/GNOME/ocrfeeder/-/tags/0.8.5 has just been released with the fix. CVE request is still pending.

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

CVE-2022-27811 has just been assigned to this vulnerability. What would be the next step in the process of getting the new release packaged and shipped?

Revision history for this message
René Walendy (0k53-rene-f830) wrote :

On the details page to CVE-2022-27811 (https://ubuntu.com/security/CVE-2022-27811) I've just seen alexmurray's comment "https://gitlab.gnome.org/GNOME/ocrfeeder/-/commit/5286120c8bc8b7ba74e0f9b19b5262b509f38cee may also be needed for additional hardening".

I would consider the changes introduced in the mentioned commit to be unnecessary (though I am certainly open for a counter-argument!), which is why my patch in 9209bce8afaf6fde19cdac7f5eaea1b744c3e79e replaces a significant portion of that code. The original code would try to symlink the input PDF file such that the symlink would not contain any shell metacharacters. While this works around the second attack vector (#1), I consider this a rather contrived hack that does not solve the underlying problem, which is the improper handling of the ghostscript invocation's arguments. With proper argument handling, I do not see any benefit in continuing to use that hack.

Revision history for this message
Alex Murray (alexmurray) wrote :

Thanks for the clarification - this makes sense, I'll remove it.

Steve Beattie (sbeattie)
information type: Private Security → Public Security
Steve Beattie (sbeattie)
Changed in ocrfeeder (Ubuntu):
status: New → Confirmed
Revision history for this message
René Walendy (0k53-rene-f830) wrote :

The currently shipped package is still vulnerable about a year after this report. What can I do to speed things up?

To post a comment you must log in.
This report contains Public Security information  
Everyone can see this security related information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.