1typeattribute kernel coredomain; 2 3domain_auto_trans(kernel, init_exec, init) 4domain_auto_trans(kernel, snapuserd_exec, snapuserd) 5userdebug_or_eng(` 6 domain_auto_trans(kernel, overlay_remounter_exec, overlay_remounter) 7') 8 9# Allow the kernel to read otapreopt_chroot's file descriptors and files under 10# /postinstall, as it uses apexd logic to mount APEX packages in /postinstall/apex. 11allow kernel otapreopt_chroot:fd use; 12allow kernel postinstall_file:file read; 13 14# The following sections are for the transition period during a Virtual A/B 15# OTA. Once sepolicy is loaded, snapuserd must be re-launched in the correct 16# context, and with properly labelled devices. This must be done before 17# enabling enforcement, eg, in permissive mode while still in the kernel 18# context. 19allow kernel tmpfs:blk_file { getattr relabelfrom }; 20allow kernel tmpfs:chr_file { getattr relabelfrom }; 21allow kernel tmpfs:lnk_file { getattr relabelfrom }; 22allow kernel tmpfs:dir { open read relabelfrom }; 23 24allow kernel block_device:blk_file relabelto; 25allow kernel block_device:lnk_file relabelto; 26allow kernel dm_device:chr_file relabelto; 27allow kernel dm_device:blk_file relabelto; 28allow kernel dm_user_device:dir { read open search relabelto }; 29allow kernel dm_user_device:chr_file relabelto; 30allow kernel kmsg_device:chr_file relabelto; 31allow kernel null_device:chr_file relabelto; 32allow kernel random_device:chr_file relabelto; 33allow kernel snapuserd_exec:file relabelto; 34 35allow kernel kmsg_device:chr_file write; 36allow kernel gsid:fd use; 37 38allow kernel self:global_capability_class_set sys_nice; 39 40# Root fs. 41r_dir_file(kernel, rootfs) 42 43# Used to read androidboot.selinux property 44allow kernel { 45 proc_bootconfig 46 proc_cmdline 47}:file r_file_perms; 48 49# Get SELinux enforcing status. 50allow kernel selinuxfs:dir r_dir_perms; 51allow kernel selinuxfs:file r_file_perms; 52 53# Get file contexts during first stage 54allow kernel file_contexts_file:file r_file_perms; 55 56# Allow init relabel itself. 57allow kernel rootfs:file relabelfrom; 58allow kernel init_exec:file relabelto; 59# TODO: investigate why we need this. 60allow kernel init:process share; 61 62# cgroup filesystem initialization prior to setting the cgroup root directory label. 63allow kernel unlabeled:dir search; 64 65# Mount usbfs. 66allow kernel usbfs:filesystem mount; 67allow kernel usbfs:dir search; 68 69# Initial setenforce by init prior to switching to init domain. 70# We use dontaudit instead of allow to prevent a kernel spawned userspace 71# process from turning off SELinux once enabled. 72dontaudit kernel self:security setenforce; 73 74# Write to /proc/1/oom_adj prior to switching to init domain. 75allow kernel self:global_capability_class_set sys_resource; 76 77# Init reboot before switching selinux domains under certain error 78# conditions. Allow it. 79# As part of rebooting, init writes "u" to /proc/sysrq-trigger to 80# remount filesystems read-only. /data is not mounted at this point, 81# so we could ignore this. For now, we allow it. 82allow kernel self:global_capability_class_set sys_boot; 83allow kernel proc_sysrq:file w_file_perms; 84 85# Allow writing to /dev/kmsg which was created prior to loading policy. 86allow kernel tmpfs:chr_file write; 87 88# Set checkreqprot by init.rc prior to switching to init domain. 89allow kernel selinuxfs:file write; 90allow kernel self:security setcheckreqprot; 91 92# kernel thread "loop0", used by the loop block device, for ASECs (b/17158723) 93allow kernel { sdcard_type fuse }:file { read write }; 94 95# f_mtp driver accesses files from kernel context. 96allow kernel mediaprovider:fd use; 97 98# Allow the kernel to read OBB files from app directories. (b/17428116) 99# Kernel thread "loop0" reads a vold supplied file descriptor. 100# Fixes CTS tests: 101# * android.os.storage.cts.StorageManagerTest#testMountAndUnmountObbNormal 102# * android.os.storage.cts.StorageManagerTest#testMountAndUnmountTwoObbs 103allow kernel vold:fd use; 104allow kernel { app_data_file privapp_data_file }:file read; 105allow kernel asec_image_file:file read; 106 107# Allow mounting loop device in update_engine_unittests. (b/28319454) 108# and for LTP kernel tests (b/73220071) 109userdebug_or_eng(` 110 allow kernel update_engine_data_file:file { read write }; 111 allow kernel nativetest_data_file:file { read write }; 112') 113 114# Access to /data/media. 115# This should be removed if sdcardfs is modified to alter the secontext for its 116# accesses to the underlying FS. 117allow kernel media_rw_data_file:dir create_dir_perms; 118allow kernel media_rw_data_file:file create_file_perms; 119 120# Access to /data/misc/vold/virtual_disk. 121allow kernel vold_data_file:file { read write }; 122 123# Allow the kernel to read APEX file descriptors and (staged) data files; 124# Needed because APEX uses the loopback driver, which issues requests from 125# a kernel thread in earlier kernel version. 126allow kernel apexd:fd use; 127allow kernel { 128 apex_data_file 129 staging_data_file 130 vendor_apex_file 131}:file read; 132# Also allow the kernel to read/write /data/local/tmp files via loop device 133# for ApexTestCases and fiemap_image_test. 134userdebug_or_eng(` 135 allow kernel shell_data_file:file { read write }; 136') 137 138# Allow the first-stage init (which is running in the kernel domain) to execute the 139# dynamic linker when it re-executes /init to switch into the second stage. 140# Until Linux 4.8, the program interpreter (dynamic linker in this case) is executed 141# before the domain is switched to the target domain. So, we need to allow the kernel 142# domain (the source domain) to execute the dynamic linker (system_file type). 143# TODO(b/110147943) remove these allow rules when we no longer need to support Linux 144# kernel older than 4.8. 145allow kernel system_file:file execute; 146# The label for the dynamic linker is rootfs in the recovery partition. This is because 147# the recovery partition which is rootfs does not support xattr and thus labeling can't be 148# done at build-time. All files are by default labeled as rootfs upon booting. 149recovery_only(` 150 allow kernel rootfs:file execute; 151') 152 153# required by VTS lidbm unit test 154allow kernel appdomain_tmpfs:file { read write }; 155 156# Allow first stage init to copy and then launch overlay_remounter 157userdebug_or_eng(` 158 allow kernel tmpfs:dir rw_dir_perms; 159 allow kernel tmpfs:file { create_file_perms relabelfrom }; 160 allow kernel overlay_remounter_exec:file { relabelto unlink }; 161 allow kernel overlay_remounter:process2 nosuid_transition; 162 allow kernel overlay_remounter:process share; 163') 164 165dontaudit kernel metadata_file:dir search; 166dontaudit kernel ota_metadata_file:dir rw_dir_perms; 167dontaudit kernel sysfs:dir r_dir_perms; 168dontaudit kernel sysfs:file { open read write }; 169dontaudit kernel sysfs:chr_file { open read write }; 170dontaudit kernel dm_device:chr_file ioctl; 171dontaudit kernel self:capability { sys_admin setgid mknod }; 172 173dontaudit kernel dm_user_device:dir { write add_name }; 174dontaudit kernel dm_user_device:chr_file { create setattr }; 175dontaudit kernel tmpfs:lnk_file read; 176dontaudit kernel tmpfs:blk_file { open read }; 177 178### 179### neverallow rules 180### 181 182# The initial task starts in the kernel domain (assigned via 183# initial_sid_contexts), but nothing ever transitions to it. 184neverallow * kernel:process { transition dyntransition }; 185 186# The kernel domain is never entered via an exec, nor should it 187# ever execute a program outside the rootfs without changing to another domain. 188# If you encounter an execute_no_trans denial on the kernel domain, then 189# possible causes include: 190# - The program is a kernel usermodehelper. In this case, define a domain 191# for the program and domain_auto_trans() to it. 192# - You are running an exploit which switched to the init task credentials 193# and is then trying to exec a shell or other program. You lose! 194neverallow kernel *:file { entrypoint execute_no_trans }; 195 196# the kernel should not be accessing files owned by other users. 197# Instead of adding dac_{read_search,override}, fix the unix permissions 198# on files being accessed. 199neverallow kernel self:global_capability_class_set { dac_override dac_read_search }; 200 201# Nobody should be ptracing kernel threads 202neverallow * kernel:process ptrace; 203