Running adb comands inside application gives permission denied

I am creating an application that tries to execute some commands, such as "top" and others. I am working on android 7.0. I try to use this code in older versions of Android, but when I try to do it in nougat, I get the following Permission denied error:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.matant.cmdlineapp, PID: 7662 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:325) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.lang.RuntimeException: java.io.IOException: Cannot run program "adb shell top": error=13, Permission denied at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:88) at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:36) at android.os.AsyncTask$2.call(AsyncTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.io.IOException: Cannot run program "adb shell top": error=13, Permission denied at java.lang.ProcessBuilder.start(ProcessBuilder.java:983) at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:62) at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:36) at android.os.AsyncTask$2.call(AsyncTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.io.IOException: error=13, Permission denied at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:133) at java.lang.ProcessImpl.start(ProcessImpl.java:128) at java.lang.ProcessBuilder.start(ProcessBuilder.java:964) at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:62) at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:36) at android.os.AsyncTask$2.call(AsyncTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) 

code executed by me:

 protected Void doInBackground(String... params) { //Execution en BackGround Log.d("BACKGROUND", "START"); Log.d("BACK PARAM 0", params[0]); //Log.d("BACK PARAM 1", params[1]); try { // Executes the command. //Process process = Runtime.getRuntime().exec("adb version"); ProcessBuilder processBuilder = new ProcessBuilder("adb shell top"); Process process = processBuilder.start(); // Reads stdout. // NOTE: You can write to stdin of the command using // process.getOutputStream(). BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; char[] buffer = new char[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { output.append(buffer, 0, read); Log.d("reader val: ",output.toString()); publishProgress(output.toString()); } reader.close(); return null; } catch (IOException e) { throw new RuntimeException(e); } } 
+1
source share
1 answer

From this

/ sys and / proc are known to leak side channel information about processes, information that can be used for processes. For example, it has been documented for many years that / proc access can be used to monitor the launch of applications, which allows phishing attacks.

Thus, with the release of N, the development team intentionally restricted access to /sys and /proc . If I'm not mistaken, the adb top command also relies only on reading the /proc states. Thus, I suppose, therefore, it is not possible to execute the top command through ProcessBuilder without encountering an error that is denied permission starting with Android N.

+2
source

Source: https://habr.com/ru/post/1014262/


All Articles