From 17f0fea3fcdcb205b9bcb305e768cbe594ae56da Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 16 Dec 2017 04:40:54 +0800 Subject: [PATCH] Fix unreleased resource in rootfs --- core/jni/core/bootstages.c | 4 ++-- core/jni/core/daemon.c | 15 ++++++++------- core/jni/core/magiskinit.c | 10 ++++++++++ core/jni/magiskhide/proc_monitor.c | 1 + 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/jni/core/bootstages.c b/core/jni/core/bootstages.c index 080daed24..c60197317 100644 --- a/core/jni/core/bootstages.c +++ b/core/jni/core/bootstages.c @@ -410,8 +410,6 @@ static int prepare_img() { if (magiskloop == NULL) return 1; - vec_init(&module_list); - xmkdir(COREDIR, 0755); xmkdir(COREDIR "/post-fs-data.d", 0755); xmkdir(COREDIR "/service.d", 0755); @@ -506,6 +504,7 @@ void post_fs_data(int client) { // Allocate buffer if (buf == NULL) buf = xmalloc(PATH_MAX); if (buf2 == NULL) buf2 = xmalloc(PATH_MAX); + vec_init(&module_list); // Initialize if (!is_daemon_init) @@ -627,6 +626,7 @@ void late_start(int client) { // Wait till the full patch is done wait_till_exists(PATCHDONE); + unlink(PATCHDONE); // Run scripts after full patch, most reliable way to run scripts LOGI("* Running service.d scripts\n"); diff --git a/core/jni/core/daemon.c b/core/jni/core/daemon.c index 884a85c3c..6720fab32 100644 --- a/core/jni/core/daemon.c +++ b/core/jni/core/daemon.c @@ -127,6 +127,7 @@ void daemon_init() { rm_rf("/data/magisk"); unlink("/data/magisk.img"); unlink("/data/magisk_debug.log"); + chmod("/data/adb", 0700); // Use shell glob to match files exec_command_sync("sh", "-c", @@ -154,13 +155,6 @@ void daemon_init() { } close(sbin); - // Backward compatibility - xsymlink(DATABIN, "/data/magisk"); - xsymlink(MAINIMG, "/data/magisk.img"); - xsymlink(MOUNTPOINT, "/magisk"); - - xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL); - xmount("tmpfs", "/sbin", "tmpfs", 0, NULL); chmod("/sbin", 0755); setfilecon("/sbin", "u:object_r:rootfs:s0"); @@ -181,6 +175,13 @@ void daemon_init() { } close(root); + // Backward compatibility + xsymlink(DATABIN, "/data/magisk"); + xsymlink(MAINIMG, "/data/magisk.img"); + xsymlink(MOUNTPOINT, "/magisk"); + + xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL); + LOGI("* Mounting mirrors"); struct vector mounts; vec_init(&mounts); diff --git a/core/jni/core/magiskinit.c b/core/jni/core/magiskinit.c index e228d5c17..53b8db0fe 100644 --- a/core/jni/core/magiskinit.c +++ b/core/jni/core/magiskinit.c @@ -362,6 +362,12 @@ static void magisk_init_daemon() { // Wait till init cold boot done wait_till_exists("/dev/.coldboot_done"); + int null = open("/dev/null", O_RDWR | O_CLOEXEC); + dup3(null, STDIN_FILENO, O_CLOEXEC); + dup3(null, STDOUT_FILENO, O_CLOEXEC); + dup3(null, STDERR_FILENO, O_CLOEXEC); + close(null); + // Transit our context to su (mimic setcon) int fd, crap; fd = open("/proc/self/attr/current", O_WRONLY); @@ -414,6 +420,8 @@ int main(int argc, char *argv[]) { dup3(null, STDIN_FILENO, O_CLOEXEC); dup3(null, STDOUT_FILENO, O_CLOEXEC); dup3(null, STDERR_FILENO, O_CLOEXEC); + if (null > STDERR_FILENO) + close(null); // Extract and link files mkdir("/overlay", 0000); @@ -504,6 +512,8 @@ int main(int argc, char *argv[]) { if (fork_dont_care() == 0) { strcpy(argv[0], "magiskinit"); + close(overlay); + close(root); magisk_init_daemon(); } } diff --git a/core/jni/magiskhide/proc_monitor.c b/core/jni/magiskhide/proc_monitor.c index 991b3e2bd..d421ef9e5 100644 --- a/core/jni/magiskhide/proc_monitor.c +++ b/core/jni/magiskhide/proc_monitor.c @@ -76,6 +76,7 @@ static void lazy_unmount(const char* mountpoint) { static void hide_daemon(int pid, int ppid) { LOGD("hide_daemon: start unmount for pid=[%d]\n", pid); + strcpy(argv0, "hide_daemon"); char *line, buffer[PATH_MAX]; struct vector mount_list;