1*e4a36f41SAndroid Build Coastguard Worker# Life begins with the kernel. 2*e4a36f41SAndroid Build Coastguard Workertype kernel, domain, mlstrustedsubject; 3*e4a36f41SAndroid Build Coastguard Worker 4*e4a36f41SAndroid Build Coastguard Workerallow kernel self:global_capability_class_set sys_nice; 5*e4a36f41SAndroid Build Coastguard Worker 6*e4a36f41SAndroid Build Coastguard Worker# Root fs. 7*e4a36f41SAndroid Build Coastguard Workerr_dir_file(kernel, rootfs) 8*e4a36f41SAndroid Build Coastguard Workerallow kernel proc_cmdline:file r_file_perms; 9*e4a36f41SAndroid Build Coastguard Worker 10*e4a36f41SAndroid Build Coastguard Worker# Get SELinux enforcing status. 11*e4a36f41SAndroid Build Coastguard Workerallow kernel selinuxfs:dir r_dir_perms; 12*e4a36f41SAndroid Build Coastguard Workerallow kernel selinuxfs:file r_file_perms; 13*e4a36f41SAndroid Build Coastguard Worker 14*e4a36f41SAndroid Build Coastguard Worker# Get file contexts during first stage 15*e4a36f41SAndroid Build Coastguard Workerallow kernel file_contexts_file:file r_file_perms; 16*e4a36f41SAndroid Build Coastguard Worker 17*e4a36f41SAndroid Build Coastguard Worker# Allow init relabel itself. 18*e4a36f41SAndroid Build Coastguard Workerallow kernel rootfs:file relabelfrom; 19*e4a36f41SAndroid Build Coastguard Workerallow kernel init_exec:file relabelto; 20*e4a36f41SAndroid Build Coastguard Worker# TODO: investigate why we need this. 21*e4a36f41SAndroid Build Coastguard Workerallow kernel init:process share; 22*e4a36f41SAndroid Build Coastguard Worker 23*e4a36f41SAndroid Build Coastguard Worker# cgroup filesystem initialization prior to setting the cgroup root directory label. 24*e4a36f41SAndroid Build Coastguard Workerallow kernel unlabeled:dir search; 25*e4a36f41SAndroid Build Coastguard Worker 26*e4a36f41SAndroid Build Coastguard Worker# Mount usbfs. 27*e4a36f41SAndroid Build Coastguard Workerallow kernel usbfs:filesystem mount; 28*e4a36f41SAndroid Build Coastguard Workerallow kernel usbfs:dir search; 29*e4a36f41SAndroid Build Coastguard Worker 30*e4a36f41SAndroid Build Coastguard Worker# Initial setenforce by init prior to switching to init domain. 31*e4a36f41SAndroid Build Coastguard Worker# We use dontaudit instead of allow to prevent a kernel spawned userspace 32*e4a36f41SAndroid Build Coastguard Worker# process from turning off SELinux once enabled. 33*e4a36f41SAndroid Build Coastguard Workerdontaudit kernel self:security setenforce; 34*e4a36f41SAndroid Build Coastguard Worker 35*e4a36f41SAndroid Build Coastguard Worker# Write to /proc/1/oom_adj prior to switching to init domain. 36*e4a36f41SAndroid Build Coastguard Workerallow kernel self:global_capability_class_set sys_resource; 37*e4a36f41SAndroid Build Coastguard Worker 38*e4a36f41SAndroid Build Coastguard Worker# Init reboot before switching selinux domains under certain error 39*e4a36f41SAndroid Build Coastguard Worker# conditions. Allow it. 40*e4a36f41SAndroid Build Coastguard Worker# As part of rebooting, init writes "u" to /proc/sysrq-trigger to 41*e4a36f41SAndroid Build Coastguard Worker# remount filesystems read-only. /data is not mounted at this point, 42*e4a36f41SAndroid Build Coastguard Worker# so we could ignore this. For now, we allow it. 43*e4a36f41SAndroid Build Coastguard Workerallow kernel self:global_capability_class_set sys_boot; 44*e4a36f41SAndroid Build Coastguard Workerallow kernel proc_sysrq:file w_file_perms; 45*e4a36f41SAndroid Build Coastguard Worker 46*e4a36f41SAndroid Build Coastguard Worker# Allow writing to /dev/kmsg which was created prior to loading policy. 47*e4a36f41SAndroid Build Coastguard Workerallow kernel tmpfs:chr_file write; 48*e4a36f41SAndroid Build Coastguard Worker 49*e4a36f41SAndroid Build Coastguard Worker# Set checkreqprot by init.rc prior to switching to init domain. 50*e4a36f41SAndroid Build Coastguard Workerallow kernel selinuxfs:file write; 51*e4a36f41SAndroid Build Coastguard Workerallow kernel self:security setcheckreqprot; 52*e4a36f41SAndroid Build Coastguard Worker 53*e4a36f41SAndroid Build Coastguard Worker# kernel thread "loop0", used by the loop block device, for ASECs (b/17158723) 54*e4a36f41SAndroid Build Coastguard Workerallow kernel sdcard_type:file { read write }; 55*e4a36f41SAndroid Build Coastguard Worker 56*e4a36f41SAndroid Build Coastguard Worker# f_mtp driver accesses files from kernel context. 57*e4a36f41SAndroid Build Coastguard Workerallow kernel mediaprovider:fd use; 58*e4a36f41SAndroid Build Coastguard Worker 59*e4a36f41SAndroid Build Coastguard Worker# Allow the kernel to read OBB files from app directories. (b/17428116) 60*e4a36f41SAndroid Build Coastguard Worker# Kernel thread "loop0" reads a vold supplied file descriptor. 61*e4a36f41SAndroid Build Coastguard Worker# Fixes CTS tests: 62*e4a36f41SAndroid Build Coastguard Worker# * android.os.storage.cts.StorageManagerTest#testMountAndUnmountObbNormal 63*e4a36f41SAndroid Build Coastguard Worker# * android.os.storage.cts.StorageManagerTest#testMountAndUnmountTwoObbs 64*e4a36f41SAndroid Build Coastguard Workerallow kernel vold:fd use; 65*e4a36f41SAndroid Build Coastguard Workerallow kernel { app_data_file privapp_data_file }:file read; 66*e4a36f41SAndroid Build Coastguard Workerallow kernel asec_image_file:file read; 67*e4a36f41SAndroid Build Coastguard Worker 68*e4a36f41SAndroid Build Coastguard Worker# Allow mounting loop device in update_engine_unittests. (b/28319454) 69*e4a36f41SAndroid Build Coastguard Worker# and for LTP kernel tests (b/73220071) 70*e4a36f41SAndroid Build Coastguard Workeruserdebug_or_eng(` 71*e4a36f41SAndroid Build Coastguard Worker allow kernel update_engine_data_file:file { read write }; 72*e4a36f41SAndroid Build Coastguard Worker allow kernel nativetest_data_file:file { read write }; 73*e4a36f41SAndroid Build Coastguard Worker') 74*e4a36f41SAndroid Build Coastguard Worker 75*e4a36f41SAndroid Build Coastguard Worker# Access to /data/media. 76*e4a36f41SAndroid Build Coastguard Worker# This should be removed if sdcardfs is modified to alter the secontext for its 77*e4a36f41SAndroid Build Coastguard Worker# accesses to the underlying FS. 78*e4a36f41SAndroid Build Coastguard Workerallow kernel media_rw_data_file:dir create_dir_perms; 79*e4a36f41SAndroid Build Coastguard Workerallow kernel media_rw_data_file:file create_file_perms; 80*e4a36f41SAndroid Build Coastguard Worker 81*e4a36f41SAndroid Build Coastguard Worker# Access to /data/misc/vold/virtual_disk. 82*e4a36f41SAndroid Build Coastguard Workerallow kernel vold_data_file:file { read write }; 83*e4a36f41SAndroid Build Coastguard Worker 84*e4a36f41SAndroid Build Coastguard Worker# Allow the kernel to read APEX file descriptors and (staged) data files; 85*e4a36f41SAndroid Build Coastguard Worker# Needed because APEX uses the loopback driver, which issues requests from 86*e4a36f41SAndroid Build Coastguard Worker# a kernel thread in earlier kernel version. 87*e4a36f41SAndroid Build Coastguard Workerallow kernel apexd:fd use; 88*e4a36f41SAndroid Build Coastguard Workerallow kernel { 89*e4a36f41SAndroid Build Coastguard Worker apex_data_file 90*e4a36f41SAndroid Build Coastguard Worker staging_data_file 91*e4a36f41SAndroid Build Coastguard Worker vendor_apex_file 92*e4a36f41SAndroid Build Coastguard Worker}:file read; 93*e4a36f41SAndroid Build Coastguard Worker 94*e4a36f41SAndroid Build Coastguard Worker# Allow the first-stage init (which is running in the kernel domain) to execute the 95*e4a36f41SAndroid Build Coastguard Worker# dynamic linker when it re-executes /init to switch into the second stage. 96*e4a36f41SAndroid Build Coastguard Worker# Until Linux 4.8, the program interpreter (dynamic linker in this case) is executed 97*e4a36f41SAndroid Build Coastguard Worker# before the domain is switched to the target domain. So, we need to allow the kernel 98*e4a36f41SAndroid Build Coastguard Worker# domain (the source domain) to execute the dynamic linker (system_file type). 99*e4a36f41SAndroid Build Coastguard Worker# TODO(b/110147943) remove these allow rules when we no longer need to support Linux 100*e4a36f41SAndroid Build Coastguard Worker# kernel older than 4.8. 101*e4a36f41SAndroid Build Coastguard Workerallow kernel system_file:file execute; 102*e4a36f41SAndroid Build Coastguard Worker# The label for the dynamic linker is rootfs in the recovery partition. This is because 103*e4a36f41SAndroid Build Coastguard Worker# the recovery partition which is rootfs does not support xattr and thus labeling can't be 104*e4a36f41SAndroid Build Coastguard Worker# done at build-time. All files are by default labeled as rootfs upon booting. 105*e4a36f41SAndroid Build Coastguard Workerrecovery_only(` 106*e4a36f41SAndroid Build Coastguard Worker allow kernel rootfs:file execute; 107*e4a36f41SAndroid Build Coastguard Worker') 108*e4a36f41SAndroid Build Coastguard Worker 109*e4a36f41SAndroid Build Coastguard Worker# required by VTS lidbm unit test 110*e4a36f41SAndroid Build Coastguard Workerallow kernel appdomain_tmpfs:file { read write }; 111*e4a36f41SAndroid Build Coastguard Worker 112*e4a36f41SAndroid Build Coastguard Worker### 113*e4a36f41SAndroid Build Coastguard Worker### neverallow rules 114*e4a36f41SAndroid Build Coastguard Worker### 115*e4a36f41SAndroid Build Coastguard Worker 116*e4a36f41SAndroid Build Coastguard Worker# The initial task starts in the kernel domain (assigned via 117*e4a36f41SAndroid Build Coastguard Worker# initial_sid_contexts), but nothing ever transitions to it. 118*e4a36f41SAndroid Build Coastguard Workerneverallow * kernel:process { transition dyntransition }; 119*e4a36f41SAndroid Build Coastguard Worker 120*e4a36f41SAndroid Build Coastguard Worker# The kernel domain is never entered via an exec, nor should it 121*e4a36f41SAndroid Build Coastguard Worker# ever execute a program outside the rootfs without changing to another domain. 122*e4a36f41SAndroid Build Coastguard Worker# If you encounter an execute_no_trans denial on the kernel domain, then 123*e4a36f41SAndroid Build Coastguard Worker# possible causes include: 124*e4a36f41SAndroid Build Coastguard Worker# - The program is a kernel usermodehelper. In this case, define a domain 125*e4a36f41SAndroid Build Coastguard Worker# for the program and domain_auto_trans() to it. 126*e4a36f41SAndroid Build Coastguard Worker# - You are running an exploit which switched to the init task credentials 127*e4a36f41SAndroid Build Coastguard Worker# and is then trying to exec a shell or other program. You lose! 128*e4a36f41SAndroid Build Coastguard Workerneverallow kernel *:file { entrypoint execute_no_trans }; 129*e4a36f41SAndroid Build Coastguard Worker 130*e4a36f41SAndroid Build Coastguard Worker# the kernel should not be accessing files owned by other users. 131*e4a36f41SAndroid Build Coastguard Worker# Instead of adding dac_{read_search,override}, fix the unix permissions 132*e4a36f41SAndroid Build Coastguard Worker# on files being accessed. 133*e4a36f41SAndroid Build Coastguard Workerneverallow kernel self:global_capability_class_set { dac_override dac_read_search }; 134*e4a36f41SAndroid Build Coastguard Worker 135*e4a36f41SAndroid Build Coastguard Worker# Nobody should be ptracing kernel threads 136*e4a36f41SAndroid Build Coastguard Workerneverallow * kernel:process ptrace; 137