皇冠新体育APP

IT技术之家

Android 应用(7)——untrusted_app访问底层硬件_横山郡守

推出的时间:2023-08-23 00:50:29 移动 45次 标签:皇冠新体育APP:android studio android sepolicy
参考链接://blog.csdn.net/Sunxiaolin2016/article/details/91039775//blog.csdn.net/scottmvp/article/details/115871037背景:用户自行开发的app需要访问底层serial port。我们开发的app在SELinux(或SEAndroid)中分为主要三种类型(根据user不同,也有其他的domain类型):1)untrusted_app 第三方app,没有Android平台签名...

参考链接:
//blog.csdn.net/Sunxiaolin2016/article/details/91039775
//blog.csdn.net/scottmvp/article/details/115871037
背景图片:客户自愿搭建的app所需网页访问表层serial port。人们搭建的app在SELinux(或SEAndroid)中分发型有利于要3种的型(只能根据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
设定tag标签的方式:
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
 # //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;