16

I'm working on AOSP. I've successfully added my apk to build. Now I want to give root access to my app. I don't want to provide root access to other apps or to install the Superuser app in my build. I just want to add my app to get root access. How can I achieve that?

I went through the su.c file in path system/extras/su but I'm unable to understand the whole code.

When I went through the code, I think my objective can be achieved if I could modify su.c to provide root access to my app,compile it and add the binary to the build. Am I right?

8
  • 1
    Wouldn't it be a huge security barngate if any app could just get root? Dec 10, 2012 at 14:40
  • If you do not know how to do this, it is highly unlikely that you know how to audit code for safety before letting it run as root. Dec 10, 2012 at 14:59
  • from the code i found tht it gives root access to shell, in the same way i want to parse the xml file in data/system/packages.xml and find the UID of my app and allow it to have root access to the system
    – Neji
    Dec 11, 2012 at 6:33
  • Why you need root access? I'm asking because in android it is not used, there are other mechanisms.
    – auselen
    Dec 11, 2012 at 7:06
  • i've a app that deals with IPtables and network monitoring. For accessing IPtables i need to have root access
    – Neji
    Dec 11, 2012 at 7:11

2 Answers 2

7

You're moving in the right direction. You need to check the sources for su.c The only problem that you can face is how to run your program as root. To do this you need to set SUID sticky bit for the executable of your application. To do this you need to modify system/core/include/private/android_filesystem_config.h file (structure android_files[]), for instance for su program you can see how this bit is set:

{ 06755, AID_ROOT,      AID_ROOT,      "system/xbin/su" },
10
  • 1
    This approach will not work, as Android applications do not have unique executables. Instead, they are ultimately libraries to a common executable. Zygote forks to create an application process, but that never exec()'s, it just changes UID and loads a library with the application code. Dec 15, 2012 at 18:15
  • I do not understand. This is the way how su works. I managed to use this approach several times in my projects.
    – Yury
    Dec 15, 2012 at 20:01
  • Yury, can you tell me what part you updated in both the files to give root access to your app??
    – Neji
    Dec 15, 2012 at 22:59
  • You need to create your application similarly to su. In your program you should call setuid(0) function to obtain root priviledges. In android_filesystem_config.h you need to add a line with the name of your program in the structure I've specified in the answer. The only change you need to do is change the path to executable of your application.
    – Yury
    Dec 16, 2012 at 8:59
  • 1
    At first you need to create a native application that will perform your functionality (I usually put sources for these into /system/core directory). Then you need to add this application to the build (check appropriate .mk files - for instance, core.mk). Then you need to modify android_filesystem_config.h file as I explained in the post. After that you should build the system. Now you can create Java applications and inside these apps run a new Process with your native application.
    – Yury
    Dec 18, 2012 at 9:28
3

ChainFire has written a a guide on su and how to use it for normal apps. If you're intending on working with anything other than your own phone I suggest this is the approach you follow.

1
  • 1
    I don't have any executable binary. I want to set root permission, to a prebuilt/preinstalled Apk. What should I do?
    – Dr.jacky
    May 15, 2016 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.