1# init is its own domain.
2type init, domain, domain_deprecated, mlstrustedsubject;
3tmpfs_domain(init)
4
5# The init domain is entered by execing init.
6type init_exec, exec_type, file_type;
7
8# /dev/__null__ node created by init.
9allow init tmpfs:chr_file create_file_perms;
10
11#
12# init direct restorecon calls.
13#
14# /dev/socket
15allow init { device socket_device }:dir relabelto;
16# /dev/__properties__
17allow init properties_device:dir relabelto;
18allow init properties_serial:file { write relabelto };
19allow init property_type:file { create_file_perms relabelto };
20
21# setrlimit
22allow init self:capability sys_resource;
23
24# Remove /dev/.booting, created before initial policy load or restorecon /dev.
25allow init tmpfs:file unlink;
26
27# Access pty created for fsck.
28allow init devpts:chr_file { read write open };
29
30# Create /dev/fscklogs files.
31allow init fscklogs:file create_file_perms;
32
33# Access /dev/__null__ node created prior to initial policy load.
34allow init tmpfs:chr_file write;
35
36# Access /dev/console.
37allow init console_device:chr_file rw_file_perms;
38
39# Access /dev/tty0.
40allow init tty_device:chr_file rw_file_perms;
41
42# Call mount(2).
43allow init self:capability sys_admin;
44
45# Create and mount on directories in /.
46allow init rootfs:dir create_dir_perms;
47allow init { rootfs cache_file cgroup storage_file system_data_file system_file }:dir mounton;
48
49# Mount on /dev/usb-ffs/adb.
50allow init device:dir mounton;
51
52# Create and remove symlinks in /.
53allow init rootfs:lnk_file { create unlink };
54
55# Mount debugfs on /sys/kernel/debug.
56allow init sysfs:dir mounton;
57
58# Create cgroups mount points in tmpfs and mount cgroups on them.
59allow init tmpfs:dir create_dir_perms;
60allow init tmpfs:dir mounton;
61allow init cgroup:dir create_dir_perms;
62allow init cpuctl_device:dir { create mounton };
63
64# /config
65allow init configfs:dir mounton;
66allow init configfs:dir create_dir_perms;
67
68# Use tmpfs as /data, used for booting when /data is encrypted
69allow init tmpfs:dir relabelfrom;
70
71# Create directories under /dev/cpuctl after chowning it to system.
72allow init self:capability dac_override;
73
74# Set system clock.
75allow init self:capability sys_time;
76
77allow init self:capability { sys_rawio mknod };
78
79# Mounting filesystems from block devices.
80allow init dev_type:blk_file r_file_perms;
81
82# Mounting filesystems.
83# Only allow relabelto for types used in context= mount options,
84# which should all be assigned the contextmount_type attribute.
85# This can be done in device-specific policy via type or typeattribute
86# declarations.
87allow init fs_type:filesystem ~relabelto;
88allow init unlabeled:filesystem ~relabelto;
89allow init contextmount_type:filesystem relabelto;
90
91# Allow read-only access to context= mounted filesystems.
92allow init contextmount_type:dir r_dir_perms;
93allow init contextmount_type:notdevfile_class_set r_file_perms;
94
95# restorecon /adb_keys or any other rootfs files and directories to a more
96# specific type.
97allow init rootfs:{ dir file } relabelfrom;
98
99# mkdir, symlink, write, rm/rmdir, chown/chmod, restorecon/restorecon_recursive from init.rc files.
100# chown/chmod require open+read+setattr required for open()+fchown/fchmod().
101# system/core/init.rc requires at least cache_file and data_file_type.
102# init.<board>.rc files often include device-specific types, so
103# we just allow all file types except /system files here.
104allow init self:capability { chown fowner fsetid };
105allow init {file_type -system_file -exec_type -app_data_file}:dir { create search getattr open read setattr ioctl };
106allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file -misc_logd_file }:dir { write add_name remove_name rmdir relabelfrom };
107allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file -misc_logd_file }:file { create getattr open read write setattr relabelfrom unlink };
108allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file -misc_logd_file }:{ sock_file fifo_file } { create getattr open read setattr relabelfrom unlink };
109allow init {file_type -system_file -exec_type -keystore_data_file -security_file -app_data_file -shell_data_file -vold_data_file -misc_logd_file }:lnk_file { create getattr setattr relabelfrom unlink };
110allow init {file_type -system_file -exec_type}:dir_file_class_set relabelto;
111allow init { sysfs debugfs debugfs_tracing }:{ dir file lnk_file } { getattr relabelfrom };
112allow init { sysfs_type debugfs_type }:{ dir file lnk_file } relabelto;
113allow init dev_type:dir create_dir_perms;
114allow init dev_type:lnk_file create;
115
116# Disable tracing by writing to /sys/kernel/debug/tracing/tracing_on
117allow init debugfs_tracing:file w_file_perms;
118
119# chown/chmod on pseudo files.
120allow init { fs_type -contextmount_type -sdcard_type -rootfs }:file { open read setattr };
121allow init { fs_type -contextmount_type -sdcard_type -rootfs }:dir  { open read setattr search };
122
123# chown/chmod on devices.
124allow init { dev_type -kmem_device }:chr_file { read open setattr };
125
126# Unlabeled file access for upgrades from 4.2.
127allow init unlabeled:dir { create_dir_perms relabelfrom };
128allow init unlabeled:notdevfile_class_set { create_file_perms relabelfrom };
129
130# Create /data/security from init.rc post-fs-data.
131allow init security_file:dir { create setattr };
132
133# Reload policy upon setprop selinux.reload_policy 1.
134# Note: this requires the following allow rule
135#   allow init kernel:security load_policy;
136# which can be configured on a device-by-device basis if needed.
137r_dir_file(init, security_file)
138
139# Any operation that can modify the kernel ring buffer, e.g. clear
140# or a read that consumes the messages that were read.
141allow init kernel:system syslog_mod;
142allow init self:capability2 syslog;
143
144# Set usermodehelpers and /proc security settings.
145allow init usermodehelper:file rw_file_perms;
146allow init proc_security:file rw_file_perms;
147
148# Write to /proc/sys/kernel/panic_on_oops.
149allow init proc:file w_file_perms;
150
151# Write to /proc/sys/net/ping_group_range and other /proc/sys/net files.
152allow init proc_net:file w_file_perms;
153allow init self:capability net_admin;
154
155# Write to /proc/sysrq-trigger.
156allow init proc_sysrq:file w_file_perms;
157
158# Reboot.
159allow init self:capability sys_boot;
160
161# Write to sysfs nodes.
162allow init sysfs_type:dir r_dir_perms;
163allow init sysfs_type:file w_file_perms;
164
165# disksize
166allow init sysfs_zram:file getattr;
167
168# Transitions to seclabel processes in init.rc
169domain_trans(init, rootfs, adbd)
170domain_trans(init, rootfs, healthd)
171domain_trans(init, rootfs, slideshow)
172recovery_only(`
173  domain_trans(init, rootfs, recovery)
174')
175domain_trans(init, shell_exec, shell)
176domain_trans(init, init_exec, ueventd)
177domain_trans(init, init_exec, watchdogd)
178# case where logpersistd is actually logcat -f in logd context (nee: logcatd)
179userdebug_or_eng(`
180  domain_auto_trans(init, logcat_exec, logd)
181')
182
183# Init will create /data/misc/logd when the property persist.logd.logpersistd is "logcatd".
184# Init will also walk through the directory as part of a recursive restorecon.
185allow init misc_logd_file:dir { open create read getattr setattr search };
186allow init misc_logd_file:file { getattr };
187
188# Support "adb shell stop"
189allow init self:capability kill;
190allow init domain:process { sigkill signal };
191
192# Init creates keystore's directory on boot, and walks through
193# the directory as part of a recursive restorecon.
194allow init keystore_data_file:dir { open create read getattr setattr search };
195allow init keystore_data_file:file { getattr };
196
197# Init creates vold's directory on boot, and walks through
198# the directory as part of a recursive restorecon.
199allow init vold_data_file:dir { open create read getattr setattr search };
200allow init vold_data_file:file { getattr };
201
202# Init creates /data/local/tmp at boot
203allow init shell_data_file:dir { open create read getattr setattr search };
204allow init shell_data_file:file { getattr };
205
206# Set UID and GID for services.
207allow init self:capability { setuid setgid };
208
209# For bootchart to read the /proc/$pid/cmdline file of each process,
210# we need to have following line to allow init to have access
211# to different domains.
212r_dir_file(init, domain)
213
214# Use setexeccon(), setfscreatecon(), and setsockcreatecon().
215# setexec is for services with seclabel options.
216# setfscreate is for labeling directories and socket files.
217# setsockcreate is for labeling local/unix domain sockets.
218allow init self:process { setexec setfscreate setsockcreate };
219
220# Perform SELinux access checks on setting properties.
221selinux_check_access(init)
222
223# Ask the kernel for the new context on services to label their sockets.
224allow init kernel:security compute_create;
225
226# Create sockets for the services.
227allow init domain:unix_stream_socket { create bind };
228allow init domain:unix_dgram_socket { create bind };
229
230# Create /data/property and files within it.
231allow init property_data_file:dir create_dir_perms;
232allow init property_data_file:file create_file_perms;
233
234# Set any property.
235allow init property_type:property_service set;
236
237# Run "ifup lo" to bring up the localhost interface
238allow init self:udp_socket { create ioctl };
239allow init self:capability net_raw;
240
241# This line seems suspect, as it should not really need to
242# set scheduling parameters for a kernel domain task.
243allow init kernel:process setsched;
244
245# swapon() needs write access to swap device
246# system/core/fs_mgr/fs_mgr.c - fs_mgr_swapon_all
247allow init swap_block_device:blk_file rw_file_perms;
248
249# Read from /dev/hw_random if present.
250# system/core/init/init.c - mix_hwrng_into_linux_rng_action
251allow init hw_random_device:chr_file r_file_perms;
252
253# Create and access /dev files without a specific type,
254# e.g. /dev/.coldboot_done, /dev/.booting
255# TODO:  Move these files into their own type unless they are
256# only ever accessed by init.
257allow init device:file create_file_perms;
258
259# Access character devices without a specific type,
260# e.g. /dev/keychord.
261# TODO: Move these devices into their own type unless they
262# are only ever accessed by init.
263allow init device:chr_file { rw_file_perms setattr };
264
265# keychord configuration
266allow init self:capability sys_tty_config;
267
268# Access device mapper for setting up dm-verity
269allow init dm_device:chr_file rw_file_perms;
270allow init dm_device:blk_file rw_file_perms;
271
272# Access metadata block device for storing dm-verity state
273allow init metadata_block_device:blk_file rw_file_perms;
274
275# Read /sys/fs/pstore/console-ramoops to detect restarts caused
276# by dm-verity detecting corrupted blocks
277allow init pstorefs:dir search;
278allow init pstorefs:file r_file_perms;
279allow init kernel:system syslog_read;
280
281# linux keyring configuration
282allow init init:key { write search setattr };
283
284# Allow init to create /data/unencrypted
285allow init unencrypted_data_file:dir create_dir_perms;
286
287unix_socket_connect(init, vold, vold)
288
289# Raw writes to misc block device
290allow init misc_block_device:blk_file w_file_perms;
291
292###
293### neverallow rules
294###
295
296# The init domain is only entered via setcon from the kernel domain,
297# never via an exec-based transition.
298neverallow domain init:process dyntransition;
299neverallow { domain -kernel} init:process transition;
300neverallow init { file_type fs_type -init_exec }:file entrypoint;
301
302# Never read/follow symlinks created by shell or untrusted apps.
303neverallow init shell_data_file:lnk_file read;
304neverallow init app_data_file:lnk_file read;
305
306# init should never execute a program without changing to another domain.
307neverallow init { file_type fs_type }:file execute_no_trans;
308
309# Init never adds or uses services via service_manager.
310neverallow init service_manager_type:service_manager { add find };
311neverallow init servicemanager:service_manager list;
312
313# Init should not be creating subdirectories in /data/local/tmp
314neverallow init shell_data_file:dir { write add_name remove_name };
315