From 9fae52277efe3502cfd15694ad2936cd7d24c53e Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 7 Aug 2020 09:53:08 +0800 Subject: [PATCH] Use /data if possible --- .../be/mygod/librootkotlinx/RootServer.kt | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt index b3c58ccb..71a002a0 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt @@ -157,28 +157,38 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U } val token2 = UUID.randomUUID().toString() - val persistence = File(context.codeCacheDir, ".librootkotlinx-uuid") - val uuid = context.packageName + '@' + if (persistence.canRead()) persistence.readText() else { - UUID.randomUUID().toString().also { persistence.writeText(it) } + val uuid by lazy { + val persistence = File(context.codeCacheDir, ".librootkotlinx-uuid") + context.packageName + '@' + if (persistence.canRead()) persistence.readText() else { + UUID.randomUUID().toString().also { persistence.writeText(it) } + } } // to workaround Samsung's stupid kernel patch, we need to relocate outside of /data: https://github.com/Chainfire/librootjava/issues/19 val (baseDir, relocated) = if (Build.VERSION.SDK_INT < 29) "/dev" to "/dev/app_process_$uuid" else { - val apexPath = "/apex/$uuid" - writer.writeBytes("[ -d $apexPath ] || " + - "mkdir $apexPath && " + - // we need to mount a new tmpfs to override noexec flag - "mount -t tmpfs -o size=1M tmpfs $apexPath || exit 1\n") - // unfortunately native ld.config.txt only recognizes /data,/system,/system_ext as system directories; - // to link correctly, we need to add our path to the linker config too - val ldConfig = "$apexPath/etc/ld.config.txt" - val masterLdConfig = if (Build.VERSION.SDK_INT == 29) { - "/system/etc/ld.config.29.txt" - } else "/linkerconfig/ld.config.txt" - writer.writeBytes("[ -f $ldConfig ] || " + - "mkdir -p $apexPath/etc && " + - "echo dir.system = $apexPath >$ldConfig && " + - "cat $masterLdConfig >>$ldConfig || exit 1\n") - "$apexPath/bin" to "$apexPath/bin/app_process" + val cachePath = context.codeCacheDir.canonicalPath + if (cachePath.startsWith("/data/")) { + val relocated = File(context.codeCacheDir, "relocated") + relocated.mkdir() + check(relocated.isDirectory) + relocated.absolutePath to File(relocated, "app_process").absolutePath + } else { + val apexPath = "/apex/$uuid" + writer.writeBytes("[ -d $apexPath ] || " + + "mkdir $apexPath && " + + // we need to mount a new tmpfs to override noexec flag + "mount -t tmpfs -o size=1M tmpfs $apexPath || exit 1\n") + // unfortunately native ld.config.txt only recognizes /data,/system,/system_ext as system directories; + // to link correctly, we need to add our path to the linker config too + val ldConfig = "$apexPath/etc/ld.config.txt" + val masterLdConfig = if (Build.VERSION.SDK_INT == 29) { + "/system/etc/ld.config.29.txt" + } else "/linkerconfig/ld.config.txt" + writer.writeBytes("[ -f $ldConfig ] || " + + "mkdir -p $apexPath/etc && " + + "echo dir.system = $apexPath >$ldConfig && " + + "cat $masterLdConfig >>$ldConfig || exit 1\n") + "$apexPath/bin" to "$apexPath/bin/app_process" + } } writer.writeBytes("[ -f $relocated ] || " + "mkdir -p $baseDir && " +