Android native - How does native code written in jni run with root permission?

牧云@^-^@ 提交于 2021-02-02 09:11:28

问题


I have an Android application that call a native shared library by jni. This shared library invokes some loadable kernel modules .ko

My app run fail when jni calls the function in shared library that invokes kernel module. But when I write an executable using this shared library, it works fine when call above function.

I found that my app run with user name is "u0_axx" and my executable run by command line with root. So maybe it doesn't have permission to invokes kernel module.

My question is how does this native code run with root permission? Or some solution to solve such kind of issue?

Ps: I also tried to use Runtime.getRuntime().exec("su") and added <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/> into manifest but it doesn't help and get an exception that permission denied. My device is rooted and my app is built as system app.


回答1:


You can set your app to run as System,

android:sharedUserId="android.uid.system"

This requires some extra steps, but it won't give your JNI code access to kernel module.

For that, you need some mediator process (running as root) which can receive a request from your Java or native code, and call the kernel function for you. You can use

Runtime.getRuntime().exec("su …")

But it may be easier to start this mediator process as a service from init.rc. A specialized library like https://github.com/SpazeDog/rootfw may help, too.

You can find more explanations at https://boundarydevices.com/android-security-part-1-application-signatures-permissions/.



来源:https://stackoverflow.com/questions/51856601/android-native-how-does-native-code-written-in-jni-run-with-root-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!