Magisk/native/jni/magiskpolicy/rules.cpp

207 lines
7.8 KiB
C++
Raw Normal View History

2020-03-09 08:50:30 +00:00
#include <logging.hpp>
2019-07-01 02:09:31 +00:00
#include <flags.h>
2020-03-09 08:50:30 +00:00
#include <magiskpolicy.hpp>
2019-02-10 08:57:51 +00:00
2017-04-19 20:28:56 +00:00
#include "sepolicy.h"
2016-09-12 22:19:07 +00:00
2020-05-21 13:48:02 +00:00
void sepolicy::allow_su_client(const char *type) {
if (!exists(type))
2018-03-10 23:23:30 +00:00
return;
2020-05-21 13:48:02 +00:00
allow(type, SEPOL_PROC_DOMAIN, "unix_stream_socket", "connectto");
allow(type, SEPOL_PROC_DOMAIN, "unix_stream_socket", "getopt");
allow(SEPOL_PROC_DOMAIN, type, "fd", "use");
allow(SEPOL_PROC_DOMAIN, type, "fifo_file", ALL);
2018-04-14 19:13:01 +00:00
2018-09-20 20:55:16 +00:00
// Allow binder service
2020-05-21 13:48:02 +00:00
allow(type, SEPOL_PROC_DOMAIN, "binder", "call");
allow(type, SEPOL_PROC_DOMAIN, "binder", "transfer");
2019-04-30 01:25:57 +00:00
// Allow termios ioctl
2020-05-21 13:48:02 +00:00
allow(type, "devpts", "chr_file", "ioctl");
allow(type, "untrusted_app_devpts", "chr_file", "ioctl");
allow(type, "untrusted_app_25_devpts", "chr_file", "ioctl");
allow(type, "untrusted_app_all_devpts", "chr_file", "ioctl");
if (db->policyvers >= POLICYDB_VERSION_XPERMS_IOCTL) {
allowxperm(type, "devpts", "chr_file", "0x5400-0x54FF");
allowxperm(type, "untrusted_app_devpts", "chr_file", "0x5400-0x54FF");
allowxperm(type, "untrusted_app_25_devpts", "chr_file", "0x5400-0x54FF");
allowxperm(type, "untrusted_app_all_devpts", "chr_file", "0x5400-0x54FF");
2019-04-30 01:25:57 +00:00
}
2016-09-12 22:19:07 +00:00
}
2020-05-21 13:48:02 +00:00
void sepolicy::magisk_rules() {
// Temp suppress warnings
auto bak = log_cb.w;
log_cb.w = nop_log;
2018-09-20 20:55:16 +00:00
// First prevent anything to change sepolicy except ourselves
2020-05-21 13:48:02 +00:00
deny(ALL, "kernel", "security", "load_policy");
2018-09-20 20:55:16 +00:00
2020-05-21 13:48:02 +00:00
if (!exists(SEPOL_PROC_DOMAIN))
create(SEPOL_PROC_DOMAIN);
if (!exists(SEPOL_FILE_DOMAIN))
create(SEPOL_FILE_DOMAIN);
permissive(SEPOL_PROC_DOMAIN);
2018-09-20 20:55:16 +00:00
2020-05-21 13:48:02 +00:00
typeattribute(SEPOL_PROC_DOMAIN, "mlstrustedsubject");
typeattribute(SEPOL_PROC_DOMAIN, "netdomain");
typeattribute(SEPOL_PROC_DOMAIN, "bluetoothdomain");
typeattribute(SEPOL_FILE_DOMAIN, "mlstrustedobject");
2018-09-20 20:55:16 +00:00
2019-06-26 06:31:59 +00:00
// Let everyone access tmpfs files (for SAR sbin overlay)
2020-05-21 13:48:02 +00:00
allow(ALL, "tmpfs", "file", ALL);
2019-06-26 06:31:59 +00:00
// For normal rootfs file/directory operations when rw (for SAR / overlay)
2020-05-21 13:48:02 +00:00
allow("rootfs", "labeledfs", "filesystem", "associate");
2019-04-30 00:26:51 +00:00
// Let init transit to SEPOL_PROC_DOMAIN
2020-05-21 13:48:02 +00:00
allow("kernel", "kernel", "process", "setcurrent");
allow("kernel", SEPOL_PROC_DOMAIN, "process", "dyntransition");
2018-09-20 20:55:16 +00:00
// Let init run stuffs
2020-05-21 13:48:02 +00:00
allow("kernel", SEPOL_PROC_DOMAIN, "fd", "use");
allow("init", SEPOL_PROC_DOMAIN, "process", ALL);
allow("init", "tmpfs", "file", "getattr");
allow("init", "tmpfs", "file", "execute");
2018-09-20 20:55:16 +00:00
// Shell, properties, logs
2020-05-21 13:48:02 +00:00
if (exists("default_prop"))
allow(SEPOL_PROC_DOMAIN, "default_prop", "property_service", "set");
allow(SEPOL_PROC_DOMAIN, "init", "unix_stream_socket", "connectto");
allow(SEPOL_PROC_DOMAIN, "rootfs", "filesystem", "remount");
if (exists("logd"))
allow(SEPOL_PROC_DOMAIN, "logd", "unix_stream_socket", "connectto");
allow(SEPOL_PROC_DOMAIN, SEPOL_PROC_DOMAIN, ALL, ALL);
2018-09-20 20:55:16 +00:00
// For sepolicy live patching
2020-05-21 13:48:02 +00:00
allow(SEPOL_PROC_DOMAIN, "kernel", "security", "read_policy");
allow(SEPOL_PROC_DOMAIN, "kernel", "security", "load_policy");
2018-09-20 20:55:16 +00:00
// Allow these processes to access MagiskSU
2020-05-21 13:48:02 +00:00
allow_su_client("init");
allow_su_client("shell");
allow_su_client("system_app");
allow_su_client("priv_app");
allow_su_client("platform_app");
allow_su_client("untrusted_app");
allow_su_client("untrusted_app_25");
allow_su_client("untrusted_app_27");
allow_su_client("untrusted_app_29");
allow_su_client("update_engine");
2018-09-20 20:55:16 +00:00
2018-07-20 14:22:49 +00:00
// suRights
2020-05-21 13:48:02 +00:00
allow("servicemanager", SEPOL_PROC_DOMAIN, "dir", "search");
allow("servicemanager", SEPOL_PROC_DOMAIN, "dir", "read");
allow("servicemanager", SEPOL_PROC_DOMAIN, "file", "open");
allow("servicemanager", SEPOL_PROC_DOMAIN, "file", "read");
allow("servicemanager", SEPOL_PROC_DOMAIN, "process", "getattr");
allow("servicemanager", SEPOL_PROC_DOMAIN, "binder", "transfer");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "dir", "search");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "dir", "read");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "file", "open");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "file", "read");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "process", "getattr");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "binder", "transfer");
allow(SEPOL_PROC_DOMAIN, "servicemanager", "binder", "call");
allow(ALL, SEPOL_PROC_DOMAIN, "process", "sigchld");
2016-09-12 22:19:07 +00:00
// allowLog
2020-05-21 13:48:02 +00:00
allow("logd", SEPOL_PROC_DOMAIN, "dir", "search");
allow("logd", SEPOL_PROC_DOMAIN, "file", "read");
allow("logd", SEPOL_PROC_DOMAIN, "file", "open");
allow("logd", SEPOL_PROC_DOMAIN, "file", "getattr");
2016-09-12 22:19:07 +00:00
// suBackL0
2020-05-21 13:48:02 +00:00
allow("system_server", SEPOL_PROC_DOMAIN, "binder", "call");
allow("system_server", SEPOL_PROC_DOMAIN, "binder", "transfer");
allow(SEPOL_PROC_DOMAIN, "system_server", "binder", "call");
allow(SEPOL_PROC_DOMAIN, "system_server", "binder", "transfer");
2016-09-12 22:19:07 +00:00
// suBackL6
2020-05-21 13:48:02 +00:00
allow("surfaceflinger", "app_data_file", "dir", ALL);
allow("surfaceflinger", "app_data_file", "file", ALL);
allow("surfaceflinger", "app_data_file", "lnk_file", ALL);
typeattribute("surfaceflinger", "mlstrustedsubject");
2016-11-02 17:20:35 +00:00
// suMiscL6
2020-05-21 13:48:02 +00:00
if (exists("audioserver"))
allow("audioserver", "audioserver", "process", "execmem");
2017-06-07 03:42:51 +00:00
// Liveboot
2020-05-21 13:48:02 +00:00
allow("surfaceflinger", SEPOL_PROC_DOMAIN, "process", "ptrace");
allow("surfaceflinger", SEPOL_PROC_DOMAIN, "binder", "transfer");
allow("surfaceflinger", SEPOL_PROC_DOMAIN, "binder", "call");
allow("surfaceflinger", SEPOL_PROC_DOMAIN, "fd", "use");
allow("debuggerd", SEPOL_PROC_DOMAIN, "process", "ptrace");
// dumpsys
2020-05-21 13:48:02 +00:00
allow(ALL, SEPOL_PROC_DOMAIN, "fd", "use");
allow(ALL, SEPOL_PROC_DOMAIN, "fifo_file", "write");
allow(ALL, SEPOL_PROC_DOMAIN, "fifo_file", "read");
allow(ALL, SEPOL_PROC_DOMAIN, "fifo_file", "open");
allow(ALL, SEPOL_PROC_DOMAIN, "fifo_file", "getattr");
2018-07-20 14:22:49 +00:00
// bootctl
2020-05-21 13:48:02 +00:00
allow("hwservicemanager", SEPOL_PROC_DOMAIN, "dir", "search");
allow("hwservicemanager", SEPOL_PROC_DOMAIN, "file", "read");
allow("hwservicemanager", SEPOL_PROC_DOMAIN, "file", "open");
allow("hwservicemanager", SEPOL_PROC_DOMAIN, "process", "getattr");
allow("hwservicemanager", SEPOL_PROC_DOMAIN, "binder", "transfer");
2017-12-31 11:20:49 +00:00
// For mounting loop devices, mirrors, tmpfs
2020-05-21 13:48:02 +00:00
allow(SEPOL_PROC_DOMAIN, "kernel", "process", "setsched");
allow(SEPOL_PROC_DOMAIN, "labeledfs", "filesystem", "mount");
allow(SEPOL_PROC_DOMAIN, "labeledfs", "filesystem", "unmount");
allow(SEPOL_PROC_DOMAIN, "tmpfs", "filesystem", "mount");
allow(SEPOL_PROC_DOMAIN, "tmpfs", "filesystem", "unmount");
allow("kernel", ALL, "file", "read");
allow("kernel", ALL, "file", "write");
2017-03-29 18:02:39 +00:00
2019-04-30 00:26:51 +00:00
// Allow us to do anything to any files/dir/links
2020-05-21 13:48:02 +00:00
allow(SEPOL_PROC_DOMAIN, ALL, "file", ALL);
allow(SEPOL_PROC_DOMAIN, ALL, "dir", ALL);
allow(SEPOL_PROC_DOMAIN, ALL, "lnk_file", ALL);
allow(SEPOL_PROC_DOMAIN, ALL, "blk_file", ALL);
allow(SEPOL_PROC_DOMAIN, ALL, "sock_file", ALL);
allow(SEPOL_PROC_DOMAIN, ALL, "chr_file", ALL);
allow(SEPOL_PROC_DOMAIN, ALL, "fifo_file", ALL);
2019-04-30 00:26:51 +00:00
// Allow us to do any ioctl on all block devices
2020-05-21 13:48:02 +00:00
if (db->policyvers >= POLICYDB_VERSION_XPERMS_IOCTL)
allowxperm(SEPOL_PROC_DOMAIN, ALL, "blk_file", "0x0000-0xFFFF");
2019-04-30 00:26:51 +00:00
// Allow all binder transactions
2020-05-21 13:48:02 +00:00
allow(ALL, SEPOL_PROC_DOMAIN, "binder", ALL);
// Super files
2020-05-21 13:48:02 +00:00
allow(ALL, SEPOL_FILE_DOMAIN, "file", ALL);
allow(ALL, SEPOL_FILE_DOMAIN, "dir", ALL);
allow(ALL, SEPOL_FILE_DOMAIN, "fifo_file", ALL);
allow(ALL, SEPOL_FILE_DOMAIN, "chr_file", ALL);
allow(SEPOL_FILE_DOMAIN, ALL, "filesystem", "associate");
2017-07-13 16:49:40 +00:00
// For changing attributes
2020-05-21 13:48:02 +00:00
allow("rootfs", "tmpfs", "filesystem", "associate");
2017-07-13 16:49:40 +00:00
2016-10-02 14:48:49 +00:00
// Xposed
2020-05-21 13:48:02 +00:00
allow("untrusted_app", "untrusted_app", "capability", "setgid");
allow("system_server", "dex2oat_exec", "file", ALL);
2018-07-06 17:36:57 +00:00
// Support deodexed ROM on Oreo
2020-05-21 13:48:02 +00:00
allow("zygote", "dalvikcache_data_file", "file", "execute");
2018-08-03 14:40:49 +00:00
// Support deodexed ROM on Pie (Samsung)
2020-05-21 13:48:02 +00:00
allow("system_server", "dalvikcache_data_file", "file", "write");
allow("system_server", "dalvikcache_data_file", "file", "execute");
magiskpolicy: rules: standardize update_engine sepolicy when rooted The state of ROM A/B OTA addon.d-v2 support is an inconsistent mess currently: - LineageOS builds userdebug with permissive update_engine domain, OmniROM builds userdebug with a more restricted update_engine domain, and CarbonROM builds user with a hybrid closer to Omni's - addon.d-v2 scripts cannot function to the full extent they should when there is a more restricted update_engine domain sepolicy in place, which is likely why Lineage made update_engine completely permissive Evidence for the above: - many addon.d-v2 scripts only work (or fully work) on Lineage, see below - Magisk's addon.d-v2 script would work on Lineage without issue, but would work on Carbon and Omni only if further allow rules were added for basic things like "file read" and "dir search" suggesting these ROMs' addon.d-v2 is severely limited - Omni includes a /system/addon.d/69-gapps.sh script with the ROM itself (despite shipping without GApps), and with Magisk's more permissive sepolicy and no GApps installed it will remove important ROM files during OTA, resulting in a bootloop; the issue with shipping this script was therefore masked by Omni's overly restrictive update_engine sepolicy not allowing the script to function as intended The solution: - guarantee a consistent addon.d-v2 experience for users across ROMs when rooted with Magisk by making update_engine permissive as Lineage has - hopefully ROMs can work together to come up with something standard for unrooted addon.d-v2 function
2019-09-19 14:53:05 +00:00
// Allow update_engine/addon.d-v2 to run permissive on all ROMs
2020-05-21 13:48:02 +00:00
permissive("update_engine");
2018-11-29 11:28:37 +00:00
2019-11-23 22:18:55 +00:00
#if 0
2019-04-30 00:26:51 +00:00
// Remove all dontaudit in debug mode
2020-05-21 13:48:02 +00:00
strip_dontaudit(db);
2019-04-30 00:26:51 +00:00
#endif
log_cb.w = bak;
2017-04-15 18:29:42 +00:00
}