|
| 1 | +<p>Temporary files are considered insecurely created when the file existence check is performed separately from the actual file creation. Such a |
| 2 | +situation can occur when creating temporary files using normal file handling functions or when using dedicated temporary file handling functions that |
| 3 | +are not atomic.</p> |
1 | 4 | <h2>Why is this an issue?</h2>
|
2 |
| -<p>Creating temporary files using insecure methods exposes the application to race conditions on filenames: a malicious user can try to create a file |
3 |
| -with a predictable name before the application does. A successful attack can result in other files being accessed, modified, corrupted or deleted. |
4 |
| -This risk is even higher if the application run with elevated permissions.</p> |
5 |
| -<p>In the past, it has led to the following vulnerabilities:</p> |
6 |
| -<ul> |
7 |
| - <li> <a href="https://nvd.nist.gov/vuln/detail/CVE-2014-1858">CVE-2014-1858</a> </li> |
8 |
| - <li> <a href="https://nvd.nist.gov/vuln/detail/CVE-2014-1932">CVE-2014-1932</a> </li> |
9 |
| -</ul> |
10 |
| -<h3>Noncompliant code example</h3> |
11 |
| -<pre> |
| 5 | +<p>Creating temporary files in a non-atomic way introduces race condition issues in the application’s behavior. Indeed, a third party can create a |
| 6 | +given file between when the application chooses its name and when it creates it.</p> |
| 7 | +<p>In such a situation, the application might use a temporary file that it does not entirely control. In particular, this file’s permissions might be |
| 8 | +different than expected. This can lead to trust boundary issues.</p> |
| 9 | +<h3>What is the potential impact?</h3> |
| 10 | +<p>Attackers with control over a temporary file used by a vulnerable application will be able to modify it in a way that will affect the application’s |
| 11 | +logic. By changing this file’s Access Control List or other operating system-level properties, they could prevent the file from being deleted or |
| 12 | +emptied. They may also alter the file’s content before or while the application uses it.</p> |
| 13 | +<p>Depending on why and how the affected temporary files are used, the exploitation of a race condition in an application can have various |
| 14 | +consequences. They can range from sensitive information disclosure to more serious application or hosting infrastructure compromise.</p> |
| 15 | +<h4>Information disclosure</h4> |
| 16 | +<p>Because attackers can control the permissions set on temporary files and prevent their removal, they can read what the application stores in them. |
| 17 | +This might be especially critical if this information is sensitive.</p> |
| 18 | +<p>For example, an application might use temporary files to store users' session-related information. In such a case, attackers controlling those |
| 19 | +files can access session-stored information. This might allow them to take over authenticated users' identities and entitlements.</p> |
| 20 | +<h4>Attack surface extension</h4> |
| 21 | +<p>An application might use temporary files to store technical data for further reuse or as a communication channel between multiple components. In |
| 22 | +that case, it might consider those files part of the trust boundaries and use their content without additional security validation or sanitation. In |
| 23 | +such a case, an attacker controlling the file content might use it as an attack vector for further compromise.</p> |
| 24 | +<p>For example, an application might store serialized data in temporary files for later use. In such a case, attackers controlling those files' |
| 25 | +content can change it in a way that will lead to an insecure deserialization exploitation. It might allow them to execute arbitrary code on the |
| 26 | +application hosting server and take it over.</p> |
| 27 | +<h2>How to fix it</h2> |
| 28 | +<h3>Code examples</h3> |
| 29 | +<p>The following code example is vulnerable to a race condition attack because it creates a temporary file using an unsafe API function.</p> |
| 30 | +<h4>Noncompliant code example</h4> |
| 31 | +<pre data-diff-id="1" data-diff-type="noncompliant"> |
12 | 32 | import tempfile
|
13 | 33 |
|
14 | 34 | filename = tempfile.mktemp() # Noncompliant
|
15 | 35 | tmp_file = open(filename, "w+")
|
16 | 36 | </pre>
|
17 |
| -<h3>Compliant solution</h3> |
18 |
| -<pre> |
| 37 | +<h4>Compliant solution</h4> |
| 38 | +<pre data-diff-id="1" data-diff-type="compliant"> |
19 | 39 | import tempfile
|
20 | 40 |
|
21 |
| -tmp_file1 = tempfile.NamedTemporaryFile(delete=False) # Compliant; Easy replacement to tempfile.mktemp() |
22 |
| -tmp_file2 = tempfile.NamedTemporaryFile() # Compliant; Created file will be automatically deleted |
| 41 | +tmp_file1 = tempfile.NamedTemporaryFile(delete=False) |
| 42 | +tmp_file2 = tempfile.NamedTemporaryFile() |
23 | 43 | </pre>
|
| 44 | +<h3>How does this work?</h3> |
| 45 | +<p>Applications should create temporary files so that no third party can read or modify their content. It requires that the files' name, location, and |
| 46 | +permissions are carefully chosen and set. This can be achieved in multiple ways depending on the applications' technology stacks.</p> |
| 47 | +<h4>Use a secure API function</h4> |
| 48 | +<p>Temporary files handling APIs generally provide secure functions to create temporary files. In most cases, they operate in an atomical way, |
| 49 | +creating and opening a file with a unique and unpredictable name in a single call. Those functions can often be used to replace less secure |
| 50 | +alternatives without requiring important development efforts.</p> |
| 51 | +<p>Here, the example compliant code uses the more secure <code>tempfile.NamedTemporaryFile</code> function to handle the temporary file creation.</p> |
| 52 | +<h4>Strong security controls</h4> |
| 53 | +<p>Temporary files can be created using unsafe functions and API as long as strong security controls are applied. Non-temporary file-handling |
| 54 | +functions and APIs can also be used for that purpose.</p> |
| 55 | +<p>In general, applications should ensure that attackers can not create a file before them. This turns into the following requirements when creating |
| 56 | +the files:</p> |
| 57 | +<ul> |
| 58 | + <li> Files should be created in a non-public directory. </li> |
| 59 | + <li> File names should be unique. </li> |
| 60 | + <li> File names should be unpredictable. They should be generated using a cryptographically secure random generator. </li> |
| 61 | + <li> File creation should fail if a target file already exists. </li> |
| 62 | +</ul> |
| 63 | +<p>Moreover, when possible, it is recommended that applications destroy temporary files after they have finished using them.</p> |
24 | 64 | <h2>Resources</h2>
|
| 65 | +<h3>Documentation</h3> |
| 66 | +<ul> |
| 67 | + <li> <a href="https://owasp.org/www-community/vulnerabilities/Insecure_Temporary_File">OWASP</a> - Insecure Temporary File </li> |
| 68 | + <li> <a href="https://docs.python.org/3/library/tempfile.html#deprecated-functions-and-variables">Python documentation</a> - tempfile </li> |
| 69 | +</ul> |
| 70 | +<h3>Standards</h3> |
25 | 71 | <ul>
|
26 |
| - <li> <a href="https://owasp.org/Top10/A01_2021-Broken_Access_Control/">OWASP Top 10 2021 Category A1</a> - Broken Access Control </li> |
27 |
| - <li> <a href="https://owasp.org/www-project-top-ten/2017/A9_2017-Using_Components_with_Known_Vulnerabilities">OWASP Top 10 2017 Category A9</a> - |
| 72 | + <li> <a href="https://owasp.org/Top10/A01_2021-Broken_Access_Control/">OWASP</a> - Top 10 2021 - A01:2021 - Broken Access Control </li> |
| 73 | + <li> <a href="https://owasp.org/www-project-top-ten/2017/A9_2017-Using_Components_with_Known_Vulnerabilities">OWASP</a> - Top 10 2017 - A9:2017 - |
28 | 74 | Using Components with Known Vulnerabilities </li>
|
29 |
| - <li> <a href="https://cwe.mitre.org/data/definitions/377">MITRE, CWE-377</a> - Insecure Temporary File </li> |
30 |
| - <li> <a href="https://cwe.mitre.org/data/definitions/379">MITRE, CWE-379</a> - Creation of Temporary File in Directory with Incorrect Permissions |
| 75 | + <li> <a href="https://cwe.mitre.org/data/definitions/377">MITRE</a> - CWE-377: Insecure Temporary File </li> |
| 76 | + <li> <a href="https://cwe.mitre.org/data/definitions/379">MITRE</a> - CWE-379: Creation of Temporary File in Directory with Incorrect Permissions |
31 | 77 | </li>
|
32 |
| - <li> <a href="https://owasp.org/www-community/vulnerabilities/Insecure_Temporary_File">OWASP, Insecure Temporary File</a> </li> |
33 |
| - <li> <a href="https://docs.python.org/3/library/tempfile.html#deprecated-functions-and-variables">Python tempfile module</a> </li> |
34 |
| - <li> <a href="https://docs.python.org/2.7/library/os.html">Python 2.7 os module</a> </li> |
35 | 78 | </ul>
|
36 | 79 |
|
0 commit comments