File.setReadable() used to make file world-readable

File.setReadable() used to make file world-readable

This is a warning.

Id

SetWorldReadable

Summary

File.setReadable() used to make file world-readable

Severity

Warning

Category

Security

Platform

Android

Vendor

Android Open Source Project

Feedback

https://issuetracker.google.com/issues/new?component=192708

Since

1.5.0 (November 2015)

Affects

Kotlin and Java files

Editing

This check runs on the fly in the IDE editor

See

https://goo.gle/SetWorldReadable

Implementation

Source Code

Tests

Source Code

Setting files world-readable is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanisms for interactions such as ContentProvider, BroadcastReceiver, and Service.

Example

Here is an example of lint warnings produced by this check:

src/test/pkg/WorldWriteableFile.java:41:Warning: Setting file
permissions to world-readable can be risky, review carefully
[SetWorldReadable]
    mFile.setReadable(true, false);
    ------------------------------

Here is the source file referenced above:

src/test/pkg/WorldWriteableFile.java:

package test.pkg; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.InputStream; import java.io.FileNotFoundException; import android.content.Context; import android.content.SharedPreferences; import android.app.Activity; import android.os.Bundle; public class WorldWriteableFile extends Activity { File mFile; Context mContext; public void foo() { OutputStream out = null; SharedPreferences prefs = null; File dir = null; boolean success = false; try { //out = openFileOutput(mFile.getName()); // ok out = openFileOutput(mFile.getName(), MODE_PRIVATE); // ok out = openFileOutput(mFile.getName(), MODE_WORLD_WRITEABLE); out = openFileOutput(mFile.getName(), MODE_WORLD_READABLE); prefs = getSharedPreferences(mFile.getName(), 0); // ok prefs = getSharedPreferences(mFile.getName(), MODE_PRIVATE); // ok prefs = getSharedPreferences(mFile.getName(), MODE_WORLD_WRITEABLE); prefs = getSharedPreferences(mFile.getName(), MODE_WORLD_READABLE); dir = getDir(mFile.getName(), MODE_PRIVATE); // ok dir = getDir(mFile.getName(), MODE_WORLD_WRITEABLE); dir = getDir(mFile.getName(), MODE_WORLD_READABLE); mFile.setReadable(true, true); // ok mFile.setReadable(false, true); // ok mFile.setReadable(false, false); // ok mFile.setReadable(true, false); mFile.setReadable(true); // ok mFile.setReadable(false); // ok mFile.setWritable(true, true); // ok mFile.setWritable(false, true); // ok mFile.setWritable(false, false); // ok mFile.setWritable(true, false); mFile.setWritable(true); // ok mFile.setWritable(false); // ok // Flickr.get().downloadPhoto(params[0], Flickr.PhotoSize.LARGE, // out); success = true; } catch (FileNotFoundException e) { } } }

You can also visit the source code for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test found for this lint check, SecurityDetector.testWorldWriteable. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708.

Suppressing

You can suppress false positives using one of the following mechanisms:

formatted by Markdeep 1.18