irpas技术客

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

网络投稿 3277

参考链接: 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;


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #untrusted_app #port