前言
為了讓應(yīng)用程序可以直接調(diào)用su執(zhí)行系統(tǒng)命令和獲取root權(quán)限,本文基于Purple Pi OH3566主板的Android SDK,介紹如何修改和編譯一個root版本的Android11系統(tǒng);以下為sdk源碼修改方法。
Purple Pi OH作為一款兼容樹莓派的開源主板,采用瑞芯微RK3566 (Cortex-A55) 四核64位超強CPU,主頻最高達1.8 GHz,算力高達1Tops,支持INT8/INT16,支持TensorFlow/MXNet/PyTorch/Caffe框架,多路視頻輸出和輸入,支持4K、H.265/H.264視頻解碼,接口豐富。
玩法豐富,支持OpenHarmony、Ubuntu、Debian、Android等多種系統(tǒng),提供豐富開源資料。
一、關(guān)閉selinux
device/rockchip/common/BoardConfig.mk
diff --git a/device/rockchip/common/BoardConfig.mk b/device/rockchip/common/BoardConfig.mk
index e03c54f6a0..4fc6dc9868 100755
--- a/device/rockchip/common/BoardConfig.mk
+++ b/device/rockchip/common/BoardConfig.mk
@@ -59,7 +59,7 @@ BOARD_BOOT_HEADER_VERSION ?= 2
BOARD_MKBOOTIMG_ARGS :=
BOARD_PREBUILT_DTBOIMAGE ?= $(TARGET_DEVICE_DIR)/dtbo.img
BOARD_ROCKCHIP_VIRTUAL_AB_ENABLE ?= false
-BOARD_SELINUX_ENFORCING ?= true
+BOARD_SELINUX_ENFORCING ?= false
# Use the non-open-source parts, if they're present
二、注釋用戶組權(quán)限檢測
system/extras/su/su.cpp
diff --git a/system/extras/su/su.cpp b/system/extras/su/su.cpp
index 1a1ab6bf40..af3d2a68c7 100644
--- a/system/extras/su/su.cpp
+++ b/system/extras/su/su.cpp
@@ -80,8 +80,8 @@ void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, i
}
int main(int argc, char** argv) {
- 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");
// Handle -h and --help.
++argv;
三、給 su 文件默認授予 root 權(quán)限
system/core/libcutils/fs_config.cpp
diff --git a/system/core/libcutils/fs_config.cpp b/system/core/libcutils/fs_config.cpp
index 5805a4d19b..92e93e76ff 100644
--- a/system/core/libcutils/fs_config.cpp
+++ b/system/core/libcutils/fs_config.cpp
@@ -188,7 +188,7 @@ static const struct fs_path_config android_files[] = {
// the following two files are INTENTIONALLY set-uid, but they
// are NOT included on user builds.
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
- { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
+ { 06755, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
// the following files have enhanced capabilities and ARE included
// in user builds.
frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
diff --git a/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp b/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
index 9eede83e21..d161e6fad3 100644
--- a/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
+++ b/frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
@@ -656,7 +656,7 @@ static void EnableKeepCapabilities(fail_fn_t fail_fn) {
}
static void DropCapabilitiesBoundingSet(fail_fn_t fail_fn) {
- for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {;
+/* for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {;
if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) == -1) {
if (errno == EINVAL) {
ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
@@ -665,7 +665,7 @@ static void DropCapabilitiesBoundingSet(fail_fn_t fail_fn) {
fail_fn(CREATE_ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno)));
}
}
- }
+ }*/
}
kernel/security/commoncap.c
diff --git a/kernel/security/commoncap.c b/kernel/security/commoncap.c
index 876cfe01d9..ce87b1b780 100644
--- a/kernel/security/commoncap.c
+++ b/kernel/security/commoncap.c
@@ -1166,12 +1166,12 @@ int cap_task_setnice(struct task_struct *p, int nice)
static int cap_prctl_drop(unsigned long cap)
{
struct cred *new;
-
+/*
if (!ns_capable(current_user_ns(), CAP_SETPCAP))
return -EPERM;
if (!cap_valid(cap))
return -EINVAL;
-
+*/
new = prepare_creds();
if (!new)
return -ENOMEM;
四、編譯鏡像
修改后需要重新編譯內(nèi)核和AOSP,Android編譯需要選擇rk3566_r-userdebug版本
source build/envsetup.sh
lunch rk3566_r-userdebug
五、測試驗證root是否成功
可在應(yīng)用程序中調(diào)用 /system/xbin/su來測試系統(tǒng)是否root成功。
public static void RootCommand() {
Process process = null;
try {
process = Runtime.getRuntime().exec("/system/xbin/su");
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
}
root的系統(tǒng)可正常執(zhí)行,而非root的系統(tǒng)會提示沒有權(quán)限。
java.io.IOException: Cannot run program "/system/xbin/su": error=13, Permission denied