Files on multiuser systems are generally owned by a particular user. The owner of the file can determine which other users on the system should be allowed to access the contents of these files.
These filesystems generally use a privileges and permissions model to protect file access. When a file is created, the access permissions of the file immediately dictate who may access or operate on the file. If a program creates a file with insufficiently restrictive access permissions, an attacker may read or modify the file before the program can modify the permissions. Consequently, files must be created with access permissions that prevent unauthorized file access.
Noncompliant Code Example
The constructors for FileOutputStream
and FileWriter
do not allow the programmer to explicitly specify file access permissions.
In this noncompliant code example, the access permissions of any file created isimplementation-defined and may fail to prevent unathorized access.
Writer out = new FileWriter("file");
Compliant Solution
Java 1.6 and earlier provide no mechanism for specifying default permissions upon file creation. Consequently, the problem must be avoided or solved using some mechanism external to Java, such as by using native code and JNI.
Compliant Solution (Java 1.7, POSIX)
The Java 1.7 new I/O facility (java.nio
) provides classes for managing file access permissions. Additionally, many of the methods and constructors that create files accept an argument allowing the program to specify the initial file permissions.
The Files.newByteChannel()
method allows a file to be created with specific permissions. This method is platform-independent, but the actual permissions are platform-specific. The following compliant solution illustrates sufficiently restrictive permissions for POSIX platforms.
Path file = new File("file").toPath(); // Throw exception rather than overwrite existing file Set<OpenOption> options = new HashSet<OpenOption>(); options.add(StandardOpenOption.CREATE_NEW); options.add(StandardOpenOption.APPEND); // File permissions should be that only user may read/write file Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------"); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); try (SeekableByteChannel sbc = Files.newByteChannel(file, options, attr)) { // write data };
Exceptions
FIO03-EX0: If a file is created inside a directory that is both secure and unreadable by untrusted users, then that file may be created with the default access permissions. See FIO07-J. Remove temporary files before termination for the definition of a secure directory. This could be the case if, for example, the entire filesystem is trusted, or is accessible only to trusted users.
FIO03-EX1: Files that do not contain sensitive information need not be created with specific access permissions.
Risk Assessment
The ability to determine if an existing file has been opened or a new file has been created provides greater assurance that a file other than the intended file is not acted upon.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
FIO03-J |
medium |
probable |
high |
P4 |
L3 |
Automated Detection
TODO
Related Guidelines
CWE-279, "Incorrect Execution-Assigned Permissions" |
|
|
CWE-276, "Incorrect Default Permissions" |
|
CWE-732, "Incorrect Permission Assignment for Critical Resource" |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="af40f391-0ed9-439e-aa1c-e1867a0a8a66"><ac:plain-text-body><![CDATA[ |
[[API 2006 |
AA. Bibliography#API 06]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ddab45e3-ca2e-402a-ac18-101b501b0e2b"><ac:plain-text-body><![CDATA[ |
[[CVE |
AA. Bibliography#CVE]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7cf8b283-fa1b-4908-be0d-181dfbb47caf"><ac:plain-text-body><![CDATA[ |
[[Dowd 2006 |
AA. Bibliography#Dowd 06]] |
Chapter 9, "UNIX 1: Privileges and Files" |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="32ced9da-05e1-4eb0-b182-738d5b923118"><ac:plain-text-body><![CDATA[ |
[[J2SE 2011 |
AA. Bibliography#J2SE 11]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cb48da02-286a-4c25-bd10-76a7a8db442d"><ac:plain-text-body><![CDATA[ |
[[OpenBSD |
AA. Bibliography#OpenBSD]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="96b57778-7ad3-4dcd-801d-a0f8a44f9120"><ac:plain-text-body><![CDATA[ |
[[Open Group 2004 |
AA. Bibliography#Open Group 04]] |
"The |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2b57c83e-826b-4ab7-bd5d-24bf887873f9"><ac:plain-text-body><![CDATA[ |
[[Viega 2003 |
AA. Bibliography#Viega 03]] |
Section 2.7, "Restricting Access Permissions for New Files on UNIX" |
]]></ac:plain-text-body></ac:structured-macro> |
FIO02-J. Ensure the array is filled when using read() to fill an array 12. Input Output (FIO) FIO04-J. Do not operate on files in shared directories