Execute root commands in app - permission denied

Search This thread

blakegriplingph

Senior Member
May 13, 2011
1,076
159
Amazon Fire
Realme C3
Truth to be told, I'll write down your name on a scrap of paper so to never ever run anything written by you. No offense, but I don't want my phone to fry 'cause you're learning how to fry phones.

Having said that, I'll give you some hints, so that maybe in the future I'll try one of your apps (not going to happen).

1. If you want to write an Android app, it has to run Java, not shell code. If you want to write a shell script, write a shell script. This isn't bashing, it's a hearted hint. Trust me here!

2. If you still want to wrap an Android app around a shell script, then at the very least use Android/Java as much as possible and restrict the shell part to the strictly least required: special commands or commands requiring root!

Code:
*snip*

What should you do? You should use Java (with proper try/catch blocks) checking for errors at each step you take, taking care of the possible failures (download not available, directory not existing, ...). Taking care of errors with shell code is possible but it's a PITA and you can't debug it with the debugger. Why am I even pointing this out in the first place?!? Just do it.

1. You can create the directory with Java. Not only that, but you don't need to hardcode "/sdcard" in it! Just use the API to get the correct path: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

2. You can downlad your remote file with Java. In the worst case scenario, you can use an HttpURLConnection + FileOutputStream. In the best case scenario there's an Android API for that.

3. You can unzip your remote file with Java, checking that all the files you expect it to contain are there and it got transferred correctly. Check this out: http://developer.android.com/reference/java/util/zip/ZipFile.html

4. You can delete the files you downloaded with Java. This is so easy I want to cry. http://developer.android.com/reference/java/io/File.html#delete()

5. The few commands you need root to run, and you need them to be shell scripts (you can't run Java stuff as root as far as I've harshly found out), you write them this way:
Code:
*snip*

Pay attention and don't forget the "\n" at the end of each command.

This will make your app SuperUser.apk friendly and will get you to the result you wanted.

Hope you don't mind if I use this, but I needed a quick and dirty way for me to issue commands progmatically on an app I cobbled up to fix issues with my phone. And don't worry, I do have an idea of what the code does so as not to give the impression that I'm merely pasting yours into my app.
 

gonz7474

New member
Jun 13, 2016
1
0
Hi!

I'm writing a small application that allows you to enable or disable RamHack. There is a problem. The logs show that the app does not have permission.

At the beginning of the code doing the command "su":
Code:
final Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("su");
}
catch (IOException e) {
e.printStackTrace();
}
Then after tap a button I execute this:
Code:
final Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("mkdir /sdcard/switch_rh");
runtime.exec("wget -P /sdcard/switch_rh URL_to_my_site/rhpack.zip");
runtime.exec("unzip -o /sdcard/switch_rh/rhpack.zip -d /sdcard");
runtime.exec("rwsystem");
runtime.exec("flash_image boot /sdcard/2.img");
runtime.exec("cp /sdcard/2.ko /system/lib/modules/wlan.ko");
runtime.exec("rosystem");
runtime.exec("rm -r /sdcard/2.ko");
runtime.exec("rm -r /sdcard/2.img");
runtime.exec("reboot");
}
catch (IOException e) {
e.printStackTrace();
}

The logs show like this:
Code:
03-24 19:55:30.682: WARN/System.err(12444): java.io.IOException: Error running exec(). Commands: [rwsystem] Working Directory: null Environment: null
03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.ProcessManager.exec(ProcessManager.java:196)
03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.Runtime.exec(Runtime.java:225)
03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.Runtime.exec(Runtime.java:313)
03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.Runtime.exec(Runtime.java:246)
03-24 19:55:30.692: WARN/System.err(12444):     at pl.zezol.switch_rh.MainActivity$1.onClick(MainActivity.java:76)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.View.performClick(View.java:2360)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.View.onTouchEvent(View.java:4194)
03-24 19:55:30.692: WARN/System.err(12444):     at android.widget.TextView.onTouchEvent(TextView.java:6534)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.View.dispatchTouchEvent(View.java:3724)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-24 19:55:30.722: WARN/System.err(12444):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
03-24 19:55:30.722: WARN/System.err(12444):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
03-24 19:55:30.742: WARN/System.err(12444):     at android.app.Activity.dispatchTouchEvent(Activity.java:2019)
03-24 19:55:30.742: WARN/System.err(12444):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
03-24 19:55:30.742: WARN/System.err(12444):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1660)
03-24 19:55:30.742: WARN/System.err(12444):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 19:55:30.742: WARN/System.err(12444):     at android.os.Looper.loop(Looper.java:123)
03-24 19:55:30.742: WARN/System.err(12444):     at android.app.ActivityThread.main(ActivityThread.java:4358)
03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.reflect.Method.invoke(Method.java:521)
03-24 19:55:30.742: WARN/System.err(12444):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-24 19:55:30.742: WARN/System.err(12444):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-24 19:55:30.742: WARN/System.err(12444):     at dalvik.system.NativeStart.main(Native Method)
03-24 19:55:30.742: WARN/System.err(12444): Caused by: java.io.IOException: Permission denied
03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.ProcessManager.exec(Native Method)
03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.ProcessManager.exec(ProcessManager.java:194)
03-24 19:55:30.742: WARN/System.err(12444):     ... 25 more
03-24 19:55:31.102: WARN/su(12516): request rejected (10085:10085->0:0 /system/bin/sh)

What can I do that the program had access to the root?

PS. Sorry for my bad English.

Cheers



Sent from my 4009F using XDA Free mobile app
 

Top Liked Posts

  • There are no posts matching your filters.
  • 5
    Truth to be told, I'll write down your name on a scrap of paper so to never ever run anything written by you. No offense, but I don't want my phone to fry 'cause you're learning how to fry phones.

    Having said that, I'll give you some hints, so that maybe in the future I'll try one of your apps (not going to happen).

    1. If you want to write an Android app, it has to run Java, not shell code. If you want to write a shell script, write a shell script. This isn't bashing, it's a hearted hint. Trust me here!

    2. If you still want to wrap an Android app around a shell script, then at the very least use Android/Java as much as possible and restrict the shell part to the strictly least required: special commands or commands requiring root!

    Code:
    runtime.exec("mkdir /sdcard/switch_rh"); [B]NO ROOT REQUIRED![/B]
    runtime.exec("wget -P /sdcard/switch_rh URL_to_my_site/rhpack.zip"); [B]NO ROOT REQUIRED![/B]
    runtime.exec("unzip -o /sdcard/switch_rh/rhpack.zip -d /sdcard"); [B]NO ROOT REQUIRED![/B]
    runtime.exec("rwsystem");
    runtime.exec("flash_image boot /sdcard/2.img");
    runtime.exec("cp /sdcard/2.ko /system/lib/modules/wlan.ko");
    runtime.exec("rosystem");
    runtime.exec("rm -r /sdcard/2.ko"); [B]NO ROOT REQUIRED![/B]
    runtime.exec("rm -r /sdcard/2.img"); [B]NO ROOT REQUIRED![/B]
    runtime.exec("reboot");

    What should you do? You should use Java (with proper try/catch blocks) checking for errors at each step you take, taking care of the possible failures (download not available, directory not existing, ...). Taking care of errors with shell code is possible but it's a PITA and you can't debug it with the debugger. Why am I even pointing this out in the first place?!? Just do it.

    1. You can create the directory with Java. Not only that, but you don't need to hardcode "/sdcard" in it! Just use the API to get the correct path: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

    2. You can downlad your remote file with Java. In the worst case scenario, you can use an HttpURLConnection + FileOutputStream. In the best case scenario there's an Android API for that.

    3. You can unzip your remote file with Java, checking that all the files you expect it to contain are there and it got transferred correctly. Check this out: http://developer.android.com/reference/java/util/zip/ZipFile.html

    4. You can delete the files you downloaded with Java. This is so easy I want to cry. http://developer.android.com/reference/java/io/File.html#delete()

    5. The few commands you need root to run, and you need them to be shell scripts (you can't run Java stuff as root as far as I've harshly found out), you write them this way:
    Code:
    try {
       // You previously found out which is the right path for the su binary,
       // being it /system/bin/su or /system/xbin/su
       Process suProcess = Runtime.getRuntime().exec(suBinaryPath);
       DataOutputStream dos = new DataOutputStream(suProcess.getOutputStream());
       dos.writeBytes("flash_image boot " + sdcardPath + "2.img\n");
       dos.flush();
       dos.writeBytes("cp " +sdcardPath + "2.ko /system/lib/modules/wlan.ko\n");
       dos.flush();
       dos.writeBytes("exit\n");
       dos.flush();
       suProcess.waitFor();
    } catch (Exception ex) {
       // Please do something, don't let Exceptions be raised and silently trapped.
    }

    Pay attention and don't forget the "\n" at the end of each command.

    This will make your app SuperUser.apk friendly and will get you to the result you wanted.
    1
    Hi!

    I'm writing a small application that allows you to enable or disable RamHack. There is a problem. The logs show that the app does not have permission.

    At the beginning of the code doing the command "su":
    Code:
    final Runtime runtime = Runtime.getRuntime();
    try {
    	runtime.exec("su");
    }
    catch (IOException e) {
    	e.printStackTrace();
    }
    Then after tap a button I execute this:
    Code:
    final Runtime runtime = Runtime.getRuntime();
    try {
    	runtime.exec("mkdir /sdcard/switch_rh");
    	runtime.exec("wget -P /sdcard/switch_rh URL_to_my_site/rhpack.zip");
    	runtime.exec("unzip -o /sdcard/switch_rh/rhpack.zip -d /sdcard");
    	runtime.exec("rwsystem");
    	runtime.exec("flash_image boot /sdcard/2.img");
    	runtime.exec("cp /sdcard/2.ko /system/lib/modules/wlan.ko");
    	runtime.exec("rosystem");
    	runtime.exec("rm -r /sdcard/2.ko");
    	runtime.exec("rm -r /sdcard/2.img");
    	runtime.exec("reboot");
    }
    catch (IOException e) {
    	e.printStackTrace();
    }

    The logs show like this:
    Code:
    03-24 19:55:30.682: WARN/System.err(12444): java.io.IOException: Error running exec(). Commands: [rwsystem] Working Directory: null Environment: null
    03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.ProcessManager.exec(ProcessManager.java:196)
    03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.Runtime.exec(Runtime.java:225)
    03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.Runtime.exec(Runtime.java:313)
    03-24 19:55:30.692: WARN/System.err(12444):     at java.lang.Runtime.exec(Runtime.java:246)
    03-24 19:55:30.692: WARN/System.err(12444):     at pl.zezol.switch_rh.MainActivity$1.onClick(MainActivity.java:76)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.View.performClick(View.java:2360)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.View.onTouchEvent(View.java:4194)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.widget.TextView.onTouchEvent(TextView.java:6534)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.View.dispatchTouchEvent(View.java:3724)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
    03-24 19:55:30.692: WARN/System.err(12444):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
    03-24 19:55:30.722: WARN/System.err(12444):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
    03-24 19:55:30.722: WARN/System.err(12444):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
    03-24 19:55:30.742: WARN/System.err(12444):     at android.app.Activity.dispatchTouchEvent(Activity.java:2019)
    03-24 19:55:30.742: WARN/System.err(12444):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
    03-24 19:55:30.742: WARN/System.err(12444):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1660)
    03-24 19:55:30.742: WARN/System.err(12444):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-24 19:55:30.742: WARN/System.err(12444):     at android.os.Looper.loop(Looper.java:123)
    03-24 19:55:30.742: WARN/System.err(12444):     at android.app.ActivityThread.main(ActivityThread.java:4358)
    03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.reflect.Method.invoke(Method.java:521)
    03-24 19:55:30.742: WARN/System.err(12444):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
    03-24 19:55:30.742: WARN/System.err(12444):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
    03-24 19:55:30.742: WARN/System.err(12444):     at dalvik.system.NativeStart.main(Native Method)
    03-24 19:55:30.742: WARN/System.err(12444): Caused by: java.io.IOException: Permission denied
    03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.ProcessManager.exec(Native Method)
    03-24 19:55:30.742: WARN/System.err(12444):     at java.lang.ProcessManager.exec(ProcessManager.java:194)
    03-24 19:55:30.742: WARN/System.err(12444):     ... 25 more
    03-24 19:55:31.102: WARN/su(12516): request rejected (10085:10085->0:0 /system/bin/sh)

    What can I do that the program had access to the root?

    PS. Sorry for my bad English.

    Cheers
    1
    Hi!

    I'm writing a small application that allows you to enable or disable RamHack. There is a problem. The logs show that the app does not have permission.

    At the beginning of the code doing the command "su":
    Code:
    		final Runtime runtime = Runtime.getRuntime();
    		try {
    			runtime.exec("su");
    		}
    		catch (IOException e) {
    		    e.printStackTrace();
    		}
    Then after doing a command button is pressed:
    Code:
    final Runtime runtime = Runtime.getRuntime();
    try {
    	runtime.exec("mkdir /sdcard/switch_rh");
    	runtime.exec("wget -P /sdcard/switch_rh URL/rhpack.zip");
    	runtime.exec("unzip -o /sdcard/switch_rh/rhpack.zip -d /sdcard");
    	runtime.exec("rwsystem");
    	runtime.exec("flash_image boot /sdcard/2.img");
    	runtime.exec("cp /sdcard/2.ko /system/lib/modules/wlan.ko");
    	runtime.exec("rosystem");
    	runtime.exec("rm -r /sdcard/2.ko");
    	runtime.exec("rm -r /sdcard/2.img");
    	runtime.exec("reboot");
    }
    catch (IOException e) {
    	e.printStackTrace();
    }

    What do I do that the program had access to the root?

    PS. Sorry for my bad English.

    Cheers

    don't rely on rwsystem and rosystem scripts, they are often broken, don't have hash bangs and have dos eol

    instead use
    Code:
    busybox mount rw,remount /system

    I would also point out that you don't need to use recursion ( -r ) this is only required if you wish to delete a directory

    having said that you *should* be using recursion
    but here
    runtime.exec("unzip -o /sdcard/switch_rh/rhpack.zip -d /sdcard");

    you are unziping to the root of the SD card, and then only deleting the two files
    you are expecting
    better to unzip into a new directory, and then delete the directory after
    ( therefore you do not leave anything unexpected behind )
    being a script , you should probably check to see tf what you expect exists

    with
    runtime.exec("wget -P /sdcard/switch_rh URL/rhpack.zip");
    it looks like you are pulling from the internet, are you checking it in anyway?
    the download could be corrupt, it won't unzip and you end up flashing something that is on the root of the sdcard and just happens to have the same name...

    it is inviting trouble........... and one day trouble may just turn up
    1
    Sorry for the bump, but I am a bit confused about this... I am trying the following:

    Code:
            Process p = null;
            final Runtime runtime = Runtime.getRuntime();
            try {
    	        p = runtime.exec("su -c 'mount -o rw,remount /system'");
    	        p.waitFor();
    	        
    	        p = runtime.exec("su -c 'mkdir /system/bin/test'");
    	        p.waitFor();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }

    But nothing happens... Do I HAVE to have busybox installed? am i not allowed to use the built in commands?

    I tried running it in the emulator with logcat running but it denied root permissions (no dialog was prompted) and on my rooted evo4g (was prompted for root permissions and allowed it). As a sidenote, is there anything I can use to get logging output from my production phone?

    Thanks much in advance!