Properly handle factory resets

Close #3345
This commit is contained in:
topjohnwu 2020-11-04 04:49:43 -08:00
parent 1e0f96d0fd
commit e21a78164e
2 changed files with 60 additions and 57 deletions

View File

@ -57,51 +57,10 @@ else if (MNT_DIR_IS(dir) && me->mnt_type != "tmpfs"sv) { \
#define link_orig(part) link_orig_dir("/" #part, part) #define link_orig(part) link_orig_dir("/" #part, part)
static bool magisk_env() { static void mount_mirrors() {
LOGI("* Initializing Magisk environment\n");
string pkg;
check_manager(&pkg);
char buf1[4096]; char buf1[4096];
char buf2[4096]; char buf2[4096];
sprintf(buf1, "%s/0/%s/install", APP_DATA_DIR, pkg.data());
// Alternative binaries paths
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk", buf1 };
for (auto alt : alt_bin) {
struct stat st;
if (lstat(alt, &st) == 0) {
if (S_ISLNK(st.st_mode)) {
unlink(alt);
continue;
}
rm_rf(DATABIN);
cp_afc(alt, DATABIN);
rm_rf(alt);
break;
}
}
// Remove stuffs
rm_rf("/cache/data_adb");
rm_rf("/data/adb/modules/.core");
unlink("/data/adb/magisk.img");
unlink("/data/adb/magisk_merge.img");
unlink("/data/magisk.img");
unlink("/data/magisk_merge.img");
unlink("/data/magisk_debug.log");
sprintf(buf1, "%s/" MODULEMNT, MAGISKTMP.data());
xmkdir(buf1, 0755);
// Directories in /data/adb
xmkdir(DATABIN, 0755);
xmkdir(MODULEROOT, 0755);
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
LOGI("* Mounting mirrors"); LOGI("* Mounting mirrors");
parse_mnt("/proc/mounts", [&](mntent *me) { parse_mnt("/proc/mounts", [&](mntent *me) {
@ -137,6 +96,48 @@ static bool magisk_env() {
link_mirror(vendor) link_mirror(vendor)
link_mirror(product) link_mirror(product)
link_mirror(system_ext) link_mirror(system_ext)
}
static bool magisk_env() {
char buf[4096];
LOGI("* Initializing Magisk environment\n");
string pkg;
check_manager(&pkg);
sprintf(buf, "%s/0/%s/install", APP_DATA_DIR, pkg.data());
// Alternative binaries paths
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk", buf };
for (auto alt : alt_bin) {
struct stat st;
if (lstat(alt, &st) == 0) {
if (S_ISLNK(st.st_mode)) {
unlink(alt);
continue;
}
rm_rf(DATABIN);
cp_afc(alt, DATABIN);
rm_rf(alt);
break;
}
}
// Remove stuffs
rm_rf("/cache/data_adb");
rm_rf("/data/adb/modules/.core");
unlink("/data/adb/magisk.img");
unlink("/data/adb/magisk_merge.img");
unlink("/data/magisk.img");
unlink("/data/magisk_merge.img");
unlink("/data/magisk_debug.log");
// Directories in /data/adb
xmkdir(DATABIN, 0755);
xmkdir(MODULEROOT, 0755);
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
// Disable/remove magiskhide, resetprop // Disable/remove magiskhide, resetprop
if (SDK_INT < 19) { if (SDK_INT < 19) {
@ -149,10 +150,10 @@ static bool magisk_env() {
// TODO: Remove. Backwards compatibility for old manager // TODO: Remove. Backwards compatibility for old manager
LOGI("* Setting up internal busybox\n"); LOGI("* Setting up internal busybox\n");
sprintf(buf1, "%s/" BBPATH "/busybox", MAGISKTMP.data()); sprintf(buf, "%s/" BBPATH "/busybox", MAGISKTMP.data());
mkdir(dirname(buf1), 0755); mkdir(dirname(buf), 0755);
cp_afc(DATABIN "/busybox", buf1); cp_afc(DATABIN "/busybox", buf);
exec_command_sync(buf1, "--install", "-s", dirname(buf1)); exec_command_sync(buf, "--install", "-s", dirname(buf));
return true; return true;
} }
@ -307,25 +308,25 @@ void post_fs_data(int client) {
LOGI("** post-fs-data mode running\n"); LOGI("** post-fs-data mode running\n");
// Unlock all blocks for rw
unlock_blocks(); unlock_blocks();
mount_mirrors();
if (access(SECURE_DIR, F_OK) != 0) { if (access(SECURE_DIR, F_OK) != 0) {
if (SDK_INT < 24) { if (SDK_INT < 24) {
// There is no FBE pre 7.0, we can directly create the folder without issues // There is no FBE pre 7.0, we can directly create the folder without issues
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);
} else { } else {
/* If the folder is not automatically created by Android, // If the folder is not automatically created by Android,
* do NOT proceed further. Manual creation of the folder // do NOT proceed further. Manual creation of the folder
* will cause bootloops on FBE devices. */ // will cause bootloops on FBE devices.
LOGE(SECURE_DIR " is not present, abort...\n"); LOGE(SECURE_DIR " is not present, abort\n");
goto unblock_init; goto early_abort;
} }
} }
if (!magisk_env()) { if (!magisk_env()) {
LOGE("* Magisk environment setup incomplete, abort\n"); LOGE("* Magisk environment incomplete, abort\n");
goto unblock_init; goto early_abort;
} }
if (getprop("persist.sys.safemode", true) == "1" || check_key_combo()) { if (getprop("persist.sys.safemode", true) == "1" || check_key_combo()) {
@ -339,9 +340,11 @@ void post_fs_data(int client) {
handle_modules(); handle_modules();
} }
pfs_done = true;
early_abort:
// We still do magic mount because root itself might need it // We still do magic mount because root itself might need it
magic_mount(); magic_mount();
pfs_done = true;
unblock_init: unblock_init:
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0)); close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
@ -373,8 +376,7 @@ void boot_complete(int client) {
if (safe_mode) if (safe_mode)
return; return;
/* It's safe to create the folder at this point if the system didn't create it // At this point it's safe to create the folder
* Magisk Manager should finish up the remaining environment setup */
if (access(SECURE_DIR, F_OK) != 0) if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);

View File

@ -636,6 +636,7 @@ static void prepare_modules() {
// Setup module mount (workaround nosuid selabel issue) // Setup module mount (workaround nosuid selabel issue)
auto src = MAGISKTMP + "/" MIRRDIR MODULEROOT; auto src = MAGISKTMP + "/" MIRRDIR MODULEROOT;
auto dest = MAGISKTMP + "/" MODULEMNT; auto dest = MAGISKTMP + "/" MODULEMNT;
xmkdir(dest.data(), 0755);
bind_mount(src.data(), dest.data()); bind_mount(src.data(), dest.data());
restorecon(); restorecon();