Magisk v8

This commit is contained in:
topjohnwu 2016-10-19 06:22:00 +08:00
parent 696d256fa0
commit a06ef6fe25
12 changed files with 137 additions and 206 deletions

View File

@ -56,6 +56,7 @@ 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;
free(path);
path = NULL; path = NULL;
asprintf(&path, "/proc/%d/mounts", pid); asprintf(&path, "/proc/%d/mounts", pid);
FILE *mount_fp = fopen(path, "r"); FILE *mount_fp = fopen(path, "r");
@ -63,6 +64,7 @@ int hideMagisk(int pid) {
fprintf(stderr, "Error opening mount list!\n"); fprintf(stderr, "Error opening mount list!\n");
return 1; return 1;
} }
free(path);
int mount_size; int mount_size;
char **mount_list = file_to_str_arr(mount_fp, &mount_size), mountpoint[256], *sbstr; 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(mount_list[i]);
} }
// Free memory // Free memory
free(mount_list); free(mount_list);
return 0; return 0;
} }

View File

@ -185,7 +185,7 @@ repack_boot() {
########################################################################################## ##########################################################################################
ui_print "****************************" ui_print "****************************"
ui_print "Magisk v7 Boot Image Patcher" ui_print "Magisk v8 Boot Image Patcher"
ui_print "****************************" ui_print "****************************"
if [ ! -d "$INSTALLER/common" ]; then if [ ! -d "$INSTALLER/common" ]; then
@ -270,6 +270,7 @@ if (is_mounted /data); then
rm -rf /data/busybox /data/magisk 2>/dev/null rm -rf /data/busybox /data/magisk 2>/dev/null
mkdir -p /data/busybox mkdir -p /data/busybox
cp -af $BINDIR /data/magisk 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/* chmod 755 /data/busybox /data/magisk /data/magisk/*
chcon 'u:object_r:system_file:s0' /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 /data/magisk/busybox --install -s /data/busybox
@ -279,6 +280,7 @@ else
rm -rf /cache/data_bin 2>/dev/null rm -rf /cache/data_bin 2>/dev/null
mkdir -p /cache/data_bin mkdir -p /cache/data_bin
cp -af $BINDIR /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 fi
########################################################################################## ##########################################################################################
@ -311,7 +313,10 @@ MAGISKLOOP=$LOOPDEVICE
mkdir -p /magisk/.core/magiskhide 2>/dev/null mkdir -p /magisk/.core/magiskhide 2>/dev/null
cp -af $INSTALLER/common/magiskhide/. /magisk/.core/magiskhide 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 # Boot image patch
@ -336,92 +341,45 @@ dd if=$BOOTIMAGE of=$ORIGBOOT
ui_print "- Unpacking boot image" ui_print "- Unpacking boot image"
unpack_boot $ORIGBOOT unpack_boot $ORIGBOOT
# Restore ramdisk
SUPERSU=false SUPERSU=false
if (! $NORESTORE); then if (! $NORESTORE); then
# Backups
if [ -d ".backup" ]; then if [ -d ".backup" ]; then
ui_print "- Restoring ramdisk with backup" ui_print "- Restoring ramdisk with ramdisk backup"
cp -af .backup/* . cp -af .backup/. .
rm -rf magisk init.magisk.rc sbin/magic_mask.sh 2>/dev/null rm -rf magisk init.magisk.rc sbin/magic_mask.sh 2>/dev/null
else else
if [ -f "sbin/launch_daemonsu.sh" ]; then [ -f "sbin/launch_daemonsu.sh" ] && SUPERSU=true
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
if ($SUPERSU); then if ($SUPERSU); then
ui_print "- SuperSU patched boot detected" ui_print "- SuperSU patched boot detected!"
# Restore with SuperSU's backup ui_print "- Adding auto patch script for SuperSU"
MOUNTSU=false cp -af $INSTALLER/common/custom_ramdisk_patch.sh /data/custom_ramdisk_patch.sh
(! 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 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 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 cp -af /data/stock_boot_*.gz /data/stock_boot.img.gz 2>/dev/null
gzip -d /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 if [ -f "/data/stock_boot.img" ]; then
ui_print "- Restoring boot image with backup" ui_print "- Restoring boot image with backup"
cp -af /data/stock_boot.img $ORIGBOOT cp -af /data/stock_boot.img $ORIGBOOT
unpack_boot $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
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
fi
if (! $SUPERSU); then
ui_print "- Creating backups" ui_print "- Creating backups"
mkdir .backup mkdir .backup 2>/dev/null
cp -af init.rc *fstab* verity_key sepolicy .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 if (is_mounted /data); then
cp -af $ORIGBOOT /data/stock_boot.img cp -af $ORIGBOOT /data/stock_boot.img
else else
@ -430,19 +388,33 @@ if (! $NORESTORE); then
fi fi
fi fi
ui_print "- Installing root helper module"
cp -af $INSTALLER/common/roothelper /magisk/00roothelper
chmod 755 /magisk/00roothelper /magisk/00roothelper/*
# Patch ramdisk # Patch ramdisk
ui_print "- Patching ramdisk" ui_print "- Patching ramdisk"
if [ $(grep -c "import /init.magisk.rc" init.rc) -eq "0" ]; then # Add magisk entrypoint
sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" init.rc 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
# 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 fi
sed -i "/selinux.reload_policy/d" init.rc if (! $SUPERSU); then
find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do sed -i "/selinux.reload_policy/d" init.rc
find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do
if (! $KEEPVERITY); then if (! $KEEPVERITY); then
sed -i "s/,support_scfs//g" $FSTAB sed -i "s/,support_scfs//g" $FSTAB
sed -i 's;,\{0,1\}verify\(=[^,]*\)\{0,1\};;g' $FSTAB sed -i 's;,\{0,1\}verify\(=[^,]*\)\{0,1\};;g' $FSTAB
@ -451,9 +423,10 @@ find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do
sed -i "s/forceencrypt/encryptable/g" $FSTAB sed -i "s/forceencrypt/encryptable/g" $FSTAB
sed -i "s/forcefdeorfbe/encryptable/g" $FSTAB sed -i "s/forcefdeorfbe/encryptable/g" $FSTAB
fi fi
done done
if (! $KEEPVERITY); then if (! $KEEPVERITY); then
rm verity_key 2>/dev/null rm verity_key 2>/dev/null
fi
fi fi
# sepolicy patches # 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/init.magisk.rc init.magisk.rc
cp -af $INSTALLER/common/magic_mask.sh sbin/magic_mask.sh 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 chmod 0750 init.magisk.rc sbin/magic_mask.sh
ui_print "- Repacking boot image" ui_print "- Repacking boot image"

Binary file not shown.

View File

@ -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

View File

@ -1,9 +1,6 @@
# Triggers # Triggers
on post-fs on post-fs
# Paths
export PATH /magisk/.core/bin:/sbin:/vendor/bin:/system/sbin:/system/bin:/magisk/.core/busybox:/system/xbin
start magisk_pfs start magisk_pfs
wait /dev/.magisk.unblock 20 wait /dev/.magisk.unblock 20
rm /dev/.magisk.unblock rm /dev/.magisk.unblock

View File

@ -1,6 +1,7 @@
#!/system/bin/sh #!/system/bin/sh
LOGFILE=/cache/magisk.log LOGFILE=/cache/magisk.log
HIDELOG=/cache/magiskhide.log
IMG=/data/magisk.img IMG=/data/magisk.img
export MOUNTPOINT=/magisk export MOUNTPOINT=/magisk
@ -193,6 +194,7 @@ merge_image() {
# Merge (will reserve selinux contexts) # Merge (will reserve selinux contexts)
cd /cache/merge_img cd /cache/merge_img
for MOD in *; do for MOD in *; do
log_print "Merging: $MOD"
rm -rf /cache/data_img/$MOD rm -rf /cache/data_img/$MOD
cp -afc $MOD /cache/data_img/ cp -afc $MOD /cache/data_img/
done done
@ -227,8 +229,9 @@ case $1 in
if [ -d "/cache/magisk_merge" ]; then if [ -d "/cache/magisk_merge" ]; then
cd /cache/magisk_merge cd /cache/magisk_merge
for MOD in *; do for MOD in *; do
log_print "Merging: $MOD"
rm -rf /cache/magisk/$MOD rm -rf /cache/magisk/$MOD
cp -afc $MOD /cache/magisk/ mv $MOD /cache/magisk/$MOD
done done
rm -rf /cache/magisk_merge rm -rf /cache/magisk_merge
fi fi
@ -299,8 +302,6 @@ case $1 in
unblock unblock
fi fi
echo $MOUNTPOINT >> $MOUNTLIST
log_print "Preparing modules" log_print "Preparing modules"
# First do cleanups # First do cleanups
rm -rf $DUMMDIR rm -rf $DUMMDIR
@ -404,7 +405,7 @@ case $1 in
service ) service )
# Version info # Version info
setprop magisk.version 7 setprop magisk.version 8
log_print "Magisk late_start service mode running..." log_print "Magisk late_start service mode running..."
run_scripts service run_scripts service
[ -f "$COREDIR/magiskhide/enable" ] && setprop magisk.hide 1 [ -f "$COREDIR/magiskhide/enable" ] && setprop magisk.hide 1
@ -420,8 +421,9 @@ case $1 in
cat $COREDIR/magiskhide/hidelist.tmp >> $COREDIR/magiskhide/hidelist cat $COREDIR/magiskhide/hidelist.tmp >> $COREDIR/magiskhide/hidelist
rm -f $COREDIR/magiskhide/hidelist.tmp rm -f $COREDIR/magiskhide/hidelist.tmp
fi fi
chmod 755 $COREDIR/magiskhide $COREDIR/magiskhide/*
log_print "Starting Magisk Hide" log_print "Starting Magisk Hide"
exec /data/magisk/magiskhide $COREDIR/magiskhide/hidelist exec /data/magisk/magiskhide $COREDIR/magiskhide/hidelist > $HIDELOG
;; ;;
esac esac

View File

@ -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

View File

@ -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 &)

View File

@ -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

View File

@ -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

View File

@ -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

Binary file not shown.