Android 安全策略实例讲解:SEAndroid和SELinux

来源:互联网 发布:mac flash插件错误 编辑:程序博客网 时间:2024/05/01 03:42

一、原理讲解

参见:

鸟哥的Linux私房菜基础篇第三版---第十七章、程序管理不 SELinux 初探 鸟哥的Linux私房菜服务器架设篇(第三版)---第七章、网络安全与主机基本防护:限制端口口, 网络升级与 SELinuxhttp://blog.csdn.net/luoshengyang/article/details/35392905

二、实例

以访问打印机节点/dev/usb/lp0为例

注意:确认系统SE模式是Enforcing,用getenforce命令查询。
1、正常情况下,


ls /dev/usb/lp0

是正常执行的,现在,

2、在./external/sepolicy/目录下,修改/dev/usb/lp0的文件上下文:
file_contexts:



3、在./external/sepolicy/目录下添加针对/dev/usb/lp0的访问策略文件(.te文件,名称可任意),内容:



4、编译烧写boot.img,再次执行ls /dev/usb/lp0命令,命令行错误提示:

同时,用代码测试:


    int main(void)    {    int ret;        ret = open("/dev/usb/lp0", O_RDWR);    printf("ret = %d\n", ret);    if (ret > 0) {    printf("O<span style="font-family:Courier New;">P</span>en sucess\n");    sleep(2);    close(ret);    } else {    printf("O<span style="font-family:Courier New;">P</span>en failed\n");    }        printf("exit\n\n");    return 0;    }
同样无法访问:


5、内核log:

说明:
第一条log对应ls,第二条对应测试代码;
"scontext=":访问过程中的主体,对应ls、App、进程等;
"tcontext=":访问过程中的客体,即访问的资源、文件等。
可见,"tcontext="正是在策略文件中指定的数据。

6、添加对/dev/usb/lp0的访问允许(.te文件):
allow shell selfdefinedtype:chr_file open;allow shell selfdefinedtype:chr_file write;allow shell selfdefinedtype:chr_file read;allow shell selfdefinedtype:chr_file rw_file_perms;

selfdefinedtype:访问文件的type;
chr_file:对应log的tclass;
以后可以参照这log写该文件。

7、再次编译烧写boot.img,这次类型为shell的主体都可访问/dev/usb/lp0

三、实例:限制程序设置属性

目的:只允许我们指定的程序设置系统属性。
属性:sys.test.property

1、设置sys.test.property的上下文
<del>external/sepolicy/property_contexts:sys.koolpos.debuggable  u:object_r:selfdefinedtype:s0</del>

2、测试
这样,就只有selfdefinedtype类型的程序可以设置属性sys.test.propertylog和上面的例子相似。

0 0
原创粉丝点击