? ? 在android原生系統(tǒng)中,只有root權(quán)限和shell權(quán)限下才可以使用su命令,雖然在userdebug模式下編譯的系統(tǒng)鏡像有自帶的su文件,但是第三方應(yīng)用卻無(wú)法使用。于是在這種場(chǎng)景下,有兩種方式可以實(shí)現(xiàn)第三方應(yīng)用使用su命令。
? ? 1.修改原來(lái)的su相關(guān)的源碼(所有的應(yīng)用都可以使用)
? ? 2.通過(guò)supersu.apk 的方式進(jìn)行實(shí)現(xiàn)(可以通過(guò)supersu進(jìn)行控制應(yīng)用是否可以使用su)
一、修改原來(lái)的su相關(guān)的源碼
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判斷進(jìn)行注釋。
2.修改 system/core/libcutils/fs_config.c, 做如下修改:
修改 fs_path_config android_files結(jié)構(gòu)體中的
?
修改前
?
{ 04750, AID_ROOT, ? ? ?AID_SHELL, ? ? 0, "system/xbin/su" },
?
修改后
?
{ 06755, AID_ROOT, ? ? ?AID_SHELL, ? ? 0, "system/xbin/su" },
修改su文件的訪問(wèn)權(quán)限,將其他用戶的權(quán)限修改為可讀,可執(zhí)行權(quán)限。
3.修改 frameworks/base/cmds/app_process/app_main.cpp,做如下的修改:
注釋 main 函數(shù)中的以下代碼
?
修改前
?
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");
? ? ? }
? ? }
? } */
注釋后就是一個(gè)空方法了。
5.修改 system/core/adb/daemon/main.cpp,做如下的修改
將 should_drop_privileges函數(shù) 直接返回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函數(shù)直接返回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下,通過(guò)grep "init.rc" ?./* -r -n ?查找自己設(shè)備的 init.設(shè)備名.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
?
修改后
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-708916.html
# ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
# endif
10、對(duì)源碼進(jìn)行 make clean,再進(jìn)行編譯。生成鏡像就可以了。
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-708916.html
到了這里,關(guān)于Android 源碼修改,使第三方應(yīng)用可以直接使用su命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!