Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 1 | typeattribute init coredomain; |
| 2 | |
| 3 | tmpfs_domain(init) |
| 4 | |
| 5 | domain_trans(init, shell_exec, shell) |
| 6 | domain_trans(init, init_exec, ueventd) |
| 7 | domain_trans(init, init_exec, vendor_init) |
| 8 | |
| 9 | # Allow init to figure out name of dm-device from it's /dev/block/dm-XX path. |
| 10 | # This is useful in case of remounting ext4 userdata into checkpointing mode, |
| 11 | # since it potentially requires tearing down dm-devices (e.g. dm-bow, dm-crypto) |
| 12 | # that userdata is mounted onto. |
| 13 | allow init sysfs_dm:file read; |
| 14 | |
| 15 | # Second-stage init performs a test for whether the kernel has SELinux hooks |
| 16 | # for the perf_event_open() syscall. This is done by testing for the syscall |
| 17 | # outcomes corresponding to this policy. |
| 18 | allow init self:perf_event { open cpu }; |
| 19 | allow init self:global_capability2_class_set perfmon; |
| 20 | dontaudit init self:perf_event { kernel tracepoint read write }; |
| 21 | |
| 22 | # Allow init to restore contexts of vd_device(/dev/block/vd[..]) when labeling |
| 23 | # /dev/block. |
| 24 | allow init vd_device:blk_file relabelto; |
| 25 | |
| 26 | # chown/chmod on devices. |
| 27 | allow init { |
| 28 | dev_type |
| 29 | -hw_random_device |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 30 | }:chr_file setattr; |
| 31 | |
| 32 | # /dev/__null__ node created by init. |
| 33 | allow init tmpfs:chr_file { create setattr unlink rw_file_perms }; |
| 34 | |
Nate Myren | 0e15f2d | 2023-08-15 16:41:17 -0700 | [diff] [blame] | 35 | # /dev/__properties__ and /dev/__properties__/appcompat_override |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 36 | allow init properties_device:dir relabelto; |
| 37 | allow init properties_serial:file { write relabelto }; |
| 38 | allow init property_type:file { append create getattr map open read relabelto rename setattr unlink write }; |
Nate Myren | 0e15f2d | 2023-08-15 16:41:17 -0700 | [diff] [blame] | 39 | # /dev/__properties__/property_info and /dev/__properties__/appcompat_override/property_info |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 40 | allow init properties_device:file create_file_perms; |
| 41 | allow init property_info:file relabelto; |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 42 | # /dev/socket |
| 43 | allow init { device socket_device dm_user_device }:dir relabelto; |
Inseob Kim | 85091cd | 2022-03-22 21:50:09 +0900 | [diff] [blame] | 44 | # Relabel /dev nodes created in first stage init: /dev/console, /dev/null, /dev/ptmx, /dev/random |
| 45 | # and /dev/urandom |
| 46 | allow init { console_device null_device ptmx_device random_device } : chr_file relabelto; |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 47 | # /dev/device-mapper, /dev/block(/.*)? |
| 48 | allow init tmpfs:{ chr_file blk_file } relabelfrom; |
| 49 | allow init tmpfs:blk_file getattr; |
| 50 | allow init block_device:{ dir blk_file lnk_file } relabelto; |
| 51 | allow init dm_device:{ chr_file blk_file } relabelto; |
| 52 | allow init dm_user_device:chr_file relabelto; |
| 53 | allow init kernel:fd use; |
| 54 | # restorecon for early mount device symlinks |
| 55 | allow init tmpfs:lnk_file { getattr read relabelfrom }; |
| 56 | |
| 57 | # setrlimit |
| 58 | allow init self:global_capability_class_set sys_resource; |
| 59 | |
Alan Stokes | 3105e3e | 2023-08-23 16:25:50 +0100 | [diff] [blame] | 60 | # Set usermodehelpers. |
| 61 | allow init { usermodehelper sysfs_usermodehelper }:file rw_file_perms; |
| 62 | |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 63 | # Remove /dev/.booting and load /debug_ramdisk/* files |
| 64 | allow init tmpfs:file { getattr unlink }; |
| 65 | |
| 66 | # Access pty created for fsck. |
| 67 | allow init devpts:chr_file { read write open }; |
| 68 | |
| 69 | # Access /dev/__null__ node created prior to initial policy load. |
| 70 | allow init tmpfs:chr_file write; |
| 71 | |
| 72 | # Access /dev/console. |
| 73 | allow init console_device:chr_file rw_file_perms; |
| 74 | |
| 75 | # Access /dev/tty0. |
| 76 | allow init tty_device:chr_file rw_file_perms; |
| 77 | |
| 78 | # Call mount(2). |
| 79 | allow init self:global_capability_class_set sys_admin; |
| 80 | |
| 81 | # Call setns(2). |
| 82 | allow init self:global_capability_class_set sys_chroot; |
| 83 | |
| 84 | # Create and mount on directories in /. |
| 85 | allow init rootfs:dir create_dir_perms; |
| 86 | allow init { |
| 87 | rootfs |
| 88 | cgroup |
| 89 | linkerconfig_file |
| 90 | system_data_file |
| 91 | system_data_root_file |
| 92 | system_file |
| 93 | vendor_file |
| 94 | }:dir mounton; |
| 95 | |
| 96 | # Mount bpf fs on sys/fs/bpf |
| 97 | allow init fs_bpf:dir mounton; |
| 98 | |
| 99 | # Mount on /dev/usb-ffs/adb. |
| 100 | allow init device:dir mounton; |
| 101 | |
| 102 | # Mount tmpfs on /apex |
| 103 | allow init apex_mnt_dir:dir mounton; |
| 104 | |
| 105 | # Create and remove symlinks in /. |
| 106 | allow init rootfs:lnk_file { create unlink }; |
| 107 | |
| 108 | # Mount debugfs on /sys/kernel/debug. |
| 109 | allow init sysfs:dir mounton; |
| 110 | |
| 111 | # Create cgroups mount points in tmpfs and mount cgroups on them. |
| 112 | allow init tmpfs:dir create_dir_perms; |
| 113 | allow init tmpfs:dir mounton; |
| 114 | allow init cgroup:dir create_dir_perms; |
| 115 | allow init cgroup:file rw_file_perms; |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 116 | allow init cgroup_desc_file:file r_file_perms; |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 117 | allow init cgroup_v2:dir { mounton create_dir_perms}; |
| 118 | allow init cgroup_v2:file rw_file_perms; |
| 119 | |
| 120 | # Use tmpfs as /data, used for booting when /data is encrypted |
| 121 | allow init tmpfs:dir relabelfrom; |
| 122 | |
| 123 | # Create directories under /dev/cpuctl after chowning it to system. |
| 124 | allow init self:global_capability_class_set { dac_override dac_read_search }; |
| 125 | |
| 126 | allow init self:global_capability_class_set { sys_rawio mknod }; |
| 127 | |
| 128 | # Mounting filesystems from block devices. |
| 129 | allow init dev_type:blk_file r_file_perms; |
| 130 | allowxperm init dev_type:blk_file ioctl BLKROSET; |
| 131 | |
| 132 | # Mounting filesystems. |
| 133 | # Only allow relabelto for types used in context= mount options, |
| 134 | # which should all be assigned the contextmount_type attribute. |
| 135 | # This can be done in device-specific policy via type or typeattribute |
| 136 | # declarations. |
| 137 | allow init { |
| 138 | fs_type |
| 139 | }:filesystem ~relabelto; |
| 140 | |
| 141 | # Allow init to mount tracefs in /sys/kernel/tracing |
| 142 | allow init debugfs_tracing_debug:filesystem mount; |
| 143 | |
| 144 | allow init unlabeled:filesystem ~relabelto; |
| 145 | allow init contextmount_type:filesystem relabelto; |
| 146 | |
| 147 | # Allow read-only access to context= mounted filesystems. |
| 148 | allow init contextmount_type:dir r_dir_perms; |
| 149 | allow init contextmount_type:notdevfile_class_set r_file_perms; |
| 150 | |
| 151 | # restorecon /adb_keys or any other rootfs files and directories to a more |
| 152 | # specific type. |
| 153 | allow init rootfs:{ dir file } relabelfrom; |
| 154 | |
| 155 | # mkdir, symlink, write, rm/rmdir, chown/chmod, restorecon/restorecon_recursive from init.rc files. |
| 156 | # chown/chmod require open+read+setattr required for open()+fchown/fchmod(). |
| 157 | # system/core/init.rc requires at least cache_file and data_file_type. |
| 158 | # init.<board>.rc files often include device-specific types, so |
| 159 | # we just allow all file types except /system files here. |
| 160 | allow init self:global_capability_class_set { chown fowner fsetid }; |
| 161 | |
| 162 | allow init { |
| 163 | file_type |
| 164 | -exec_type |
| 165 | -system_file_type |
| 166 | -vendor_file_type |
| 167 | }:dir { create search getattr open read setattr ioctl }; |
| 168 | |
| 169 | allow init { |
| 170 | file_type |
| 171 | -exec_type |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 172 | -shell_data_file |
| 173 | -system_file_type |
| 174 | -vendor_file_type |
| 175 | }:dir { write add_name remove_name rmdir relabelfrom }; |
| 176 | |
| 177 | allow init { |
| 178 | file_type |
| 179 | -apex_info_file |
| 180 | -exec_type |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 181 | -shell_data_file |
| 182 | -system_file_type |
| 183 | -vendor_file_type |
| 184 | }:file { create getattr open read write setattr relabelfrom unlink map }; |
| 185 | |
| 186 | allow init tracefs_type:file { create_file_perms relabelfrom }; |
| 187 | |
| 188 | allow init { |
| 189 | file_type |
| 190 | -exec_type |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 191 | -shell_data_file |
| 192 | -system_file_type |
| 193 | -vendor_file_type |
| 194 | }:{ sock_file fifo_file } { create getattr open read setattr relabelfrom unlink }; |
| 195 | |
| 196 | allow init { |
| 197 | file_type |
| 198 | -apex_mnt_dir |
| 199 | -exec_type |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 200 | -shell_data_file |
| 201 | -system_file_type |
| 202 | -vendor_file_type |
| 203 | }:lnk_file { create getattr setattr relabelfrom unlink }; |
| 204 | |
| 205 | allow init { |
| 206 | file_type |
| 207 | -system_file_type |
| 208 | -vendor_file_type |
| 209 | -exec_type |
| 210 | }:dir_file_class_set relabelto; |
| 211 | |
| 212 | allow init { sysfs debugfs_tracing debugfs_tracing_debug }:{ dir file lnk_file } { getattr relabelfrom }; |
| 213 | allow init { sysfs_type tracefs_type }:{ dir file lnk_file } { relabelto getattr }; |
| 214 | allow init dev_type:dir create_dir_perms; |
| 215 | allow init dev_type:lnk_file create; |
| 216 | |
| 217 | # chown/chmod on pseudo files. |
| 218 | allow init { |
| 219 | fs_type |
| 220 | -contextmount_type |
| 221 | -proc_type |
| 222 | -fusefs_type |
| 223 | -sysfs_type |
| 224 | -rootfs |
| 225 | }:file { open read setattr }; |
| 226 | allow init { fs_type -contextmount_type -fusefs_type -rootfs }:dir { open read setattr search }; |
| 227 | |
| 228 | allow init { |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 229 | console_device |
| 230 | devpts |
| 231 | dm_device |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 232 | kmsg_device |
| 233 | null_device |
| 234 | owntty_device |
| 235 | ptmx_device |
| 236 | random_device |
| 237 | tty_device |
| 238 | zero_device |
| 239 | }:chr_file { read open }; |
| 240 | |
| 241 | # Any operation that can modify the kernel ring buffer, e.g. clear |
| 242 | # or a read that consumes the messages that were read. |
| 243 | allow init kernel:system syslog_mod; |
| 244 | allow init self:global_capability2_class_set syslog; |
| 245 | |
| 246 | # init access to /proc. |
| 247 | r_dir_file(init, proc_net_type) |
| 248 | allow init proc_filesystems:file r_file_perms; |
| 249 | |
| 250 | allow init { |
| 251 | proc # b/67049235 processes /proc/<pid>/* files are mislabeled. |
| 252 | proc_bootconfig |
| 253 | proc_cmdline |
| 254 | proc_diskstats |
| 255 | proc_kmsg # Open /proc/kmsg for logd service. |
| 256 | proc_meminfo |
| 257 | proc_stat # Read /proc/stat for bootchart. |
| 258 | proc_uptime |
| 259 | proc_version |
| 260 | }:file r_file_perms; |
| 261 | |
| 262 | allow init { |
| 263 | proc_abi |
| 264 | proc_dirty |
| 265 | proc_hostname |
| 266 | proc_hung_task |
| 267 | proc_extra_free_kbytes |
| 268 | proc_net_type |
| 269 | proc_max_map_count |
| 270 | proc_min_free_order_shift |
| 271 | proc_overcommit_memory # /proc/sys/vm/overcommit_memory |
| 272 | proc_panic |
| 273 | proc_page_cluster |
| 274 | proc_perf |
| 275 | proc_sched |
| 276 | proc_sysrq |
| 277 | }:file w_file_perms; |
| 278 | |
| 279 | allow init { |
| 280 | proc_security |
| 281 | }:file rw_file_perms; |
| 282 | |
| 283 | # init chmod/chown access to /proc files. |
| 284 | allow init { |
| 285 | proc_cmdline |
| 286 | proc_bootconfig |
| 287 | proc_kmsg |
| 288 | proc_net |
| 289 | proc_pagetypeinfo |
Bart Sears | dbca625 | 2024-08-04 23:06:24 +0000 | [diff] [blame] | 290 | proc_qtaguid_stat |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 291 | proc_slabinfo |
| 292 | proc_sysrq |
Bart Sears | dbca625 | 2024-08-04 23:06:24 +0000 | [diff] [blame] | 293 | proc_qtaguid_ctrl |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 294 | proc_vmallocinfo |
| 295 | }:file setattr; |
| 296 | |
| 297 | # init access to /sys files. |
| 298 | allow init { |
| 299 | sysfs_android_usb |
| 300 | sysfs_dm_verity |
| 301 | sysfs_leds |
| 302 | sysfs_power |
| 303 | sysfs_fs_f2fs |
| 304 | sysfs_dm |
Kalesh Singh | d60a38b | 2024-05-24 11:37:23 -0700 | [diff] [blame] | 305 | sysfs_pgsize_migration |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 306 | }:file w_file_perms; |
| 307 | |
| 308 | allow init { |
| 309 | sysfs_dt_firmware_android |
| 310 | sysfs_fs_ext4_features |
| 311 | }:file r_file_perms; |
| 312 | |
| 313 | allow init { |
| 314 | sysfs_zram |
| 315 | }:file rw_file_perms; |
| 316 | |
| 317 | # allow init to create loop devices with /dev/loop-control |
| 318 | allow init loop_control_device:chr_file rw_file_perms; |
| 319 | allow init loop_device:blk_file rw_file_perms; |
| 320 | allowxperm init loop_device:blk_file ioctl { |
| 321 | LOOP_SET_FD |
| 322 | LOOP_CLR_FD |
| 323 | LOOP_CTL_GET_FREE |
| 324 | LOOP_SET_BLOCK_SIZE |
| 325 | LOOP_SET_DIRECT_IO |
| 326 | LOOP_GET_STATUS |
| 327 | }; |
| 328 | |
| 329 | # init chmod/chown access to /sys files. |
| 330 | allow init { |
| 331 | sysfs_android_usb |
| 332 | sysfs_devices_system_cpu |
| 333 | sysfs_ipv4 |
| 334 | sysfs_leds |
| 335 | sysfs_lowmemorykiller |
| 336 | sysfs_power |
| 337 | sysfs_vibrator |
| 338 | sysfs_wake_lock |
| 339 | sysfs_zram |
| 340 | }:file setattr; |
| 341 | |
| 342 | allow init self:global_capability_class_set net_admin; |
| 343 | |
| 344 | # Reboot. |
| 345 | allow init self:global_capability_class_set sys_boot; |
| 346 | |
| 347 | # Support "adb shell stop" |
| 348 | allow init self:global_capability_class_set kill; |
| 349 | allow init domain:process { getpgid sigkill signal }; |
| 350 | |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 351 | # Init creates /data/local/tmp at boot |
| 352 | allow init shell_data_file:dir { open create read getattr setattr search }; |
| 353 | allow init shell_data_file:file { getattr }; |
| 354 | |
| 355 | # Set UID, GID, and adjust capability bounding set for services. |
| 356 | allow init self:global_capability_class_set { setuid setgid setpcap }; |
| 357 | |
| 358 | # For bootchart to read the /proc/$pid/cmdline file of each process, |
| 359 | # we need to have following line to allow init to have access |
| 360 | # to different domains. |
| 361 | r_dir_file(init, domain) |
| 362 | |
| 363 | # Use setexeccon(), setfscreatecon(), and setsockcreatecon(). |
| 364 | # setexec is for services with seclabel options. |
| 365 | # setfscreate is for labeling directories and socket files. |
| 366 | # setsockcreate is for labeling local/unix domain sockets. |
| 367 | allow init self:process { setexec setfscreate setsockcreate }; |
| 368 | |
| 369 | # Get file context |
| 370 | allow init file_contexts_file:file r_file_perms; |
| 371 | |
| 372 | # sepolicy access |
| 373 | allow init sepolicy_file:file r_file_perms; |
| 374 | |
| 375 | # Perform SELinux access checks on setting properties. |
| 376 | selinux_check_access(init) |
| 377 | |
| 378 | # Ask the kernel for the new context on services to label their sockets. |
| 379 | allow init kernel:security compute_create; |
| 380 | |
| 381 | # Create sockets for the services. |
| 382 | allow init domain:unix_stream_socket { create bind setopt }; |
| 383 | allow init domain:unix_dgram_socket { create bind setopt }; |
| 384 | |
| 385 | # Set any property. |
| 386 | allow init property_type:property_service set; |
| 387 | |
| 388 | # Send an SELinux userspace denial to the kernel audit subsystem, |
| 389 | # so it can be picked up and processed by logd. These denials are |
| 390 | # generated when an attempt to set a property is denied by policy. |
| 391 | allow init self:netlink_audit_socket { create_socket_perms_no_ioctl nlmsg_relay }; |
| 392 | allow init self:global_capability_class_set audit_write; |
| 393 | |
| 394 | # Run "ifup lo" to bring up the localhost interface |
| 395 | allow init self:udp_socket { create ioctl }; |
| 396 | # in addition to unpriv ioctls granted to all domains, init also needs: |
| 397 | allowxperm init self:udp_socket ioctl SIOCSIFFLAGS; |
| 398 | allow init self:global_capability_class_set net_raw; |
| 399 | |
| 400 | # Set scheduling info for psi monitor thread. |
| 401 | # TODO: delete or revise this line b/131761776 |
| 402 | allow init kernel:process { getsched setsched }; |
| 403 | |
| 404 | # Create and access /dev files without a specific type, |
| 405 | # e.g. /dev/.coldboot_done, /dev/.booting |
| 406 | # TODO: Move these files into their own type unless they are |
| 407 | # only ever accessed by init. |
| 408 | allow init device:file create_file_perms; |
| 409 | |
| 410 | # Access device mapper for setting up dm-verity |
| 411 | allow init dm_device:chr_file rw_file_perms; |
| 412 | allow init dm_device:blk_file rw_file_perms; |
| 413 | |
| 414 | # linux keyring configuration |
| 415 | allow init init:key { write search setattr }; |
| 416 | |
| 417 | r_dir_file(init, system_file) |
| 418 | r_dir_file(init, vendor_file_type) |
| 419 | |
| 420 | allow init system_data_file:file { getattr read }; |
| 421 | allow init system_data_file:lnk_file r_file_perms; |
| 422 | |
| 423 | # Allow init to touch PSI monitors |
| 424 | allow init proc_pressure_mem:file { rw_file_perms setattr }; |
| 425 | |
| 426 | # init is using bootstrap bionic |
Jiyong Park | 16c1ae3 | 2022-01-23 23:55:41 +0900 | [diff] [blame] | 427 | use_bootstrap_libs(init) |
Inseob Kim | e138997 | 2021-07-19 07:48:34 +0000 | [diff] [blame] | 428 | |
| 429 | # stat the root dir of fuse filesystems (for the mount handler) |
| 430 | allow init fuse:dir { search getattr }; |
| 431 | |
| 432 | set_prop(init, property_type) |
Inseob Kim | 4e141f6 | 2022-10-12 16:38:20 +0900 | [diff] [blame] | 433 | |
| 434 | allow init self:netlink_audit_socket { create_socket_perms_no_ioctl nlmsg_relay }; |
Jiyong Park | 02df74a | 2022-12-20 19:27:49 +0900 | [diff] [blame] | 435 | |
| 436 | # PRNG seeder daemon socket is created and listened on by init before forking. |
| 437 | allow init prng_seeder:unix_stream_socket { create bind listen }; |
Inseob Kim | 7bb2d4a | 2024-01-03 09:33:37 +0900 | [diff] [blame] | 438 | |
| 439 | # Allow init to load vendor modules |
| 440 | r_dir_file(init, vendor_kernel_modules) |
| 441 | allow init self:capability sys_module; |
| 442 | allow init vendor_kernel_modules:system module_load; |
Inseob Kim | 9bad60c | 2024-03-28 15:23:18 +0900 | [diff] [blame] | 443 | |
| 444 | allow init tmpfs:chr_file relabelfrom; |
| 445 | allow init kmsg_device:chr_file { getattr write relabelto }; |
| 446 | allow init kmsg_debug_device:chr_file { open write relabelto }; |