Search code examples
androidfileandroid-source

How can I write/read app-specific files in Android frameworks? [open failed: EACCES (Permission denied) while modifying AOSP]


I am trying to modify one of the Android framework codes for making it read/write the app-specific files of user apps.

And when I check logcat, it shows the below error:

java.io.FileNotFoundException: /data/user/0/com.example.APPS/files/FILES: open failed: EACCES (Permission denied)

Based on my search, I found that this issue can be resolved by using some adb commands which change access mode and disable SELinux:

$ adb root
$ adb shell chmod 777 /data/user/0/com.example.APPS/files
$ adb shell setenforce 0    

However, I think it is just a temporal solution because it doesn't work if the device is once turned-off.

So, my question is, are there any nontemporal methods for the Android frameworks to handle the app-specific files?

In detail, I modified 'frameworks/base/core/java/com/android/server/am/BatteryStatsService.java' by adding:

FileWriter writer = null;
try {    
    JSONObject oneHist = new JSONObject();
    oneHist.put("a", "b");
    File file = new File(path);
    writer = new FileWriter(file, true);
    writer.write(oneHist.toString() + "\n");
    writer.flush();                
} catch (JSONException | IOException e){
    e.printStackTrace();
} finally {
    try { writer.close(); } catch (Exception e) { }
}

Solution

  • are there any nontemporal methods for the Android frameworks to handle the app-specific files?

    Basically the answer is no. The entire security design of Android is to keep processes isolated except for well defined interfaces. Letting system server have blanket access to files of another app would violate that principle since read/write on arbitrary file bytes is not a well defined API.

    Please explain what your actual goal is, file access is just a means to some end. There should be some alternative to reach your end goal. Most likely a more acceptable alternative might involve adding new methods to a service manager interface but without knowing the end goal it's not possible to say what the best approach is.