Skip to main content

Excessive privileges - Temporary Files

Description

The application creates temporary withouth properly restricting their privileges or access modes, allowing an attacker to craft new attack vectors

Impact

  • Get access to the created temporary files.
  • Tamper data contained in the created temporary files.

Recommendation

Create the temporary files in a different directory than the default provided by the Operative system and ensure it has 0600 permission mask.

Threat

Authenticated attacker with local access to the server.

Expected Remediation Time

⌚ 60 minutes.

Score

Default score using CVSS 3.1. It may change depending on the context of the vulnerability.

Base

  • Attack vector: L
  • Attack complexity: H
  • Privileges required: H
  • User interaction: R
  • Scope: U
  • Confidentiality: L
  • Integrity: L
  • Availability: N

Temporal

  • Exploit code madurity: P
  • Remediation level: U
  • Report confidence: C

Result

  • Vector string: CVSS:3.1/AV:L/AC:H/PR:H/UI:R/S:U/C:L/I:L/A:N/E:P/RL:U/RC:C
  • Score:
    • Base: 2.9
    • Temporal: 2.8
  • Severity:
    • Base: Low
    • Temporal: Low

Code Examples

Compliant code

When using temporary files, the application clearly defines a directory path and user permissions

public class Test {
public static void main(String[] args){
try {
// Provide attrs to define different directory path and correct permissions
System.out.println(Files.createTempFile("xxx", attrs=[xxx]));
} catch (Exception e) {}
}
}

Non compliant code

The application creates temporary files in a default path

import java.io.File;

public class Test {
public static void main(String[] args){
try {
System.out.println(File.createTempFile("xxx", null));
} catch (Exception e) {}
}
}

Create a temporary path without limiting values

using System;
using System.IO;

namespace StreamReadWrite{
class Program{
static void Main(string[] args){
var tempPath = Path.GetTempFileName();
var randomPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
using (var writer = new StreamWriter(tempPath)){
writer.WriteLine("content");
}
}
}
}

Details

This java method has the following signature:

public static File createTempFile(
// The prefix string defines the files name;
// must be at least three characters long
String prefix,
// The suffix string defines the file's extension;
// if null the suffix ".tmp" will be used
String suffix,
// The directory in which the file is to be created.
// For default temporary-file directory null is to passed.
File directory,
)

The first two arguments do not affect security of the created file.

In Linux, files and directories are different entities, they have their own permissions and are isolated from other files and directories.

Protections you apply to the directory do not affect the files inside of it. For example, if I protect the directory I can prevent an attacker from executing the ls command, but nothing impedes the attacker from executing cat directory/file.

To prevent access to the file we must protect the file, not the directory.

Additionally, we make sure that permissions are set atomically. If we do not, the following situation can happen:

  • At moment A we File.createTempFile() a file.
  • At moment B we add secure permissions to the file.

After moment B the file is secured. However, between moment A and moment B the file has insecure permissions and an attacker had enough opportunity to get control over it.

Vulnerable implementation

The java.io.File.createTempFile method creates files with write permissions in groups and other:


import java.io.File;

public class Test {
public static void main(String[] args){
try {
System.out.println(File.createTempFile("xxx", null));
}
catch (Exception e) {}
}
}

/*
* $ ls -al $(javac Test.java && java Test)
*
* -rw-r--r-- 1 fluid fluid 0 Aug 28 14:44 /tmp/xxx948760279845756007.tmp
*/

Secure implementation

  • Use java.nio.file.Files.createTempFile.
  • Use the attrs argument (an optional list of file attributes to set atomically when creating the file).

Requirements