Magisk/native/jni/utils
topjohnwu 5f1174de27 Introduce new boot flow to handle SAR 2SI
The existing method for handling legacy SAR is:
1. Mount /sbin tmpfs overlay
2. Dump all patched/new files into /sbin
3. Magic mount root dir and re-exec patched stock init

With Android 11 removing the /sbin folder, it is quite obvious that
things completely break down right in step 1.

To overcome this issue, we have to find a way to swap out the init
binary AFTER we re-exec stock init. This is where 2SI comes to rescue!

2SI normal boot procedure is:
1st stage -> Load sepolicy -> 2nd stage -> boot continue...

2SI Magisk boot procedure is:
MagiskInit 1st stage -> Stock 1st stage -> MagiskInit 2nd Stage ->
-> Stock init load sepolicy -> Stock 2nd stage -> boot continue...

As you can see, the trick is to make stock 1st stage init re-exec back
into MagiskInit so we can do our setup. This is possible by manipulating
some ramdisk files on initramfs based 2SI devices (old ass non SAR
devices AND super modern devices like Pixel 3/4), but not possible
on device that are stuck using legacy SAR (device that are not that
modern but not too old, like Pixel 1/2. Fucking Google logic!!)

This commit introduces a new way to intercept stock init re-exec flow:
ptrace init with forked tracer, monitor PTRACE_EVENT_EXEC, then swap
out the init file with bind mounts right before execv returns!

Going through this flow however will lose some necessary backup files,
so some bookkeeping has to be done by making the tracer hold these
files in memory and act as a daemon. 2nd stage MagiskInit will ack the
daemon to release these files at the correct time.

It just works™  ¯\_(ツ)_/¯
2020-04-01 04:39:28 -07:00
..
include Small native code reorganization 2020-03-09 01:50:30 -07:00
Android.mk Create generic streams on top of stdio 2019-11-20 21:48:49 -05:00
cpio.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
file.cpp Introduce new boot flow to handle SAR 2SI 2020-04-01 04:39:28 -07:00
files.hpp Introduce new boot flow to handle SAR 2SI 2020-04-01 04:39:28 -07:00
logging.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
misc.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
misc.hpp Small native code reorganization 2020-03-09 01:50:30 -07:00
missing.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
missing.hpp Small native code reorganization 2020-03-09 01:50:30 -07:00
new.cpp Bump to C++17 2019-01-20 00:07:58 -05:00
selinux.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
stream.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
xwrap.cpp Small native code reorganization 2020-03-09 01:50:30 -07:00
xwrap.hpp Small native code reorganization 2020-03-09 01:50:30 -07:00