Commit Graph

94 Commits

Author SHA1 Message Date
topjohnwu f008420891 Make magiskinit not magiskpolicy 2022-03-17 03:36:40 -07:00
topjohnwu 49f259065d Introduce new sepolicy injection mechanism
In the current implementation, Magisk will either have to recreate
all early mount implementation (for legacy SAR and rootfs devices) or
delegate early mount to first stage init (for 2SI devices) to access
required partitions for loading sepolicy. It then has to recreate the
split sepolicy loading implementation in-house, apply patches, then
dump the compiled + patched policies into monolithic format somewhere.
Finally, it patches the original init to force it to load the sepolicy
file we just created.

With the increasing complexity involved in early mount and split
sepolicy (there is even APEX module involved in the future!),
it is about time to rethink Magisk's sepolicy strategy as rebuilding
init's functionality is not scalable and easy to maintain.

In this commit, instead of building sepolicy ourselves, we mock
selinuxfs with FIFO files connected to a pre-init daemon, waiting
for the actual init process to directly write the sepolicy file into
MagiskInit. We then patch the file and load it into the kernel. Some
FIFO tricks has to be used to hijack the original init process's
control flow and prevent race conditions, details are directly in the
comments in code.

At the moment, only system-as-root (read-only root) support is added.
Support for legacy rootfs devices will come with a follow up commit.
2022-03-16 00:31:55 -07:00
topjohnwu 3bf47a6838 Update selinux 2022-01-30 08:18:04 -08:00
John Wu c826318da4 Update CLI usage text 2022-01-20 01:59:01 -08:00
LoveSy 6582a4abd9 Make magiskpolicy supports multiple --apply 2022-01-20 01:59:01 -08:00
LoveSy b2cd24ed1b Fix an UB when cil compile error 2022-01-11 03:01:27 -08:00
topjohnwu 1443a5b175 Use mmap_data more widely 2021-11-30 01:50:55 -08:00
topjohnwu 2c44e1bb93 Update rules again 2021-10-29 03:37:14 -07:00
vvb2060 6a086bb222 Load *_compat_cil_file from system_ext
https://android-review.googlesource.com/c/platform/system/core/+/1650271
2021-10-29 03:14:26 -07:00
topjohnwu 6c5b261804 Update spolicy rules 2021-10-29 03:12:48 -07:00
LoveSy 4c747c4148 Add rule: allow * magisk_file lnk_file { * } 2021-10-26 00:41:04 -07:00
vvb2060 b0292d7319 Add execmem to allow hook 2021-10-09 02:59:03 -07:00
vvb2060 cb4361b7b7 Fix terminal on android 8
No need to handle untrusted_app_all_devpts on modern devices, but devpts policy is different for older devices.
2021-09-25 12:08:35 -07:00
topjohnwu 63cfe7b47b Make sanitize_environ work properly 2021-09-18 05:11:10 -07:00
vvb2060 f5c02be5bf Add new targetSdk domain
https://android-review.googlesource.com/c/platform/system/sepolicy/+/1752122
2021-09-02 01:48:25 -07:00
vvb2060 f98c68a280 Clean up unneeded rules 2021-08-29 13:03:50 -07:00
Wang Han e136fb3a4f Remove outdated sepolicies
* Support deodexed ROM: This should not be done and dexpreopt is mandatory since P
   Xposed: Xposed handles them just fine, at least in the latest version 89.3
   suMiscL6: For whatever audio mods, a leftover of phh time
   Liveboot and suBackL6: Was for CF.lumen and LiveBoot, not needed now

 * Also cleanup binder sepolicies since we allow all binder transactions.
2021-04-09 21:34:51 -07:00
topjohnwu 9a28dd4f6e Implement MagiskHide through code injection 2021-01-12 03:28:00 -08:00
topjohnwu f9bde347bc Convert indentation to spaces
The tab war is lost
2020-12-30 22:11:24 -08:00
topjohnwu 17b5291bbb Fix Android 8.0 selinux rules 2020-09-20 14:39:11 -07:00
topjohnwu c3586fe0a5 Upstream external/selinux
Fix #2645
2020-09-04 19:20:08 -07:00
topjohnwu 11f254e5e5 Fix SELinux support for Android 8.0 - 10
Fix #3139
2020-09-04 14:42:09 -07:00
topjohnwu 4dacffd7a1 Fix some issues with selinux rules 2020-09-04 00:03:24 -07:00
topjohnwu b73d5753f2 Minor code cleanups 2020-09-02 02:49:32 -07:00
Simon Shi 2eee335b5f Track more sepolicy cil files.
Reference: https://android.googlesource.com/platform/system/core/+/refs/tags/android-r-beta-3/init/selinux.cpp
2020-08-31 21:38:55 -07:00
topjohnwu 97b72a5941 Revert to old SElinux rules on pre 8.0 devices
Fix #2910
2020-07-06 01:13:50 -07:00
topjohnwu 1e2f776b83 Move logging.hpp 2020-06-17 01:17:28 -07:00
topjohnwu ec3705f2ed Redesign of MagiskSU's sepolicy model
Introduce new domain `magisk_client` and new file type `magisk_exec`.

Connection to magiskd's always-on socket is restricted to magisk_client
only. Whitelisted process domains can transit to magisk_client through
executing files labelled magisk_exec. The main magisk binary shall be
the only file labelled as magisk_exec throughout the whole system.
All processes thus are no longer allowed to connect to magiskd directly
without going through the proper magisk binary.

Connection failures are silenced from audit logs with dontaudit rules,
so crazy processes which traverse through all unix domain sockets to try
connection can no longer check logcat to know the actual reason behind
EACCES, leaking the denied process policy (which is u:r:magisk:s0).

This also allows us to remove many rules that open up holes in
untrusted_app domains that were used to make remote shell work properly.
Since all processes establishing the remote shell are now restricted to
the magisk_client domain, all these rules are moved to magisk_client.
This makes Magisk require fewer compromises in Android's security model.

Note: as of this commit, requesting new root access via Magisk Manager
will stop working as Magisk Manager can no longer communicate with
magiskd directly. This will be addressed in a future commit that
involves changes in both native and application side.
2020-06-03 23:29:42 -07:00
topjohnwu ae0dcabf43 Fix typo in sepolicy.cpp 2020-06-03 03:11:10 -07:00
topjohnwu 2f824f59dc Better logging system
Use C++ magic to strip out debug logs at compile time
2020-06-01 04:15:37 -07:00
topjohnwu c071ac8973 Remove unused code 2020-05-29 10:41:52 -07:00
topjohnwu 599ee57d39 Simplify sepolicy rules 2020-05-25 02:30:39 -07:00
topjohnwu 4499cebcd9 Support new sepolicy rules
Support declare new type with attribute and declare new attributes
2020-05-25 02:09:43 -07:00
topjohnwu cd6eca1dc2 Optimize match-all-type rules
For match-all-type rules (e.g. "allow magisk * * *" used in Magisk),
we used to iterate and apply rules on all existing types. However, this
is actually unnecessary as all selinux types should have at least 1
attributes assigned to it (process types "domain", file context types
"file_type" etc.). This means in order to create rules that applies to
all types, we actually only need to create rules for all attributes.

This optimization SIGNIFICANTLY reduces the patched sepolicy that is
loaded into the kernel when running Magisk. For example on Pixel 4 XL
running Android R DP4, the sepolicy sizes are
patched (before) : 3455948
patched (after)  : 843176
stock            : 630229

The active sepolicy size actually impacts the performance of every single
operation in the operating system, because the larger the policies gets,
the longer it takes for the kernel to lookup and match rules.
2020-05-24 05:41:19 -07:00
topjohnwu 951273f8ef Cleanup some implementations 2020-05-24 04:16:40 -07:00
topjohnwu 0efa73d96c Update selinux libs 2020-05-23 05:02:26 -07:00
topjohnwu f392ade78d Rewrite sepolicy.c in C++ 2020-05-23 00:18:25 -07:00
topjohnwu 0236ab887e Several statement parsing improvements
- Update help message to match the spec
- Make tokenization not seg fault in certain conditions
- Moar template + macro magic to reduce boilerplate
2020-05-22 14:05:56 -07:00
topjohnwu d4baae411b Modernize magiskpolicy 2020-05-21 06:48:02 -07:00
vvb2060 d7b87fcb8e Add untrusted_app_29 for Android 11 2020-04-20 21:50:52 -07:00
topjohnwu ba7cb47383 Make version reporting consistent 2020-03-23 01:17:13 -07:00
topjohnwu a0998009c1 Small native code reorganization 2020-03-09 01:50:30 -07:00
topjohnwu 0d229dac3b Support Android 11 SELinux paths
This is NOT proper Android 11 support
2020-02-21 00:49:33 -08:00
topjohnwu 6180558068 Add support for genfscon sepolicy rules
Close #2367
2020-02-02 01:16:42 +08:00
topjohnwu 12fda29280 Add support for pre-init custom sepolicy patches
Close #1685
2019-12-13 06:05:12 -05:00
topjohnwu af060b3132 General QoL changes 2019-12-13 00:37:06 -05:00
topjohnwu 490e6a6f23 Add new API to load sepolicy rule file 2019-12-09 04:14:30 -05:00
topjohnwu 34bb18448c Fix compile errors 2019-11-23 17:18:55 -05:00
topjohnwu 01253f050a Use smart pointers 2019-11-23 04:57:52 -05:00
topjohnwu 4f9a25ee89 Create generic streams on top of stdio
WIP
2019-11-20 21:48:49 -05:00