Detailed Explanation of su Permission Acquisition and Use in Android Root Devices

  • 2021-08-12 03:39:20
  • OfStack

Introduction to ROOT Permissions:

ROOT permissions are the highest permissions in the Linux kernel, and if you are ROOT, you have ROOT permissions. With the highest authority, You can do whatever you want, In other words, If a malicious program acquires ROOT privileges, Then you can destroy your mobile phone at will, Access to privacy... so when manufacturers 1 produce mobile phones, The user will not be provided with ROOT permission. The official claim is to protect the safety of the user's mobile phone, and then a pile of boot will be installed to start automatically. The user will not use or uninstall junk software in his life (I believe all the students who use Android know what I mean), and Apple's jailbreak means obtaining ROOT permission.

Why do you need ROOT permissions?

Apple users get ROOT permissions in order to install various software for free, and in order to obtain a more flexible operation experience, Apple will not install a pile of disgusting software; Ordinary Android users get ROOT permission, the biggest purpose is to uninstall these disgusting self-contained software, Android geek users are tossing Android phones, and Android developers are to get log files and analyze BUG.

After we boot up, the identity of using the mobile phone is an ordinary user (user). If su is executed, we can directly switch to ROOT identity. Just like Jingtian in Legend of the Sword and the Chivalrous Man 3, He is a mortal with limited mana, but as we all know, his previous life was General Feipeng, with high mana, and no one in heaven can compete with him. When they came to heaven, the Jade Emperor cast spells and let Jingtian switch directly to General Feipeng, so he had the memory and mana of General Feipeng, and fought another war with Paris. su is such a magical command.

On Qualcomm platform, the relevant codes of su are located in: LINUX/android/system/extras/su/su. c

In the development of Android, I occasionally encounter the development of some customized devices. Generally, this device will have root permission, and root permission will be used to do some operations in the development.

For example, when installing apk, ordinary Android mobile phones will jump out of the installation interface, but when using root permission, this can be skipped. The operation is as follows:


Process process = Runtime.getRuntime().exec("su");
OutputStream out = process.getOutputStream();
out.write((("pm install -r " + path) + "\n").getBytes());

Here use Process to obtain su authority, and then execute adb installation instructions. Note that these operations need to be done under the try-catch code block.

In addition, I have seen one method to execute the command line on the Internet before:


public static void execShell(String cmd){
  try {
    Process p=Runtime.getRuntime().exec(new String[]{"su","-c",cmd});
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
    String readLine=br.readLine();
    while(readLine!=null){
      System.out.println(readLine);
      readLine=br.readLine();
    }
    if(br!=null){
      br.close();
    }
    p.destroy();
    p=null;
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}

Here, the parameters are passed into the command line and executed in the method body. As for the specific command line, it is good to find the corresponding command when necessary.


Related articles: