Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SECURITY] Fix Partial Path Traversal Vulnerability #4444

Conversation

JLLeitschuh
Copy link

@JLLeitschuh JLLeitschuh commented Jul 29, 2022

Security Vulnerability Fix

This pull request fixes a partial-path traversal vulnerability due to an insufficient path traversal guard.

Even if you deem, as the maintainer of this project, this is not necessarily fixing a security vulnerability, it is still a valid security hardening.

Preamble

Impact

This issue allows a malicious actor to potentially break out of the expected directory. The impact is limited to sibling directories. For example, userControlled.getCanonicalPath().startsWith("/usr/out") will allow an attacker to access a directory with a name like /usr/outnot.

Why?

To demonstrate this vulnerability, consider "/usr/outnot".startsWith("/usr/out").
The check is bypassed although /outnot is not under the /out directory.
It's important to understand that the terminating slash may be removed when using various String representations of the File object.
For example, on Linux, println(new File("/var")) will print /var, but println(new File("/var", "/") will print /var/;
however, println(new File("/var", "/").getCanonicalPath()) will print /var.

The Fix

Comparing paths with the java.nio.files.Path#startsWith will adequately protect againts this vulnerability.

For example: file.getCanonicalFile().toPath().startsWith(BASE_DIRECTORY) or file.getCanonicalFile().toPath().startsWith(BASE_DIRECTORY_FILE.getCanonicalFile().toPath())

Other Examples

➡️ Vulnerability Disclosure ⬅️

👋 Vulnerability disclosure is a super important part of the vulnerability handling process and should not be skipped! This may be completely new to you, and that's okay, I'm here to assist!

First question, do we need to perform vulnerability disclosure? It depends!

  1. Is the vulnerable code only in tests or example code? No disclosure required!
  2. Is the vulnerable code in code shipped to your end users? Vulnerability disclosure is probably required!

Vulnerability Disclosure How-To

You have a few options options to perform vulnerability disclosure. However, I'd like to suggest the following 2 options:

  1. Request a CVE number from GitHub by creating a repository-level GitHub Security Advisory. This has the advantage that, if you provide sufficient information, GitHub will automatically generate Dependabot alerts for your downstream consumers, resolving this vulnerability more quickly.
  2. Reach out to the team at Snyk to assist with CVE issuance. They can be reached at the Snyk's Disclosure Email. Note: Please include JLLeitschuh Disclosure in the subject of your email so it is not missed.

Detecting this and Future Vulnerabilities

You can automatically detect future vulnerabilities like this by enabling the free (for open-source) GitHub Action.

I'm not an employee of GitHub, I'm simply an open-source security researcher.

Source

This contribution was automatically generated with an OpenRewrite refactoring recipe, which was lovingly hand crafted to bring this security fix to your repository.

The source code that generated this PR can be found here:
PartialPathTraversalVulnerability

Why didn't you disclose privately (ie. coordinated disclosure)?

This PR was automatically generated, in-bulk, and sent to this project as well as many others, all at the same time.

This is technically what is called a "Full Disclosure" in vulnerability disclosure, and I agree it's less than ideal. If GitHub offered a way to create private pull requests to submit pull requests, I'd leverage it, but that infrastructure, sadly, doesn't exist yet.

The problem is that as an open source software security researcher, I (exactly like open source maintainers), I only have so much time in a day. I'm able to find vulnerabilities impacting hundreds, or sometimes thousands of open source projects with tools like GitHub Code Search and CodeQL. The problem is that my knowledge of vulnerabilities doesn't scale very well.

Individualized vulnerability disclosure takes time and care. It's a long and tedious process, and I have a significant amount of experience with it (I have over 50 CVEs to my name). Even tracking down the reporting channel (email, Jira, ect..) can take time and isn't automatable. Unfortunately, when facing prblems of this scale, individual reporting doesn't work well either.

Additionally, if I just spam out emails or issues, I'll just overwhelm already over taxed maintainers, I don't want to do this either.

By creating a pull request, I am aiming to provide maintainers something highly actionable to actually fix the identified vulnerability; a pull request.

There's a larger discussion on this topic that can be found here: JLLeitschuh/security-research#12

Opting-Out

If you'd like to opt-out of future automated security vulnerability fixes like this, please consider adding a file called
.github/GH-ROBOTS.txt to your repository with the line:

User-agent: JLLeitschuh/security-research
Disallow: *

This bot will respect the ROBOTS.txt format for future contributions.

Alternatively, if this project is no longer actively maintained, consider archiving the repository.

CLA Requirements

This section is only relevant if your project requires contributors to sign a Contributor License Agreement (CLA) for external contributions.

It is unlikely that I'll be able to directly sign CLAs. However, all contributed commits are already automatically signed-off.

The meaning of a signoff depends on the project, but it typically certifies that committer has the rights to submit this work under the same license and agrees to a Developer Certificate of Origin
(see https://developercertificate.org/ for more information).

- Git Commit Signoff documentation

If signing your organization's CLA is a strict-requirement for merging this contribution, please feel free to close this PR.

Sponsorship & Support

This contribution is sponsored by HUMAN Security Inc. and the new Dan Kaminsky Fellowship, a fellowship created to celebrate Dan's memory and legacy by funding open-source work that makes the world a better (and more secure) place.

This PR was generated by Moderne, a free-for-open source SaaS offering that uses format-preserving AST transformations to fix bugs, standardize code style, apply best practices, migrate library versions, and fix common security vulnerabilities at scale.

Tracking

All PR's generated as part of this fix are tracked here: JLLeitschuh/security-research#13

This fixes a partial path traversal vulnerability.

Replaces `dir.getCanonicalPath().startsWith(parent.getCanonicalPath())`, which is vulnerable to partial path traversal attacks, with the more secure `dir.getCanonicalFile().toPath().startsWith(parent.getCanonicalFile().toPath())`.

To demonstrate this vulnerability, consider `"/usr/outnot".startsWith("/usr/out")`.
The check is bypassed although `/outnot` is not under the `/out` directory.
It's important to understand that the terminating slash may be removed when using various `String` representations of the `File` object.
For example, on Linux, `println(new File("/var"))` will print `/var`, but `println(new File("/var", "/")` will print `/var/`;
however, `println(new File("/var", "/").getCanonicalPath())` will print `/var`.

Weakness: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Severity: Medium
CVSSS: 6.1
Detection: CodeQL & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.PartialPathTraversalVulnerability)

Reported-by: Jonathan Leitschuh <[email protected]>
Signed-off-by: Jonathan Leitschuh <[email protected]>

Bug-tracker: JLLeitschuh/security-research#13

Co-authored-by: Moderne <[email protected]>
@JLLeitschuh JLLeitschuh force-pushed the fix/JLL/partial-path-traversal-vulnerability branch from bb8a337 to 47a8352 Compare July 29, 2022 13:38
@vietj
Copy link
Member

vietj commented Aug 1, 2022

can you add a test for this ?

@vietj vietj added this to the 4.3.3 milestone Aug 1, 2022
@JLLeitschuh
Copy link
Author

Unfortunately, no. I don't have time at present. But I do believe that it fixes a valid security vulnerability.

Please consider following the eclipse foundation vulnerability handling process: https://www.eclipse.org/security/policy.php

CC: @waynebeaton

@vietj
Copy link
Member

vietj commented Aug 1, 2022

I don't see the relationship between the EFDN process and the fact to not provide a test (unless you want to make an argument of authority). A change cannot be accepted without a test asserting the behavior.

@vietj vietj self-assigned this Aug 1, 2022
@JLLeitschuh
Copy link
Author

Hi @vietj,

This pull request was created at-scale with an automated refactoring tool called OpenRewrite. I used it to create this and 23 other pull requests to fix Partial Path Traversal. Additionally, I created 85 pull requests to fix a different vulnerability, Zip Slip, and 64 to fix Temporary Directory Hijacking. Unfortunately, fixing security vulnerabilities at this sort of scale means I don't have the time to additionally hand-craft unit tests for the 170+ pull requests I've generated to fix security vulnerabilities across OSS.

If your process and procedures require a test for this to be merged I leave you with two options: any maintainer should feel free to push additional commits to this branch adding a test; alternatively, feel free to close this pull request. I respect whatever decision you make.

However, I do believe that a vulnerability may be present here and a CVE will most likely need to be issued, regardless of the decision your team makes. 🙂

@vietj
Copy link
Member

vietj commented Aug 1, 2022

I'll have a look at the issue later this week

@vietj
Copy link
Member

vietj commented Aug 3, 2022

After analysis I concluded that there cannot be a security issue for several reasons:

1/ although the method is using File#getCanonicalPath() when the check is true, the selected code branch checks that the fileName is the same length than cachePath or that it contains a separator char (e.g /) at the cachePath.length() position
2/ fileNameCheck is private and used by FileResolverImpl (through cacheDir and cacheDir) when unpacking jar file resources (or similar) for performance reason and thus will not accept arbitrary file argument

However we consider the advice to use Path comparison as advocated in the PR as it seems to be a better way to achieve the same implementation.

@vietj vietj closed this Aug 3, 2022
@vietj vietj removed this from the 4.3.3 milestone Aug 3, 2022
@JLLeitschuh
Copy link
Author

Hi @vietj,

I think I agree with your assessment re-reviewing the code. Thank you for your flexibility on this, I really appreciate it. It was a pleasure working with you 🙂

@JLLeitschuh JLLeitschuh deleted the fix/JLL/partial-path-traversal-vulnerability branch August 3, 2022 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants