1.为独立进程静态的设置安全上下文
on early-init
这一行的作用是让init进程将自己的安全上下文设为u:r:init:s0。
查看external/sepolicy/init.te中,init这个domain的规则:
# init switches to init domain (via init.rc).
type init, domain;
# init is unconfined.
unconfined_domain(init)
tmpfs_domain(init)
allow init self:capability { sys_rawio mknod };
.........
domain_trans(init, rootfs, adbd)
.........
neverallow { domain -kernel} init:process dyntransition;
..........
首先(除注释以外)第一行,type init, domain;声明init具有domain的属性。这样它就可以和u,r一起,组成合法的安全上下文。
第二行unconfined_domain(init)
#####################################
# unconfined_domain(domain)
# Allow the specified domain to perform more privilegedoperations
# than would be typically allowed. Please see the comments atthe
# top of unconfined.te.
#
define(`unconfined_domain', `
typeattribute $1 mlstrustedsubject;
typeattribute $1 unconfineddomain;
')
第三行tmpfs_domain(init)
#####################################
# tmpfs_domain(domain)
# Define and allow access to a unique type for
# this domain when creating tmpfs / shmem / ashmem files.
define(`tmpfs_domain', `
type $1_tmpfs, file_type;
type_transition $1 tmpfs:file $1_tmpfs;
allow $1 $1_tmpfs:file { read write };
')
第四行是一个allow语句,关于allow语句的含义,之前写过,这里不重复了。
第五行domain_trans(init, rootfs, adbd)
#####################################
# domain_trans(olddomain, type, newdomain)
# Allow a transition from olddomain to newdomain
# upon executing a file labeled with type.
# This only allows the transition; it does not
# cause it to occur automatically - use domain_auto_trans
# if that is what you want.
#
define(`domain_trans', `
# Old domain may exec the file and transition to the newdomain.
allow $1 $2:file { getattr open read execute };
allow $1 $3:process transition;
# New domain is entered by executing the file.
allow $3 $2:file { entrypoint open read execute getattr };
# New domain can send SIGCHLD to its caller.
allow $3 $1:process sigchld;
# Enable AT_SECURE, i.e. libc secure mode.
dontaudit $1 $3:process noatsecure;
# XXX dontaudit candidate but requires further study.
allow $1 $3:process { siginh rlimitinh };
')
第六行neverallow { domain -kernel} init:process dyntransition,neverallow规则主要作用是确保allow规则的正确性。具体含义之前也写过,不重复了。
再看zygote进程,与init进程类似。在system/core/rootdir/init.zygote32.rc中(类似的还有三个文件32,64之类的,应该和处理器有关):
service zygote /system/bin/app_process -Xzygote /system/bin--zygote --start-system-server
说明zygote进程对应的可执行文件为/system/bin/app_process,查看external/sepolicy/file_contexts,发现/system/bin/app_process的安全上下文是u:object_r:zygote_exec:s0
/system/bin/app_process32
/system/bin/app_process64
查看zygote.te,其中大部分规则都很熟悉,剩下不熟悉的:
第一个,init_daemon_domain(zygote)
第二个,typeattribute zygote mlstrustedsubject;
第三组,三个跟selinux相关的宏:
# Check validity of SELinux context before use.
selinux_check_context(zygote)
# Check SELinux permissions.
selinux_check_access(zygote)
# Read /seapp_contexts and /data/security/seapp_contexts
security_access_policy(zygote)
宏定义如下:
#####################################
# selinux_check_context(domain)
# Allow domain to check SELinux contexts via selinuxfs.
define(`selinux_check_context', `
allow $1 selinuxfs:file rw_file_perms;
allow $1 kernel:security check_context;
')
#####################################
# selinux_check_access(domain)
# Allow domain to check SELinux permissions via selinuxfs.
define(`selinux_check_access', `
allow $1 selinuxfs:file rw_file_perms;
allow $1 kernel:security compute_av;
allow $1 self:netlink_selinux_socket *;
')
#####################################
# security_access_policy(domain)
# Read only access to all policy files and
# selinuxfs
define(`security_access_policy', `
allow $1 security_file:dir r_dir_perms;
allow $1 security_file:file r_file_perms;
')
2.为应用程序进程设置安全上下文
其中,AID_USER是一个常量,值为100000。
- isSystemServer=true domain=system_server
- user=system domain=system_app type=system_app_data_file
- user=bluetooth domain=bluetooth type=bluetooth_data_file
- user=nfc domain=nfc type=nfc_data_file
- user=radio domain=radio type=radio_data_file
- user=shared_relro domain=shared_relro
- user=shell domain=shell type=shell_data_file
- user=_isolated domain=isolated_app
- user=_app seinfo=platform domain=platform_apptype=app_data_file
- user=_app domain=untrusted_app type=app_data_file
user=anrom_app domain=anrom_app type=anrom_app_data_file
就可以将所有访客用户的app的domain和type改变。