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