Android 源码修改,使第三方应用可以直接使用su命令

    在android原生系统中,只有root权限和shell权限下才可以使用su命令,虽然在userdebug模式下编译的系统镜像有自带的su文件,但是第三方应用却无法使用。于是在这种场景下,有两种方式可以实现第三方应用使用su命令。

    1.修改原来的su相关的源码(所有的应用都可以使用)

    2.通过supersu.apk 的方式进行实现(可以通过supersu进行控制应用是否可以使用su)

一、修改原来的su相关的源码
1.修改 system/extras/su/su.c ,屏蔽如下代码:

修改前
 

uid_t current_uid = getuid();   
if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");


修改后
 

// uid_t current_uid = getuid();   
// if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");


将su文件中的判断是否是root,或是shell的用户id判断进行注释。

2.修改 system/core/libcutils/fs_config.c, 做如下修改:

修改 fs_path_config android_files结构体中的
 
修改前
 

{ 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },


 
修改后
 

扫描二维码关注公众号,回复: 16749811 查看本文章
{ 06755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },


修改su文件的访问权限,将其他用户的权限修改为可读,可执行权限。

3.修改 frameworks/base/cmds/app_process/app_main.cpp,做如下的修改:

注释 main 函数中的以下代码
 
修改前
 

if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
        // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return
        // EINVAL. Don't die on such kernels.
        if (errno != EINVAL) {
            LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
            return 12;
        }
 }


 
修改后
 

/* if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
        // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return
        // EINVAL. Don't die on such kernels.
        if (errno != EINVAL) {
            LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
            return 12;
        }
    } */

4、修改 frameworks/base/core/jni/com_android_internal_os_Zygote.cpp,做如下的修改:

注释 DropCapabilitiesBoundingSet 中的如下代码
 
修改前
 

for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
    int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
    if (rc == -1) {
      if (errno == EINVAL) {
        ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
              "your kernel is compiled with file capabilities support");
      } else {
        RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
      }
    }
  }
 


修改后
 

/* for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
    int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
    if (rc == -1) {
      if (errno == EINVAL) {
        ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
              "your kernel is compiled with file capabilities support");
      } else {
        RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
      }
    }
  } */

注释后就是一个空方法了。

5.修改 system/core/adb/daemon/main.cpp,做如下的修改

将 should_drop_privileges函数 直接返回false
 
修改前
 

static bool should_drop_privileges() {
#if defined(ALLOW_ADBD_ROOT)
    char value[PROPERTY_VALUE_MAX];
 
    // The properties that affect `adb root` and `adb unroot` are ro.secure and
    // ro.debuggable. In this context the names don't make the expected behavior
    // particularly obvious.
    //
    // ro.debuggable:
    //   Allowed to become root, but not necessarily the default. Set to 1 on
    //   eng and userdebug builds.
    //
    // ro.secure:
    //   Drop privileges by default. Set to 1 on userdebug and user builds.
    property_get("ro.secure", value, "1");
    bool ro_secure = (strcmp(value, "1") == 0);
 
    property_get("ro.debuggable", value, "");
    bool ro_debuggable = (strcmp(value, "1") == 0);


 
修改后
 

static bool should_drop_privileges() {
    return false;
#if defined(ALLOW_ADBD_ROOT)
    char value[PROPERTY_VALUE_MAX];
 
    // The properties that affect `adb root` and `adb unroot` are ro.secure and
    // ro.debuggable. In this context the names don't make the expected behavior
    // particularly obvious.
    //
    // ro.debuggable:
    //   Allowed to become root, but not necessarily the default. Set to 1 on
    //   eng and userdebug builds.
    //
    // ro.secure:
    //   Drop privileges by default. Set to 1 on userdebug and user builds.
    property_get("ro.secure", value, "1");
    bool ro_secure = (strcmp(value, "1") == 0);
 
    property_get("ro.debuggable", value, "");
    bool ro_debuggable = (strcmp(value, "1") == 0);

6、修改 system/core/init/init.cpp,做如下的修改:

将selinux_is_enforcing函数直接返回false
 
修改前
 

static bool selinux_is_enforcing(void)
{
    if (ALLOW_PERMISSIVE_SELINUX) {
        return selinux_status_from_cmdline() == SELINUX_ENFORCING;
    }
    return true;
}


 
修改后
 

static bool selinux_is_enforcing(void)
{
    return false;
    if (ALLOW_PERMISSIVE_SELINUX) {
        return selinux_status_from_cmdline() == SELINUX_ENFORCING;
    }
    return true;
}

7、修改 system/core/init/Android.mk,做如下修改:

修改前
 

init_options += -DALLOW_LOCAL_PROP_OVERRIDE=0 -DALLOW_DISABLE_SELINUX=0


修改后
 

init_options += -DALLOW_LOCAL_PROP_OVERRIDE=0 -DALLOW_DISABLE_SELINUX=1


8、修改  device/**/aa/init.aa.rc,在文件最后添加如下代码 (在device下,通过grep "init.rc"  ./* -r -n  查找自己设备的 init.设备名.rc文件)

# wtw let other user can use su 
service    superuser /system/xbin/su --daemon
    class super-user
    user root
    oneshot
 
on property:superuser.start=on
    class_start super-user


9、修改 system/core/adb/Android.mk,做如下修改:

找到  # adbd device daemon 位置,注释掉判断。
 
修改前
 

ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
endif


 
修改后
 

# ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
# endif

10、对源码进行 make clean,再进行编译。生成镜像就可以了。
 

猜你喜欢

转载自blog.csdn.net/u010823818/article/details/131088563