GHSA-g3wg-6mcf-8jj6 Local Temp Directory Hijacking Vulnerability
ImpactOn Unix like systems, the system's temporary directory is shared between all users on that system.  A collocated user can observe the process of creating a temporary sub directory in the shared temporary directory and race to complete the creation of the temporary subdirectory.  If the attacker wins the race then they will have read and write permission to the subdirectory used to unpack web applications, including their WEB-INF/lib jar files and JSP files.  If any code is ever executed out of this temporary directory, this can lead to a local privilege escalation vulnerability.Additionally, any user code uses of [WebAppContext::getTempDirectory](https://www.eclipse.org/jetty/javadoc/9.4.31.v20200723/org/eclipse/jetty/webapp/WebAppContext.html#getTempDirectory()) would similarly be vulnerable.Additionally, any user application code using the `ServletContext` attribute for the tempdir will also be impacted.
See: https://javaee.github.io/javaee-spec/javadocs/javax/servlet/ServletContext.html#TEMPDIRFor example:Example: The JSP library itself will use the container temp directory for compiling the JSP source into Java classes before executing them.CVSSv3.1 EvaluationThis vulnerability has been calculated to have a [CVSSv3.1 score of 7.8/10 (AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H)](https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator?vector=AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H&version=3.1)PatchesFixes were applied to the 9.4.x branch with:https://github.com/eclipse/jetty.project/commit/53e0e0e9b25a6309bf24ee3b10984f4145701edbhttps://github.com/eclipse/jetty.project/commit/9ad6beb80543b392c91653f6bfce233fc75b9d5fThese will be included in releases: 9.4.33, 10.0.0.beta3, 11.0.0.beta3WorkaroundsA work around is to set a temporary directory, either for the server or the context, to a directory outside of the shared temporary file system.
For recent releases, a temporary directory can be created simple by creating a directory called `work` in the ${jetty.base} directory (the parent directory of the `webapps` directory).
Alternately the java temporary directory can be set with the System Property `java.io.tmpdir`.    A more detailed description of how jetty selects a temporary directory is below.The Jetty search order for finding a temporary directory is as follows:If the [`WebAppContext` has a temp directory specified](https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/webapp/WebAppContext.html#setTempDirectory(java.io.File)), use it.If the `ServletContext` has the `javax.servlet.context.tempdir` attribute set, and if directory exists, use it.If a `${jetty.base}/work` directory exists, use it (since Jetty 9.1)If a `ServletContext` has the `org.eclipse.jetty.webapp.basetempdir` attribute set, and if the directory exists, use it.Use `System.getProperty("java.io.tmpdir")` and use it.Jetty will end traversal at the first successful step.
To mitigate this vulnerability the directory must be set to one that is not writable by an attacker.  To avoid information leakage, the directory should also not be readable by an attacker.Setting a Jetty server temporary directory.Choices 3 and 5 apply to the server level, and will impact all deployed webapps on the server.For choice 3  just create that work directory underneath your `${jetty.base}` and restart Jetty.For choice 5, just specify your own `java.io.tmpdir` when you start the JVM for Jetty.Setting a Context specific temporary directory.The rest of the choices require you to configure the context for that deployed webapp (seen as `${jetty.base}/webapps/<context>.xml`)Example (excluding the DTD which is version specific):Referenceshttps://github.com/eclipse/jetty.project/issues/5451[CWE-378: Creation of Temporary File With Insecure Permissions](https://cwe.mitre.org/data/definitions/378.html)[CWE-379: Creation of Temporary File in Directory with Insecure Permissions](https://cwe.mitre.org/data/definitions/379.html)[CodeQL Query PR To Detect Similar Vulnerabilities](https://github.com/github/codeql/pull/4473)Similar VulnerabilitiesSimilar, but not the same.JUnit 4 - https://github.com/junit-team/junit4/security/advisories/GHSA-269g-pwp5-87ppGoogle Guava - https://github.com/google/guava/issues/4011Apache Ant - https://nvd.nist.gov/vuln/detail/CVE-2020-1945JetBrains Kotlin Compiler - https://nvd.nist.gov/vuln/detail/CVE-2020-15824For more informationThe original report of this vulnerability is below:On Thu, 15 Oct 2020 at 21:14, Jonathan Leitschuh <jonathan.leitschuh@gmail.com> wrote:
Hi WebTide Security Team,I'm a security researcher writing some custom CodeQL queries to find Local Temporary Directory Hijacking Vulnerabilities. One of my queries flagged an issue in Jetty.https://lgtm.com/query/5615014766184643449/I've recently been looking into security vulnerabilities involving the temporary directory because on unix-like systems, the system temporary directory is shared between all users.
There exists a race condition between the deletion of the temporary file and the creation of the directory.https://github.com/eclipse/jetty.project/blob/1b59672b7f668b8a421690154b98b4b2b03f254b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebInfConfiguration.java#L511-L518In several cases the `parent` parameter will not be the system temporary directory. However, there is one case where it will be, as the last fallback.https://github.com/eclipse/jetty.project/blob/1b59672b7f668b8a421690154b98b4b2b03f254b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebInfConfiguration.java#L467-L468If any code is ever executed out of this temporary directory, this can lead to a local privilege escalation vulnerability.Would your team be willing to open a GitHub security advisory to continue the discussion and disclosure there? https://github.com/eclipse/jetty.project/security/advisories**This vulnerability disclosure follows Google's [90-day vulnerability disclosure policy](https://www.google.com/about/appsecurity/) (I'm not an employee of Google, I just like their policy). Full disclosure will occur either at the end of the 90-day deadline or whenever a patch is made widely available, whichever occurs first.**Cheers,
Jonathan Leitschuh
CVSS v3.1 score: 7