1# Rules for all domains.
2
3# Allow reaping by init.
4allow domain init:process sigchld;
5
6# Read access to properties mapping.
7allow domain kernel:fd use;
8allow domain tmpfs:file { read getattr };
9allow domain tmpfs:lnk_file { read getattr };
10
11# Search /storage/emulated tmpfs mount.
12allow domain tmpfs:dir r_dir_perms;
13
14# Intra-domain accesses.
15allow domain self:process {
16    fork
17    sigchld
18    sigkill
19    sigstop
20    signull
21    signal
22    getsched
23    setsched
24    getsession
25    getpgid
26    setpgid
27    getcap
28    setcap
29    getattr
30    setrlimit
31};
32allow domain self:fd use;
33allow domain self:dir r_dir_perms;
34allow domain self:lnk_file r_file_perms;
35allow domain self:{ fifo_file file } rw_file_perms;
36allow domain self:unix_dgram_socket { create_socket_perms sendto };
37allow domain self:unix_stream_socket { create_stream_socket_perms connectto };
38
39# Inherit or receive open files from others.
40allow domain init:fd use;
41allow domain system_server:fd use;
42
43# Connect to adbd and use a socket transferred from it.
44# This is used for e.g. adb backup/restore.
45allow domain adbd:unix_stream_socket connectto;
46allow domain adbd:fd use;
47allow domain adbd:unix_stream_socket { getattr getopt read write shutdown };
48
49userdebug_or_eng(`
50  # Same as adbd rules above, except allow su to do the same thing
51  allow domain su:unix_stream_socket connectto;
52  allow domain su:fd use;
53  allow domain su:unix_stream_socket { getattr getopt read write shutdown };
54
55  binder_call({ domain -init }, su)
56
57  # Running something like "pm dump com.android.bluetooth" requires
58  # fifo writes
59  allow domain su:fifo_file { write getattr };
60
61  # allow "gdbserver --attach" to work for su.
62  allow domain su:process sigchld;
63
64  # Allow writing coredumps to /cores/*
65  allow domain coredump_file:file create_file_perms;
66  allow domain coredump_file:dir ra_dir_perms;
67')
68
69###
70### Talk to debuggerd.
71###
72allow domain debuggerd:process sigchld;
73allow domain debuggerd:unix_stream_socket connectto;
74
75# Root fs.
76allow domain rootfs:dir r_dir_perms;
77allow domain rootfs:file r_file_perms;
78allow domain rootfs:lnk_file r_file_perms;
79
80# Device accesses.
81allow domain device:dir search;
82allow domain dev_type:lnk_file r_file_perms;
83allow domain devpts:dir search;
84allow domain device:file read;
85allow domain socket_device:dir r_dir_perms;
86allow domain owntty_device:chr_file rw_file_perms;
87allow domain null_device:chr_file rw_file_perms;
88allow domain zero_device:chr_file rw_file_perms;
89allow domain ashmem_device:chr_file rw_file_perms;
90allow domain binder_device:chr_file rw_file_perms;
91allow domain ptmx_device:chr_file rw_file_perms;
92allow domain alarm_device:chr_file r_file_perms;
93allow domain urandom_device:chr_file rw_file_perms;
94allow domain random_device:chr_file rw_file_perms;
95allow domain properties_device:file r_file_perms;
96allow domain init:key search;
97allow domain vold:key search;
98
99# logd access
100write_logd(domain)
101
102# Filesystem accesses.
103allow domain fs_type:filesystem getattr;
104allow domain fs_type:dir getattr;
105
106# System file accesses.
107allow domain system_file:dir r_dir_perms;
108allow domain system_file:file r_file_perms;
109allow domain system_file:file execute;
110allow domain system_file:lnk_file r_file_perms;
111
112# Run toolbox.
113# Kernel and init never run anything without changing domains.
114allow { domain -kernel -init } toolbox_exec:file rx_file_perms;
115
116# Read files already opened under /data.
117allow domain system_data_file:dir { search getattr };
118allow domain system_data_file:file { getattr read };
119allow domain system_data_file:lnk_file r_file_perms;
120
121# Read apk files under /data/app.
122allow domain apk_data_file:dir { getattr search };
123allow domain apk_data_file:file r_file_perms;
124allow domain apk_data_file:lnk_file r_file_perms;
125
126# Read /data/dalvik-cache.
127allow domain dalvikcache_data_file:dir { search getattr };
128allow domain dalvikcache_data_file:file r_file_perms;
129
130# Read already opened /cache files.
131allow domain cache_file:dir r_dir_perms;
132allow domain cache_file:file { getattr read };
133allow domain cache_file:lnk_file r_file_perms;
134
135# Read timezone related information
136r_dir_file(domain, zoneinfo_data_file)
137
138# For /acct/uid/*/tasks.
139allow domain cgroup:dir { search write };
140allow domain cgroup:file w_file_perms;
141
142#Allow access to ion memory allocation device
143allow domain ion_device:chr_file rw_file_perms;
144
145# Read access to pseudo filesystems.
146r_dir_file(domain, proc)
147r_dir_file(domain, sysfs)
148r_dir_file(domain, sysfs_devices_system_cpu)
149r_dir_file(domain, inotify)
150r_dir_file(domain, cgroup)
151r_dir_file(domain, proc_net)
152allow domain proc_cpuinfo:file r_file_perms;
153
154# debugfs access
155allow domain debugfs:dir r_dir_perms;
156allow domain debugfs:file w_file_perms;
157
158# Get SELinux enforcing status.
159allow domain selinuxfs:dir r_dir_perms;
160allow domain selinuxfs:file r_file_perms;
161
162# /data/security files
163allow domain security_file:dir { search getattr };
164allow domain security_file:file getattr;
165allow domain security_file:lnk_file r_file_perms;
166
167# World readable asec image contents
168allow domain asec_public_file:file r_file_perms;
169allow domain { asec_public_file asec_apk_file }:dir r_dir_perms;
170
171###
172### neverallow rules
173###
174
175# Do not allow any domain other than init or recovery to create unlabeled files.
176neverallow { domain -init -recovery } unlabeled:dir_file_class_set create;
177
178# Limit ability to ptrace or read sensitive /proc/pid files of processes
179# with other UIDs to these whitelisted domains.
180neverallow {
181  domain
182  -debuggerd
183  -vold
184  -dumpstate
185  -system_server
186  userdebug_or_eng(`-procrank')
187  userdebug_or_eng(`-perfprofd')
188} self:capability sys_ptrace;
189
190# Limit device node creation to these whitelisted domains.
191neverallow {
192  domain
193  -kernel
194  -init
195  -ueventd
196  -vold
197} self:capability mknod;
198
199# Limit raw I/O to these whitelisted domains.
200neverallow { domain -kernel -init -recovery -ueventd -watchdogd -healthd -vold -uncrypt -tee } self:capability sys_rawio;
201
202# No process can map low memory (< CONFIG_LSM_MMAP_MIN_ADDR).
203neverallow domain self:memprotect mmap_zero;
204
205# No domain needs mac_override as it is unused by SELinux.
206neverallow domain self:capability2 mac_override;
207
208# Only recovery needs mac_admin to set contexts not defined in current policy.
209neverallow { domain -recovery } self:capability2 mac_admin;
210
211# Only init should be able to load SELinux policies.
212# The first load technically occurs while still in the kernel domain,
213# but this does not trigger a denial since there is no policy yet.
214# Policy reload requires allowing this to the init domain.
215neverallow { domain -init } kernel:security load_policy;
216
217# Only init and the system_server can set selinux.reload_policy 1
218# to trigger a policy reload.
219neverallow { domain -init -system_server } security_prop:property_service set;
220
221# Only init and system_server can write to /data/security, where runtime
222# policy updates live.
223# Only init can relabel /data/security (for init.rc restorecon_recursive /data).
224neverallow { domain -init } security_file:{ dir file lnk_file } { relabelfrom relabelto };
225# Only init and system_server can create/setattr directories with this type.
226# init is for init.rc mkdir /data/security.
227# system_server is for creating subdirectories under /data/security.
228neverallow { domain -init -system_server } security_file:dir { create setattr };
229# Only system_server can create subdirectories and files under /data/security.
230neverallow { domain -system_server } security_file:dir { rename write add_name remove_name rmdir };
231neverallow { domain -system_server } security_file:file { create setattr write append unlink link rename };
232neverallow { domain -system_server } security_file:lnk_file { create setattr unlink rename };
233
234# Only init prior to switching context should be able to set enforcing mode.
235# init starts in kernel domain and switches to init domain via setcon in
236# the init.rc, so the setenforce occurs while still in kernel. After
237# switching domains, there is never any need to setenforce again by init.
238neverallow domain kernel:security setenforce;
239neverallow { domain -kernel } kernel:security setcheckreqprot;
240
241# No booleans in AOSP policy, so no need to ever set them.
242neverallow domain kernel:security setbool;
243
244# Adjusting the AVC cache threshold.
245# Not presently allowed to anything in policy, but possibly something
246# that could be set from init.rc.
247neverallow { domain -init } kernel:security setsecparam;
248
249# Only init, ueventd and system_server should be able to access HW RNG
250neverallow { domain -init -system_server -ueventd } hw_random_device:chr_file *;
251
252# Ensure that all entrypoint executables are in exec_type.
253neverallow domain { file_type -exec_type }:file entrypoint;
254
255# Ensure that nothing in userspace can access /dev/mem or /dev/kmem
256neverallow { domain -kernel -ueventd -init } kmem_device:chr_file *;
257neverallow domain kmem_device:chr_file ~{ create relabelto unlink setattr };
258
259# Only init should be able to configure kernel usermodehelpers or
260# security-sensitive proc settings.
261neverallow { domain -init } usermodehelper:file { append write };
262neverallow { domain -init } proc_security:file { append write };
263
264# No domain should be allowed to ptrace init.
265neverallow domain init:process ptrace;
266
267# Init can't do anything with binder calls. If this neverallow rule is being
268# triggered, it's probably due to a service with no SELinux domain.
269neverallow domain init:binder *;
270
271# Don't allow raw read/write/open access to block_device
272# Rather force a relabel to a more specific type
273neverallow { domain -kernel -init -recovery -vold -uncrypt } block_device:blk_file { open read write };
274
275# Don't allow raw read/write/open access to generic devices.
276# Rather force a relabel to a more specific type.
277# init is exempt from this as there are character devices that only it uses.
278# ueventd is exempt from this, as it is managing these devices.
279neverallow { domain -init -ueventd } device:chr_file { open read write };
280
281# Limit what domains can mount filesystems or change their mount flags.
282# sdcard_type / vfat is exempt as a larger set of domains need
283# this capability, including device-specific domains.
284neverallow { domain -kernel -init -recovery -vold -zygote } { fs_type -sdcard_type }:filesystem { mount remount relabelfrom relabelto };
285
286#
287# Assert that, to the extent possible, we're not loading executable content from
288# outside the rootfs or /system partition except for a few whitelisted domains.
289#
290neverallow {
291    domain
292    -appdomain
293    -dumpstate
294    -shell
295    userdebug_or_eng(`-su')
296    -system_server
297    -zygote
298} { file_type -system_file -exec_type }:file execute;
299neverallow {
300    domain
301    -appdomain # for oemfs
302    -recovery # for /tmp/update_binary in tmpfs
303} { fs_type -rootfs }:file execute;
304
305# Only the init property service should write to /data/property.
306neverallow { domain -init } property_data_file:dir no_w_dir_perms;
307neverallow { domain -init } property_data_file:file no_w_file_perms;
308
309# Only recovery should be doing writes to /system
310neverallow { domain -recovery } { system_file exec_type }:dir_file_class_set
311    { create write setattr relabelfrom append unlink link rename };
312neverallow { domain -recovery -kernel } { system_file exec_type }:dir_file_class_set relabelto;
313
314# Don't allow mounting on top of /system files or directories
315neverallow domain { system_file exec_type }:dir_file_class_set mounton;
316
317# Nothing should be writing to files in the rootfs.
318neverallow domain rootfs:file { create write setattr relabelto append unlink link rename };
319
320# Restrict context mounts to specific types marked with
321# the contextmount_type attribute.
322neverallow domain {fs_type -contextmount_type}:filesystem relabelto;
323
324# Ensure that context mount types are not writable, to ensure that
325# the write to /system restriction above is not bypassed via context=
326# mount to another type.
327neverallow { domain -recovery } contextmount_type:dir_file_class_set
328    { create write setattr relabelfrom relabelto append unlink link rename };
329
330# Do not allow service_manager add for default_android_service.
331# Instead domains should use a more specific type such as
332# system_app_service rather than the generic type.
333# New service_types are defined in service.te and new mappings
334# from service name to service_type are defined in service_contexts.
335neverallow domain default_android_service:service_manager add;
336
337# Require that domains explicitly label unknown properties, and do not allow
338# anyone but init to modify unknown properties.
339neverallow { domain -init } default_prop:property_service set;
340
341neverallow { domain -init -recovery -system_server } frp_block_device:blk_file rw_file_perms;
342
343# No domain other than recovery can write to system.
344neverallow { domain -recovery } system_block_device:blk_file write;
345
346# No domains other than install_recovery or recovery can write to recovery.
347neverallow { domain -install_recovery -recovery } recovery_block_device:blk_file write;
348
349# Only servicemanager should be able to register with binder as the context manager
350neverallow { domain -servicemanager } *:binder set_context_mgr;
351
352# Only authorized processes should be writing to files in /data/dalvik-cache
353# (excluding /data/dalvik-cache/profiles, which is labeled differently)
354neverallow {
355  domain
356  -init # TODO: limit init to relabelfrom for files
357  -zygote
358  -installd
359  -dex2oat
360} dalvikcache_data_file:file no_w_file_perms;
361
362neverallow {
363  domain
364  -init
365  -installd
366  -dex2oat
367  -zygote
368} dalvikcache_data_file:dir no_w_dir_perms;
369
370# Only system_server should be able to send commands via the zygote socket
371neverallow { domain -zygote -system_server } zygote:unix_stream_socket connectto;
372neverallow { domain -system_server } zygote_socket:sock_file write;
373
374# Android does not support System V IPCs.
375#
376# The reason for this is due to the fact that, by design, they lead to global
377# kernel resource leakage.
378#
379# For example, there is no way to automatically release a SysV semaphore
380# allocated in the kernel when:
381#
382# - a buggy or malicious process exits
383# - a non-buggy and non-malicious process crashes or is explicitly killed.
384#
385# Killing processes automatically to make room for new ones is an
386# important part of Android's application lifecycle implementation. This means
387# that, even assuming only non-buggy and non-malicious code, it is very likely
388# that over time, the kernel global tables used to implement SysV IPCs will fill
389# up.
390neverallow domain domain:{ shm sem msg msgq } *;
391
392# Do not mount on top of symlinks, fifos, or sockets.
393# Feature parity with Chromium LSM.
394neverallow domain { file_type fs_type dev_type }:{ lnk_file fifo_file sock_file } mounton;
395
396# Nobody should be able to execute su on user builds.
397# On userdebug/eng builds, only dumpstate, shell, and
398# su itself execute su.
399neverallow { domain userdebug_or_eng(`-dumpstate -shell -su') } su_exec:file no_x_file_perms;
400
401# Do not allow the introduction of new execmod rules. Text relocations
402# and modification of executable pages are unsafe.
403# The only exceptions are for NDK text relocations associated with
404# https://code.google.com/p/android/issues/detail?id=23203
405# which, long term, need to go away.
406neverallow domain {
407  file_type
408  -system_file      # needs to die. b/20013628
409  -system_data_file
410  -apk_data_file
411  -app_data_file
412  -asec_public_file
413}:file execmod;
414
415# TODO: prohibit non-zygote spawned processes from using shared libraries
416# with text relocations. b/20013628 .
417# neverallow { domain -appdomain } file_type:file execmod;
418
419neverallow { domain -init } proc:{ file dir } mounton;
420
421# Ensure that all types assigned to processes are included
422# in the domain attribute, so that all allow and neverallow rules
423# written on domain are applied to all processes.
424# This is achieved by ensuring that it is impossible to transition
425# from a domain to a non-domain type and vice versa.
426neverallow domain ~domain:process { transition dyntransition };
427neverallow ~domain domain:process { transition dyntransition };
428
429#
430# Only system_app and system_server should be creating or writing
431# their files. The proper way to share files is to setup
432# type transitions to a more specific type or assigning a type
433# to its parent directory via a file_contexts entry.
434# Example type transition:
435#  mydomain.te:file_type_auto_trans(mydomain, system_data_file, new_file_type)
436#
437neverallow {
438  domain
439  -system_server
440  -system_app
441  -init
442  -installd # for relabelfrom and unlink, check for this in explicit neverallow
443} system_data_file:file no_w_file_perms;
444# do not grant anything greater than r_file_perms and relabelfrom unlink
445# to installd
446neverallow installd system_data_file:file ~{ r_file_perms relabelfrom unlink };
447
448#
449# Only these domains should transition to shell domain. This domain is
450# permissible for the "shell user". If you need a process to exec a shell
451# script with differing privilege, define a domain and set up a transition.
452#
453neverallow {
454  domain
455  -adbd
456  -init
457  -runas
458  -zygote
459} shell:process { transition dyntransition };
460
461# Minimize read access to shell- or app-writable symlinks.
462# This is to prevent malicious symlink attacks.
463neverallow {
464  domain
465  -appdomain
466  -installd
467  -uncrypt  # TODO: see if we can remove
468} app_data_file:lnk_file read;
469
470neverallow {
471  domain
472  -shell
473  userdebug_or_eng(`-uncrypt')
474  -installd
475} shell_data_file:lnk_file read;
476