Android 应用(7)——untrusted_app访问底层硬件

参考链接:
https://blog.csdn.net/Sunxiaolin2016/article/details/91039775
https://blog.csdn.net/scottmvp/article/details/115871037

背景:用户自行开发的app需要访问底层serial port。我们开发的app在SELinux(或SEAndroid)中分为主要三种类型(根据user不同,也有其他的domain类型):

1)untrusted_app 第三方app,没有Android平台签名,没有system权限
2)platform_app 有android平台签名,没有system权限
3)system_app 有android平台签名和system权限
从上面划分,权限等级,理论上:untrusted_app < platform_app < system_app

APP的domain和type
查看seapp_contexts文件,APP的domain和type由user和seinfo两个参数决定。

Policy files
以 *.te 结尾的文件是 SELinux 政策源代码文件,用于定义域及其标签。

标签、规则和域
规则采用以下形式:allow domains types:classes permissions;,其中:
Domain - 一个进程或一组进程的标签。也称为域类型,因为它只是指进程的类型。
Type - 一个对象(例如,文件、套接字)或一组对象的标签。
Class - 要访问的对象(例如,文件、套接字)的类型。
Permission - 要执行的操作(例如,读取、写入)。

添加serial port对象的标签

添加对象(我们要操作的串口设备文件)的标签:

diff --git a/device/softwinner/common/sepolicy/vendor/file_contexts b/device/softwinner/common/sepolicy/vendor/file_contexts
index c2600e0..13b3f73 100755
--- a/device/softwinner/common/sepolicy/vendor/file_contexts
+++ b/device/softwinner/common/sepolicy/vendor/file_contexts
@@ -72,6 +72,7 @@

 # Bluetooth
 /dev/ttyS1              u:object_r:hci_attach_dev:s0
+/dev/ttyS5              u:object_r:ttyS5_device:s0
 /dev/ttyBT0             u:object_r:hci_attach_dev:s0
 /sys/class/rfkill/rfkill0/state u:object_r:sysfs_bluetooth_writable:s0

定义标签的类型:

diff --git a/device/softwinner/common/sepolicy/vendor/device.te b/device/softwinner/common/sepolicy/vendor/device.te
index 80d18c5..66f14a0 100755
--- a/device/softwinner/common/sepolicy/vendor/device.te
+++ b/device/softwinner/common/sepolicy/vendor/device.te
@@ -4,3 +4,4 @@ type zram_backing_device, dev_type;
 type sunxi_soc_device, dev_type;
 type deinterlace_device, dev_type;
 type sst_storage_device, dev_type, fs_type;
+type ttyS5_device, dev_type, mlstrustedobject;

添加访问权限

添加untrusted_app对serial port的访问权限:

diff --git a/device/softwinner/common/sepolicy/vendor/untrusted_app.te b/device/softwinner/common/sepolicy/vendor/untrusted_app.te
index fb078dd..43496eb 100755
--- a/device/softwinner/common/sepolicy/vendor/untrusted_app.te
+++ b/device/softwinner/common/sepolicy/vendor/untrusted_app.te
@@ -10,3 +10,6 @@ allow untrusted_app su_exec:file { execute read open getattr execute_no_trans };
 allow untrusted_app untrusted_app:capability { setgid setuid };
 allow untrusted_app selinuxfs:file { open read write };
 allow untrusted_app kernel:security { setenforce };
+allow untrusted_app ttyS5_device:chr_file rw_file_perms;
+allow untrusted_app apexd_prop:file { getattr open read };
+allow untrusted_app proc_tty_drivers:file { execute read open getattr execute_no_trans };

解除neverallow限制

android默认的安全策略配置会对不同的权限等级设置相应禁止分配的权限,untrusted_app的权限等级很低,默认很多权限都是neverallow,这就需要我们手动打开:

diff --git a/system/sepolicy/prebuilts/api/29.0/private/app_neverallows.te b/system/sepolicy/prebuilts/api/29.0/private/app_neverallows.te
index c60bf83..58fdc3c 100755
--- a/system/sepolicy/prebuilts/api/29.0/private/app_neverallows.te
+++ b/system/sepolicy/prebuilts/api/29.0/private/app_neverallows.te
@@ -327,8 +327,8 @@ full_treble_only(`
 # b/33214085 b/33814662 b/33791054 b/33211769
 # https://github.com/strazzere/anti-emulator/blob/master/AntiEmulator/src/diff/strazzere/anti/emulator/FindEmulator.java
 # This will go away in a future Android release
-neverallow { all_untrusted_apps -untrusted_app_25 } proc_tty_drivers:file r_file_perms;
-neverallow all_untrusted_apps proc_tty_drivers:file ~r_file_perms;
+#neverallow { all_untrusted_apps -untrusted_app_25 } proc_tty_drivers:file r_file_perms;
+#neverallow all_untrusted_apps proc_tty_drivers:file ~r_file_perms;
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值