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

create temporary file in directory with secure permissions #11130

Open
isandeep41 opened this issue Apr 19, 2021 · 0 comments
Open

create temporary file in directory with secure permissions #11130

isandeep41 opened this issue Apr 19, 2021 · 0 comments
Labels

Comments

@isandeep41
Copy link
Contributor

Motivation

File.createTempFile(String, String) will create a temporary file in the system temporary directory if the 'java.io.tmpdir'. The permissions on that file utilize the umask. In a majority of cases, this means that the file that java creates has the permissions: -rw-r--r--, thus, any other local user on that system can read the contents of that file.
This can be a security concern if any sensitive data is stored in this file.

Proposed changes

Create a secure temp file using PlatformDependent.createTempFile instead of File.createTempFile. The change is released in netty-4.1.63.Final and Druid is upgraded to netty-4.1.63.Final in this commit.

Rationale

Create temporary files with sane permissions by default.

nahsra referenced this issue in pixee/codemodder-java Oct 3, 2023
This change replaces the usage of
[`java.io.File#createTempFile`](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/io/File.html#createTempFile(java.lang.String,java.lang.String))
with
[`java.nio.file.Files#createTempFile`](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/nio/file/Files.html#createTempFile(java.lang.String,java.lang.String,java.nio.file.attribute.FileAttribute...))
which has more secure attributes.

The `java.io.File#createTempFile()` method creates a file that is
world-readable and world-writeable, which is almost never necessary.
Also, the file created is placed in a predictable directory (e.g.,
`/tmp`). Having predictable file names, locations, and will lead to many
types of vulnerabilities. History has shown that this insecure pattern
can lead to [information
leakage](https://www.cvedetails.com/cve/CVE-2021-28168/), [privilege
escalation](https://www.cvedetails.com/cve/CVE-2021-29428/) and even
[code
execution](https://www.openwall.com/lists/oss-security/2022/02/25/3).

Our changes look something like this:

```diff
+  import java.nio.file.Files;
   ...
-  File txtFile = File.createTempFile("acme", ".txt");
+  File txtFile = Files.createTempFile("acme", ".txt").toFile();
```

<details>
  <summary>More reading</summary>

*
[https://cwe.mitre.org/data/definitions/378.html](https://cwe.mitre.org/data/definitions/378.html)
*
[https://docs.fluidattacks.com/criteria/vulnerabilities/160/](https://docs.fluidattacks.com/criteria/vulnerabilities/160/)
*
[https://github.com/apache/druid/issues/11130](https://github.com/apache/druid/issues/11130)
*
[https://owasp.org/www-community/vulnerabilities/Insecure_Temporary_File](https://owasp.org/www-community/vulnerabilities/Insecure_Temporary_File)
*
[https://nvd.nist.gov/vuln/detail/CVE-2022-41954](https://nvd.nist.gov/vuln/detail/CVE-2022-41954)
*
[https://www.cvedetails.com/vulnerability-list/cwe-378/vulnerabilities.html](https://www.cvedetails.com/vulnerability-list/cwe-378/vulnerabilities.html)
</details>

Powered by: [pixeebot](https://docs.pixee.ai/installing/) (codemod ID:
[pixee:java/upgrade-tempfile-to-nio](https://docs.pixee.ai/codemods/java/pixee_java_upgrade-tempfile-to-nio))
![](https://d1zaessa2hpsmj.cloudfront.net/pixel/v1/track?writeKey=2PI43jNm7atYvAuK7rJUz3Kcd6A&event=DRIP_PR%7Cpixee%2Fcodemodder-java%7C8c77e3cd7de8249219e2ca060a0681c0da309180)

<!--{"type":"DRIP","codemod":"pixee:java/upgrade-tempfile-to-nio"}-->

---------

Co-authored-by: pixeebot[bot] <23113631+pixeebot@users.noreply.github.com>
Co-authored-by: Arshan Dabirsiaghi <arshan.dabirsiaghi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants