Search code examples
javaandroidandroid-ndkandroid-permissionsroot

Needed permission to access /dev/diag


I am trying to open the /dev/diag using JNI as open("/dev/diag", O_RDWR | O_LARGEFILE | O_NONBLOCK); but returning errno: 13 Permission denied.

What should be done to make this work?

When checking the ownership of the /dev/diag using the command ls -l /dev/diag it returns crw-rw-rw- system qcom_diag 244, 0 2015-01-14 01:47 diag and when trying to use the command id i get uid=0(root) gid=0(root) groups=0(root) context=u:r:init:s0

So I thought the problem is related to ownership?

Update: Based on @HamidShatu answer

I tried to set the SELinux to permissive with no luck.

I tried the command su 0 setenforce 0 it returns OK while still being Enforcing if checking immediately using getenforce command.

I even tried to change the prop.build: this file did not exist instead a prop.build.bak did so i copied it modified the SELinux to 0 instead of 1 and pushed it without the .bak extension. Even i checked with external applications that modify the prop.build where the SELinux is set to 0 but when checking with the getenforce command it is still returning Enforcing

here is the extract of the dmesg:

[18177.676603]  [0: servicemanager:  743] avc:  received setenforce notice   (enforcing=1)
[18182.768070]  [1: servicemanager:  743] avc:  received setenforce notice (enforcing=1)
[18183.231867]  [0:           init:    1] avc:  received setenforce notice (enforcing=1)
[18183.232006]  [0:           init:    1] avc:  received setenforce notice (enforcing=1)

I even tried to make the app run as system app by adding to the Manifest: <application--> android:sharedUserId="android.uid.system"

Hint: I am using apps from play store that can access the /dev/diag successfully.


Solution

  • After days trying to figure out the solution I was able to bypass the problem by doing the following:

    • Instead of using the C code as a shared library where I was struggling to open the /dev/diag I made it as executable C (.c file should have a main function) using Cmake as follows:

       cmake_minimum_required(VERSION 3.4.1)
       file(GLOB SRC_FILES
           src/main/cpp/*.c)
       add_executable(
           mylib.so
           ${SRC_FILES}
       )
      
    • Create a new command in Java that can execute mylib with root priveleage as:

      new String[]{"su", "-c", "exec " + context.getApplicationInfo().nativeLibraryDir + "/" + "mylib.so"}
      
    • Execute the command using a process builder

      process = new ProcessBuilder(command)
                   .start();
      

    The executable is able to use open("/dev/diag", O_RDWR | O_LARGEFILE | O_NONBLOCK); with no error!

    The reason for that is even in SU mode the application will downgrade to normal mode while executing the function but executing the library in SU mode will ensure its run in su