/************************************ Author:刘江明* Environment:MTK Android 6.0* Date:2016年11月06日***********************************/
基础知识都已经学习完了,但是还不知道怎么样,下面从不同的场景,实现了几个例子,可以参考学习一下
对于/extern/sepolicy的修改用如下方法编译:
1. mmm external/sepolicy2. make bootimg
不过对于mtk的android系统,不建议修改external/sepolicy,而是修改device/mediatek/common/sepolicy
在policy目录下,make relabel可更新或创建标识映射
一. 添加一个系统服务的权限声明
情景:定义一个init启动的service,demo_service,对应的执行文件是/system/bin/demo.
(1) 创建一个demo.te在/device/mediatke/common/sepolicy 目录下如果是Android4.4的源码,在/device/mediatke/common/BoardConfig.mk 的BOARD_SEPOLICY_UNION 宏中新增demo.te如果是Android5.0以上的编译会自动包含了整个文件夹里的文件,则不用在BoardConfig.mk中添加文件声明(2) 在demo.te中添加:demo的域(domain)类型定义type demo, domain;(3) 在demo.te中添加:demo的可执行文件(客体)的类型定义type demo_exec, exec_type(4) 在demo.te中添加:init启动service时类型转换声明,直接用一个宏,主要是用于把demo_exec(客体)转换成demo(进程域)init_daemon_domain(demo)(5) 绑定执行档 file_contexts 类型(安全上下文),由这个可执行文件启动起来的进程都是demo域里的/system/bin/demo u:object_r:demo_exec:s0(6) 根据demo需要访问的文件以及设备,定义其它的权限在demo.te中.
上面的例子最大的疑点就是init_daemon_domain这个宏,我们来看看
下面所有的宏都在/external/sepolicy/te_macros文件里定义,具体的定义,请自行查看
init_daemon_domain:
init_daemon_domain宏的作用是:设置init转换到daemon域,可执行文件是xxx_exec上面的例子是这样子使用的:init_daemon_domain(demo)把$1=demo代换进去,相当于执行下面两条语句domain_auto_trans(init, demo_exec, demo)声明tmpfs的读写和转换权限,tmpfs是一些内存临时文件tmpfs_domain(demo)
domain_auto_trans:
domain_auto_trans的作用是:定义旧的域转换成新的域的声明,直接把上面的参数代入到domain_auto_trans里相当于执行#这个宏应该就是上一篇文章里提到的域转换的权限声明domain_trans(init,demo_exec,demo)#声明init域执行demo_exec可执行文件,新的域转换成demo域type_transition init demo_exec:process demo;
已经大致完成了一个服务的权限声明,但是还有第(6)点没有完成,如果该服务需要对底层设备文件的读写,该如何声明权限?
二. 添加一个进程服务对内核文件节点的读写权限
直接上例程
情景:一个域(服务或者进程)需要访问一个专属的char device /dev/demo(1) 定义device 类型,创建一个device.te文件type demo_device dev_type;(2) 绑定demo device的上下文,在file_contexts/dev/demo u:object_r:demo_device:s0(3) 声明demo进行 使用demo device 的权限 在demo.teallow demo demo_device:chr_file rw_file_perms;
rw_file_perms是一个集合的宏定义
define(`r_file_perms', `{ getattr open read ioctl lock }')define(`w_file_perms', `{ open append write }')define(`rw_file_perms', `{ r_file_perms w_file_perms }')
三. 添加一个Socket访问权限
这个类似于文件的权限声明
情景:一个native service 通过init 创建一个socket 并绑定在 /dev/socket/demo,并且允许某些process 访问.
(1) 定义socket 的类型 file.tetype demo_socket,file_type;(2) 绑定socket 的类型 file_contexts/dev/socket/demo_socket u:object_r:demo_socket:s0(3) 允许所有的process 访问.# allow app connectto & writeunix_socket_connect(appdomain,demo,demo)
四. 添加一个进程服务对一个属性的访问权限
情景:允许一个进程域对一个属性sys.powerctl进行设置
#声明一个property_type类型powerctl_proptype powerctl_prop, property_type;#设置属性的上下文,把属性sys.powerctl的上下文设置成powerctl_prop类型sys.powerctl u:object_r:powerctl_prop:s0#允许adbd进程对powerctl_prop上下文的属性进行设置#set_prop里面做了两个动作,允许进行对property的socket进行连接和写入,允许进程设置属性set_prop(adbd, powerctl_prop)
五. 添加一个服务的binder,对外提供服务
type surfaceflinger_service, service_manager_type;type surfaceflinger, domain;SurfaceFlinger u:object_r:surfaceflinger_service:s0type surfaceflinger_exec, exec_type, file_type;/system/bin/surfaceflinger u:object_r:surfaceflinger_exec:s0#允许surfaceflinger读,写,调用Binder IPCbinder_use(surfaceflinger)#允许surfaceflinger访问,调用binderservicedomain的Binder IPCbinder_call(surfaceflinger, binderservicedomain)binder_call(surfaceflinger, appdomain)binder_call(surfaceflinger, bootanim)#将surfaceflinger与binder service的属性域binderservicedomain相连binder_service(surfaceflinger)
六. 添加一个APP的节点访问权限
定义节点类型device/mediatek/common/sepolicy/file.tetype demo_file, fs_type,sysfs_type;定义文件上下文device/mediatek/common/sepolicy/file_contexts/dev/demo_file u:object_r:demo_file:s0定义App权限ps -Z查看应用权限上下文,如Setting是u:r:system_app:s0rw_file_perms是一组权限的集合,包含读与写权限device/mediatek/common/sepolicy/system_app.teallow system_app demo_file:file rw_file_perms;定义服务权限,rw_file_perms是权限集,包含读写权限device/mediatek/common/sepolicy/system_server.teallow system_server demo_file:file rw_file_perms;
七. 添加一个APP的节点访问权限2
定义文件类型device/mediatek/common/sepolicy/file.tetype proc_mtk_demo, fs_type;定义虚拟文件系统的文件安全上下文device/mediatek/common/sepolicy/genfs_contextsgenfscon proc /mtk_demo/current_demo u:object_r:proc_mtk_demo:s0给予shell的进程读写权限,由/system/bin/sh启动的命令会与shell有同样的进程权限域device/mediatek/common/sepolicy/shell.teallow shell proc_mtk_demo:file {open read write getattr};APP的读取权限device/mediatek/common/sepolicy/system_app.teallow system_app proc_mtk_demo:file rw_file_perms;
使用方法
App使用方法之一:String[] cmd = {"/system/bin/sh", "-c", "echo " + st + " > " + "/proc/mtk_demo/current_demo",};Runtime.getRuntime().exec(cmd);App使用方法之二,可以直接使用Java File来读写该文件:
本文详细介绍了如何在Android系统中为不同场景的服务和进程定义安全策略,包括系统服务权限声明、进程服务内核文件节点访问权限、Socket访问权限等。
1531

被折叠的 条评论
为什么被折叠?



