前言:

本篇blog主要从SELinux历史、DAC和MAC、SELinux运行框图(混合模式)、Apache的例子(Legacy and SELinux)、SELinux Mode、SEPolicy文件结构、SELinux Policy语言介绍,这7个方面,让大家对selinux有整体的了解。


一、SELinux历史

SEAndroid是Google在Android 4.4上正式推出的一套以SELinux为基础于核心的系统安全机制。而SELinux则是由美国NSA(国安局)和一些公司(RedHat、Tresys)设计的一个针对Linux的安全加强系统。
NSA最初设计的安全模型叫FLASK,全称为Flux Advanced Security Kernel(由Uta大学和美国国防部开发,后来由NSA将其开源),当时这套模型针对DTOS系统。后来,NSA觉得Linux更具发展和普及前景,所以就在Linux系统上重新实现了FLASK,称之为SELinux。
Linux Kernel中,SELinux通过Linux Security Modules实现。在2.6之前,SElinux通过Patch方式发布。从2.6开始,SELinux正式入驻内核,成为标配。


二、DAC和MAC

SELinux出现之前,Linux上的安全模型叫DAC,全称是Discretionary Access Control,翻译为自主访问控制。DAC的核心思想很简单,就是:进程理论上所拥有的权限与执行它的用户的权限相同。比如,以root用户启动Browser,那么Browser就有root用户的权限,在Linux系统上能干任何事情;

MAC(Mandatory Access Control),强制访问控制,即任何进程想在SELinux系统中干任何事情,都必须先在安全策略配置文件中赋予权限。凡是没有出现在安全策略配置文件中的权限,进程就没有该权限;
e.g: allow netd sysfs:file write;

DAC:
1.自主访问控制
2.主体(Process)的权限Capability决定了它能访问和操作什么ROOT进程可以访问和操作一切!
3.传统(legacy)Linux的安全模式,基于UID/GID/Capability

MAC:
1.强制访问控制
2.系统的Policy决定了主体能操作访问哪些客体
3.即便是ROOT进程,系统Policy配置了你能做什么,你只能做什么,在下MAC模式,ROOT进程和普通进程是无区别对待的。


三、SELinux运行框图(混合模式)

Linux系统先做DAC检查。如果没有通过DAC权限检查,则操作直接失败。通过DAC检查之后,再做MAC权限检查。

安卓selinux架构 安卓selinux有什么用_Domain

安卓selinux架构 安卓selinux有什么用_Domain_02


四、Apache的例子(Legacy and SELinux)

安卓selinux架构 安卓selinux有什么用_linux_03

安卓selinux架构 安卓selinux有什么用_linux_04

五、SELinux Mode

Disabled
No SELinux policy is loaded.

Permissive
SELinux grants access & prints errors (denials)

Enforcing
SELinux security policy is enforced – access will be blocked.

Before JBMR2(4.3)
Not Introduce SEAndroid/SELinux

JBMR2
Introduced as Permissive

KitKat
Introduced as Enforcing


Get Mode runtime
getenforce

Set mode runtime
setenforce [enforcing,permissive,1,0]

Change mode in build time: init.c


安卓selinux架构 安卓selinux有什么用_linux_05

五、SEPolicy文件结构

/LINUX/android/external/sepolicy$

-rw-rw-r--   1 jonathan jonathan  9375 12月  6 06:32 access_vectors
-rw-rw-r--   1 jonathan jonathan  2303 12月  6 06:32 adbd.te
-rw-rw-r--   1 jonathan jonathan 13787 12月  6 06:32 app.te
-rw-rw-r--   1 jonathan jonathan  1614 12月  6 06:32 attributes
-rw-rw-r--   1 jonathan jonathan   784 12月  6 06:32 binderservicedomain.te
-rw-rw-r--   1 jonathan jonathan  2223 12月  6 06:32 bluetooth.te
-rw-rw-r--   1 jonathan jonathan   424 12月  6 06:32 bootanim.te
-rw-rw-r--   1 jonathan jonathan   841 12月  6 06:32 clatd.te
-rw-rw-r--   1 jonathan jonathan  1355 12月  6 06:32 debuggerd.te
-rw-rw-r--   1 jonathan jonathan  2133 12月  6 06:32 device.te
-rw-rw-r--   1 jonathan jonathan   917 12月  6 06:32 dnsmasq.te
-rw-rw-r--   1 jonathan jonathan 11995 12月  6 06:32 domain.te
-rw-rw-r--   1 jonathan jonathan  1770 12月  6 06:32 drmserver.te
-rw-rw-r--   1 jonathan jonathan  3507 12月  6 06:32 dumpstate.te
-rw-rw-r--   1 jonathan jonathan 10773  1月 23 14:53 file_contexts
-rw-rw-r--   1 jonathan jonathan  2471 12月  6 06:32 zygote.te


netd.te

# network manager
type netd, domain;
type netd_exec, exec_type, file_type;

init_daemon_domain(netd)
net_domain(netd)

allow netd self:capability { net_admin net_raw kill };


attributes

# All types used for devices.
attribute dev_type;
# All types used for processes.
attribute domain;
# All types used for filesystems.
attribute fs_type;
# All types used for context= mounts.
attribute contextmount_type;
# All types used for files that can exist on a labeled fs.
attribute file_type;
# All types used for domain entry points.
attribute exec_type;

Attribute 需要声明


Domain.te

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

# Get SELinux enforcing status.
allow domain selinuxfs:dir r_dir_perms;
allow domain selinuxfs:file r_file_perms;

# /data/security files
allow domain security_file:dir { search getattr };
allow domain security_file:file getattr;
allow domain security_file:lnk_file r_file_perms;

Attribute的作用为一组Type定义Policy


File_context

/proc                           u:object_r:rootfs:s0
/variant                      u:object_r:rootfs:s0
# SELinux policy files
/property_contexts       u:object_r:rootfs:s0
/seapp_contexts          u:object_r:rootfs:s0
/sepolicy                      u:object_r:rootfs:s0
 

/system/bin/vold          u:object_r:vold_exec:s0
/system/bin/netd          u:object_r:netd_exec:s0
/system/bin/rild            u:object_r:rild_exec:s0
客体(文件)的Type:
Image打包时确定:基于  File_Contexts
运行时创建则默认继承父Folder的Type
运行时Type Transition

#####################################
# init_daemon_domain(domain)
# Set up a transition from init to the daemon domain
# upon executing its binary.
define(`init_daemon_domain', `
domain_auto_trans(init, $1_exec, $1)     
tmpfs_domain($1)
')

六、SELinux Policy语言介绍


SELinux中,每种个体都会被赋予一个安全属性,叫Security Context(SContext),是一个字符串。


进程的Scontext,可通过ps -Z命令查看,

 ps -Z netd

LABEL                          USER     PID   PPID  NAME

u:r:netd:s0                    root      260   1     /system/bin/netd

u为user的意思。SEAndroid中定义了一个SELinux用户,值为u。

r为role的意思。role是角色之意,它是SELinux中一种比较高层次,更方便的权限管理思路,即Role Based Access Control(基于角色的访问控制,简称为RBAC)。简单点说,一个u可以属于多个role,不同的role具有不同的权限

netd: 即netd 的type, MAC的基础管理思路是基于Type Enforcement Accesc Control(简称TEAC,一般用TE表示),netd 所具有的权限,都需要netd.te来定义

S0和SELinux为了满足军用和教育行业而设计的Multi-Level Security(MLS)机制有关


文件的SContext,可通过ls –Z命令查看:
ls -Z /system/bin/netd
-rwxr-xr-x root     shell             u:object_r:netd_exec:s0 netd

u:同样是user之意,它代表创建这个文件的SELinux user。
object_r:文件在SELinux中,用object_r来表示它的role。
netd_exec:netd 作为一个可执行文件的Type;
s0:MLS的级别。


附:1.为什么要分别创建te?

举例:实现一个Camera_d的Native Service
Init.rc中启动之
权衡Policy:
不定义dedicated的Domain Type?则会继承Init Domain Type
后期一系列的policy需求都需要加到Init.TE中?
NO,这些Policy是Camer_d独有的,不该影响所有Init Type的Process Domain
权限分离管理!

2.定义TE,设置Domain Transition
camera-daemon.te
type camerad, domain;
permissive camerad;
type camerad_exec, exec_type, file_type;
init_daemon_domain(camerad)

3.修改File_Contexts
file_contexts
 
/system/bin/camera-daemon      u:object_r:camerad_exec:s0

4.Permissive模式编译
原因
不Block Operation的前提下,一次性暴露所有的问题, 适合开发过程中使用
必须make且重新下载Boot和System img

5.Check Runtime Subject and Object Type

Ps –Z 
 
u:r:camerad:s0             camera    243   1     /system/bin/camera-daemon
 
ls –l - Z
 
-rwxr-xr-x root     shell             u:object_r:camerad_exec:s0 camera-daemon

6.Check AVC Message

adb shell dmesg | grep avc | grep “pid=243” > avc.txt      //catch the kernel log which include the avc logs

Ex: type=1400 msg=audit(1381419041.046:660117): avc: denied { open } for pid=243 comm="camera-daemon" name="kgsl-3d0" dev="tmpfs" ino=2735 scontext= u:r:camerad:s0 tcontext=u:object_r:device:s0 tclass=chr_file

安卓selinux架构 安卓selinux有什么用_sed_06

7.修正Policy

依据dmesg 的AVC信息,分析后手动修改camera-daemon.te 增加Policy。
或者Run audit2allow utility on linux machine。
将仅包含特定Process的AVC信息保存在单独log中,比如前面的avc.txt
audit2allow –i avc.txt > avc.te
avc.te
allow camerad device:chr_file open;

将avc.te中的Allow相关的Rules添加到camera-daemon.te中
camera-daemon.te 
type camerad, domain;
permissive camerad;
type camerad_exec, exec_type, file_type;
init_daemon_domain(camerad)
allow camerad device:chr_file open;
重新make并download boot/system img