[Magisk] topjohnwu:
This thread is outdated and will never be updated

Please check the official Magisk documentation on Github!

Magisk Details (DEV)

sepolicy-inject

Before starting, check the complete document from Chainfire

SELinux Policy Section in How-To SU

The included sepolicy manipulation tool is called "sepolicy-inject", it supports live patching, and also supports policy statements following the same syntax as supolicy:

sepolicy-inject [--live] [--minimal] [--load <infile>] [--save <outfile>] [policystatement...]

--live: directly load patched policy to device
--minimal: minimal patches for boot image to let Magisk live patch on boot

Supported policy statements:

"allow #source-class #target-class permission-class #permission"
"deny #source-class #target-class permission-class #permission"
"auditallow #source-class #target-class permission-class #permission"
"auditdeny #source-class #target-class permission-class #permission"
"create #class"
"permissive #class"
"enforcing #class"
"attradd #class #attribute"
"typetrans source-class target-class permission-class default-class (optional: object-name)"

source-class and target-class can be attributes (patches the whole group)
All sections (except typetrans) can be replaced with '*' to patch every possible matches
Sections marked with '#' can be replaced with collections in curly brackets
e.g: allow { source1 source2 } { target1 target2 } permission-class { permission1 permission2 }
Will be expanded to:
allow source1 target1 permission-class permission1
allow source1 target1 permission-class permission2
allow source1 target2 permission-class permission1
allow source1 target2 permission-class permission2
allow source2 target1 permission-class permission1
allow source2 target1 permission-class permission2
allow source2 target2 permission-class permission1
allow source2 target2 permission-class permission2

Work flow

--------post-fs---------
(Device Boots) -> Mount Magisk cache files -> (build.prop and persist prop loads) ->

-----------------------------post-fs-data---------------------------------
-> Live patch sepolicy -> mount magisk.img -> Preparation -> 5 stage of tasks ->

--------------late_start--------------
-> (System Start Up) -> run all service.sh -> Start Magisk Hide
-> (Parallel with other processes....)

(Preparation) Magisk will travel through all enabled module directories to collect info

(1st stage) Mount system (vendor) mirrors. This is used to revert dummy files

(2nd stage) Clone the system structure, and mount the dummy directories to provide a dummy skeleton

(3rd stage) Mount module items into the system (and dummy skeletons)

(4th stage) Execute scripts

(5th stage) Mount the mirror items back to the remaining dummy files

File Structures

A "Magisk Module" is a subfolder under magisk root directory

The structure of a Magisk Module do not have a strict restriction. However, here are some key files / folders:

/magisk
|
|-.core <--- Magisk auto-generated files. Do not mess with them :)
| |
| |
| |-hosts <--- This file will be mounted to /system/etc/hosts to enable
| | systemless support for AdBlocker apps.
| |-magiskhide <--- This folder holds the hide list and add/rm/list/enable/disable scripts
| | | Include the magiskhide binary
| | |-magiskhide
| | |-hidelist
| | |-add
| | |-rm
| | |-list
| | |-enable
| | |-disable
| |
| |-su <--- This folder holds the MagiskSU binaries and scripts
| | | Include the magiskhide binary
| | |-su
| | |-magisksu.sh
| | |-sbin_bind <--- This folder will bind mount to /sbin
| | |-.....
| |
| |-post-fs-data.d <--- Place scripts that should be executed at post-fs-data here
| | |-.....
| |
| |-service.d <--- Place scripts that should be executed at late_start service here
| |-.....
|
|
|-other_module
| |-.....
|
|-your_module
| |
| |-auto_mount <--- If this file exists, auto mount is enabled
| |
| |-disable <--- If this file exists, the module is disabled
| |
| |-remove <--- If this file exists, the whole module folder
| | will be removed in the next reboot
| |-module.prop <--- This files stores the info of your module
| |
| |-system.prop <--- This file will be read by resetprop
| |
| |-post-fs-data.sh <--- This script will be executed in post-fs-data
| |
| |-service.sh <--- This script will be executed in late_start service
| |
| |-system <--- If auto mount is enabled, Magisk will "Magic Mount" this folder
| | |-....
| | |-....
| | |-....
| | |-....
| |
| |-vendor <--- Auto generated. A symlink to $MODID/system/vendor
| | Dealing with separate vendor partitions
| |-..... <--- Any other files/folders are allowed
|
|-another_module
| |-.....
|-...

Other important folders (under /dev), these will be created every boot

/dev/busybox <--- Busybox applet symlinks will be created here
|-.... Will be bind mounted to /system/xbin if enable busybox in Magisk Manager
|-....
|-....

/dev/magisk
|
|-dummy <--- Lies all cloned dummy structures
| | Folders in here will be mounted to /system
| |-.....
|
|-mirror <--- Lies all mirrors
| |
| |-system
| | |-....
| |
| |-vendor <--- Could be a symlink to /dev/mirror/system/vendor
| |-....
|
|-mnt <--- Lies all mounting info
|
|-dummy <--- Lists all dummy mounts
| |-....
|
|-mirror <--- Lists all mirror mounts
| |-....
|
|-system <--- Lists all /system mounts
| |-....
|
|-vendor <--- Lists all /vendor mounts
|-....

Boot Stages

If you are working on complicated projects, you shall need more control to the whole process.

Magisk can run scripts in different boot stages, you can fine tune exactly what you want to do.

post-fs mode:

This stage is BLOCKING. Boot process will NOT continue until everything is done, or 20 seconds has passed

Happens after most partitions are mounted, except /data

Magisk will bind mount items in: /cache/magisk_mount/system (*1)

post-fs-data mode:

This stage is BLOCKING. Boot process will NOT continue until everything is done, or 60 seconds has passed

Happens after /data is ready (including encrypted data)

Happens before Zygote is started (which means pretty much everything)

/data/magisk.img will be mounted to /magisk

Magisk will bind mount items in: /magisk/$MODID/system (*2)

Magisk will run script: /magisk/$MODID/post-fs-data.sh

Magisk will run scripts in: /magisk/.core/post-fs-data.d

late_start service mode:

This stage is NON-BLOCKING, it will run parallel with other processes

Put time consuming but non time critical tasks here. In other modes, the boot process will be stuck if it took too long to finish your tasks.

Happens when class late_start is started . Most things are are loaded/loading in this case.

Magisk will run script: /magisk/$MODID/service.sh

Magisk will run scripts in: /magisk/.core/service.d

(*1) Bind mounting in post-fs only support simple bind mounts, which mean you cannot add files or replace directories.

It will only mount files that exist in the current system.

(*2) Bind mounting in post-fs-data support complex bind mounts. Please refer to the "Magic Mount" section