Nick Kralevich | c450759 | 2015-04-24 16:59:43 +0000 | [diff] [blame] | 1 | # init switches to init domain (via init.rc). |
Yongqin Liu | cc38e6d | 2014-12-05 13:40:22 +0800 | [diff] [blame] | 2 | type init, domain, mlstrustedsubject; |
Stephen Smalley | 2dd4e51 | 2012-01-04 12:33:27 -0500 | [diff] [blame] | 3 | tmpfs_domain(init) |
Nick Kralevich | 0c9708b | 2013-07-10 14:46:05 -0700 | [diff] [blame] | 4 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 5 | # setrlimit |
| 6 | allow init self:capability sys_resource; |
| 7 | |
| 8 | # Remove /dev/.booting, created before initial policy load or restorecon /dev. |
| 9 | allow init tmpfs:file unlink; |
| 10 | |
| 11 | # Access pty created for fsck. |
| 12 | allow init devpts:chr_file { read write open }; |
| 13 | |
| 14 | # Create /dev/fscklogs files. |
| 15 | allow init fscklogs:file create_file_perms; |
| 16 | |
| 17 | # Access /dev/__null__ node created prior to initial policy load. |
| 18 | allow init tmpfs:chr_file write; |
| 19 | |
| 20 | # Access /dev/console. |
| 21 | allow init console_device:chr_file rw_file_perms; |
| 22 | |
| 23 | # Access /dev/tty0. |
| 24 | allow init tty_device:chr_file rw_file_perms; |
| 25 | |
| 26 | # Call mount(2). |
| 27 | allow init self:capability sys_admin; |
| 28 | |
| 29 | # Create and mount on directories in /. |
| 30 | allow init rootfs:dir create_dir_perms; |
| 31 | allow init rootfs:dir mounton; |
| 32 | |
| 33 | # Mount on /dev/usb-ffs/adb. |
| 34 | allow init device:dir mounton; |
| 35 | |
Nick Kralevich | 8809341 | 2014-12-23 17:08:58 -0800 | [diff] [blame] | 36 | # Create and remove symlinks in /. |
| 37 | allow init rootfs:lnk_file { create unlink }; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 38 | |
| 39 | # Mount debugfs on /sys/kernel/debug. |
| 40 | allow init sysfs:dir mounton; |
| 41 | |
| 42 | # Create cgroups mount points in tmpfs and mount cgroups on them. |
| 43 | allow init tmpfs:dir create_dir_perms; |
| 44 | allow init tmpfs:dir mounton; |
| 45 | allow init cgroup:dir create_dir_perms; |
| 46 | allow init cpuctl_device:dir { create mounton }; |
| 47 | |
Nick Kralevich | 543facc | 2015-02-27 14:54:40 -0800 | [diff] [blame] | 48 | # Use tmpfs as /data, used for booting when /data is encrypted |
| 49 | allow init tmpfs:dir relabelfrom; |
| 50 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 51 | # Create directories under /dev/cpuctl after chowning it to system. |
| 52 | allow init self:capability dac_override; |
| 53 | |
| 54 | # Set system clock. |
| 55 | allow init self:capability sys_time; |
| 56 | |
Stephen Smalley | 5487ca0 | 2014-02-10 16:31:04 -0500 | [diff] [blame] | 57 | allow init self:capability { sys_rawio mknod }; |
| 58 | |
Stephen Smalley | 8a0c25e | 2014-09-23 09:11:30 -0400 | [diff] [blame] | 59 | # Mounting filesystems from block devices. |
| 60 | allow init dev_type:blk_file r_file_perms; |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 61 | |
| 62 | # Mounting filesystems. |
Stephen Smalley | 75e2ef9 | 2014-06-16 13:05:38 -0400 | [diff] [blame] | 63 | # Only allow relabelto for types used in context= mount options, |
| 64 | # which should all be assigned the contextmount_type attribute. |
| 65 | # This can be done in device-specific policy via type or typeattribute |
| 66 | # declarations. |
| 67 | allow init fs_type:filesystem ~relabelto; |
| 68 | allow init unlabeled:filesystem ~relabelto; |
| 69 | allow init contextmount_type:filesystem relabelto; |
| 70 | |
| 71 | # Allow read-only access to context= mounted filesystems. |
| 72 | allow init contextmount_type:dir r_dir_perms; |
| 73 | allow init contextmount_type:notdevfile_class_set r_file_perms; |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 74 | |
Stephen Smalley | c626a88 | 2014-06-23 09:17:51 -0400 | [diff] [blame] | 75 | # restorecon /adb_keys or any other rootfs files to a more specific type. |
| 76 | allow init rootfs:file relabelfrom; |
| 77 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 78 | # mkdir, symlink, write, rm/rmdir, chown/chmod, restorecon/restorecon_recursive from init.rc files. |
| 79 | # chown/chmod require open+read+setattr required for open()+fchown/fchmod(). |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 80 | # system/core/init.rc requires at least cache_file and data_file_type. |
| 81 | # init.<board>.rc files often include device-specific types, so |
| 82 | # we just allow all file types except /system files here. |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 83 | allow init self:capability { chown fowner fsetid }; |
| 84 | allow init {file_type -system_file -exec_type -app_data_file}:dir { create search getattr open read setattr }; |
Jeff Sharkey | 4423ecd | 2015-03-31 15:03:13 -0700 | [diff] [blame] | 85 | allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file}:dir { write add_name remove_name rmdir relabelfrom }; |
| 86 | allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file}:file { create getattr open read write setattr relabelfrom unlink }; |
| 87 | allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file}:{ sock_file fifo_file } { create getattr open read setattr relabelfrom unlink }; |
| 88 | allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file}:lnk_file { create getattr setattr relabelfrom unlink }; |
Nick Kralevich | 03ce512 | 2014-05-20 11:09:16 -0700 | [diff] [blame] | 89 | allow init {file_type -system_file -exec_type}:dir_file_class_set relabelto; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 90 | allow init sysfs:{ dir file lnk_file } { getattr relabelfrom }; |
Stephen Smalley | 3291565 | 2014-07-17 14:54:44 -0400 | [diff] [blame] | 91 | allow init sysfs_type:{ dir file lnk_file } relabelto; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 92 | allow init dev_type:dir create_dir_perms; |
| 93 | allow init dev_type:lnk_file create; |
| 94 | |
| 95 | # chown/chmod on pseudo files. |
Nick Kralevich | fcd8691 | 2015-02-06 13:29:25 -0800 | [diff] [blame] | 96 | allow init { fs_type -contextmount_type -sdcard_type -rootfs }:file { open read setattr }; |
| 97 | allow init { fs_type -contextmount_type -sdcard_type -rootfs }:dir { open read setattr search }; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 98 | |
| 99 | # chown/chmod on devices. |
Stephen Smalley | 1c16abd | 2014-11-10 12:15:33 -0500 | [diff] [blame] | 100 | allow init { dev_type -kmem_device }:chr_file { read open setattr }; |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 101 | |
Stephen Smalley | 73b0346 | 2014-05-30 09:53:00 -0400 | [diff] [blame] | 102 | # Unlabeled file access for upgrades from 4.2. |
| 103 | allow init unlabeled:dir { create_dir_perms relabelfrom }; |
| 104 | allow init unlabeled:notdevfile_class_set { create_file_perms relabelfrom }; |
| 105 | |
Stephen Smalley | 3235f61 | 2014-05-30 10:25:00 -0400 | [diff] [blame] | 106 | # Create /data/security from init.rc post-fs-data. |
| 107 | allow init security_file:dir { create setattr }; |
| 108 | |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 109 | # Reload policy upon setprop selinux.reload_policy 1. |
Stephen Smalley | 3235f61 | 2014-05-30 10:25:00 -0400 | [diff] [blame] | 110 | r_dir_file(init, security_file) |
Stephen Smalley | fea6e66 | 2013-12-06 08:05:53 -0500 | [diff] [blame] | 111 | allow init kernel:security load_policy; |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 112 | |
| 113 | # Any operation that can modify the kernel ring buffer, e.g. clear |
| 114 | # or a read that consumes the messages that were read. |
Nick Kralevich | 685e2f9 | 2014-05-28 13:48:52 -0700 | [diff] [blame] | 115 | allow init kernel:system syslog_mod; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 116 | allow init self:capability2 syslog; |
| 117 | |
Stephen Smalley | eb1bbf2 | 2014-05-29 14:35:55 -0400 | [diff] [blame] | 118 | # Set usermodehelpers and /proc security settings. |
Stephen Smalley | 7adb999 | 2013-12-06 09:31:40 -0500 | [diff] [blame] | 119 | allow init usermodehelper:file rw_file_perms; |
| 120 | allow init proc_security:file rw_file_perms; |
Nick Kralevich | fed8a2a | 2014-01-24 20:43:07 -0800 | [diff] [blame] | 121 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 122 | # Write to /proc/sys/kernel/panic_on_oops. |
| 123 | allow init proc:file w_file_perms; |
| 124 | |
| 125 | # Write to /proc/sys/net/ping_group_range and other /proc/sys/net files. |
Nick Kralevich | 5cf3994 | 2015-02-25 13:28:40 -0800 | [diff] [blame] | 126 | allow init proc_net:file w_file_perms; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 127 | allow init self:capability net_admin; |
| 128 | |
| 129 | # Write to /proc/sysrq-trigger. |
| 130 | allow init proc_sysrq:file w_file_perms; |
| 131 | |
| 132 | # Reboot. |
| 133 | allow init self:capability sys_boot; |
| 134 | |
| 135 | # Write to sysfs nodes. |
| 136 | allow init sysfs_type:dir r_dir_perms; |
| 137 | allow init sysfs_type:file w_file_perms; |
| 138 | |
Nick Kralevich | fed8a2a | 2014-01-24 20:43:07 -0800 | [diff] [blame] | 139 | # Transitions to seclabel processes in init.rc |
Nick Kralevich | a7c04dc | 2014-07-03 22:13:14 -0700 | [diff] [blame] | 140 | domain_trans(init, rootfs, adbd) |
| 141 | domain_trans(init, rootfs, healthd) |
Sami Tolvanen | 9d87c64 | 2015-02-18 13:59:38 +0000 | [diff] [blame] | 142 | domain_trans(init, rootfs, slideshow) |
Nick Kralevich | a7c04dc | 2014-07-03 22:13:14 -0700 | [diff] [blame] | 143 | recovery_only(` |
| 144 | domain_trans(init, rootfs, recovery) |
| 145 | ') |
| 146 | domain_trans(init, shell_exec, shell) |
Nick Kralevich | c450759 | 2015-04-24 16:59:43 +0000 | [diff] [blame] | 147 | domain_trans(init, rootfs, ueventd) |
| 148 | domain_trans(init, rootfs, watchdogd) |
Nick Kralevich | a7c04dc | 2014-07-03 22:13:14 -0700 | [diff] [blame] | 149 | |
Nick Kralevich | a7c04dc | 2014-07-03 22:13:14 -0700 | [diff] [blame] | 150 | # Support "adb shell stop" |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 151 | allow init self:capability kill; |
Nick Kralevich | a7c04dc | 2014-07-03 22:13:14 -0700 | [diff] [blame] | 152 | allow init domain:process sigkill; |
Nick Kralevich | cd905ec | 2014-05-08 23:28:52 -0700 | [diff] [blame] | 153 | |
| 154 | # Init creates keystore's directory on boot, and walks through |
| 155 | # the directory as part of a recursive restorecon. |
| 156 | allow init keystore_data_file:dir { open create read getattr setattr search }; |
| 157 | allow init keystore_data_file:file { getattr }; |
Stephen Smalley | 356f4be | 2014-05-23 11:26:19 -0400 | [diff] [blame] | 158 | |
Jeff Sharkey | 4423ecd | 2015-03-31 15:03:13 -0700 | [diff] [blame] | 159 | # Init creates vold's directory on boot, and walks through |
| 160 | # the directory as part of a recursive restorecon. |
| 161 | allow init vold_data_file:dir { open create read getattr setattr search }; |
| 162 | allow init vold_data_file:file { getattr }; |
| 163 | |
Nick Kralevich | ee49c0e | 2014-06-07 10:00:59 -0700 | [diff] [blame] | 164 | # Init creates /data/local/tmp at boot |
| 165 | allow init shell_data_file:dir { open create read getattr setattr search }; |
| 166 | allow init shell_data_file:file { getattr }; |
| 167 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 168 | # Set UID and GID for services. |
| 169 | allow init self:capability { setuid setgid }; |
| 170 | |
Yongqin Liu | cc38e6d | 2014-12-05 13:40:22 +0800 | [diff] [blame] | 171 | # For bootchart to read the /proc/$pid/cmdline file of each process, |
| 172 | # we need to have following line to allow init to have access |
| 173 | # to different domains. |
| 174 | r_dir_file(init, domain) |
| 175 | |
Stephen Smalley | 356f4be | 2014-05-23 11:26:19 -0400 | [diff] [blame] | 176 | # Use setexeccon(), setfscreatecon(), and setsockcreatecon(). |
| 177 | # setexec is for services with seclabel options. |
| 178 | # setfscreate is for labeling directories and socket files. |
| 179 | # setsockcreate is for labeling local/unix domain sockets. |
| 180 | allow init self:process { setexec setfscreate setsockcreate }; |
Stephen Smalley | ad0d0fc | 2014-05-29 09:22:16 -0400 | [diff] [blame] | 181 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 182 | # Perform SELinux access checks on setting properties. |
| 183 | selinux_check_access(init) |
| 184 | |
| 185 | # Ask the kernel for the new context on services to label their sockets. |
| 186 | allow init kernel:security compute_create; |
| 187 | |
| 188 | # Create sockets for the services. |
| 189 | allow init domain:unix_stream_socket { create bind }; |
| 190 | allow init domain:unix_dgram_socket { create bind }; |
| 191 | |
Stephen Smalley | ad0d0fc | 2014-05-29 09:22:16 -0400 | [diff] [blame] | 192 | # Create /data/property and files within it. |
| 193 | allow init property_data_file:dir create_dir_perms; |
| 194 | allow init property_data_file:file create_file_perms; |
Stephen Smalley | bac4ccc | 2014-06-18 10:09:35 -0400 | [diff] [blame] | 195 | |
Stephen Smalley | fee4915 | 2014-06-19 10:27:02 -0400 | [diff] [blame] | 196 | # Set any property. |
| 197 | allow init property_type:property_service set; |
| 198 | |
Nick Kralevich | 0db95cc | 2014-06-20 21:15:56 -0700 | [diff] [blame] | 199 | # Run "ifup lo" to bring up the localhost interface |
| 200 | allow init self:udp_socket { create ioctl }; |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 201 | allow init self:capability net_raw; |
Nick Kralevich | 0db95cc | 2014-06-20 21:15:56 -0700 | [diff] [blame] | 202 | |
Nick Kralevich | a7c04dc | 2014-07-03 22:13:14 -0700 | [diff] [blame] | 203 | # This line seems suspect, as it should not really need to |
| 204 | # set scheduling parameters for a kernel domain task. |
| 205 | allow init kernel:process setsched; |
| 206 | |
Nick Kralevich | beb279c | 2014-10-20 11:52:19 -0700 | [diff] [blame] | 207 | # swapon() needs write access to swap device |
| 208 | # system/core/fs_mgr/fs_mgr.c - fs_mgr_swapon_all |
| 209 | allow init swap_block_device:blk_file rw_file_perms; |
| 210 | |
Stephen Smalley | 0d08d47 | 2014-10-21 10:09:33 -0400 | [diff] [blame] | 211 | # Read from /dev/hw_random if present. |
| 212 | # system/core/init/init.c - mix_hwrng_into_linux_rng_action |
| 213 | allow init hw_random_device:chr_file r_file_perms; |
| 214 | |
| 215 | # Create and access /dev files without a specific type, |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 216 | # e.g. /dev/.coldboot_done, /dev/.booting |
Stephen Smalley | 0d08d47 | 2014-10-21 10:09:33 -0400 | [diff] [blame] | 217 | # TODO: Move these files into their own type unless they are |
| 218 | # only ever accessed by init. |
| 219 | allow init device:file create_file_perms; |
Stephen Smalley | 0d08d47 | 2014-10-21 10:09:33 -0400 | [diff] [blame] | 220 | |
| 221 | # Access character devices without a specific type, |
| 222 | # e.g. /dev/keychord. |
| 223 | # TODO: Move these devices into their own type unless they |
| 224 | # are only ever accessed by init. |
| 225 | allow init device:chr_file { rw_file_perms setattr }; |
Stephen Smalley | 0d08d47 | 2014-10-21 10:09:33 -0400 | [diff] [blame] | 226 | |
Stephen Smalley | a523aac | 2014-10-24 15:56:15 -0400 | [diff] [blame] | 227 | # keychord configuration |
| 228 | allow init self:capability sys_tty_config; |
| 229 | |
Sami Tolvanen | 35f537c | 2015-03-05 00:55:29 +0000 | [diff] [blame] | 230 | # Access device mapper for setting up dm-verity |
| 231 | allow init dm_device:chr_file rw_file_perms; |
| 232 | allow init dm_device:blk_file rw_file_perms; |
| 233 | |
| 234 | # Access metadata block device for storing dm-verity state |
| 235 | allow init metadata_block_device:blk_file rw_file_perms; |
| 236 | |
| 237 | # Read /sys/fs/pstore/console-ramoops to detect restarts caused |
| 238 | # by dm-verity detecting corrupted blocks |
| 239 | allow init pstorefs:dir search; |
| 240 | allow init pstorefs:file r_file_perms; |
| 241 | |
Paul Lawrence | 38af1da | 2015-03-11 15:44:14 -0700 | [diff] [blame] | 242 | # linux keyring configuration |
| 243 | allow init init:key { write search setattr }; |
| 244 | |
| 245 | # Allow init to link temp fs to unencrypted data on userdata |
| 246 | allow init tmpfs:lnk_file { create read getattr relabelfrom }; |
| 247 | |
| 248 | # Allow init to manipulate /data/unencrypted |
| 249 | allow init unencrypted_data_file:{ file lnk_file } create_file_perms; |
| 250 | allow init unencrypted_data_file:dir create_dir_perms; |
| 251 | |
| 252 | unix_socket_connect(init, vold, vold) |
| 253 | |
Stephen Smalley | bac4ccc | 2014-06-18 10:09:35 -0400 | [diff] [blame] | 254 | ### |
| 255 | ### neverallow rules |
| 256 | ### |
| 257 | |
| 258 | # The init domain is only entered via setcon from the kernel domain, |
| 259 | # never via an exec-based transition. |
Nick Kralevich | c450759 | 2015-04-24 16:59:43 +0000 | [diff] [blame] | 260 | neverallow { domain -kernel} init:process dyntransition; |
| 261 | neverallow domain init:process transition; |
| 262 | neverallow init { file_type fs_type }:file entrypoint; |
Stephen Smalley | 018e940 | 2014-09-02 17:05:44 -0400 | [diff] [blame] | 263 | |
| 264 | # Never read/follow symlinks created by shell or untrusted apps. |
| 265 | neverallow init shell_data_file:lnk_file read; |
| 266 | neverallow init app_data_file:lnk_file read; |
Stephen Smalley | 8a0c25e | 2014-09-23 09:11:30 -0400 | [diff] [blame] | 267 | |
| 268 | # init should never execute a program without changing to another domain. |
| 269 | neverallow init { file_type fs_type }:file execute_no_trans; |