ANDROID: Superuser + Shared JNI LIBs

If you need access to system libs (in my case, uinput), you will usually need to do one of 2 things:

1. create another unix executable (binary), which will call uinput, and invoke it with su (super user). Put this file in the /raw directory, and copy it to the device during run-time. This seems to be the easier method. The source is as such:

your_copy_binary(R.raw.your_binary, getFilesDir().getAbsolutePath()+"/your_binary");
Process sh = Runtime.getRuntime().exec("su");
OutputStream os = sh.getOutputStream();
os.write("chmod 777 " + getFilesDir().getAbsolutePath() + "/your_binary" + "\n", "ASCII");
os.write(getFilesDir().getAbsolutePath() _ "/your_binary", "ASCII");

2. create a unix shared library embedded within your android app, call open(...) to directly access the system lib, give the manifest SUPERUSER permissions, and finally set the lib to chmod 777.

Here's the JNI sourcecode:
int success = open("/dev/input", O_WRONLY | O_NONBLOCK);

Here's the Manifest added permission:
< uses-permission android:name="android.permission.ACCESS_SUPERUSER" />

Here's the final step to chmod
chmod 777 /dev/uinput


I prefer the second method, because it's closer to how the android NDK was intended to run.

2 comments:

Unknown said...

Hello, I would love to gain superuser privileges in java-callable code, however I'm confused as to your second approach..... can you explain in greater detail how this works? I've looked around and almost everyone goes for the "separate executable" approach. (Which is what I'm using right now)

Unknown said...

Hello!

Do these techniques require a rooted device or can they run on a non-rooted phone too?