AOSP 5.1 - 如何使用real-root权限执行我的应用程序?

时间:2017-10-23 11:58:21

标签: java android android-source su android-5.1.1-lollipop

在我的 AOSP 5.1-build 中,我正在尝试创建一个MySetupApp.apk - 应用,根据用户执行一些设置操作。
 例如。除了其他东西(大多数也需要root)之外,用户应该能够在MySetupApp.apk中选择一个bootanimation。 bootanimation保存在/sdcard/上,apk应该将其移动到正确的系统文件夹/system/media/bootanimation.zip 这在adb-shell上工作正常。以下命令提供了所需的结果(新的bootanimation):

adb remount
adb shell cp /sdcard/bootanimation.zip /system/media/
adb shell chmod 777 /system/media/bootanimation.zip

该应用以系统用户身份运行: android:sharedUserId="android.uid.system"我可以在adb-shell上验证这一点:

root@astar-yh:/system/bin # ps
system    9634  170   1242144 35536 ffffffff b6e51bf4 S com.mycompany.MySetupApp

我在AOSP-build中添加了应用程序:

/android/device/softwinner/common/prebuild/apk/Android.mk

include $(CLEAR_VARS)
LOCAL_MODULE := MySetupApp
LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_PRIVILEGED_MODULE := true
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
include $(BUILD_PREBUILT)

因此,应用程序安装在/system/priv-app中,并使用平台密钥进行签名。

我也调整了 /android/system/extras/su/su.c

myuid = getuid();
// added AID_SYSTEM
if (myuid != AID_ROOT && myuid != AID_SHELL && myuid != AID_SYSTEM) {
   fprintf(stderr,"su: uid %d not allowed to su\n", myuid);
   return 1;
}

// Removed this code-block
/*if(setgid(gid) || setuid(uid)) {
    fprintf(stderr,"su: permission denied\n");
    return 1;
}*/

这是我的Java代码。产出的背后是评论:

public void doRootStuff(){
  try {
    String line;
    Process process = Runtime.getRuntime().exec("su");

    OutputStream stdin = process.getOutputStream();
    InputStream stderr = process.getErrorStream();
    InputStream stdout = process.getInputStream();

    stdin.write(("getenforce\n").getBytes()); // "Permissive"
    stdin.write(("mount -o rw,remount -t ext4 /system\n").getBytes()); //E/[Error]: mount: Operation not permitted
    stdin.write(("ls /data/data/\n").getBytes()); // Works! Shows the directory contents
    stdin.write(("touch /system/media/test\n").getBytes()); // Does nothing - no error but also no file
    stdin.write(("cp -f /sdcard/bootanimation.zip /system/media/bootanimation_test.zip\n").getBytes()); // E/[Error]: cp: /system/media/bootanimation_test.zip: Read-only file system
    stdin.write("exit\n".getBytes());
    stdin.flush();

    stdin.close();
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
    while ((line = br.readLine()) != null) {
      Log.d("[Output]", line);
    }
    br.close();
    br = new BufferedReader(new InputStreamReader(stderr));
    while ((line = br.readLine()) != null) {
      Log.e("[Error]", line);
    }
    br.close();
    process.waitFor();
    process.destroy();

  } catch (Exception ex) {
    Log.e(TAG, ex.toString());
    ex.printStackTrace();
  }
}

根据建议here,我还编辑了android_filesystem_config.h,但没有运气

    { 06755, AID_ROOT,      AID_ROOT,      0, "system/priv-app/MySetupApp/*" },

在我的应用中调用setuid(0);也不起作用:

com.mycompany.MySetupApp W/art: Failed to find OatDexFile for DexFile /system/priv-app/MySetupApp/MySetupApp.apk ( canonical path /system/priv-app/MySetupApp/MySetupApp.apk) with checksum 0x73b2ee8d in OatFile /data/dalvik-cache/arm/system@priv-app@MySetupApp@MySetupApp.apk@classes.dex
com.mycompany.MySetupApp W/System.err: android.system.ErrnoException: setuid failed: EPERM (Operation not permitted)

因此无法从apk重新安装系统目录并在其上复制内容!

我能做些什么来使这项工作成功?我应该在Android应用程序或Android资源中更改哪些内容?

更新

我还尝试将功能转移到脚本setup_device.sh并将其放入/system/bin/setup_script.sh。当我尝试在java中运行它时:

Process process = Runtime.getRuntime().exec("su");
stdin.write(("./system/bin/setup_device.sh\n").getBytes());

它说:

E/[Error]: sh: <stdin>[2]: ./system/bin/setup_device.sh: Permission denied

1 个答案:

答案 0 :(得分:0)

我能够解决我的问题:

1.。正如我在问题中所提到的,我写了一个简单的脚本,它将引导从/sdcard/复制到/system/media/。将此脚本放入/system/bin/

<强> setup_device.sh

#!/sbin/busybox sh

BUSYBOX="/sbin/busybox"

echo "Remount /system-partition to rw"
mount -o remount,rw /system

if [ -e /sdcard/bootanimation.zip ] ; then
    echo "copy bootanimation"
    $BUSYBOX cp -fp "/sdcard/bootanimation.zip" "/system/media/bootanimation.zip"
    chmod 777 "/system/media/bootanimation.zip"
    chown 0.0 "/system/media/bootanimation.zip"
    $BUSYBOX rm "/sdcard/bootanimation.zip"
else
    echo "Bootanimation not in /sdcard/-folder"
fi

2.。)在init.rc中调用此脚本并在property-change

上执行
service setup_device /sbin/busybox sh /system/bin/setup_device.sh
    user root
    group root
    disabled
    oneshot

on property:start.setup=1
    start setup_device

3.)在setup.apk中,只需设置此属性:

Process proc = Runtime.getRuntime().exec(new String[] { "/system/bin/sh", "-c", "setprop start.setup 1" });
proc.waitFor();

在应用设置此属性后,setup_device.sh - 脚本将触发并执行 root-rights 的代码! bootanimation.zip最终将复制到/system/media/,您可以在重新启动设备后立即看到新的引导。

缺陷:

  • 在init.rc中声明script-name时,请确保它不超过16个字符的字符数限制!同样只有用户字母数字字符和-_