Magisk Hide: Unmount everything

This commit is contained in:
topjohnwu 2016-10-06 17:53:52 +08:00
parent 14946da163
commit aa54ef10ae
5 changed files with 109 additions and 59 deletions

View File

@ -1,4 +1,3 @@
# Magisk # Magisk
Static binaries included: Static binaries included:
* Busybox: http://forum.xda-developers.com/android/software-hacking/tool-busybox-flashable-archs-t3348543 * Busybox: http://forum.xda-developers.com/android/software-hacking/tool-busybox-flashable-archs-t3348543
* Open source su binary: https://github.com/seSuperuser/Superuser

View File

@ -17,9 +17,13 @@ typedef unsigned short int sa_family_t;
#include <sys/resource.h> #include <sys/resource.h>
#include <unistd.h> #include <unistd.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/inotify.h>
#define HIDE_LIST "/magisk/.core/hidelist" #define HIDE_LIST "/magisk/.core/magiskhide/hidelist"
#define MOUNT_LIST "/dev/mountlist"
int hide_size = 0, mount_size = 0;
char **hide_list, **mount_list;
time_t last_update = 0;
//WARNING: Calling this will change our current namespace //WARNING: Calling this will change our current namespace
//We don't care because we don't want to run from here anyway //We don't care because we don't want to run from here anyway
@ -33,74 +37,97 @@ int hideMagisk(int pid) {
int res = syscall(SYS_setns, fd, 0); int res = syscall(SYS_setns, fd, 0);
if(res == -1) return 3; if(res == -1) return 3;
res = mount("/magisk/.core/mirror/system", "/system", "bind", MS_BIND, ""); int i;
if(res == -1) return 4; for(i = mount_size - 1; i >= 0; --i) {
res = umount2(mount_list[i], MNT_DETACH);
if (res != -1) printf("Unmounted: %s\n", mount_list[i]);
else printf("Failed: %s\n", mount_list[i]);
}
res = umount2("/magisk", MNT_DETACH); res = umount2("/magisk", MNT_DETACH);
if(res == -1) return 4; if (res != -1) printf("Unmounted: %s\n", "/magisk");
else printf("Failed: %s\n", "/magisk");
return 0; return 0;
} }
int loadList(int fd, char ***list, int *line, time_t *last_update) { char** file_to_str_arr(const int fd, int *size) {
int allocated = 16, i; int allocated = 16;
char *buffer, *tok; char *buffer, *tok, **array;
struct stat file_stat;
fstat(fd, &file_stat);
if (file_stat.st_mtime == *last_update) {
return 0;
}
off_t filesize = lseek(fd, 0, SEEK_END); off_t filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
buffer = malloc(sizeof(char) * filesize); buffer = malloc(sizeof(char) * filesize);
lseek(fd, 0, SEEK_SET);
read(fd, buffer, filesize); read(fd, buffer, filesize);
fstat(fd, &file_stat);
*last_update = file_stat.st_mtime;
// Free memory array = (char **) malloc(sizeof(char*) * allocated);
for (; *line >= 0; --(*line))
free((*list)[*line]);
*line = 0;
*list = (char **) malloc(sizeof(char*) * allocated);
*size = 0;
tok = strtok(buffer, "\r\n"); tok = strtok(buffer, "\r\n");
while (tok != NULL) { while (tok != NULL) {
if (*line >= allocated) { if (*size >= allocated) {
// Double our allocation and re-allocate // Double our allocation and re-allocate
allocated = allocated * 2; allocated = allocated * 2;
*list = (char **) realloc((*list), sizeof(char*) * allocated); array = (char **) realloc(array, sizeof(char*) * allocated);
} }
if (strlen(tok)) { if (strlen(tok)) {
(*list)[*line] = malloc(strlen(tok)); array[*size] = malloc(strlen(tok));
strcpy((*list)[*line], tok); strcpy(array[*size], tok);
++(*line); ++(*size);
} }
tok = strtok(NULL, "\r\n"); tok = strtok(NULL, "\r\n");
} }
free(buffer);
return array;
}
int load_hide_list(const int fd) {
int i;
struct stat file_stat;
fstat(fd, &file_stat);
if (file_stat.st_mtime == last_update) {
return 0;
}
// Free memory
for(i = 0; i < hide_size; ++i)
free(hide_list[i]);
free(hide_list);
hide_list = file_to_str_arr(fd, &hide_size);
fstat(fd, &file_stat);
last_update = file_stat.st_mtime;
printf("Get package name from config:\n"); printf("Get package name from config:\n");
for(i = 0; i < *line; i++) for(i = 0; i < hide_size; i++)
printf("%s\n", (*list)[i]); printf("%s\n", hide_list[i]);
printf("\n"); printf("\n");
free(buffer);
} }
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {
int line = -1, i; int i;
char **list;
time_t last_update = 0;
int fd = open(HIDE_LIST, O_RDONLY); int hide_fd = open(HIDE_LIST, O_RDONLY);
if (fd == -1){ if (hide_fd == -1){
printf("Error opening file\n"); printf("Error opening hide list\n");
exit(1); exit(1);
} }
int mount_fd = open(MOUNT_LIST, O_RDONLY);
if (mount_fd == -1){
printf("Error opening mount list\n");
exit(1);
}
mount_list = file_to_str_arr(mount_fd, &mount_size);
close(mount_fd);
char buffer[512]; char buffer[512];
FILE *p = popen("while true;do logcat -b events -v raw -s am_proc_start;sleep 1;done", "r"); FILE *p = popen("while true;do logcat -b events -v raw -s am_proc_start;sleep 1;done", "r");
while(!feof(p)) { while(!feof(p)) {
loadList(fd, &list, &line, &last_update); load_hide_list(hide_fd);
//Format of am_proc_start is (as of Android 5.1 and 6.0) //Format of am_proc_start is (as of Android 5.1 and 6.0)
//UserID, pid, unix uid, processName, hostingType, hostingName //UserID, pid, unix uid, processName, hostingType, hostingName
@ -126,19 +153,26 @@ int main(int argc, char **argv, char **envp) {
if(ret != 6) { if(ret != 6) {
continue; continue;
} }
for (i = 0; i < line; ++i) { for (i = 0; i < hide_size; ++i) {
if(strstr(processName, list[i]) != NULL) { if(strstr(processName, hide_list[i]) != NULL) {
printf("Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid); printf("Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid);
hideMagisk(pid); hideMagisk(pid);
break;
} }
} }
} }
close(fd);
pclose(p); pclose(p);
for (; line >= 0; line--) close(hide_fd);
free(list[line]);
free(list); // Free memory
for(i = 0; i < hide_size; ++i)
free(hide_list[i]);
free(hide_list);
for(i = 0; i < mount_size; ++i)
free(mount_list[i]);
free(mount_list);
return 0; return 0;
} }

Binary file not shown.

View File

@ -2,8 +2,11 @@
LOGFILE=/cache/magisk.log LOGFILE=/cache/magisk.log
IMG=/data/magisk.img IMG=/data/magisk.img
MOUNTLIST=/dev/mountlist
COREDIR=/magisk/.core MOUNTPOINT=/magisk
COREDIR=$MOUNTPOINT/.core
DUMMDIR=$COREDIR/dummy DUMMDIR=$COREDIR/dummy
MIRRDIR=$COREDIR/mirror MIRRDIR=$COREDIR/mirror
@ -38,7 +41,7 @@ unblock() {
} }
run_scripts() { run_scripts() {
BASE=/magisk BASE=$MOUNTPOINT
if [ "$1" = "post-fs" ]; then if [ "$1" = "post-fs" ]; then
BASE=/cache/magisk BASE=/cache/magisk
fi fi
@ -137,8 +140,12 @@ travel() {
bind_mount() { bind_mount() {
if [ -e "$1" -a -e "$2" ]; then if [ -e "$1" -a -e "$2" ]; then
mount -o bind $1 $2 mount -o bind $1 $2
if [ "$?" -eq "0" ]; then log_print "Mount: $1"; if [ "$?" -eq "0" ]; then
else log_print "Mount Fail: $1"; fi log_print "Mount: $1"
echo $2 >> $MOUNTLIST
else
log_print "Mount Fail: $1"
fi
fi fi
} }
@ -260,6 +267,8 @@ case $1 in
# Live patch sepolicy # Live patch sepolicy
/data/magisk/sepolicy-inject --live -s su /data/magisk/sepolicy-inject --live -s su
[ ! -d "$MOUNTPOINT" ] && mkdir -p $MOUNTPOINT
# Cache support # Cache support
if [ -d "/cache/data_bin" ]; then if [ -d "/cache/data_bin" ]; then
rm -rf /data/busybox /data/magisk rm -rf /data/busybox /data/magisk
@ -280,14 +289,14 @@ case $1 in
merge_image /data/magisk_merge.img merge_image /data/magisk_merge.img
# Mount magisk.img # Mount magisk.img
if [ `cat /proc/mounts | grep /magisk >/dev/null 2>&1; echo $?` -ne 0 ]; then if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then
loopsetup $IMG loopsetup $IMG
if [ ! -z "$LOOPDEVICE" ]; then if [ ! -z "$LOOPDEVICE" ]; then
mount -t ext4 -o rw,noatime $LOOPDEVICE /magisk mount -t ext4 -o rw,noatime $LOOPDEVICE $MOUNTPOINT
fi fi
fi fi
if [ `cat /proc/mounts | grep /magisk >/dev/null 2>&1; echo $?` -ne 0 ]; then if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then
log_print "magisk.img mount failed, nothing to do :(" log_print "magisk.img mount failed, nothing to do :("
unblock unblock
fi fi
@ -295,14 +304,14 @@ case $1 in
log_print "Preparing modules" log_print "Preparing modules"
# First do cleanups # First do cleanups
rm -rf $DUMMDIR rm -rf $DUMMDIR
rmdir $(find /magisk -type d -depth ! -path "*core*" ) 2>/dev/null rmdir $(find $MOUNTPOINT -type d -depth ! -path "*core*" ) 2>/dev/null
rm -rf $COREDIR/bin rm -rf $COREDIR/bin
mkdir -p $DUMMDIR mkdir -p $DUMMDIR
mkdir -p $MIRRDIR/system mkdir -p $MIRRDIR/system
# Travel through all mods # Travel through all mods
for MOD in /magisk/* ; do for MOD in $MOUNTPOINT/* ; do
if [ -f "$MOD/remove" ]; then if [ -f "$MOD/remove" ]; then
log_print "Remove module: $MOD" log_print "Remove module: $MOD"
rm -rf $MOD rm -rf $MOD
@ -325,7 +334,7 @@ case $1 in
fi fi
# Unmount, shrink, remount # Unmount, shrink, remount
if [ `umount /magisk >/dev/null 2>&1; echo $?` -eq 0 ]; then if [ `umount $MOUNTPOINT >/dev/null 2>&1; echo $?` -eq 0 ]; then
losetup -d $LOOPDEVICE losetup -d $LOOPDEVICE
target_size_check $IMG target_size_check $IMG
NEWDATASIZE=$(((curUsedM / 32 + 2) * 32)) NEWDATASIZE=$(((curUsedM / 32 + 2) * 32))
@ -335,17 +344,17 @@ case $1 in
fi fi
loopsetup $IMG loopsetup $IMG
if [ ! -z "$LOOPDEVICE" ]; then if [ ! -z "$LOOPDEVICE" ]; then
mount -t ext4 -o rw,noatime $LOOPDEVICE /magisk mount -t ext4 -o rw,noatime $LOOPDEVICE $MOUNTPOINT
fi fi
fi fi
if [ `cat /proc/mounts | grep /magisk >/dev/null 2>&1; echo $?` -ne 0 ]; then if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then
log_print "magisk.img mount failed, nothing to do :(" log_print "magisk.img mount failed, nothing to do :("
unblock unblock
fi fi
# Remove crap folder # Remove crap folder
rm -rf /magisk/lost+found rm -rf $MOUNTPOINT/lost+found
# Start doing tasks # Start doing tasks
@ -400,8 +409,16 @@ case $1 in
run_scripts service run_scripts service
# Enable magiskhide # Enable magiskhide
[ ! -f "$COREDIR/magiskhide/hidelist" ] && mktouch $COREDIR/magiskhide/hidelist
# Add preset for Safety Net
if [ $(grep -c "com.google.android.gms.unstable" $COREDIR/magiskhide/hidelist) -eq "0" ]; then
mv $COREDIR/magiskhide/hidelist $COREDIR/magiskhide/hidelist.tmp
echo "com.google.android.gms.unstable" > $COREDIR/magiskhide/hidelist
cat $COREDIR/magiskhide/hidelist.tmp >> $COREDIR/magiskhide/hidelist
fi
log_print "Starting Magisk Hide" log_print "Starting Magisk Hide"
/data/magisk/magiskhide & (/data/magisk/magiskhide &)
;; ;;
esac esac

Binary file not shown.