xref: /aosp_15_r20/system/sepolicy/prebuilts/api/33.0/public/init.te (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1# init is its own domain.
2type init, domain, mlstrustedsubject;
3type init_exec, system_file_type, exec_type, file_type;
4type init_tmpfs, file_type;
5
6# /dev/__null__ node created by init.
7allow init tmpfs:chr_file { create setattr unlink rw_file_perms };
8
9#
10# init direct restorecon calls.
11#
12# /dev/kmsg
13allow init tmpfs:chr_file relabelfrom;
14allow init kmsg_device:chr_file { getattr write relabelto };
15# /dev/kmsg_debug
16userdebug_or_eng(`
17  allow init kmsg_debug_device:chr_file { open write relabelto };
18')
19
20# allow init to mount and unmount debugfs in debug builds
21userdebug_or_eng(`
22  allow init debugfs:dir mounton;
23')
24
25# /dev/__properties__
26allow init properties_device:dir relabelto;
27allow init properties_serial:file { write relabelto };
28allow init property_type:file { append create getattr map open read relabelto rename setattr unlink write };
29# /dev/__properties__/property_info
30allow init properties_device:file create_file_perms;
31allow init property_info:file relabelto;
32# /dev/event-log-tags
33allow init device:file relabelfrom;
34allow init runtime_event_log_tags_file:file { open write setattr relabelto create };
35# /dev/socket
36allow init { device socket_device dm_user_device }:dir relabelto;
37# allow init to establish connection and communicate with lmkd
38unix_socket_connect(init, lmkd, lmkd)
39# Relabel /dev nodes created in first stage init: /dev/console, /dev/null, /dev/ptmx, /dev/random
40# and /dev/urandom
41allow init { console_device null_device ptmx_device random_device } : chr_file relabelto;
42# /dev/device-mapper, /dev/block(/.*)?
43allow init tmpfs:{ chr_file blk_file } relabelfrom;
44allow init tmpfs:blk_file getattr;
45allow init block_device:{ dir blk_file lnk_file } relabelto;
46allow init dm_device:{ chr_file blk_file } relabelto;
47allow init dm_user_device:chr_file relabelto;
48allow init kernel:fd use;
49# restorecon for early mount device symlinks
50allow init tmpfs:lnk_file { getattr read relabelfrom };
51allow init {
52  metadata_block_device
53  misc_block_device
54  recovery_block_device
55  system_block_device
56  userdata_block_device
57}:{ blk_file lnk_file } relabelto;
58
59allow init super_block_device:lnk_file relabelto;
60
61# Create /mnt/sdcard -> /storage/self/primary symlink.
62allow init mnt_sdcard_file:lnk_file create;
63
64# setrlimit
65allow init self:global_capability_class_set sys_resource;
66
67# Remove /dev/.booting and load /debug_ramdisk/* files
68allow init tmpfs:file { getattr unlink };
69
70# Access pty created for fsck.
71allow init devpts:chr_file { read write open };
72
73# Create /dev/fscklogs files.
74allow init fscklogs:file create_file_perms;
75
76# Access /dev/__null__ node created prior to initial policy load.
77allow init tmpfs:chr_file write;
78
79# Access /dev/console.
80allow init console_device:chr_file rw_file_perms;
81
82# Access /dev/tty0.
83allow init tty_device:chr_file rw_file_perms;
84
85# Call mount(2).
86allow init self:global_capability_class_set sys_admin;
87
88# Call setns(2).
89allow init self:global_capability_class_set sys_chroot;
90
91# Create and mount on directories in /.
92allow init rootfs:dir create_dir_perms;
93allow init {
94    rootfs
95    cache_file
96    cgroup
97    linkerconfig_file
98    storage_file
99    mnt_user_file
100    system_data_file
101    system_data_root_file
102    system_dlkm_file
103    system_file
104    vendor_file
105    postinstall_mnt_dir
106    mirror_data_file
107}:dir mounton;
108
109# Mount bpf fs on sys/fs/bpf
110allow init fs_bpf:dir mounton;
111
112# Mount on /dev/usb-ffs/adb.
113allow init device:dir mounton;
114
115# Mount tmpfs on /apex
116allow init apex_mnt_dir:dir mounton;
117
118# Bind-mount on /system/apex/com.android.art
119allow init art_apex_dir:dir mounton;
120
121# Create and remove symlinks in /.
122allow init rootfs:lnk_file { create unlink };
123
124# Mount debugfs on /sys/kernel/debug.
125allow init sysfs:dir mounton;
126
127# Create cgroups mount points in tmpfs and mount cgroups on them.
128allow init tmpfs:dir create_dir_perms;
129allow init tmpfs:dir mounton;
130allow init cgroup:dir create_dir_perms;
131allow init cgroup:file rw_file_perms;
132allow init cgroup_rc_file:file rw_file_perms;
133allow init cgroup_desc_file:file r_file_perms;
134allow init cgroup_desc_api_file:file r_file_perms;
135allow init vendor_cgroup_desc_file:file r_file_perms;
136allow init cgroup_v2:dir { mounton create_dir_perms};
137allow init cgroup_v2:file rw_file_perms;
138
139# /config
140allow init configfs:dir mounton;
141allow init configfs:dir create_dir_perms;
142allow init configfs:{ file lnk_file } create_file_perms;
143
144# /metadata
145allow init metadata_file:dir mounton;
146
147# Run restorecon on /dev
148allow init tmpfs:dir relabelfrom;
149
150# Create directories under /dev/cpuctl after chowning it to system.
151allow init self:global_capability_class_set { dac_override dac_read_search };
152
153# Set system clock.
154allow init self:global_capability_class_set sys_time;
155
156allow init self:global_capability_class_set { sys_rawio mknod };
157
158# Mounting filesystems from block devices.
159allow init dev_type:blk_file r_file_perms;
160allowxperm init dev_type:blk_file ioctl BLKROSET;
161
162# Mounting filesystems.
163# Only allow relabelto for types used in context= mount options,
164# which should all be assigned the contextmount_type attribute.
165# This can be done in device-specific policy via type or typeattribute
166# declarations.
167allow init {
168  fs_type
169  enforce_debugfs_restriction(`-debugfs_type')
170}:filesystem ~relabelto;
171
172# Allow init to mount/unmount debugfs in non-user builds.
173enforce_debugfs_restriction(`
174  userdebug_or_eng(`allow init debugfs_type:filesystem { mount unmount };')
175')
176
177# Allow init to mount tracefs in /sys/kernel/tracing
178allow init debugfs_tracing_debug:filesystem mount;
179
180allow init unlabeled:filesystem ~relabelto;
181allow init contextmount_type:filesystem relabelto;
182
183# Allow read-only access to context= mounted filesystems.
184allow init contextmount_type:dir r_dir_perms;
185allow init contextmount_type:notdevfile_class_set r_file_perms;
186
187# restorecon /adb_keys or any other rootfs files and directories to a more
188# specific type.
189allow init rootfs:{ dir file } relabelfrom;
190
191# mkdir, symlink, write, rm/rmdir, chown/chmod, restorecon/restorecon_recursive from init.rc files.
192# chown/chmod require open+read+setattr required for open()+fchown/fchmod().
193# system/core/init.rc requires at least cache_file and data_file_type.
194# init.<board>.rc files often include device-specific types, so
195# we just allow all file types except /system files here.
196allow init self:global_capability_class_set { chown fowner fsetid };
197
198allow init {
199  file_type
200  -app_data_file
201  -exec_type
202  -misc_logd_file
203  -nativetest_data_file
204  -privapp_data_file
205  -system_app_data_file
206  -system_dlkm_file_type
207  -system_file_type
208  -vendor_file_type
209}:dir { create search getattr open read setattr ioctl };
210
211allow init {
212  file_type
213  -app_data_file
214  -exec_type
215  -iorapd_data_file
216  -credstore_data_file
217  -keystore_data_file
218  -misc_logd_file
219  -nativetest_data_file
220  -privapp_data_file
221  -shell_data_file
222  -system_app_data_file
223  -system_dlkm_file_type
224  -system_file_type
225  -vendor_file_type
226  -vold_data_file
227}:dir { write add_name remove_name rmdir relabelfrom };
228
229allow init {
230  file_type
231  -apex_info_file
232  -app_data_file
233  -exec_type
234  -gsi_data_file
235  -iorapd_data_file
236  -credstore_data_file
237  -keystore_data_file
238  -misc_logd_file
239  -nativetest_data_file
240  -privapp_data_file
241  -runtime_event_log_tags_file
242  -shell_data_file
243  -system_app_data_file
244  -system_dlkm_file_type
245  -system_file_type
246  -vendor_file_type
247  -vold_data_file
248  enforce_debugfs_restriction(`-debugfs_type')
249}:file { create getattr open read write setattr relabelfrom unlink map };
250
251allow init tracefs_type:file { create_file_perms relabelfrom };
252
253allow init {
254  file_type
255  -app_data_file
256  -exec_type
257  -gsi_data_file
258  -iorapd_data_file
259  -credstore_data_file
260  -keystore_data_file
261  -misc_logd_file
262  -nativetest_data_file
263  -privapp_data_file
264  -shell_data_file
265  -system_app_data_file
266  -system_dlkm_file_type
267  -system_file_type
268  -vendor_file_type
269  -vold_data_file
270}:{ sock_file fifo_file } { create getattr open read setattr relabelfrom unlink };
271
272allow init {
273  file_type
274  -apex_mnt_dir
275  -app_data_file
276  -exec_type
277  -gsi_data_file
278  -iorapd_data_file
279  -credstore_data_file
280  -keystore_data_file
281  -misc_logd_file
282  -nativetest_data_file
283  -privapp_data_file
284  -shell_data_file
285  -system_app_data_file
286  -system_dlkm_file_type
287  -system_file_type
288  -vendor_file_type
289  -vold_data_file
290}:lnk_file { create getattr setattr relabelfrom unlink };
291
292allow init cache_file:lnk_file r_file_perms;
293
294allow init {
295  file_type
296  -system_dlkm_file_type
297  -system_file_type
298  -vendor_file_type
299  -exec_type
300  -app_data_file
301  -privapp_data_file
302}:dir_file_class_set relabelto;
303
304allow init { sysfs no_debugfs_restriction(`debugfs') debugfs_tracing debugfs_tracing_debug }:{ dir file lnk_file } { getattr relabelfrom };
305allow init { sysfs_type no_debugfs_restriction(`debugfs_type') tracefs_type }:{ dir file lnk_file } { relabelto getattr };
306allow init dev_type:dir create_dir_perms;
307allow init dev_type:lnk_file create;
308
309# Disable tracing by writing to /sys/kernel/debug/tracing/tracing_on
310allow init debugfs_tracing:file w_file_perms;
311
312# Setup and control wifi event tracing (see wifi-events.rc)
313allow init debugfs_tracing_instances:dir create_dir_perms;
314allow init debugfs_tracing_instances:file w_file_perms;
315allow init debugfs_wifi_tracing:file w_file_perms;
316
317# chown/chmod on pseudo files.
318allow init {
319  fs_type
320  -contextmount_type
321  -keychord_device
322  -proc_type
323  -sdcard_type
324  -fusefs_type
325  -sysfs_type
326  -rootfs
327  enforce_debugfs_restriction(`-debugfs_type')
328}:file { open read setattr };
329allow init { fs_type -contextmount_type -sdcard_type -fusefs_type -rootfs }:dir  { open read setattr search };
330
331allow init {
332  binder_device
333  console_device
334  devpts
335  dm_device
336  hwbinder_device
337  input_device
338  kmsg_device
339  null_device
340  owntty_device
341  pmsg_device
342  ptmx_device
343  random_device
344  tty_device
345  zero_device
346}:chr_file { read open };
347
348# Unlabeled file access for upgrades from 4.2.
349allow init unlabeled:dir { create_dir_perms relabelfrom };
350allow init unlabeled:notdevfile_class_set { create_file_perms relabelfrom };
351
352# Any operation that can modify the kernel ring buffer, e.g. clear
353# or a read that consumes the messages that were read.
354allow init kernel:system syslog_mod;
355allow init self:global_capability2_class_set syslog;
356
357# init access to /proc.
358r_dir_file(init, proc_net_type)
359allow init proc_filesystems:file r_file_perms;
360
361userdebug_or_eng(`
362  # Overlayfs workdir write access check during mount to permit remount,rw
363  allow init overlayfs_file:dir { relabelfrom mounton write };
364  allow init overlayfs_file:file { append };
365  allow init system_block_device:blk_file { write };
366')
367
368allow init {
369  proc # b/67049235 processes /proc/<pid>/* files are mislabeled.
370  proc_bootconfig
371  proc_cmdline
372  proc_diskstats
373  proc_kmsg # Open /proc/kmsg for logd service.
374  proc_meminfo
375  proc_stat # Read /proc/stat for bootchart.
376  proc_uptime
377  proc_version
378}:file r_file_perms;
379
380allow init {
381  proc_abi
382  proc_bpf
383  proc_cpu_alignment
384  proc_dirty
385  proc_hostname
386  proc_hung_task
387  proc_extra_free_kbytes
388  proc_net_type
389  proc_max_map_count
390  proc_min_free_order_shift
391  proc_overcommit_memory      # /proc/sys/vm/overcommit_memory
392  proc_panic
393  proc_page_cluster
394  proc_perf
395  proc_sched
396  proc_sysrq
397  proc_watermark_boost_factor
398}:file w_file_perms;
399
400allow init {
401  proc_security
402}:file rw_file_perms;
403
404# init chmod/chown access to /proc files.
405allow init {
406  proc_cmdline
407  proc_bootconfig
408  proc_kmsg
409  proc_net
410  proc_pagetypeinfo
411  proc_qtaguid_stat
412  proc_slabinfo
413  proc_sysrq
414  proc_qtaguid_ctrl
415  proc_vmallocinfo
416}:file setattr;
417
418# init access to /sys files.
419allow init {
420  sysfs_android_usb
421  sysfs_dm_verity
422  sysfs_leds
423  sysfs_power
424  sysfs_fs_f2fs
425  sysfs_dm
426  sysfs_lru_gen_enabled
427}:file w_file_perms;
428
429allow init {
430  sysfs_dt_firmware_android
431  sysfs_fs_ext4_features
432}:file r_file_perms;
433
434allow init {
435  sysfs_zram
436}:file rw_file_perms;
437
438# allow init to create loop devices with /dev/loop-control
439allow init loop_control_device:chr_file rw_file_perms;
440allow init loop_device:blk_file rw_file_perms;
441allowxperm init loop_device:blk_file ioctl {
442  LOOP_SET_FD
443  LOOP_CLR_FD
444  LOOP_CTL_GET_FREE
445  LOOP_SET_BLOCK_SIZE
446  LOOP_SET_DIRECT_IO
447  LOOP_GET_STATUS
448  LOOP_SET_STATUS64
449};
450
451# Allow init to write to vibrator/trigger
452allow init sysfs_vibrator:file w_file_perms;
453
454# init chmod/chown access to /sys files.
455allow init {
456  sysfs_android_usb
457  sysfs_devices_system_cpu
458  sysfs_ipv4
459  sysfs_leds
460  sysfs_lowmemorykiller
461  sysfs_power
462  sysfs_vibrator
463  sysfs_wake_lock
464  sysfs_zram
465}:file setattr;
466
467# Set usermodehelpers.
468allow init { usermodehelper sysfs_usermodehelper }:file rw_file_perms;
469
470allow init self:global_capability_class_set net_admin;
471
472# Reboot.
473allow init self:global_capability_class_set sys_boot;
474
475# Init will create /data/misc/logd when the property persist.logd.logpersistd is "logcatd".
476# Init will also walk through the directory as part of a recursive restorecon.
477allow init misc_logd_file:dir { add_name open create read getattr setattr search write };
478allow init misc_logd_file:file { open create getattr setattr write };
479
480# Support "adb shell stop"
481allow init self:global_capability_class_set kill;
482allow init domain:process { getpgid sigkill signal };
483
484# Init creates credstore's directory on boot, and walks through
485# the directory as part of a recursive restorecon.
486allow init credstore_data_file:dir { open create read getattr setattr search };
487allow init credstore_data_file:file { getattr };
488
489# Init creates keystore's directory on boot, and walks through
490# the directory as part of a recursive restorecon.
491allow init keystore_data_file:dir { open create read getattr setattr search };
492allow init keystore_data_file:file { getattr };
493
494# Init creates vold's directory on boot, and walks through
495# the directory as part of a recursive restorecon.
496allow init vold_data_file:dir { open create read getattr setattr search };
497allow init vold_data_file:file { getattr };
498
499# Init creates /data/local/tmp at boot
500allow init shell_data_file:dir { open create read getattr setattr search };
501allow init shell_data_file:file { getattr };
502
503# Set UID, GID, and adjust capability bounding set for services.
504allow init self:global_capability_class_set { setuid setgid setpcap };
505
506# For bootchart to read the /proc/$pid/cmdline file of each process,
507# we need to have following line to allow init to have access
508# to different domains.
509r_dir_file(init, domain)
510
511# Use setexeccon(), setfscreatecon(), and setsockcreatecon().
512# setexec is for services with seclabel options.
513# setfscreate is for labeling directories and socket files.
514# setsockcreate is for labeling local/unix domain sockets.
515allow init self:process { setexec setfscreate setsockcreate };
516
517# Get file context
518allow init file_contexts_file:file r_file_perms;
519
520# sepolicy access
521allow init sepolicy_file:file r_file_perms;
522
523# Perform SELinux access checks on setting properties.
524selinux_check_access(init)
525
526# Ask the kernel for the new context on services to label their sockets.
527allow init kernel:security compute_create;
528
529# Create sockets for the services.
530allow init domain:unix_stream_socket { create bind setopt };
531allow init domain:unix_dgram_socket { create bind setopt };
532
533# Create /data/property and files within it.
534allow init property_data_file:dir create_dir_perms;
535allow init property_data_file:file create_file_perms;
536
537# Set any property.
538allow init property_type:property_service set;
539
540# Send an SELinux userspace denial to the kernel audit subsystem,
541# so it can be picked up and processed by logd. These denials are
542# generated when an attempt to set a property is denied by policy.
543allow init self:netlink_audit_socket { create_socket_perms_no_ioctl nlmsg_relay };
544allow init self:global_capability_class_set audit_write;
545
546# Run "ifup lo" to bring up the localhost interface
547allow init self:udp_socket { create ioctl };
548# in addition to unpriv ioctls granted to all domains, init also needs:
549allowxperm init self:udp_socket ioctl SIOCSIFFLAGS;
550allow init self:global_capability_class_set net_raw;
551
552# Set scheduling info for psi monitor thread.
553# TODO: delete or revise this line b/131761776
554allow init kernel:process { getsched setsched };
555
556# swapon() needs write access to swap device
557# system/core/fs_mgr/fs_mgr.c - fs_mgr_swapon_all
558allow init swap_block_device:blk_file rw_file_perms;
559
560# Create and access /dev files without a specific type,
561# e.g. /dev/.coldboot_done, /dev/.booting
562# TODO:  Move these files into their own type unless they are
563# only ever accessed by init.
564allow init device:file create_file_perms;
565
566# keychord retrieval from /dev/input/ devices
567allow init input_device:dir r_dir_perms;
568allow init input_device:chr_file rw_file_perms;
569
570# Access device mapper for setting up dm-verity
571allow init dm_device:chr_file rw_file_perms;
572allow init dm_device:blk_file rw_file_perms;
573
574# Access dm-user for OTA boot
575allow init dm_user_device:chr_file rw_file_perms;
576
577# Access metadata block device for storing dm-verity state
578allow init metadata_block_device:blk_file rw_file_perms;
579
580# Read /sys/fs/pstore/console-ramoops to detect restarts caused
581# by dm-verity detecting corrupted blocks
582allow init pstorefs:dir search;
583allow init pstorefs:file r_file_perms;
584allow init kernel:system syslog_read;
585
586# linux keyring configuration
587allow init init:key { write search setattr };
588
589# Allow init to create /data/unencrypted
590allow init unencrypted_data_file:dir create_dir_perms;
591
592# Set encryption policy on dirs in /data
593allowxperm init { data_file_type unlabeled }:dir ioctl {
594  FS_IOC_GET_ENCRYPTION_POLICY
595  FS_IOC_SET_ENCRYPTION_POLICY
596};
597
598# Raw writes to misc block device
599allow init misc_block_device:blk_file w_file_perms;
600
601r_dir_file(init, system_file)
602r_dir_file(init, system_dlkm_file_type)
603r_dir_file(init, vendor_file_type)
604
605allow init system_data_file:file { getattr read };
606allow init system_data_file:lnk_file r_file_perms;
607
608# For init to be able to run shell scripts from vendor
609allow init vendor_shell_exec:file execute;
610
611# Metadata setup
612allow init vold_metadata_file:dir create_dir_perms;
613allow init vold_metadata_file:file getattr;
614allow init metadata_bootstat_file:dir create_dir_perms;
615allow init metadata_bootstat_file:file w_file_perms;
616allow init userspace_reboot_metadata_file:file w_file_perms;
617
618# Allow init to touch PSI monitors
619allow init proc_pressure_mem:file { rw_file_perms setattr };
620
621# init is using bootstrap bionic
622use_bootstrap_libs(init)
623
624# stat the root dir of fuse filesystems (for the mount handler)
625allow init fuse:dir { search getattr };
626
627# allow filesystem tuning
628allow init userdata_sysdev:file create_file_perms;
629
630# allow disk tuning
631allow init rootdisk_sysdev:file create_file_perms;
632
633###
634### neverallow rules
635###
636
637# The init domain is only entered via an exec based transition from the
638# kernel domain, never via setcon().
639neverallow domain init:process dyntransition;
640neverallow { domain -kernel } init:process transition;
641neverallow init { file_type fs_type -init_exec }:file entrypoint;
642
643# Never read/follow symlinks created by shell or untrusted apps.
644neverallow init shell_data_file:lnk_file read;
645neverallow init { app_data_file privapp_data_file }:lnk_file read;
646
647# init should never execute a program without changing to another domain.
648neverallow init { file_type fs_type }:file execute_no_trans;
649
650# The use of sensitive environment variables, such as LD_PRELOAD, is disallowed
651# when init is executing other binaries. The use of LD_PRELOAD for init spawned
652# services is generally considered a no-no, as it injects libraries which the
653# binary was not expecting. This is especially problematic for APEXes. The use
654# of LD_PRELOAD via APEXes is a layering violation, and inappropriately loads
655# code into a process which wasn't expecting that code, with potentially
656# unexpected side effects. (b/140789528)
657neverallow init *:process noatsecure;
658
659# init can never add binder services
660neverallow init service_manager_type:service_manager { add find };
661# init can never list binder services
662neverallow init servicemanager:service_manager list;
663
664# Init should not be creating subdirectories in /data/local/tmp
665neverallow init shell_data_file:dir { write add_name remove_name };
666
667# Init should not access sysfs node that are not explicitly labeled.
668neverallow init sysfs:file { open write };
669
670# No domain should be allowed to ptrace init.
671neverallow * init:process ptrace;
672
673# init owns the root of /data
674# TODO(b/140259336) We want to remove vendor_init
675# TODO(b/141108496) We want to remove toolbox
676neverallow { domain -init -toolbox -vendor_init -vold } system_data_root_file:dir { write add_name remove_name };
677