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
- Apply least privilege. A sensitive regular file may need owner-only read/write access (
rw-------); directories need execute permission for traversal, so do not apply the same string indiscriminately. - Use
java.nio.file.attribute.PosixFilePermissions.fromString("rw-------")where owner-only file access is appropriate. - Protect configuration, log and temporary files from unauthorized access.
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);
}
}
Explanation:
- Before: Sets
rw-rw-rw-, allowing all users to read and write the file. - After: Sets
rw-------, limiting ordinary read/write permissions to the owner.