diff --git a/jni/magiskhide.c b/jni/magiskhide.c index 3b2da2b10..13db9191f 100644 --- a/jni/magiskhide.c +++ b/jni/magiskhide.c @@ -56,6 +56,7 @@ int hideMagisk(int pid) { int res = syscall(SYS_setns, fd, 0); if(res == -1) return 3; + free(path); path = NULL; asprintf(&path, "/proc/%d/mounts", pid); FILE *mount_fp = fopen(path, "r"); @@ -63,6 +64,7 @@ int hideMagisk(int pid) { fprintf(stderr, "Error opening mount list!\n"); return 1; } + free(path); int mount_size; char **mount_list = file_to_str_arr(mount_fp, &mount_size), mountpoint[256], *sbstr; @@ -88,9 +90,9 @@ int hideMagisk(int pid) { } free(mount_list[i]); } - // Free memory free(mount_list); + return 0; } diff --git a/zip_static/META-INF/com/google/android/update-binary b/zip_static/META-INF/com/google/android/update-binary index 30434ba0c..ace5e44bd 100644 --- a/zip_static/META-INF/com/google/android/update-binary +++ b/zip_static/META-INF/com/google/android/update-binary @@ -185,7 +185,7 @@ repack_boot() { ########################################################################################## ui_print "****************************" -ui_print "Magisk v7 Boot Image Patcher" +ui_print "Magisk v8 Boot Image Patcher" ui_print "****************************" if [ ! -d "$INSTALLER/common" ]; then @@ -270,6 +270,7 @@ if (is_mounted /data); then rm -rf /data/busybox /data/magisk 2>/dev/null mkdir -p /data/busybox cp -af $BINDIR /data/magisk + cp -af $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /data/magisk chmod 755 /data/busybox /data/magisk /data/magisk/* chcon 'u:object_r:system_file:s0' /data/busybox /data/magisk /data/magisk/* /data/magisk/busybox --install -s /data/busybox @@ -279,6 +280,7 @@ else rm -rf /cache/data_bin 2>/dev/null mkdir -p /cache/data_bin cp -af $BINDIR /cache/data_bin + cp -af $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /cache/data_bin fi ########################################################################################## @@ -311,7 +313,10 @@ MAGISKLOOP=$LOOPDEVICE mkdir -p /magisk/.core/magiskhide 2>/dev/null cp -af $INSTALLER/common/magiskhide/. /magisk/.core/magiskhide -chmod 755 /magisk/.core/magiskhide /magisk/.core/magiskhide/* + +# Remove legacy SuperSU module +mkdir -p /magisk/zzsupersu +touch /magisk/zzsupersu/remove ########################################################################################## # Boot image patch @@ -336,92 +341,45 @@ dd if=$BOOTIMAGE of=$ORIGBOOT ui_print "- Unpacking boot image" unpack_boot $ORIGBOOT +# Restore ramdisk SUPERSU=false - if (! $NORESTORE); then - # Backups if [ -d ".backup" ]; then - ui_print "- Restoring ramdisk with backup" - cp -af .backup/* . + ui_print "- Restoring ramdisk with ramdisk backup" + cp -af .backup/. . rm -rf magisk init.magisk.rc sbin/magic_mask.sh 2>/dev/null else - if [ -f "sbin/launch_daemonsu.sh" ]; then - SUPERSU=true - # Save it for helper module - mkdir -p /magisk/zzsupersu - touch /magisk/zzsupersu/stub - cp -af sbin/launch_daemonsu.sh $INSTALLER/common/roothelper/launch_daemonsu.sh - fi - # Non-standard boot image restores + [ -f "sbin/launch_daemonsu.sh" ] && SUPERSU=true if ($SUPERSU); then - ui_print "- SuperSU patched boot detected" - # Restore with SuperSU's backup - MOUNTSU=false - (! is_mounted /su) && (is_mounted /data) && mount_image /data/su.img /su && MOUNTSU=true - if (is_mounted /su); then - # Use sukernel's built-in functions - ui_print "- Using sukernel to restore boot image" - cd $UNPACKDIR - gunzip -c < $UNPACKDIR/ramdisk.gz > suramdisk - /su/bin/sukernel --restore suramdisk /data/stock_boot.img - if [ "$?" -ne "0" ]; then - # No boot backup found, use ramdisk backup - ui_print "- Restoring ramdisk with backup" - /su/bin/sukernel --cpio-restore suramdisk suramdisk - rm -rf $RAMDISK - mkdir -p $RAMDISK - cd $RAMDISK - cpio -i < $UNPACKDIR/suramdisk - rm -f $UNPACKDIR/suramdisk - else - ui_print "- Restoring boot image with backup" - cp -af /data/stock_boot.img $ORIGBOOT - unpack_boot $ORIGBOOT - fi - if ($MOUNTSU); then - ui_print "- Unmounting su.img" - umount /su - losetup -d $LOOPDEVICE - fi - else - # Find the boot backup ourselves - ui_print "! su.img mount failed... find the backup ourselves" - cp -af /data/stock_boot_*.gz /data/stock_boot.img.gz 2>/dev/null - gzip -d /data/stock_boot.img.gz 2>/dev/null - rm -rf /data/stock_boot.img.gz 2>/dev/null - if [ -f "/data/stock_boot.img" ]; then - ui_print "- Restoring boot image with backup" - cp -af /data/stock_boot.img $ORIGBOOT - unpack_boot $ORIGBOOT - else - ui_print "! No backups found" - ui_print "! Installer will still proceed, but might cause issues" - ui_print "! If possible, please restore to stock boot then flash Magisk again" - # Force removing SuperSU parts - rm -rf su init.supersu.rc sbin/launch_daemonsu.sh 2>/dev/null - fi - fi - else - # Magisk's own situation - if [ -d "magisk" ]; then - cp -af /data/stock_boot_*.gz /data/stock_boot.img.gz 2>/dev/null - gzip -d /data/stock_boot.img.gz 2>/dev/null - if [ -f "/data/stock_boot.img" ]; then - ui_print "- Restoring boot image with backup" - cp -af /data/stock_boot.img $ORIGBOOT - unpack_boot $ORIGBOOT - else - ui_print "! No backups found" - ui_print "! Installer will still proceed, but might cause issues" - ui_print "! If possible, please restore to stock boot then flash Magisk again" - # Removing other boot image modifications - rm -rf sbin/su init.xposed.rc sbin/mount_xposed.sh 2>/dev/null - fi - fi + ui_print "- SuperSU patched boot detected!" + ui_print "- Adding auto patch script for SuperSU" + cp -af $INSTALLER/common/custom_ramdisk_patch.sh /data/custom_ramdisk_patch.sh fi - ui_print "- Creating backups" - mkdir .backup - cp -af init.rc *fstab* verity_key sepolicy .backup 2>/dev/null + if [ -d "magisk" ]; then + # If Magisk is installed and no SuperSU and no ramdisk backups + # Restore previous stock boot image + if (! $SUPERSU); then + cp -af /data/stock_boot_*.gz /data/stock_boot.img.gz 2>/dev/null + gzip -d /data/stock_boot.img.gz 2>/dev/null + if [ -f "/data/stock_boot.img" ]; then + ui_print "- Restoring boot image with backup" + cp -af /data/stock_boot.img $ORIGBOOT + unpack_boot $ORIGBOOT + fi + fi + # Removing possible modifications + rm -rf magisk init.magisk.rc sbin/magic_mask.sh 2>/dev/null + rm -rf init.xposed.rc sbin/mount_xposed.sh 2>/dev/null + fi + fi +fi + +if (! $SUPERSU); then + ui_print "- Creating backups" + mkdir .backup 2>/dev/null + cp -af init.environ.rc *fstab* verity_key sepolicy .backup 2>/dev/null + if (! $SUPERSU); then + # SuperSU already backup stock boot, no need to do again if (is_mounted /data); then cp -af $ORIGBOOT /data/stock_boot.img else @@ -430,30 +388,45 @@ if (! $NORESTORE); then fi fi -ui_print "- Installing root helper module" -cp -af $INSTALLER/common/roothelper /magisk/00roothelper -chmod 755 /magisk/00roothelper /magisk/00roothelper/* - # Patch ramdisk ui_print "- Patching ramdisk" -if [ $(grep -c "import /init.magisk.rc" init.rc) -eq "0" ]; then - sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" init.rc -fi - -sed -i "/selinux.reload_policy/d" init.rc -find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do - if (! $KEEPVERITY); then - sed -i "s/,support_scfs//g" $FSTAB - sed -i 's;,\{0,1\}verify\(=[^,]*\)\{0,1\};;g' $FSTAB - fi - if (! $KEEPFORCEENCRYPT); then - sed -i "s/forceencrypt/encryptable/g" $FSTAB - sed -i "s/forcefdeorfbe/encryptable/g" $FSTAB +# Add magisk entrypoint +for INIT in init*.rc; do + if [ $(grep -c "import /init.environ.rc" $INIT) -ne "0" ] && [ $(grep -c "import /init.magisk.rc" $INIT) -eq "0" ]; then + cp $INIT .backup + sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $INIT + break fi done -if (! $KEEPVERITY); then - rm verity_key 2>/dev/null + +# Add magisk PATH +if [ $(grep -c "export PATH" init.environ.rc) -eq "0" ]; then + sed -i "/on init/a\ \ \ \ export PATH /magisk/.core/bin:/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin:/magisk/.core/busybox" init.environ.rc +else + if [ $(grep -c "/magisk/.core/busybox" init.environ.rc) -eq "0" ]; then + sed -i "/export PATH/ s/\/system\/xbin/\/system\/xbin:\/magisk\/.core\/busybox/g" init.environ.rc + fi + if [ $(grep -c "/magisk/.core/bin" init.environ.rc) -eq "0" ] && (! $SUPERSU); then + sed -i "/export PATH/ s/\/sbin/\/magisk\/.core\/bin:\/sbin/g" init.environ.rc + fi +fi + +if (! $SUPERSU); then + sed -i "/selinux.reload_policy/d" init.rc + find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do + if (! $KEEPVERITY); then + sed -i "s/,support_scfs//g" $FSTAB + sed -i 's;,\{0,1\}verify\(=[^,]*\)\{0,1\};;g' $FSTAB + fi + if (! $KEEPFORCEENCRYPT); then + sed -i "s/forceencrypt/encryptable/g" $FSTAB + sed -i "s/forcefdeorfbe/encryptable/g" $FSTAB + fi + done + if (! $KEEPVERITY); then + rm verity_key 2>/dev/null + fi fi # sepolicy patches @@ -464,7 +437,7 @@ mkdir -p magisk 2>/dev/null cp -af $INSTALLER/common/init.magisk.rc init.magisk.rc cp -af $INSTALLER/common/magic_mask.sh sbin/magic_mask.sh -# chmod 0755 magisk +chmod 0755 magisk chmod 0750 init.magisk.rc sbin/magic_mask.sh ui_print "- Repacking boot image" diff --git a/zip_static/arm/magiskhide b/zip_static/arm/magiskhide index 887ba5627..2b99f7098 100644 Binary files a/zip_static/arm/magiskhide and b/zip_static/arm/magiskhide differ diff --git a/zip_static/common/custom_ramdisk_patch.sh b/zip_static/common/custom_ramdisk_patch.sh new file mode 100644 index 000000000..a1eeeeb0b --- /dev/null +++ b/zip_static/common/custom_ramdisk_patch.sh @@ -0,0 +1,53 @@ +#!/system/bin/sh + +RAMDISK=$1 +BINDIR=/data/magisk + +cpio_add() { + /su/bin/sukernel --cpio-add $RAMDISK $RAMDISK $2 $1 $1 +} + +cpio_extract() { + /su/bin/sukernel --cpio-extract $RAMDISK $1 $1 +} + +cpio_mkdir() { + /su/bin/sukernel --cpio-mkdir $RAMDISK $RAMDISK $2 $1 +} + +rm -rf /tmp/magisk/ramdisk 2>/dev/null +mkdir -p /tmp/magisk/ramdisk +cd /tmp/magisk/ramdisk + +cat $RAMDISK | cpio -i + +# Patch ramdisk +echo "- Patching ramdisk" + +# Add magisk entrypoint +for INIT in init*.rc; do + if [ $(grep -c "import /init.environ.rc" $INIT) -ne "0" ] && [ $(grep -c "import /init.magisk.rc" $INIT) -eq "0" ]; then + sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $INIT + cpio_add $INIT 750 + break + fi +done + +# Add magisk PATH +if [ $(grep -c "/magisk/.core/busybox" init.environ.rc) -eq "0" ]; then + sed -i "/export PATH/ s/\/system\/xbin/\/system\/xbin:\/magisk\/.core\/busybox/g" init.environ.rc + cpio_add init.environ.rc 750 +fi + +# sepolicy patches +$BINDIR/sepolicy-inject --magisk -P sepolicy +cpio_add sepolicy 644 + +# Add new items +mkdir -p magisk 2>/dev/null +cp -af $BINDIR/init.magisk.rc init.magisk.rc +cp -af $BINDIR/magic_mask.sh sbin/magic_mask.sh + +cpio_mkdir magisk 755 +cpio_add init.magisk.rc 750 +cpio_add sbin/magic_mask.sh 750 diff --git a/zip_static/common/init.magisk.rc b/zip_static/common/init.magisk.rc index f04ab6133..bc0b4e101 100644 --- a/zip_static/common/init.magisk.rc +++ b/zip_static/common/init.magisk.rc @@ -1,9 +1,6 @@ # Triggers on post-fs - # Paths - export PATH /magisk/.core/bin:/sbin:/vendor/bin:/system/sbin:/system/bin:/magisk/.core/busybox:/system/xbin - start magisk_pfs wait /dev/.magisk.unblock 20 rm /dev/.magisk.unblock diff --git a/zip_static/common/magic_mask.sh b/zip_static/common/magic_mask.sh index 36a75383a..0d8a904f5 100644 --- a/zip_static/common/magic_mask.sh +++ b/zip_static/common/magic_mask.sh @@ -1,6 +1,7 @@ #!/system/bin/sh LOGFILE=/cache/magisk.log +HIDELOG=/cache/magiskhide.log IMG=/data/magisk.img export MOUNTPOINT=/magisk @@ -193,6 +194,7 @@ merge_image() { # Merge (will reserve selinux contexts) cd /cache/merge_img for MOD in *; do + log_print "Merging: $MOD" rm -rf /cache/data_img/$MOD cp -afc $MOD /cache/data_img/ done @@ -227,8 +229,9 @@ case $1 in if [ -d "/cache/magisk_merge" ]; then cd /cache/magisk_merge for MOD in *; do + log_print "Merging: $MOD" rm -rf /cache/magisk/$MOD - cp -afc $MOD /cache/magisk/ + mv $MOD /cache/magisk/$MOD done rm -rf /cache/magisk_merge fi @@ -299,8 +302,6 @@ case $1 in unblock fi - echo $MOUNTPOINT >> $MOUNTLIST - log_print "Preparing modules" # First do cleanups rm -rf $DUMMDIR @@ -404,7 +405,7 @@ case $1 in service ) # Version info - setprop magisk.version 7 + setprop magisk.version 8 log_print "Magisk late_start service mode running..." run_scripts service [ -f "$COREDIR/magiskhide/enable" ] && setprop magisk.hide 1 @@ -420,8 +421,9 @@ case $1 in cat $COREDIR/magiskhide/hidelist.tmp >> $COREDIR/magiskhide/hidelist rm -f $COREDIR/magiskhide/hidelist.tmp fi + chmod 755 $COREDIR/magiskhide $COREDIR/magiskhide/* log_print "Starting Magisk Hide" - exec /data/magisk/magiskhide $COREDIR/magiskhide/hidelist + exec /data/magisk/magiskhide $COREDIR/magiskhide/hidelist > $HIDELOG ;; esac diff --git a/zip_static/common/roothelper/phh.prop b/zip_static/common/roothelper/phh.prop deleted file mode 100644 index 41af7f87e..000000000 --- a/zip_static/common/roothelper/phh.prop +++ /dev/null @@ -1,9 +0,0 @@ -id=phh -name=phh's SuperUser -version=Root Helper -versionCode=1 -author=phhusson, topjohnwu -description=This is a helper, please upgrade from downloads section :) -support=http://forum.xda-developers.com/showthread.php?t=3216394 -donate=http://forum.xda-developers.com/donatetome.php?u=1915408 -cacheModule=false diff --git a/zip_static/common/roothelper/phh.sh b/zip_static/common/roothelper/phh.sh deleted file mode 100644 index 3a0a86a02..000000000 --- a/zip_static/common/roothelper/phh.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/system/bin/sh - -LOGFILE=/cache/magisk.log - -log_print() { - echo $1 - echo "phh: $1" >> $LOGFILE - log -p i -t phh "$1" -} - -launch_daemonsu() { - export PATH=$OLDPATH - # Switch contexts - echo "u:r:su_daemon:s0" > /proc/self/attr/current - # Start daemon - exec /magisk/phh/bin/su --daemon -} - -# Disable the other root -[ -d "/magisk/zzsupersu" ] && touch /magisk/zzsupersu/disable - -log_print "Live patching sepolicy" -/magisk/phh/bin/sepolicy-inject --live - -# Expose the root path -log_print "Linking supath" -rm -rf /magisk/.core/bin -ln -s /magisk/phh/bin /magisk/.core/bin - -# Run su.d -for script in /magisk/phh/su.d/* ; do - if [ -f "$script" ]; then - chmod 755 $script - log_print "su.d: $script" - $script - fi -done - -log_print "Starting su daemon" -(launch_daemonsu &) diff --git a/zip_static/common/roothelper/post-fs-data.sh b/zip_static/common/roothelper/post-fs-data.sh deleted file mode 100644 index 1b6fc1f29..000000000 --- a/zip_static/common/roothelper/post-fs-data.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/system/bin/sh - -cd /magisk/00roothelper - -if [ -f "launch_daemonsu.sh" ]; then - # SuperSU mode - rm -rf /magisk/supersu /magisk/zzsupersu - mkdir -p /magisk/zzsupersu - cp supersu.sh /magisk/zzsupersu/post-fs-data.sh - cp supersu.prop /magisk/zzsupersu/module.prop - cp launch_daemonsu.sh /magisk/zzsupersu/launch_daemonsu.sh - chmod 755 /magisk/zzsupersu /magisk/zzsupersu/* -else - # phh mode - if [ -f "/magisk/phh/su" ]; then - # Old version detected - cp /magisk/phh/su su - rm -rf /magisk/phh - mkdir -p /magisk/phh/bin - mkdir -p /magisk/phh/su.d - cp su /magisk/phh/bin/su - cp /data/magisk/sepolicy-inject /magisk/phh/bin/sepolicy-inject - cp phh.sh /magisk/phh/post-fs-data.sh - cp phh.prop /magisk/phh/module.prop - chmod 755 /magisk/phh /magisk/phh/* /magisk/phh/bin/* - fi -fi - -rm -rf /magisk/00roothelper diff --git a/zip_static/common/roothelper/supersu.prop b/zip_static/common/roothelper/supersu.prop deleted file mode 100644 index 5d407e4d1..000000000 --- a/zip_static/common/roothelper/supersu.prop +++ /dev/null @@ -1,7 +0,0 @@ -id=supersu -name=SuperSU Helper -version=v1 -versionCode=1 -author=topjohnwu -description=This is a helper module for Chainfire's SuperSU to work with Magisk -cacheModule=false diff --git a/zip_static/common/roothelper/supersu.sh b/zip_static/common/roothelper/supersu.sh deleted file mode 100644 index c64fa0748..000000000 --- a/zip_static/common/roothelper/supersu.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/system/bin/sh - -mount -o rw,remount rootfs / -mkdir /su 2>/dev/null -mount -o ro,remount rootfs / - -chmod 755 /magisk/zzsupersu/launch_daemonsu.sh -/magisk/zzsupersu/launch_daemonsu.sh post-fs-data - -rm -rf /magisk/.core/bin -ln -s /su/bin /magisk/.core/bin diff --git a/zip_static/x86/magiskhide b/zip_static/x86/magiskhide index e72fa8907..dc817bd58 100644 Binary files a/zip_static/x86/magiskhide and b/zip_static/x86/magiskhide differ