domain.te 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# Rules for all domains.

# Allow reaping by init.
allow domain init:process sigchld;

# Read access to properties mapping.
allow domain kernel:fd use;
allow domain tmpfs:file { read getattr };

# Search /storage/emulated tmpfs mount.
allow domain tmpfs:dir r_dir_perms;

# Intra-domain accesses.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
allow domain self:process {
    fork
    sigchld
    sigkill
    sigstop
    signull
    signal
    getsched
    setsched
    getsession
    getpgid
    setpgid
    getcap
    setcap
    getattr
    setrlimit
};
31 32 33 34
allow domain self:fd use;
allow domain self:dir r_dir_perms;
allow domain self:lnk_file r_file_perms;
allow domain self:{ fifo_file file } rw_file_perms;
Stephen Smalley's avatar
Stephen Smalley committed
35 36
allow domain self:unix_dgram_socket { create_socket_perms sendto };
allow domain self:unix_stream_socket { create_stream_socket_perms connectto };
37 38 39

# Inherit or receive open files from others.
allow domain init:fd use;
40
allow domain system_server:fd use;
41 42

# Connect to adbd and use a socket transferred from it.
43
# This is used for e.g. adb backup/restore.
44 45
allow domain adbd:unix_stream_socket connectto;
allow domain adbd:fd use;
46
allow domain adbd:unix_stream_socket { getattr getopt read write shutdown };
47

48 49 50 51 52 53
userdebug_or_eng(`
  # Same as adbd rules above, except allow su to do the same thing
  allow domain su:unix_stream_socket connectto;
  allow domain su:fd use;
  allow domain su:unix_stream_socket { getattr getopt read write shutdown };

54
  binder_call({ domain -init }, su)
55

56 57 58 59 60 61
  # Running something like "pm dump com.android.bluetooth" requires
  # fifo writes
  allow domain su:fifo_file { write getattr };

  # allow "gdbserver --attach" to work for su.
  allow domain su:process sigchld;
Nick Kralevich's avatar
Nick Kralevich committed
62 63 64 65

  # Allow writing coredumps to /cores/*
  allow domain coredump_file:file create_file_perms;
  allow domain coredump_file:dir ra_dir_perms;
66 67
')

68 69 70
###
### Talk to debuggerd.
###
71 72 73 74 75
allow domain debuggerd:process sigchld;
allow domain debuggerd:unix_stream_socket connectto;

# Root fs.
allow domain rootfs:dir r_dir_perms;
76
allow domain rootfs:file r_file_perms;
77
allow domain rootfs:lnk_file r_file_perms;
78 79 80

# Device accesses.
allow domain device:dir search;
81
allow domain dev_type:lnk_file r_file_perms;
82 83
allow domain devpts:dir search;
allow domain device:file read;
84
allow domain socket_device:dir r_dir_perms;
85 86
allow domain owntty_device:chr_file rw_file_perms;
allow domain null_device:chr_file rw_file_perms;
Nick Kralevich's avatar
Nick Kralevich committed
87
allow domain zero_device:chr_file rw_file_perms;
88 89 90 91
allow domain ashmem_device:chr_file rw_file_perms;
allow domain binder_device:chr_file rw_file_perms;
allow domain ptmx_device:chr_file rw_file_perms;
allow domain alarm_device:chr_file r_file_perms;
92 93
allow domain urandom_device:chr_file rw_file_perms;
allow domain random_device:chr_file rw_file_perms;
94
allow domain properties_device:file r_file_perms;
Paul Lawrence's avatar
Paul Lawrence committed
95
allow domain init:key search;
96

97 98 99
# logd access
write_logd(domain)

100 101 102 103 104 105 106 107
# Filesystem accesses.
allow domain fs_type:filesystem getattr;
allow domain fs_type:dir getattr;

# System file accesses.
allow domain system_file:dir r_dir_perms;
allow domain system_file:file r_file_perms;
allow domain system_file:file execute;
108
allow domain system_file:lnk_file r_file_perms;
109

110 111 112 113
# Run toolbox.
# Kernel and init never run anything without changing domains.
allow { domain -kernel -init } toolbox_exec:file rx_file_perms;

114 115 116
# Read files already opened under /data.
allow domain system_data_file:dir { search getattr };
allow domain system_data_file:file { getattr read };
117
allow domain system_data_file:lnk_file r_file_perms;
118 119

# Read apk files under /data/app.
120
allow domain apk_data_file:dir { getattr search };
121
allow domain apk_data_file:file r_file_perms;
122
allow domain apk_data_file:lnk_file r_file_perms;
123 124 125 126 127 128 129 130

# Read /data/dalvik-cache.
allow domain dalvikcache_data_file:dir { search getattr };
allow domain dalvikcache_data_file:file r_file_perms;

# Read already opened /cache files.
allow domain cache_file:dir r_dir_perms;
allow domain cache_file:file { getattr read };
131
allow domain cache_file:lnk_file r_file_perms;
132

Nick Kralevich's avatar
Nick Kralevich committed
133 134 135
# Read timezone related information
r_dir_file(domain, zoneinfo_data_file)

136 137 138 139 140 141 142 143 144 145
# For /acct/uid/*/tasks.
allow domain cgroup:dir { search write };
allow domain cgroup:file w_file_perms;

#Allow access to ion memory allocation device
allow domain ion_device:chr_file rw_file_perms;

# Read access to pseudo filesystems.
r_dir_file(domain, proc)
r_dir_file(domain, sysfs)
146
r_dir_file(domain, sysfs_devices_system_cpu)
147 148
r_dir_file(domain, inotify)
r_dir_file(domain, cgroup)
149
r_dir_file(domain, proc_net)
150
allow domain proc_cpuinfo:file r_file_perms;
151 152 153

# debugfs access
allow domain debugfs:dir r_dir_perms;
154
allow domain debugfs:file w_file_perms;
155

156
# Get SELinux enforcing status.
157 158
allow domain selinuxfs:dir r_dir_perms;
allow domain selinuxfs:file r_file_perms;
159

160
# /data/security files
161 162
allow domain security_file:dir { search getattr };
allow domain security_file:file getattr;
163
allow domain security_file:lnk_file r_file_perms;
164

165 166 167 168
# World readable asec image contents
allow domain asec_public_file:file r_file_perms;
allow domain { asec_public_file asec_apk_file }:dir r_dir_perms;

169
# log all access to specified system_server services
170
auditallow { domain -shell -service_manager_local_audit } tmp_system_server_service:service_manager {list find };
171

172 173 174 175
###
### neverallow rules
###

176 177
# Do not allow any domain other than init or recovery to create unlabeled files.
neverallow { domain -init -recovery } unlabeled:dir_file_class_set create;
178

179 180
# Limit ability to ptrace or read sensitive /proc/pid files of processes
# with other UIDs to these whitelisted domains.
181 182 183 184 185 186 187 188
neverallow {
  domain
  -debuggerd
  -vold
  -dumpstate
  -system_server
  userdebug_or_eng(`-procrank')
} self:capability sys_ptrace;
189

190
# Limit device node creation to these whitelisted domains.
191
neverallow { domain -kernel -init -recovery -ueventd -watchdogd -healthd -vold -uncrypt -slideshow } self:capability mknod;
192 193 194

# Limit raw I/O to these whitelisted domains.
neverallow { domain -kernel -init -recovery -ueventd -watchdogd -healthd -vold -uncrypt -tee } self:capability sys_rawio;
195

196 197 198
# No process can map low memory (< CONFIG_LSM_MMAP_MIN_ADDR).
neverallow domain self:memprotect mmap_zero;

199
# No domain needs mac_override as it is unused by SELinux.
200
neverallow domain self:capability2 mac_override;
201 202

# Only recovery needs mac_admin to set contexts not defined in current policy.
203 204
neverallow { domain -recovery } self:capability2 mac_admin;

205 206 207 208 209 210
# Only init should be able to load SELinux policies.
# The first load technically occurs while still in the kernel domain,
# but this does not trigger a denial since there is no policy yet.
# Policy reload requires allowing this to the init domain.
neverallow { domain -init } kernel:security load_policy;

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
# Only init and the system_server can set selinux.reload_policy 1
# to trigger a policy reload.
neverallow { domain -init -system_server } security_prop:property_service set;

# Only init and system_server can write to /data/security, where runtime
# policy updates live.
# Only init can relabel /data/security (for init.rc restorecon_recursive /data).
neverallow { domain -init } security_file:{ dir file lnk_file } { relabelfrom relabelto };
# Only init and system_server can create/setattr directories with this type.
# init is for init.rc mkdir /data/security.
# system_server is for creating subdirectories under /data/security.
neverallow { domain -init -system_server } security_file:dir { create setattr };
# Only system_server can create subdirectories and files under /data/security.
neverallow { domain -system_server } security_file:dir { rename write add_name remove_name rmdir };
neverallow { domain -system_server } security_file:file { create setattr write append unlink link rename };
neverallow { domain -system_server } security_file:lnk_file { create setattr unlink rename };

228 229 230 231
# Only init prior to switching context should be able to set enforcing mode.
# init starts in kernel domain and switches to init domain via setcon in
# the init.rc, so the setenforce occurs while still in kernel. After
# switching domains, there is never any need to setenforce again by init.
Nick Kralevich's avatar
Nick Kralevich committed
232 233
neverallow domain kernel:security setenforce;
neverallow { domain -kernel } kernel:security setcheckreqprot;
234

235 236 237 238 239 240 241 242
# No booleans in AOSP policy, so no need to ever set them.
neverallow domain kernel:security setbool;

# Adjusting the AVC cache threshold.
# Not presently allowed to anything in policy, but possibly something
# that could be set from init.rc.
neverallow { domain -init } kernel:security setsecparam;

William Roberts's avatar
William Roberts committed
243
# Only init, ueventd and system_server should be able to access HW RNG
244
neverallow { domain -init -system_server -ueventd } hw_random_device:chr_file *;
245

246 247
# Ensure that all entrypoint executables are in exec_type.
neverallow domain { file_type -exec_type }:file entrypoint;
248 249 250 251

# Ensure that nothing in userspace can access /dev/mem or /dev/kmem
neverallow { domain -kernel -ueventd -init } kmem_device:chr_file *;
neverallow domain kmem_device:chr_file ~{ create relabelto unlink setattr };
252 253 254 255 256

# Only init should be able to configure kernel usermodehelpers or
# security-sensitive proc settings.
neverallow { domain -init } usermodehelper:file { append write };
neverallow { domain -init } proc_security:file { append write };
257 258 259

# No domain should be allowed to ptrace init.
neverallow domain init:process ptrace;
260

261
# Init can't do anything with binder calls. If this neverallow rule is being
262
# triggered, it's probably due to a service with no SELinux domain.
263
neverallow domain init:binder *;
264 265 266

# Don't allow raw read/write/open access to block_device
# Rather force a relabel to a more specific type
267
neverallow { domain -kernel -init -recovery -vold -uncrypt } block_device:blk_file { open read write };
268 269 270

# Don't allow raw read/write/open access to generic devices.
# Rather force a relabel to a more specific type.
271 272
# init is exempt from this as there are character devices that only it uses.
# ueventd is exempt from this, as it is managing these devices.
273
neverallow { domain -init -ueventd -recovery } device:chr_file { open read write };
274 275 276 277 278

# Limit what domains can mount filesystems or change their mount flags.
# sdcard_type / vfat is exempt as a larger set of domains need
# this capability, including device-specific domains.
neverallow { domain -kernel -init -recovery -vold -zygote } { fs_type -sdcard_type }:filesystem { mount remount relabelfrom relabelto };
279 280 281

#
# Assert that, to the extent possible, we're not loading executable content from
282
# outside the rootfs or /system partition except for a few whitelisted domains.
283 284 285 286 287
#
neverallow {
    domain
    -appdomain
    -dumpstate
Stephen Smalley's avatar
Stephen Smalley committed
288
    -shell
289 290 291 292
    userdebug_or_eng(`-su')
    -system_server
    -zygote
} { file_type -system_file -exec_type }:file execute;
293 294 295 296 297
neverallow {
    domain
    -appdomain # for oemfs
    -recovery # for /tmp/update_binary in tmpfs
} { fs_type -rootfs }:file execute;
Stephen Smalley's avatar
Stephen Smalley committed
298 299

# Only the init property service should write to /data/property.
300 301
neverallow { domain -init } property_data_file:dir no_w_dir_perms;
neverallow { domain -init } property_data_file:file no_w_file_perms;
302 303 304 305

# Only recovery should be doing writes to /system
neverallow { domain -recovery } { system_file exec_type }:dir_file_class_set
    { create write setattr relabelfrom relabelto append unlink link rename };
306

307 308 309
# Don't allow mounting on top of /system files or directories
neverallow domain { system_file exec_type }:dir_file_class_set mounton;

310
# Nothing should be writing to files in the rootfs.
311
neverallow { domain -recovery } rootfs:file { create write setattr relabelto append unlink link rename };
312

313 314 315 316 317 318 319 320 321
# Restrict context mounts to specific types marked with
# the contextmount_type attribute.
neverallow domain {fs_type -contextmount_type}:filesystem relabelto;

# Ensure that context mount types are not writable, to ensure that
# the write to /system restriction above is not bypassed via context=
# mount to another type.
neverallow { domain -recovery } contextmount_type:dir_file_class_set
    { create write setattr relabelfrom relabelto append unlink link rename };
322 323 324 325 326 327 328

# Do not allow service_manager add for default_android_service.
# Instead domains should use a more specific type such as
# system_app_service rather than the generic type.
# New service_types are defined in service.te and new mappings
# from service name to service_type are defined in service_contexts.
neverallow domain default_android_service:service_manager add;
329 330 331 332

# Require that domains explicitly label unknown properties, and do not allow
# anyone but init to modify unknown properties.
neverallow { domain -init } default_prop:property_service set;
333 334

neverallow { domain -init -recovery -system_server } frp_block_device:blk_file rw_file_perms;
335 336 337 338 339 340

# No domain other than recovery can write to system.
neverallow { domain -recovery } system_block_device:blk_file write;

# No domains other than install_recovery or recovery can write to recovery.
neverallow { domain -install_recovery -recovery } recovery_block_device:blk_file write;
341 342 343

# Only servicemanager should be able to register with binder as the context manager
neverallow { domain -servicemanager } *:binder set_context_mgr;
344 345 346 347 348 349 350 351 352 353

# Only authorized processes should be writing to files in /data/dalvik-cache
# (excluding /data/dalvik-cache/profiles, which is labeled differently)
neverallow {
  domain
  -init # TODO: limit init to relabelfrom for files
  -zygote
  -installd
  -dex2oat
} dalvikcache_data_file:file no_w_file_perms;
354

355 356 357 358
# Only system_server should be able to send commands via the zygote socket
neverallow { domain -zygote -system_server } zygote:unix_stream_socket connectto;
neverallow { domain -system_server } zygote_socket:sock_file write;

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
# Android does not support System V IPCs.
#
# The reason for this is due to the fact that, by design, they lead to global
# kernel resource leakage.
#
# For example, there is no way to automatically release a SysV semaphore
# allocated in the kernel when:
#
# - a buggy or malicious process exits
# - a non-buggy and non-malicious process crashes or is explicitly killed.
#
# Killing processes automatically to make room for new ones is an
# important part of Android's application lifecycle implementation. This means
# that, even assuming only non-buggy and non-malicious code, it is very likely
# that over time, the kernel global tables used to implement SysV IPCs will fill
# up.
neverallow domain domain:{ shm sem msg msgq } *;
376 377 378 379

# Do not mount on top of symlinks, fifos, or sockets.
# Feature parity with Chromium LSM.
neverallow domain { file_type fs_type dev_type }:{ lnk_file fifo_file sock_file } mounton;
380 381 382 383 384

# Nobody should be able to execute su on user builds.
# On userdebug/eng builds, only dumpstate, shell, and
# su itself execute su.
neverallow { domain userdebug_or_eng(`-dumpstate -shell -su') } su_exec:file no_x_file_perms;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

# Do not allow the introduction of new execmod rules. Text relocations
# and modification of executable pages are unsafe.
# The only exceptions are for NDK text relocations associated with
# https://code.google.com/p/android/issues/detail?id=23203
# which, long term, need to go away.
neverallow domain {
  file_type
  -system_data_file
  -apk_data_file
  -app_data_file
  -asec_public_file
}:file execmod;

neverallow { domain -appdomain } file_type:file execmod;