Overly permissive file permissions

Overly permissive file permissions

Description

Overly broad file or directory permissions can let other users read or modify important files. In particular, an unsuitable permission set passed to java.nio.file.Files.setPosixFilePermissions() may expose sensitive data.

Potential impact

  • Data exposure: Read access may reveal passwords, API keys or user information.
  • File modification: Broad write permissions such as rw-rw-rw- permit content changes. Deletion also depends on parent-directory permissions and other controls.
  • Privilege escalation: Modifying important executable files may allow malicious code to run with another identity’s permissions.

Remediation

Identify which accounts need to read or write configuration, log, and temporary files. For a POSIX regular file used only by its owner, specify java.nio.file.attribute.PosixFilePermissions.fromString("rw-------"). Choose directory permissions separately because traversing a directory requires execute permission.

Examples

These examples change permissions on an existing regular file in a filesystem that supports POSIX permissions. Use an application-controlled path and prevent others from replacing the file or parent directory, including through symbolic links.

Before

java
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException;
import java.util.Set;

public class InsecureFilePermission {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/tmp/sensitive-data.txt");

        // Allow every user to read and write.
        Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-rw-rw-");
        Files.setPosixFilePermissions(path, permissions);
    }
}

After

java
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException;
import java.util.Set;

public class SecureFilePermission {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/tmp/sensitive-data.txt");

        // Allow only the owner to read and write (rw-------).
        Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-------");
        Files.setPosixFilePermissions(path, permissions);
    }
}

rw-rw-rw- grants every user read and write access to the file. rw------- grants those permissions only to the owner. The ability to delete the file also depends on the parent directory’s permissions.

References