参考链接:
https:///Sunxiaolin2016/article/details/91039775
https:///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 - 要执行的操作(例如,读取、写入)。
添加对象(我们要操作的串口设备文件)的标签:
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 };
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;
因篇幅问题不能全部显示,请点此查看更多更全内容