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