Java


JAVA.IO.PERM.ACCESS : Accessing File in Permissive Mode (Java)

Summary

A file is accessed in Context.MODE_WORLD_WRITABLE or Context.MODE_WORLD_READABLE mode.

Constants android.content.Context.MODE_WORLD_READABLE and android.content.Context.MODE_WORLD_WRITABLE can potentially allow access by untrusted applications, and have been deprecated since Android 4.2 (android API 17). From Android 7.0 (android API 24), their use results in a java.lang.SecurityException.

In many cases, android.content.Context.MODE_PRIVATE is more suitable.

Properties

Class Name Accessing File in Permissive Mode (Java)
Significance security
Mnemonic JAVA.IO.PERM.ACCESS
Categories
CWE CWE:732 Incorrect Permission Assignment for Critical Resource
CERT-Java CERT-Java:ENV03-J Do not grant dangerous combinations of permissions
  CERT-Java:FIO01-J Create files with appropriate access permissions
  CERT-Java:SEC01-J Do not allow tainted variables in privileged blocks
Availability Available for Java only.

Android Only. Warnings of this class will only be reported in Android code: that is, code that uses the Android API.

Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Accessing File in Permissive Mode (Java)"

Example

// MainActivity.java
package example.fileaccessexample;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      try {

            FileOutputStream fileOutputStream1 = openFileOutput("foo.txt", Context.MODE_WORLD_READABLE);  // Accessing File in Permissive Mode (Java) warning issued here 
            FileOutputStream fileOutputStream2 = openFileOutput("foo.txt", Context.MODE_WORLD_WRITEABLE); // Accessing File in Permissive Mode (Java) warning issued here 

        } catch (FileNotFoundException | SecurityException e) {
            e.printStackTrace();
        }
      extendAccessPermission(new File("myfile.txt"));
  }

  public  void extendAccessPermission(File f) {
      if ( !f.setReadable( true , false )) {           // Permissive File Mode (Java) warning issued here 
          Log.e(TAG,"Unable to set readable permission");
      }

      if ( !f.setWritable( true , false )) {           // Permissive File Mode (Java) warning issued here 
          Log.e(TAG,"Unable to set writable permission");
      }
  }
}

Resolution

Android's security design philosophy recommends the use of files only for the purpose of perpetuating or temporarily storing information. In principle the file access permissions should be private, in order to limit the risk of information leakage.

The Android API documentation includes guidelines for securely sharing files.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.