博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
selinux在 android 上的实现
阅读量:4284 次
发布时间:2019-05-27

本文共 4640 字,大约阅读时间需要 15 分钟。

1.  在init.c main函数里面初始化

int main(int argc, char **argv){
..........
union selinux_callback cb;
cb.func_log = log_callback;
selinux_set_callback(SELINUX_CB_LOG, cb);
cb.func_audit = audit_callback;
selinux_set_callback(SELINUX_CB_AUDIT, cb);
selinux_initialize();
.......
}
selinux_initialize()这个函数里面先判断SELinux 是否启用,启用则载入编译出来的策略文件,并在用户空间用mmap方式与内核共享策略文件,这样内核之后就按照拿到的策略来控制访问,然后设置SELinux启动模式。(./external/libselinux/src)
   SELinux 启动模式有两种enforcing 和permissive。 enforcing 是强制安全检查,不符合安全策略时不允许执行;permissive 模式也进行安全检测,但是遇到不符合安全策略时仅仅打印警告信息,依然运行程序执行。
   可以在编译文件里面kernel cmdline 里面加入启动模式如androidboot.selinux=permissive。 一般eng版本使用的是permissive,user和user-debug版本启动的是enforcing。当然如果对安全性要求不高,完全可以不启用SELinux。在android5.1 之前默认并没有真正意义上的使用。
 2. 策略文件在哪配置
    android模式的策略配置文件放在 external/sepolicy目录下,一般不提倡直接修改这下面的文件,而是针对平台或者项目在device或者vendor目录下添加。比如:
  自己策略文件所在目录:
    BOARD_SEPOLICY_DIRS += \
                    device/xxx/sepolicy

  所添加的策略文件

BOARD_SEPOLICY_UNION := \
        bluetooth.te \
        file.te 
  这样添加的策略就参与编译了。
3. 编译出来的策略文件在哪?
    编译出来的策略文件在out\...\root目录下
   root\file_contexts
   root\sepolicy
   root\property_contexts
   root\seapp_contexts
   root\service_contexts
   所以修改完只需要编译bootimage,但是烧录boot 就可以验证了。
4. 如何配置策略
   需要先了解系统针对客体和主体提供了哪些设置属性和操作。
   external/sepolicy/security_classes 定义了客体的基本类型,比如下面的
 # for userspace object managers
class security
class process
class system
class capability
# file-related classes
class filesystem
class file
class dir
class fd
class lnk_file
class chr_file
class blk_file
class sock_file

     external/sepolicy/access_vectors 文件列出来各种客体类型所拥有的属性,比如下面的file类型和dir 类型,类型间可以有集成关系。

common file
{
ioctl
read
write
create
getattr
setattr
lock
relabelfrom
relabelto
append
unlink
link
rename
execute
swapon
quotaon
mounton
}
class dir
inherits file
{
add_name
remove_name
reparent
search
rmdir
open
audit_access
execmod
}
external/sepolicy/global_macros全局宏变量定义文件,也就是用一个宏变量同时表示几个属性。如下例子
define(`x_file_perms', `{ getattr execute execute_no_trans }')
define(`r_file_perms', `{ getattr open read ioctl lock }')
define(`w_file_perms', `{ open append write }')
define(`rx_file_perms', `{ r_file_perms x_file_perms }')
define(`ra_file_perms', `{ r_file_perms append }')
define(`rw_file_perms', `{ r_file_perms w_file_perms }')
define(`rwx_file_perms', `{ rw_file_perms x_file_perms }')
define(`link_file_perms', `{ getattr link unlink rename }')
define(`create_file_perms', `{ create setattr rw_file_perms link_file_perms }')
external/sepolicy/te_macros 宏规则变量定义文件,也就是用一个变量来表示连续执行几条规则。如下例子
define(`domain_trans', `
# Old domain may exec the file and transition to the new domain.
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 };
')
external/sepolicy/file_contexts  文件安全上下文定义文件。就是说对于一个文件或者目录,它的安全上下文应该在这里定义,如果没有定义,那么默认和父目录的安全上下文一样。
比如说我们在data目录下建了一个log目录存放系统运行时的log,那么我们可能希望这个log目录的的安全上下文是u:object_r:data_log_file:s0
那么就应该在file_contexts文件里做如下定义:/data/log(/.*)?u:object_r:data_log_file:s0

external/sepolicy/property_contexts 属性安全上下文定义文件。比如说新加了一个persist.log. * 开头的属性,那么就需要给它添加对应的安全上下文,如果不添加或者添加的上下文不对,那么在的某个进程里写入时就写不进去。

比如做了如下定义,那么是具有system_prop域安全上下文,只能在system进程里修改值,在shell里是不能访问的。
persist.log.        u:object_r:system_prop:s0
如果希望在shell里能修改值,就应该这么写persist.log.        u:object_r:shell_prop:s0 ,当然如果通过修改相关*.te 文件也能实现,但是不是很规范。

external/sepolicy/property.te 文件允许我们定义自己的prop 类型域,还有seapp_contexts、service_contexts 文件等,都是类似的,都可以根据需要修改。

下面几个是常用到的规则操作

allow:赋予某项权限。
allowaudit:audit含义就是记录某项操作。默认情况下是SELinux只记录那些权限检查失败的操作。allowaudit则使得权限检查成功的操作也被记录。注意,allowaudit只是允许记录,它和赋予权限没关系。赋予权限必须且只能使用allow语句。
dontaudit:对那些权限检查失败的操作不做记录。
neverallow:前面讲过,用来检查安全策略文件中是否有违反该项规则的allow语句
规则命令格式:规则名 主体域 客体域:类型权限属性 ;       或者    规则名 主体域 客体域:类型 {权限属性集合} ;
比如:
allow domain system_data_file:dir { search getattr };
allow domain system_file:file r_file_perms;

规则都定义在以te为后缀的文件里面,比如见到的init.te  installd.te system_app.te platform_app.te shell.te 等等。init.te 是针对init进程定义的规则,system_app.te 针对的是app进程是system的应用定义的规则,shell.te 针对的是shell进程定义的规则,以此类推。如果我们对某种类型的进程定义了新的规则,那么我们应该把新规则添加到它对应的te文件里面。比如我们在data目录下加了一个log目录,并且它的DAC权限为777,安全上下文是u:object_r:data_log_file:s0,我们希望系统运行过程通过shell命令查看log,那么我们就可以往shell.te 文件添加如下策略:

allow shelldata_log_file:dir r_dir_perms;
allow shell data_log_file:file r_file_perms;
如果我们新启了一个进程,并且希望它运行在自己定义的安全上下文里面,那么我们应该给它添加对应的te规则文件。

————————————————

版权声明:本文为CSDN博主「谭小工」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tww85/article/details/52451606

你可能感兴趣的文章
python-day2 python的基本语法(注释,运算符,数据类型,在python中,定义变量时,不需要指定变量的类型的)
查看>>
Springboot-data-es 自动创建mapping(mapping的数据结构为空)以及自动添加新字段(生成mapping的数据结构)
查看>>
数据分析-day04-pandas-dataFrame中查询数据与修改赋值修改语句(r=df[df["z"]==6] & df["z"]=6)
查看>>
数据分析-day06-pandas-dataFrame案例分析1(方法一):获取title字段中包含物流运输业名称作为分类,统计各个分类的条数
查看>>
数据分析-day06-pandas-dataFrame案例分析1(方法二):获取title字段中包含物流运输业名称作为分类,统计各个分类的条数,
查看>>
数据分析-day06-pandas-dataFrame案例分析4:使用PeriodIndex将离散组成pandas的时间序列,然后统计不同时间段,两个地方的pm值走势
查看>>
static synchronized方法和synchronized方法的区别和联系-2(class锁和对象锁,不同锁,混合访问时)
查看>>
spring cloud 自定义ribbon实现负载均衡(二) 解决自定义ribbon和springboot启动类放在同一包下
查看>>
重要重要》》》》》springcloud fegin的基本使用,实现consumer通过fegin访问provider!!!!!
查看>>
spring cloud 远程仓库配置文件(yml或properties)中有server.port参数就无法访问,这是一个大坑吗?
查看>>
es 2.3.3 向es添加数据报NoNodeAvailableException[None of the configured nodes are available
查看>>
springboot 的@RequestBody JSONObject 与@RequestBody Object 的区别
查看>>
springboot pom文件设置<packaging>pom</packaging> 对于application.yml无法加载读取的问题
查看>>
springboot加载resouce下面的静态文件,templates目录的访问,以及经过controller后跳转页面问题
查看>>
shiro的通过md5+salt+hash散列进行注册操作
查看>>
springboot整合applicationContext实现上下文获取实例bean
查看>>
shiro目前问题记录
查看>>
shiro实现本地内存Ehcache实现将菜单权限进行缓存
查看>>
shiro使用redis实现将菜单权限进行缓存
查看>>
cmd窗口下执行jar包 logger.info输出乱码,out语句输出正常解决办法
查看>>