From e3f63994737ad062d5dbd5c9700c71f735a7874d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=8B=E9=A1=B5?= <31466456+canyie@users.noreply.github.com> Date: Fri, 29 Oct 2021 09:00:43 +0800 Subject: [PATCH] Don't use xwrite() when patching legacy rootfs init Fix topjohnwu#4810 > [ 2.927463] [1: init: 1] magiskinit: Replace [/system/etc/selinux/plat_sepolicy.cil] -> [xxx] [ 2.936801] [1: init: 1] magiskinit: write failed with 14: Bad address Since topjohnwu#4596, magisk fails to patch `/init`, xwrite() fails with EFAULT, break the original `/init` file and make the device unbootable. Reverting this commit for legacy rootfs devices fixes the problem. I think this is a Samsung kernel magic since currently I can't reproduce this on other devices or find something special in the log currently we have. --- native/jni/init/rootdir.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index a93ae7d96..436748f0f 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -343,11 +343,16 @@ void RootFSBase::patch_rootfs() { } if (patch_sepolicy("/sepolicy")) { - auto init = mmap_data::ro(access("/system/bin/init",F_OK) == 0 ? "/system/bin/init" : "/init"); - init.patch({ make_pair(SPLIT_PLAT_CIL, "xxx") }); - int dest = xopen("/init", O_TRUNC | O_WRONLY | O_CLOEXEC, 0); - xwrite(dest, init.buf, init.sz); - close(dest); + if (access("/system/bin/init", F_OK) == 0) { + auto init = mmap_data::ro("/system/bin/init"); + init.patch({ make_pair(SPLIT_PLAT_CIL, "xxx") }); + int dest = xopen("/init", O_TRUNC | O_WRONLY | O_CLOEXEC, 0); + xwrite(dest, init.buf, init.sz); + close(dest); + } else { + auto init = mmap_data::rw("/init"); + init.patch({ make_pair(SPLIT_PLAT_CIL, "xxx") }); + } } // Handle overlays