Magisk/rules.c

272 lines
12 KiB
C
Raw Normal View History

2017-03-18 08:52:38 +00:00
#include "magiskpolicy.h"
2017-04-19 20:28:56 +00:00
#include "sepolicy.h"
2016-09-12 22:19:07 +00:00
void samsung() {
2017-04-15 11:26:29 +00: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-12 22:19:07 +00:00
}
void allowSuClient(char *target) {
2017-04-15 18:29:42 +00:00
sepol_allow(target, "rootfs", "file", ALL);
sepol_allow(target, "rootfs", "lnk_file", ALL);
2017-04-15 11:26:29 +00:00
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");
2017-04-19 20:28:56 +00:00
sepol_allow(target, "devpts", "chr_file", "ioctl");
2017-04-15 18:29:42 +00:00
sepol_allow("su", target, "fd", "use");
sepol_allow("su", target, "fifo_file", ALL);
2016-09-12 22:19:07 +00:00
}
void suRights() {
2017-04-15 11:26:29 +00: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");
2017-04-15 20:11:02 +00:00
sepol_allow("system_server", "su", "fd", "use");
2017-04-15 18:29:42 +00:00
sepol_allow("su", "servicemanager", "dir", "search");
sepol_allow("su", "servicemanager", "dir", "read");
sepol_allow("su", "servicemanager", "file", "open");
sepol_allow("su", "servicemanager", "file", "read");
sepol_allow("su", "servicemanager", "process", "getattr");
sepol_allow("su", "servicemanager", "binder", "transfer");
sepol_allow("su", "servicemanager", "binder", "call");
sepol_allow("su", "system_server", "binder", "transfer");
sepol_allow("su", "system_server", "binder", "call");
2016-09-12 22:19:07 +00:00
}
void otherToSU() {
// allowLog
2017-04-15 11:26:29 +00: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-12 22:19:07 +00:00
// suBackL0
2017-04-15 11:26:29 +00:00
sepol_allow("system_server", "su", "binder", "call");
sepol_allow("system_server", "su", "binder", "transfer");
2016-09-12 22:19:07 +00:00
// ES Explorer opens a sokcet
2017-04-15 11:26:29 +00: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-12 22:19:07 +00:00
// Any domain is allowed to send su "sigchld"
2017-04-15 11:26:29 +00:00
sepol_allow(ALL, "su", "process", "sigchld");
2016-09-12 22:19:07 +00:00
// uNetworkL0
2017-04-15 11:26:29 +00:00
sepol_attradd("su", "netdomain");
sepol_attradd("su", "bluetoothdomain");
2016-09-12 22:19:07 +00:00
// suBackL6
2017-04-15 11:26:29 +00: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 17:20:35 +00:00
// suMiscL6
2017-04-15 11:26:29 +00:00
if (sepol_exists("audioserver"))
sepol_allow("audioserver", "audioserver", "process", "execmem");
2017-06-07 03:42:51 +00:00
// Liveboot
sepol_allow("surfaceflinger", "su", "process", "ptrace");
sepol_allow("surfaceflinger", "su", "binder", "transfer");
sepol_allow("surfaceflinger", "su", "binder", "call");
sepol_allow("surfaceflinger", "su", "fd", "use");
sepol_allow("debuggerd", "su", "process", "ptrace");
2016-09-12 22:19:07 +00:00
}
2017-04-15 18:29:42 +00:00
// Minimal boot image patch, Samsung requires these patches
2017-04-15 11:26:29 +00:00
void sepol_min_rules() {
if (!sepol_exists("su"))
sepol_create("su");
2017-04-15 18:29:42 +00:00
if (!sepol_exists("su_device"))
sepol_create("su_device");
2017-04-15 11:26:29 +00:00
sepol_permissive("su");
sepol_permissive("init");
2016-10-02 14:48:49 +00:00
2017-04-15 11:26:29 +00:00
sepol_attradd("su", "mlstrustedsubject");
2017-04-15 18:29:42 +00:00
sepol_attradd("su_device", "mlstrustedobject");
2016-10-02 14:48:49 +00:00
2017-03-29 18:02:39 +00:00
// Let init run stuffs in su context
2017-04-15 11:26:29 +00: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 17:20:35 +00:00
2017-04-15 18:29:42 +00:00
// Samsung specific
// Prevent system from loading policy
if(sepol_exists("knox_system_app"))
samsung();
2017-04-18 13:29:52 +00:00
// Shell, prop management, simple su rights, logs
2017-04-15 11:26:29 +00:00
sepol_allow("su", "property_socket", "sock_file", "write");
2017-04-21 22:28:56 +00:00
sepol_allow("su", "properties_device", "file", "write");
2017-04-18 13:29:52 +00:00
if (sepol_exists("default_prop")) {
2017-04-15 11:26:29 +00:00
sepol_allow("su", "default_prop", "property_service", "set");
2017-04-18 13:29:52 +00:00
sepol_allow("su", "default_prop", "file", "write");
}
if (sepol_exists("properties_serial"))
sepol_allow("su", "properties_serial", "file", "write");
2017-04-15 11:26:29 +00:00
sepol_allow("su", "init", "unix_stream_socket", "connectto");
2017-04-15 18:29:42 +00:00
sepol_allow("su", "rootfs", "file", "entrypoint");
2017-04-18 13:29:52 +00:00
sepol_allow("su", "rootfs", "file", "getattr");
sepol_allow("su", "rootfs", "file", "read");
sepol_allow("su", "rootfs", "filesystem", "remount");
2017-04-15 18:29:42 +00:00
sepol_allow("su", "devpts", "chr_file", ALL);
sepol_allow("su", "untrusted_app_devpts", "chr_file", ALL);
sepol_allow("su", "su_device", "dir", ALL);
sepol_allow("su", "su_device", "sock_file", ALL);
sepol_allow("su", "zygote_exec", "file", ALL);
sepol_allow("su", "zygote_exec", "lnk_file", ALL);
sepol_allow("su", "app_data_file", "dir", ALL);
sepol_allow("su", "app_data_file", "file", ALL);
sepol_allow("su", "shell_exec", "file", ALL);
2017-04-15 11:26:29 +00:00
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);
2017-04-21 22:28:56 +00:00
if (sepol_exists("toolbox_exec"))
sepol_allow("su", "toolbox_exec", "file", ALL);
2017-04-18 13:29:52 +00:00
if (sepol_exists("logdr_socket"))
sepol_allow("su", "logdr_socket", "sock_file", "write");
if (sepol_exists("logd"))
sepol_allow("su", "logd", "unix_stream_socket", "connectto");
2017-04-15 18:29:42 +00:00
sepol_allow("su_device", "tmpfs", "filesystem", "associate");
2016-11-02 17:20:35 +00:00
// For sepolicy live patching
2017-04-15 11:26:29 +00:00
sepol_allow("su", "kernel", "security", "read_policy");
sepol_allow("su", "kernel", "security", "load_policy");
2017-04-18 13:29:52 +00:00
sepol_allow("su", "selinuxfs", "file", "read");
sepol_allow("su", "selinuxfs", "file", "open");
sepol_allow("su", "selinuxfs", "file", "write");
2016-10-02 14:48:49 +00:00
2017-03-29 18:02:39 +00:00
// For mounting loop devices and mirrors
2017-04-15 11:26:29 +00:00
sepol_allow("su", "kernel", "process", "setsched");
sepol_allow("su", "labeledfs", "filesystem", "mount");
sepol_allow("su", "labeledfs", "filesystem", "unmount");
2017-04-18 13:29:52 +00:00
sepol_allow("kernel", "system_data_file", "file", "read");
2017-03-29 18:02:39 +00:00
2017-07-13 16:49:40 +00:00
// For changing attributes
sepol_allow("rootfs", "tmpfs", "filesystem", "associate");
2016-10-02 14:48:49 +00:00
// Xposed
2017-04-15 11:26:29 +00:00
sepol_allow("untrusted_app", "untrusted_app", "capability", "setgid");
sepol_allow("system_server", "dex2oat_exec", "file", ALL);
2017-04-19 20:28:56 +00:00
// xperms
if (policydb->policyvers >= POLICYDB_VERSION_XPERMS_IOCTL)
2017-04-21 22:28:56 +00:00
sepol_allowxperm("domain", "devpts", "chr_file", "0x5400-0x54FF");
2017-04-15 18:29:42 +00:00
}
2016-10-02 14:48:49 +00:00
2017-04-15 18:29:42 +00:00
void sepol_med_rules() {
sepol_min_rules();
// Allow su to do anything to any files/dir/links
sepol_allow("su", ALL, "file", ALL);
sepol_allow("su", ALL, "dir", ALL);
sepol_allow("su", ALL, "lnk_file", ALL);
2017-04-18 13:29:52 +00:00
sepol_allow("su", ALL, "blk_file", ALL);
2017-04-15 18:29:42 +00:00
// Allow these client to access su
allowSuClient("shell");
allowSuClient("untrusted_app");
allowSuClient("system_app");
allowSuClient("platform_app");
if (sepol_exists("priv_app"))
allowSuClient("priv_app");
if (sepol_exists("ssd_tool"))
allowSuClient("ssd_tool");
2017-04-15 20:11:02 +00:00
if (sepol_exists("untrusted_app_25"))
allowSuClient("untrusted_app_25");
2017-04-15 18:29:42 +00:00
suRights();
otherToSU();
2017-01-31 16:51:45 +00:00
}
2017-04-15 18:29:42 +00:00
void sepol_full_rules() {
// Patch su to everything
sepol_allow("su", ALL, ALL, ALL);
}