Magisk/rules.c

231 lines
9.8 KiB
C
Raw Normal View History

2017-03-18 09:52:38 +01:00
#include "magiskpolicy.h"
2016-09-13 00:19:07 +02:00
void samsung() {
2017-04-15 13:26:29 +02:00
sepol_deny("init", "kernel", "security", "load_policy");
sepol_deny("policyloader_app", "security_spota_file", "dir", "read");
sepol_deny("policyloader_app", "security_spota_file", "dir", "write");
sepol_deny("policyloader_app", "security_spota_file", "file", "read");
sepol_deny("policyloader_app", "security_spota_file", "file", "write");
sepol_deny("system_server", "security_spota_file", "dir", "read");
sepol_deny("system_server", "security_spota_file", "dir", "write");
sepol_deny("system_server", "security_spota_file", "file", "read");
sepol_deny("system_server", "security_spota_file", "file", "write");
sepol_deny("system_app", "security_spota_file", "dir", "read");
sepol_deny("system_app", "security_spota_file", "dir", "write");
sepol_deny("system_app", "security_spota_file", "file", "read");
sepol_deny("system_app", "security_spota_file", "file", "write");
sepol_deny("installd", "security_spota_file", "dir", "read");
sepol_deny("installd", "security_spota_file", "dir", "write");
sepol_deny("installd", "security_spota_file", "file", "read");
sepol_deny("installd", "security_spota_file", "file", "write");
sepol_deny("init", "security_spota_file", "dir", "read");
sepol_deny("init", "security_spota_file", "dir", "write");
sepol_deny("init", "security_spota_file", "file", "read");
sepol_deny("init", "security_spota_file", "file", "write");
sepol_deny("ueventd", "security_spota_file", "dir", "read");
sepol_deny("ueventd", "security_spota_file", "dir", "write");
sepol_deny("ueventd", "security_spota_file", "file", "read");
sepol_deny("ueventd", "security_spota_file", "file", "write");
sepol_deny("runas", "security_spota_file", "dir", "read");
sepol_deny("runas", "security_spota_file", "dir", "write");
sepol_deny("runas", "security_spota_file", "file", "read");
sepol_deny("runas", "security_spota_file", "file", "write");
sepol_deny("drsd", "security_spota_file", "dir", "read");
sepol_deny("drsd", "security_spota_file", "dir", "write");
sepol_deny("drsd", "security_spota_file", "file", "read");
sepol_deny("drsd", "security_spota_file", "file", "write");
sepol_deny("debuggerd", "security_spota_file", "dir", "read");
sepol_deny("debuggerd", "security_spota_file", "dir", "write");
sepol_deny("debuggerd", "security_spota_file", "file", "read");
sepol_deny("debuggerd", "security_spota_file", "file", "write");
sepol_deny("vold", "security_spota_file", "dir", "read");
sepol_deny("vold", "security_spota_file", "dir", "write");
sepol_deny("vold", "security_spota_file", "file", "read");
sepol_deny("vold", "security_spota_file", "file", "write");
sepol_deny("zygote", "security_spota_file", "dir", "read");
sepol_deny("zygote", "security_spota_file", "dir", "write");
sepol_deny("zygote", "security_spota_file", "file", "read");
sepol_deny("zygote", "security_spota_file", "file", "write");
sepol_deny("auditd", "security_spota_file", "dir", "read");
sepol_deny("auditd", "security_spota_file", "dir", "write");
sepol_deny("auditd", "security_spota_file", "file", "read");
sepol_deny("auditd", "security_spota_file", "file", "write");
sepol_deny("servicemanager", "security_spota_file", "dir", "read");
sepol_deny("servicemanager", "security_spota_file", "dir", "write");
sepol_deny("servicemanager", "security_spota_file", "file", "read");
sepol_deny("servicemanager", "security_spota_file", "file", "write");
sepol_deny("itsonbs", "security_spota_file", "dir", "read");
sepol_deny("itsonbs", "security_spota_file", "dir", "write");
sepol_deny("itsonbs", "security_spota_file", "file", "read");
sepol_deny("itsonbs", "security_spota_file", "file", "write");
sepol_deny("commonplatformappdomain", "security_spota_file", "dir", "read");
sepol_deny("commonplatformappdomain", "security_spota_file", "dir", "write");
sepol_deny("commonplatformappdomain", "security_spota_file", "file", "read");
sepol_deny("commonplatformappdomain", "security_spota_file", "file", "write");
2016-09-13 00:19:07 +02:00
}
void allowSuClient(char *target) {
2017-04-15 13:26:29 +02:00
sepol_allow(target, "rootfs", "file", "execute_no_trans");
sepol_allow(target, "rootfs", "file", "execute");
sepol_allow(target, "su", "unix_stream_socket", "connectto");
sepol_allow(target, "su", "unix_stream_socket", "getopt");
sepol_allow(target, "su_device", "dir", "search");
sepol_allow(target, "su_device", "dir", "read");
sepol_allow(target, "su_device", "sock_file", "read");
sepol_allow(target, "su_device", "sock_file", "write");
2016-09-13 00:19:07 +02:00
}
void suRights() {
2017-04-15 13:26:29 +02:00
sepol_allow("servicemanager", "su", "dir", "search");
sepol_allow("servicemanager", "su", "dir", "read");
sepol_allow("servicemanager", "su", "file", "open");
sepol_allow("servicemanager", "su", "file", "read");
sepol_allow("servicemanager", "su", "process", "getattr");
sepol_allow("servicemanager", "su", "binder", "transfer");
sepol_allow("system_server", "su", "binder", "call");
2016-09-13 00:19:07 +02:00
}
void otherToSU() {
// allowLog
2017-04-15 13:26:29 +02:00
sepol_allow("logd", "su", "dir", "search");
sepol_allow("logd", "su", "file", "read");
sepol_allow("logd", "su", "file", "open");
sepol_allow("logd", "su", "file", "getattr");
2016-09-13 00:19:07 +02:00
// suBackL0
2017-04-15 13:26:29 +02:00
sepol_allow("system_server", "su", "binder", "call");
sepol_allow("system_server", "su", "binder", "transfer");
2016-09-13 00:19:07 +02:00
// ES Explorer opens a sokcet
2017-04-15 13:26:29 +02:00
sepol_allow("untrusted_app", "su", "unix_stream_socket", "ioctl");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "read");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "getattr");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "write");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "setattr");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "lock");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "append");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "bind");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "connect");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "getopt");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "setopt");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "shutdown");
sepol_allow("untrusted_app", "su", "unix_stream_socket", "connectto");
2016-09-13 00:19:07 +02:00
// Any domain is allowed to send su "sigchld"
2017-04-15 13:26:29 +02:00
sepol_allow(ALL, "su", "process", "sigchld");
2016-09-13 00:19:07 +02:00
// uNetworkL0
2017-04-15 13:26:29 +02:00
sepol_attradd("su", "netdomain");
sepol_attradd("su", "bluetoothdomain");
2016-09-13 00:19:07 +02:00
// suBackL6
2017-04-15 13:26:29 +02:00
sepol_allow("surfaceflinger", "app_data_file", "dir", ALL);
sepol_allow("surfaceflinger", "app_data_file", "file", ALL);
sepol_allow("surfaceflinger", "app_data_file", "lnk_file", ALL);
sepol_attradd("surfaceflinger", "mlstrustedsubject");
2016-11-02 18:20:35 +01:00
// suMiscL6
2017-04-15 13:26:29 +02:00
if (sepol_exists("audioserver"))
sepol_allow("audioserver", "audioserver", "process", "execmem");
2016-09-13 00:19:07 +02:00
}
2017-04-15 13:26:29 +02:00
void sepol_full_rules() {
2016-09-13 00:19:07 +02:00
// Samsung specific
// Prevent system from loading policy
2017-04-15 13:26:29 +02:00
if(sepol_exists("knox_system_app"))
2016-09-13 00:19:07 +02:00
samsung();
2017-03-29 20:02:39 +02:00
// Min rules first
2017-04-15 13:26:29 +02:00
sepol_min_rules();
2017-03-29 20:02:39 +02:00
2016-09-13 00:19:07 +02:00
// Create domains if they don't exist
2017-04-15 13:26:29 +02:00
if (!sepol_exists("su_device"))
sepol_create("su_device");
sepol_enforce("su_device");
2016-09-13 00:19:07 +02:00
2017-01-31 23:00:55 +01:00
// Patch su to everything
2017-04-15 13:26:29 +02:00
sepol_allow("su", ALL, ALL, ALL);
2017-01-31 23:00:55 +01:00
2016-09-13 00:19:07 +02:00
// Autotransition su's socket to su_device
2017-04-15 13:26:29 +02:00
sepol_typetrans("su", "device", "file", "su_device", NULL);
sepol_typetrans("su", "device", "dir", "su_device", NULL);
sepol_allow("su_device", "tmpfs", "filesystem", "associate");
2016-09-13 00:19:07 +02:00
// Transition from untrusted_app to su_client
allowSuClient("shell");
allowSuClient("untrusted_app");
2016-10-02 16:48:49 +02:00
allowSuClient("system_app");
2016-09-13 00:19:07 +02:00
allowSuClient("platform_app");
2017-04-15 13:26:29 +02:00
if (sepol_exists("priv_app"))
2016-11-02 18:20:35 +01:00
allowSuClient("priv_app");
2017-04-15 13:26:29 +02:00
if (sepol_exists("ssd_tool"))
2016-09-13 00:19:07 +02:00
allowSuClient("ssd_tool");
// Allow init to execute su daemon/transition
2017-04-15 13:26:29 +02:00
sepol_allow("init", "su", "process", "transition");
sepol_allow("init", "su", "process", "rlimitinh");
sepol_allow("init", "su", "process", "siginh");
sepol_allow("init", "su", "process", "noatsecure");
2016-09-13 00:19:07 +02:00
suRights();
otherToSU();
// Need to set su_device/su as trusted to be accessible from other categories
2017-04-15 13:26:29 +02:00
sepol_attradd("su_device", "mlstrustedobject");
sepol_attradd("su", "mlstrustedsubject");
2016-11-11 20:03:59 +01:00
2016-10-02 16:48:49 +02:00
}
2016-11-02 18:20:35 +01:00
// Minimal to run Magisk script before live patching
2017-04-15 13:26:29 +02:00
void sepol_min_rules() {
2016-10-02 16:48:49 +02:00
2017-04-15 13:26:29 +02:00
if (!sepol_exists("su"))
sepol_create("su");
sepol_permissive("su");
sepol_permissive("init");
2016-10-02 16:48:49 +02:00
2017-04-15 13:26:29 +02:00
sepol_attradd("su", "mlstrustedsubject");
2016-10-02 16:48:49 +02:00
2017-03-29 20:02:39 +02:00
// Let init run stuffs in su context
2017-04-15 13:26:29 +02:00
sepol_allow("kernel", "su", "fd", "use");
sepol_allow("init", "su", "process", ALL);
sepol_allow("init", "system_file", "dir", ALL);
sepol_allow("init", "system_file", "lnk_file", ALL);
sepol_allow("init", "system_file", "file", ALL);
2016-11-02 18:20:35 +01:00
2017-03-29 20:02:39 +02:00
// Misc: basic shell scripts, prop management etc.
2017-04-15 13:26:29 +02:00
sepol_allow("su", "property_socket", "sock_file", "write");
if (sepol_exists("default_prop"))
sepol_allow("su", "default_prop", "property_service", "set");
sepol_allow("su", "init", "unix_stream_socket", "connectto");
sepol_allow("su", "su", "unix_dgram_socket", ALL);
sepol_allow("su", "su", "unix_stream_socket", ALL);
sepol_allow("su", "su", "process", ALL);
sepol_allow("su", "su", "capability", ALL);
sepol_allow("su", "su", "file", ALL);
sepol_allow("su", "su", "fifo_file", ALL);
sepol_allow("su", "su", "lnk_file", ALL);
sepol_allow("su", "su", "dir", ALL);
2016-11-02 18:20:35 +01:00
2017-03-29 20:02:39 +02:00
// Allow su to do anything to files/dir/links
2017-04-15 13:26:29 +02:00
sepol_allow("su", ALL, "file", ALL);
sepol_allow("su", ALL, "dir", ALL);
sepol_allow("su", ALL, "lnk_file", ALL);
2016-11-02 18:20:35 +01:00
// For sepolicy live patching
2017-04-15 13:26:29 +02:00
sepol_allow("su", "kernel", "security", "read_policy");
sepol_allow("su", "kernel", "security", "load_policy");
2016-10-02 16:48:49 +02:00
2017-03-29 20:02:39 +02:00
// For mounting loop devices and mirrors
2017-04-15 13:26:29 +02:00
sepol_allow("su", "kernel", "process", "setsched");
sepol_allow("su", "labeledfs", "filesystem", "mount");
sepol_allow("su", "labeledfs", "filesystem", "unmount");
sepol_allow("su", "loop_device", "blk_file", ALL);
sepol_allow("su", "block_device", "blk_file", ALL);
sepol_allow("su", "system_block_device", "blk_file", ALL);
2017-03-29 20:02:39 +02:00
2016-10-02 16:48:49 +02:00
// Xposed
2017-04-15 13:26:29 +02:00
sepol_allow("untrusted_app", "untrusted_app", "capability", "setgid");
sepol_allow("system_server", "dex2oat_exec", "file", ALL);
2016-10-02 16:48:49 +02:00
2017-01-31 17:51:45 +01:00
}