Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: REM cost reform

Files on multiuser systems are generally owned by a particular user. The owner of the file can determine specify which other users on the system should be allowed to access the contents of these files.

These file systems generally use a privileges and permissions model to protect file access. When a file is created, the file access permissions of the file immediately dictate who may access or operate on the file. If When 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 is are implementation-defined and may fail to not prevent unauthorized access.:

Code Block
bgColor#FFCCCC

Writer out = new FileWriter("file");

Compliant Solution (Java 1.6 and Earlier)

Java 1.6 and earlier provide no lack a 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 the Java Native Interface (JNI).

Compliant Solution (

...

POSIX)

The Java 1.7 new The  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 This compliant solution shows defines sufficiently restrictive permissions for POSIX platforms.:

Code Block
bgColor#ccccff

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 such 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)) {
  // writeWrite data
};

Exceptions

FIO03FIO01-J-EX0: If When 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 file system is trusted , or is accessible only to trusted users .(see FIO00-J. Do not operate on files in shared directories for the definition of a secure directory).

FIO01-JFIO03-EX1: Files that do not contain sensitive privileged 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 uponIf files are created without appropriate permissions, an attacker may read or write to the files, possibly resulting in compromised system integrity and information disclosure.

Rule

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

FIO03

FIO01-J

Medium

medium

Probable

probable

No

high

No

P4

L3

Automated Detection

...

ToolVersionChecker

Description

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

JAVA.IO.PERM.ACCESS
JAVA.IO.PERM

Accessing file in permissive mode
Permissive file mode

Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V

CERT.FIO01.ASNF
CERT.FIO01.CFAP

Avoid implicit file creation when a String is passed as an argument
Create files with appropriate access permissions

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V5318

Related Guidelines

SEI CERT C++

Secure

Coding Standard

VOID FIO06-CPP. Create files with appropriate access permissions

SEI CERT C

Secure

Coding Standard

FIO06-C. Create files with appropriate access permissions

ISO/IEC TR 24772:2010

Missing or Inconsistent Access Control [XZN]

MITRE CWE

CWE-279,

"

Incorrect Execution-Assigned Permissions

"


 

CWE-276,

Incorrect Default Permissions

"


CWE-732,

"

Incorrect Permission Assignment for Critical Resource

"

Bibliography

Android Implementation Details

Creating files with weak permissions may allow malicious applications to access the files.

Bibliography

[API 2014]


[CVE]


[Dowd 2006

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3facb27a-d545-4e7e-bdaf-5a23ac8838f9"><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="cd20d82c-3b37-4b22-934c-58a87941920f"><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="7a821eb6-eafb-4210-8416-443ebce3c0b3"><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="fff2fe9e-1d8c-443d-9129-f59ad9cea381"><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="e6c87e36-635c-453e-bb8f-512c3ff168b5"><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="bc2ded2a-b172-4b98-adf2-bd0274a6b73a"><ac:plain-text-body><![CDATA[


[

[

Open Group 2004

AA. Bibliography#Open Group 04]

]

"The open

function,

Function"

and


"The umask

function"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="06f4e4ed-d53c-46b3-a9e8-005ee4d16e31"><ac:plain-text-body><![CDATA[

[[Viega 2003

AA. Bibliography#Viega 03]]

Function"

[Viega 2003]

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)      void FIO04-J. Do not operate on files in shared directoriesImage Added Image Added Image Added