Skip to content

Commit

Permalink
Magisk v8
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Oct 18, 2016
1 parent 696d256 commit a06ef6f
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 196 deletions.
4 changes: 3 additions & 1 deletion jni/magiskhide.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ 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");
if (mount_fp == NULL) {
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;
Expand All @@ -88,9 +90,9 @@ int hideMagisk(int pid) {
}
free(mount_list[i]);
}

// Free memory
free(mount_list);

return 0;
}

Expand Down
155 changes: 64 additions & 91 deletions zip_static/META-INF/com/google/android/update-binary
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

##########################################################################################
Expand Down Expand Up @@ -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
Expand All @@ -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
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
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
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
# 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
ui_print "- Creating backups"
mkdir .backup
cp -af init.rc *fstab* verity_key sepolicy .backup 2>/dev/null
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
Expand All @@ -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
# 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

# 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

sed -i "/selinux.reload_policy/d" init.rc
find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do
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
sed -i "s/,support_scfs//g" $FSTAB
sed -i 's;,\{0,1\}verify\(=[^,]*\)\{0,1\};;g' $FSTAB
rm verity_key 2>/dev/null
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

# sepolicy patches
Expand All @@ -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"
Expand Down
Binary file modified zip_static/arm/magiskhide
Binary file not shown.
53 changes: 53 additions & 0 deletions zip_static/common/custom_ramdisk_patch.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions zip_static/common/init.magisk.rc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 7 additions & 5 deletions zip_static/common/magic_mask.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/system/bin/sh

LOGFILE=/cache/magisk.log
HIDELOG=/cache/magiskhide.log
IMG=/data/magisk.img

export MOUNTPOINT=/magisk
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -299,8 +302,6 @@ case $1 in
unblock
fi

echo $MOUNTPOINT >> $MOUNTLIST

log_print "Preparing modules"
# First do cleanups
rm -rf $DUMMDIR
Expand Down Expand Up @@ -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
Expand All @@ -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
9 changes: 0 additions & 9 deletions zip_static/common/roothelper/phh.prop

This file was deleted.

Loading

1 comment on commit a06ef6f

@netizen-ais
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we saw recently that SafetyNet reads some setprop values, shouldn't (I don't really know if it's possible) magiskhide unset Magisk.version?

Please sign in to comment.