selinux - Android编写sepolicy

2023-05-16

为service编写sepolicy

由init启动的service服务要在各自的selinux domain中运行。具体flow如下:
init.devices.rc中声明service,将在init时启动:
init.rc z中服务定义
Note:seclabel标签用作设定service运行domain,如果设置了如下的init_daemon_domain便不需要该标签,该标签的优先级比init_daemon_domain要高

1 创建一个新的domain及file type

创建sepolicy文件device/manufacturer/device-name/sepolicy/mt_wlan_dongle_detect.te
内容如下:
在这里插入图片描述
Note:不是一定要使用目录device/manufacturer/device-name/sepolicy,以BOARD_SEPOLICY_DIRS宏为准 指定device sepolicy目录
L1 创建mt_wlan_dongle_detect domain
L2 创建mt_wlan_dongle_detect_exec file type
L3 声明为init的 service domain
init_daemin_damain实现如下, 主要是设置selinux,在init执行service bin时,允许由init domain自动转到service对应的domain:

/system/sepolicy/prebuilts/api/30.0/public/te_macros
#####################################
159 # init_daemon_domain(domain)
160 # Set up a transition from init to the daemon domain
161 # upon executing its binary.
162 define(`init_daemon_domain', `
163 domain_auto_trans(init, $1_exec, $1)
164 ')

#####################################
24 # domain_auto_trans(olddomain, type, newdomain)
25 # Automatically transition from olddomain to newdomain
26 # upon executing a file labeled with type.
27 #
28 define(`domain_auto_trans', `
29 # Allow the necessary permissions.
30 domain_trans($1,$2,$3)
31 # Make the transition occur by default.
32 type_transition $1 $2:process $3;
33 ')

 #####################################
2 # domain_trans(olddomain, type, newdomain)
3 # Allow a transition from olddomain to newdomain
4 # upon executing a file labeled with type.
5 # This only allows the transition; it does not
6 # cause it to occur automatically - use domain_auto_trans
7 # if that is what you want.
8 #
9 define(`domain_trans', `
10 # Old domain may exec the file and transition to the new domain.
11 allow $1 $2:file { getattr open read execute map };
12 allow $1 $3:process transition;
13 # New domain is entered by executing the file.
14 allow $3 $2:file { entrypoint open read execute getattr map };
15 # New domain can send SIGCHLD to its caller.
16 ifelse($1, `init', `', `allow $3 $1:process sigchld;')
17 # Enable AT_SECURE, i.e. libc secure mode.
18 dontaudit $1 $3:process noatsecure;
19 # XXX dontaudit candidate but requires further study.
20 allow $1 $3:process { siginh rlimitinh };
21 ')

2 标记exec_bin到对应的file type

增加如下内容到 device/manufacturer/device-name/sepolicy/file_contexts
在这里插入图片描述
其中 u代表user用户,object_r代表是文件类型,mt_wlan_dongle_detect_exec是file type, s0是安全等级
sepolicy语法可参考https://www.cnblogs.com/jiangzhaowei/p/11830992.html详细介绍

3 编译并烧录 boot及system image

4 重定义sepolicy

经典的解Selinux问题的步骤
1、将平台以permissive模式运行,cmd如下

### set on mboot
### set permissive
<< MStar >># ac androidboot.selinux permissive
### set enforcing
<< MStar >># ac androidboot.selinux enforcing

### set on linux
su
setenforce 0 (0 for permissive 1 for enforcing)

2、获取logcat 或者dmesg,其中"avc: denied …“字样为selinux deny log
avc: denied { execute_no_trans } for path=”/vendor/bin/iwpriv" dev=“dm-5” ino=150 scontext=u:r:vendor_shell:s0 tcontext=u:object_r:mt_wifi_set_exec:s0 tclass=file permissive=0
3、通过audit2allow工具,生成建议的sepolicy

# run on build server. sepolicy.logcat is the logcat file
~ cat sepolicy.logcat | gerp wlan_dongle_detect |audit2allow

会生成类似如下
在这里插入图片描述
4、根据建议的条目,添加至对应sepolicy文件中device/manufacturer/device-name/sepolicy/mt_wlan_dongle_detect.te
有些添加原则可以参考如下url:
https://source.android.com/security/selinux/customize
5、重新build image,并验证service可正常运行

sepolicy的一些限制及解决方法

hal service 不允许execute_no_trans权限

/system/sepolicy/public/hal_neverallows.te
30 ###
31 # HALs are defined as an attribute and so a given domain could hypothetically
32 # have multiple HALs in it (or even all of them) with the subsequent policy of
33 # the domain comprised of the union of all the HALs.
34 #
35 # This is a problem because
36 # 1) Security sensitive components should only be accessed by specific HALs.
37 # 2) hwbinder_call and the restrictions it provides cannot be reasoned about in
38 #    the platform.
39 # 3) The platform cannot reason about defense in depth if there are
40 #    monolithic domains etc.
41 #
42 # As an example, hal_keymaster and hal_gatekeeper can access the TEE and while
43 # its OK for them to share a process its not OK with them to share processes
44 # with other hals.
45 #
46 # The following neverallow rules, in conjuntion with CTS tests, assert that
47 # these security principles are adhered to.
48 #
49 # Do not allow a hal to exec another process without a domain transition.
50 # TODO remove exemptions.
51 neverallow {
52   halserverdomain
53   -hal_dumpstate_server
54   -hal_telephony_server
55 } { file_type fs_type }:file execute_no_trans;

当HAL Service添加execute_no_trans权限是 会由于与hal_neverallows.te冲突导致,无法build pass.
从上面code可以发现 hal service不允许execute_no_trans权限。
解决方法
当HAL Service运行execv系列、fork等API时,会以domain不改变的方式执行新的程序,所以需要execute_no_trans权限。
如下logcat fail,hal_networkproxy_mediatek hal service需要执行/vendor/bin/sh

avc: denied { execute_no_trans } for 
path="/vendor/bin/sh" dev="dm-5" ino=232 
scontext=u:r:hal_networkproxy_mediatek:s0 
tcontext=u:object_r:vendor_shell_exec:s0 
tclass=file permissive=0

因execute_no_trans权限无法使用,可以使用domain_auto_trans宏,使得当hal service执行其他bin档时,可以转到对应domain中。
如下增加的sepolicy,hal_networkproxy_mediatek执行vend_shell_exec时,会自动转到vend_shell domain中
在这里插入图片描述

ioctl权限除了allow之外 还需要allowxperm

avc: denied { ioctl } for path=“socket:[38698]” dev=“sockfs” ino=38698 ioctlcmd=0x8bef scontext=u:r:mt_wifi_set:s0 tcontext=u:r:mt_wifi_set:s0 tclass=udp_socket permissive=1
1、找到ioctlcmd=0x8bef对应的CMD

/system/sepolicy/public/ioctl_defines
define(`SIOCIWFIRSTPRIV_0F', `0x00008bef')

2、编写sepolicy
在这里插入图片描述

单独编译及单独升级方法

Partial Build
you can partial build selinux with the following commands

cd $(ANDROID_BUILD_TOP)

touch system/sepolicy/private/genfs_contexts

mmm system/sepolicy

Partial Update
Windows ADB

adb root

adb remount

adb push out\target\product\$(DEVICE)\vendor\etc\selinux /vendor/etc/

adb push out\target\product\$(DEVICE)\system\etc\selinux /system/etc/

adb shell sync; sync; sync;

adb reboot

Linux ADB

adb root

adb remount

adb push out/target/product/$(DEVICE)/vendor/etc/selinux /vendor/etc/selinux

adb push out/target/product/$(DEVICE)/system/etc/selinux /system/etc/selinux

adb shell sync; sync; sync;

adb reboot
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

selinux - Android编写sepolicy 的相关文章

随机推荐

  • (二叉树)高度平衡二叉树的判定

    题目描述 给定一个二叉树 xff0c 判断它是否是高度平衡的二叉树 本题中 xff0c 一棵高度平衡二叉树定义为 xff1a 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1 题目分析 由题意可知 xff0c 高度平衡二叉树是指树
  • 屏蔽快捷键

    屏蔽鼠标右键 function document oncontextmenu event returnValue 61 false 屏蔽F1帮助 function window onhelp return false 屏蔽其他功能键 fun
  • SpringBoot中使用防止用户重复点击,后台实现

    lt 64 Aspect需要的包 gt lt https mvnrepository com artifact aopalliance aopalliance gt lt dependency gt lt groupId gt aopall
  • Java基础-基本语法

    1 Java 语言有哪些特点 简单易学 有丰富的类库 面向对象 xff08 Java最重要的特性 xff0c 让程序耦合度更低 xff0c 内聚性更高 xff09 与平台无关性 xff08 JVM是Java跨平台使用的根本 xff09 可靠
  • Spring常用注解

    bean注入与装配的方式有很多种 xff0c 可以通过xml xff0c get set方式 xff0c 构造函数或者注解等 简单易用的方式就是使用Spring的注解 xff0c Spring提供了大量的注解方式 64 Required注解
  • RabbitMQ

    什么是 RabbitMQ xff1f RabbitMQ 是使用 Erlang 语言来编写的 xff0c 并且是基于 AMQP 协议 最大的特点就是 消费并不需要确保提供方存在 xff0c 实现了服务之间的高度解耦 AMQP xff1a Ad
  • word批量设置图片大小和对齐,使用宏定义

    word使用宏定义来批量设置图片大小 打开word中开发工具 xff0c 文件 选项 word选项 新建Visual Basic文件 点击 插入 模块 复制下列任意代码 xff0c 粘贴到右侧 xff08 注意可以灵活设置Myheigth或
  • postman设置不更新

    一 关闭自动更新目前有两种方案 xff1a 第一种 xff1a Hosts文件配置以下地址屏蔽连接 xff1a 1 以下配置粘贴到文件中 xff0c 文件位置 xff1a C Windows System32 drivers etc 0 0
  • ECharts多个折线图动态获取json数据

    ECharts 多个折线图动态获取json数据 效果图如下 xff1a 一 html部分 lt p id 61 34 TwoLineChart 34 style 61 34 width 100 height 400px 34 gt lt p
  • JS中setTimeout()的用法详解

    setTimeout 是属于 window 的 method 但我们都是略去 window 这顶层物件名称 这是用来设定一个时间 时间到了 就会执行一个指定的 method 1 SetTimeOut 1 1 SetTimeOut 语法例子
  • Java写个程序00001递增到99999

    NumberFormat f 61 new DecimalFormat 34 00000 34 for int i 61 1 i lt 10000 i 43 43 System out println f format i ER 00001
  • JQuery判断数组中是否包含某个元素

    inArray 34 元素字符串 34 数组名称 var arry 61 34 C 34 34 html 34 34 css 34 34 JavaScript 34 var result 61 inArray 34 C 34 arry 如果
  • JSP获得当前时间并显示

    lt 64 page import 61 34 java text SimpleDateFormat 34 gt lt 64 page import 61 34 java util 34 gt lt 64 page language 61
  • js 判断字符串是否包含另外一个字符串

    lt script type 61 34 text javascript 34 gt var str 61 34 测试一个字符串 ehtrzes 是否包含另外一个字符串 34 if str indexOf 34 ehtrzes 34 gt
  • js判断函数是否存在、判断是否为函数

    lt script type 61 34 text javascript 34 gt 判断是否为函数 try if typeof FunName 61 61 61 34 function 34 是函数 其中 FunName 为函数名称 al
  • python中的类和对象,属性和方法

    一 面向对象的概述 面向对象是一种描述业务问题 设计业务实体和实体之间关系的方法 二 类和对象 1 类和对象得区别 xff1a 类是对客观世界中事物得抽象 xff0c 而对象是类实例化后的实体 例如 xff1a 汽车模型就是一个类 xff0
  • 什么是“约瑟夫环”

    今天遇到一个关于 约瑟夫环 的问题 xff0c 于是上网查了下什么是 约瑟夫环 出自百度 xff1a 约瑟夫问题 xff08 有时也称为约瑟夫斯置换 xff0c 是一个出现在计算机科学和数学中的问题 在计算机编程的算法中 xff0c 类似问
  • 使用Fontcreator字体制作软件及字体设计学习

    fontcreator对于字体修改爱好者而言是一款极好的文字编辑工具 xff0c 门槛要求低 专业性强 集设计和修改为一体 xff0c 可用来制作 编辑 修改ttf xff0c otf xff0c ttc格式的字体文件 xff0c 非常的实
  • 远程连接centos 7 图形化桌面

    使用xrdp工具 xff0c 类似windows系统的远程桌面 xff08 rdp xff09 xff0c 需要在服务端安装远程桌面服务 如果你的服务器可以使用阿里的yum源 xff0c 可以直接使用epel仓库安装 xff0c 执行以下命
  • selinux - Android编写sepolicy

    为service编写sepolicy 由init启动的service服务要在各自的selinux domain中运行 具体flow如下 init devices rc中声明service xff0c 将在init时启动 xff1a Note