Alessandro Roaro
Alessandro Roaro

Reputation: 4733

Read private file (/data/data folder) in rooted device

I need to access a file contained in the private folder of another app. I have granted my app the root privilege and changed the permissions - although I think it's not necessary - but when I try to read from the file, I get "permission denied".

This is the code:

File file = new File("/data/data/other.app/shared_prefs/file.xml");
if(file.exists()) {

  try {
      Runtime.getRuntime().exec("su");
      Runtime.getRuntime().exec("chmod 777 " + file.getAbsolutePath());
      InputStream in = new FileInputStream(file);

      ....
  } catch (IOException e) {
      e.printStackTrace();
  }
}

Upvotes: 0

Views: 2135

Answers (3)

Alessandro Roaro
Alessandro Roaro

Reputation: 4733

SOLVED


Solved using this code to run the commands:

public static void runAsRoot(String[] cmds){
   try {
       Process p = Runtime.getRuntime().exec("su");
       DataOutputStream os = new DataOutputStream(p.getOutputStream());
       for (String tmpCmd : cmds) {
           os.writeBytes(tmpCmd+"\n");
       }
       os.writeBytes("exit\nexit\n");
       os.flush();
       p.waitFor();
   } catch(IOException e) {
       e.printStackTrace();
   } catch(InterruptedException e) {
       e.printStackTrace();
   }
}

Upvotes: 0

Nick Palmer
Nick Palmer

Reputation: 2753

You cannot break up the su and the chmod operations like that.

This code: Runtime.getRuntime().exec("su"); Runtime.getRuntime().exec("chmod 777 " + file.getAbsolutePath());

Does not result in the chmod being executed in a root shell. Each call to exec kicks off a NEW process.

You need to run the commands you need all within a single process. The easiest way to do that is to write a shell script to your /data/data directory that does these operations for you and then run that through the sh shell processor.

Please note that good security practice would be to chmod the file back to not world readable after you are done with it in your app so that you are not leaving the other app exposed forever.

This answer looks to have what you need: Run binary from with root Android Application

Upvotes: 1

Reiksiel
Reiksiel

Reputation: 146

Probably you wont be able to do this, because each app on android has a user with unique permissions.

See this: http://developer.android.com/guide/topics/security/permissions.html#userid

Any data stored by an application will be assigned that application's user ID, and not normally accessible to other packages. When creating a new file with getSharedPreferences(String, int), openFileOutput(String, int), or openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory), you can use the MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE flags to allow any other package to read/write the file. When setting these flags, the file is still owned by your application, but its global read and/or write permissions have been set appropriately so any other application can see it.

Upvotes: 1

Related Questions