From 298f09402f1cb722b45fd440d157b3d86341742a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 1 Feb 2017 06:02:43 +0800 Subject: [PATCH] Introduce MagiskSU --- .gitignore | 3 ++ build.cmd | 8 ++- build.sh | 8 ++- scripts/custom_ramdisk_patch.sh | 5 +- scripts/flash_script.sh | 53 ++++++++----------- {zip_static/common => scripts}/init.magisk.rc | 0 scripts/magic_mask.sh | 29 +++++----- scripts/magisksu.sh | 28 ++++++++++ zip_static/common/phh/module.prop | 8 --- zip_static/common/phh/service.sh | 46 ---------------- 10 files changed, 82 insertions(+), 106 deletions(-) rename {zip_static/common => scripts}/init.magisk.rc (100%) create mode 100644 scripts/magisksu.sh delete mode 100644 zip_static/common/phh/module.prop delete mode 100644 zip_static/common/phh/service.sh diff --git a/.gitignore b/.gitignore index 270fa6a43..5cccaa97d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,9 @@ ziptools/zipadjust # Generated scripts zip_static/common/magic_mask.sh +zip_static/common/magisksu.sh +zip_static/common/init.magisk.rc +zip_static/common/custom_ramdisk_patch.sh zip_static/META-INF/com/google/android/update-binary # Leave all busybox! diff --git a/build.cmd b/build.cmd index c84ee257f..24164f764 100644 --- a/build.cmd +++ b/build.cmd @@ -53,11 +53,11 @@ EXIT /B %ERRORLEVEL% ECHO ************************ ECHO * Copying binaries ECHO ************************ - COPY /Y libs\armeabi\* zip_static\arm + COPY /Y libs\armeabi-v7a\* zip_static\arm COPY /Y libs\arm64-v8a\* zip_static\arm64 COPY /Y libs\x86\* zip_static\x86 COPY /Y libs\x86_64\* zip_static\x64 - CALL :mkcp libs\armeabi\bootimgtools uninstaller\arm + CALL :mkcp libs\armeabi-v7a\bootimgtools uninstaller\arm CALL :mkcp libs\arm64-v8a\bootimgtools uninstaller\arm64 CALL :mkcp libs\x86\bootimgtools uninstaller\x86 CALL :mkcp libs\x86_64\bootimgtools uninstaller\x64 @@ -75,6 +75,8 @@ EXIT /B %ERRORLEVEL% forfiles /P zip_static\x64 /C "cmd /C IF NOT @file == \"busybox\" DEL @file" 2>NUL DEL zip_static\META-INF\com\google\android\update-binary 2>NUL DEL zip_static\common\custom_ramdisk_patch.sh + 2>NUL DEL zip_static\common\magisksu.sh + 2>NUL DEL zip_static\common\init.magisk.rc 2>NUL DEL zip_static\common\magic_mask.sh 2>NUL RMDIR /S /Q uninstaller\arm 2>NUL RMDIR /S /Q uninstaller\arm64 @@ -100,6 +102,8 @@ EXIT /B %ERRORLEVEL% ECHO * Zipping Magisk v%~1 ECHO ************************ COPY /Y scripts\custom_ramdisk_patch.sh zip_static\common\custom_ramdisk_patch.sh + COPY /Y scripts\magisksu.sh zip_static\common\magisksu.sh + COPY /Y scripts\init.magisk.rc zip_static\common\init.magisk.rc CD zip_static 2>NUL DEL "..\Magisk-v%~1.zip" ..\ziptools\win_bin\zip "..\Magisk-v%~1.zip" -r . diff --git a/build.sh b/build.sh index 2dcb2ba4c..a72a57317 100755 --- a/build.sh +++ b/build.sh @@ -26,6 +26,8 @@ cleanup() { ls zip_static/x64/* | grep -v "busybox" | xargs rm -rfv rm -rfv zip_static/META-INF/com/google/android/update-binary rm -rfv zip_static/common/custom_ramdisk_patch.sh + rm -rfv zip_static/common/magisksu.sh + rm -rfv zip_static/common/init.magisk.rc rm -rfv zip_static/common/magic_mask.sh rm -rfv uninstaller/arm rm -rfv uninstaller/arm64 @@ -52,8 +54,8 @@ build_bin() { echo "************************" echo "* Copying binaries" echo "************************" - mkcp "libs/armeabi/*" zip_static/arm - mkcp libs/armeabi/bootimgtools uninstaller/arm + mkcp "libs/armeabi-v7a/*" zip_static/arm + mkcp libs/armeabi-v7a/bootimgtools uninstaller/arm mkcp "libs/arm64-v8a/*" zip_static/arm64 mkcp libs/arm64-v8a/bootimgtools uninstaller/arm64 mkcp "libs/x86/*" zip_static/x86 @@ -73,6 +75,8 @@ zip_package() { echo "* Zipping Magisk v$1" echo "************************" cp -afv scripts/custom_ramdisk_patch.sh zip_static/common/custom_ramdisk_patch.sh + cp -afv scripts/magisksu.sh zip_static/common/magisksu.sh + cp -afv scripts/init.magisk.rc zip_static/common/init.magisk.rc cd zip_static find . -type f -exec chmod 644 {} \; find . -type d -exec chmod 755 {} \; diff --git a/scripts/custom_ramdisk_patch.sh b/scripts/custom_ramdisk_patch.sh index f0cb5f5de..9191faf7a 100644 --- a/scripts/custom_ramdisk_patch.sh +++ b/scripts/custom_ramdisk_patch.sh @@ -1,8 +1,9 @@ #!/system/bin/sh RAMDISK=$1 -BINDIR=$2 -[ -z $BINDIR ] && BINDIR=/data/magisk +BINDIR=/data/magisk +[ ! -e $BINDIR ] && BINDIR=/cache/data_bin +[ ! -e $BINDIR ] && exit SYSTEMLIB=/system/lib [ -d /system/lib64 ] && SYSTEMLIB=/system/lib64 diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index 4437e08be..17ffb0316 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -321,25 +321,25 @@ ui_print "- Constructing environment" 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 + mkdir -p /data/busybox /data/magisk + cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop \ + $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /data/magisk cp -af $INSTALLER/common/magisk.apk /data/magisk.apk /data/magisk/busybox --install -s /data/busybox ln -s /data/magisk/busybox /data/busybox/busybox # Prevent issues rm -f /data/busybox/su /data/busybox/sh /data/busybox/reboot - chcon -hR "u:object_r:system_file:s0" /data/magisk /data/busybox + chcon -hR u:object_r:system_file:s0 /data/magisk /data/busybox chmod -R 755 /data/magisk /data/busybox - PATH=/data/busybox:$PATH - BINDIR=/data/magisk + # PATH=/data/busybox:$PATH else rm -rf /cache/data_bin 2>/dev/null - cp -af $BINDIR /cache/data_bin - cp -af $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /cache/data_bin + mkdir -p /cache/data_bin + cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop \ + $INSTALLER/common/custom_ramdisk_patch.sh $INSTALLER/common/init.magisk.rc \ + $INSTALLER/common/magic_mask.sh /cache/data_bin cp -af $INSTALLER/common/magisk.apk /cache/magisk.apk chmod -R 755 /cache/data_bin - BINDIR=/cache/data_bin fi ########################################################################################## @@ -370,8 +370,11 @@ if (! is_mounted /magisk); then fi MAGISKLOOP=$LOOPDEVICE -mkdir -p /magisk/.core/magiskhide 2>/dev/null -cp -af $INSTALLER/common/magiskhide/. /magisk/.core/magiskhide +# Core folders and scripts +mkdir -p $COREDIR/magiskhide $COREDIR/postfsdata.d $COREDIR/service.d 2>/dev/null +cp -af $INSTALLER/common/magiskhide/. $BINDIR/magiskhide $COREDIR/magiskhide +chmod -R 755 $COREDIR/magiskhide $COREDIR/postfsdata.d $COREDIR/service.d +chown -R 0.0 $COREDIR/magiskhide $COREDIR/postfsdata.d $COREDIR/service.d ########################################################################################## # Boot image patch @@ -434,7 +437,7 @@ if ($SUPERSU); then fi rm -f $TMPDIR/boottmp/stock_boot.img $UNPACKDIR/ramdisk.orig.gz $UNPACKDIR/ramdisk.gz 2>/dev/null ui_print "- Patching ramdisk with sukernel" - sh /data/custom_ramdisk_patch.sh $UNPACKDIR/ramdisk $BINDIR + sh /data/custom_ramdisk_patch.sh $UNPACKDIR/ramdisk LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-backup $UNPACKDIR/ramdisk.orig $UNPACKDIR/ramdisk $UNPACKDIR/ramdisk gzip -9 < $UNPACKDIR/ramdisk > $UNPACKDIR/ramdisk.gz rm -f $UNPACKDIR/ramdisk $UNPACKDIR/ramdisk.orig @@ -481,24 +484,13 @@ else fi fi - # Root - ROOT=false - if [ ! -d /magisk/phh ]; then - ui_print "- Installing phh's SuperUser" - ROOT=true - elif [ `grep_prop versionCode /magisk/phh/module.prop` -lt `grep_prop versionCode $INSTALLER/common/phh/module.prop` ]; then - ui_print "- Upgrading phh's SuperUser" - ROOT=true - fi - - if ($ROOT); then - mkdir -p /magisk/phh/bin 2>/dev/null - mkdir -p /magisk/phh/su.d 2>/dev/null - cp -af $INSTALLER/common/phh/. /magisk/phh - cp -af $BINDIR/su $BINDIR/sepolicy-inject /magisk/phh/bin - chmod -R 755 /magisk/phh - chown -R 0.0 /magisk/phh - fi + # MagiskSU + ui_print "- Installing MagiskSU" + rm -rf $COREDIR/su 2>/dev/null + mkdir -p $COREDIR/su + cp -af $BINDIR/su $INSTALLER/common/magisksu.sh $COREDIR/su + chmod -R 755 $COREDIR/su + chown -R 0.0 $COREDIR/su # Patch ramdisk ui_print "- Patching ramdisk" @@ -544,6 +536,7 @@ else chmod 0755 magisk chmod 0750 init.magisk.rc sbin/magic_mask.sh + chown 0.0 magisk init.magisk.rc sbin/magic_mask.sh fi ui_print "- Repacking boot image" diff --git a/zip_static/common/init.magisk.rc b/scripts/init.magisk.rc similarity index 100% rename from zip_static/common/init.magisk.rc rename to scripts/init.magisk.rc diff --git a/scripts/magic_mask.sh b/scripts/magic_mask.sh index c47e44743..753a8fa58 100644 --- a/scripts/magic_mask.sh +++ b/scripts/magic_mask.sh @@ -266,12 +266,13 @@ case $1 in # Don't run twice if [ "`getprop magisk.restart_pfsd`" != "1" ]; then - export OLDPATH=$PATH - export PATH=$TOOLPATH:$OLDPATH - log_print "** Magisk post-fs-data mode running..." # Cache support + mv /cache/stock_boot.img /data/stock_boot.img 2>/dev/null + mv /cache/magisk.apk /data/magisk.apk 2>/dev/null + mv /cache/custom_ramdisk_patch.sh /data/custom_ramdisk_patch.sh 2>/dev/null + if [ -d "/cache/data_bin" ]; then rm -rf $BINPATH $TOOLPATH mkdir -p $TOOLPATH @@ -282,17 +283,16 @@ case $1 in rm -f $TOOLPATH/su $TOOLPATH/sh $TOOLPATH/reboot fi - mv /cache/stock_boot.img /data/stock_boot.img 2>/dev/null - mv /cache/magisk.apk /data/magisk.apk 2>/dev/null + # Live patch sepolicy + $BINPATH/sepolicy-inject --live # Set up environment + export OLDPATH=$PATH + export PATH=$TOOLPATH:$OLDPATH chmod -R 755 $BINPATH $TOOLPATH chown -R 0.0 $BINPATH $TOOLPATH find $BINPATH $TOOLPATH -exec chcon -h u:object_r:system_file:s0 {} \; - # Live patch sepolicy - $BINPATH/sepolicy-inject --live -s su - # Multirom functions should go here, not available right now MULTIROM=false @@ -314,9 +314,9 @@ case $1 in # Remove empty directories, legacy paths, symlinks, old temporary images find $MOUNTPOINT -type d -depth ! -path "*core*" -exec rmdir {} \; 2>/dev/null - rm -rf $MOUNTPOINT/zzsupersu $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img 2>/dev/null + rm -rf $MOUNTPOINT/zzsupersu $MOUNTPOINT/phh $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img 2>/dev/null - # Remove modules that is labeled to be removed + # Remove modules that are labeled to be removed for MOD in $MOUNTPOINT/* ; do rm -f $MOD/system/placeholder 2>/dev/null if [ -f $MOD/remove ]; then @@ -342,13 +342,10 @@ case $1 in fi fi - log_print "* Preparing modules" + # Start MagiskSU if no SuperSU + [ ! -f /sbin/launch_daemonsu.sh ] && sh $COREDIR/su/magisksu.sh - # Disable phh and Magisk Hide for SuperSU - if [ -f /sbin/launch_daemonsu.sh ]; then - touch /magisk/phh/disable 2>/dev/null - rm -f $COREDIR/magiskhide/enable 2>/dev/null - fi + log_print "* Preparing modules" mkdir -p $DUMMDIR mkdir -p $MIRRDIR/system diff --git a/scripts/magisksu.sh b/scripts/magisksu.sh new file mode 100644 index 000000000..b5266ead8 --- /dev/null +++ b/scripts/magisksu.sh @@ -0,0 +1,28 @@ +#!/system/bin/sh + +MODDIR=${0%/*} +LOGFILE=/cache/magisk.log +PATH=$OLDPATH + +log_print() { + echo "MagiskSU: $1" + echo "MagiskSU: $1" >> $LOGFILE + log -p i -t Magisk "MagiskSU: $1" +} + +log_print "Moving and linking /sbin binaries" +mount -o rw,remount rootfs / +/data/busybox/cp -afc /sbin /sbin_orig +mount -o ro,remount rootfs / + +log_print "Exposing su binary" +rm -rf /magisk/.core/bin $MODDIR/sbin_bind +mkdir -p $MODDIR/sbin_bind +ln -s /sbin_orig/* $MODDIR/sbin_bind +chcon -h u:object_r:rootfs:s0 $MODDIR/sbin_bind/* +chmod 755 $MODDIR/sbin_bind +ln -s $MODDIR/su $MODDIR/sbin_bind/su +mount -o bind $MODDIR/sbin_bind /sbin + +log_print "Starting su daemon" +/sbin/su --daemon diff --git a/zip_static/common/phh/module.prop b/zip_static/common/phh/module.prop deleted file mode 100644 index fb3d8a38d..000000000 --- a/zip_static/common/phh/module.prop +++ /dev/null @@ -1,8 +0,0 @@ -id=phh -name=phh's SuperUser -version=topjohnwu r2 -versionCode=7 -author=phhusson & topjohnwu -description=OpenSource SELinux-capable SuperUser -support=http://forum.xda-developers.com/showthread.php?t=3216394 -donate=http://forum.xda-developers.com/donatetome.php?u=1915408 diff --git a/zip_static/common/phh/service.sh b/zip_static/common/phh/service.sh deleted file mode 100644 index 011d4130d..000000000 --- a/zip_static/common/phh/service.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/system/bin/sh - -LOGFILE=/cache/magisk.log -MODDIR=${0%/*} - -log_print() { - echo $1 - echo "phh: $1" >> $LOGFILE - log -p i -t phh "$1" -} - -# Disable the other root -[ -d "/magisk/zzsupersu" ] && touch /magisk/zzsupersu/disable - -log_print "Live patching sepolicy" -$MODDIR/bin/sepolicy-inject --live - -log_print "Moving and linking /sbin binaries" -mount -o rw,remount rootfs / -mv /sbin /sbin_orig -mkdir /sbin -chmod 755 /sbin -ln -s /sbin_orig/* /sbin -mount -o ro,remount rootfs / - -# Expose the root path -log_print "Mounting supath" -rm -rf /magisk/.core/bin $MODDIR/sbin_bind -mkdir -p $MODDIR/sbin_bind -/data/busybox/cp -afc /sbin/. $MODDIR/sbin_bind -chmod 755 $MODDIR/sbin_bind -ln -s $MODDIR/bin/* $MODDIR/sbin_bind -mount -o bind $MODDIR/sbin_bind /sbin - -# Run su.d -for script in $MODDIR/su.d/* ; do - if [ -f "$script" ]; then - chmod 755 $script - log_print "su.d: $script" - sh $script - fi -done - -log_print "Starting su daemon" -[ ! -z $OLDPATH ] && export PATH=$OLDPATH -/sbin/su --daemon