Android 8.0 工程之sepolicy权限

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Tree_in_sea/article/details/81129604

/*****************************************************************************
 * Author : Elvins Fu    [email protected]
 *
 * Info : xxx Inc,(C) 2018-01-11, All rights revseved.
 *
 * Description : This document is summed up by the author for that the company of xxxs  
 * R&D debug sepocily file on the android platform。
 *
 * This context describes that sd card problem and methods to analysis.
****************************************************************************/

注:KK 版本, Google 只有限制的启用SELinux, 即只有针对netd, installd, zygote, vold 以及它们
直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.

L 版本, Google 全面开启SELinux, 几乎所有的process 都使enforcing mode, 影响面非常广.
目前所有的SELinux check 失败,在kernel log 或者android log(L版本后)中都有对应的"avc:
denied" 或者 "avc: denied"的LOG 与之对应。反过来,有此LOG,并非就会直接失败,还需要确认
当时SELinux 的模式, 是enforcing mode 还是 permissve mode.

1.Android系统越来越对权限更为重视了,有private/public/vendor等等目录涵盖了*.te等权限文件,先分析logs

***使用命令 cat /proc/kmsg | grep "avc" 查看log,或者用串口log打开,等搜索avc字符,可以导出全部的权限问题。

***调试过程中 可以使用adb shell 进入调试工程中,setenforce 0关闭权限audit 审核。

解决问题通常三步:1.定义相关的selinux type 2.绑定文件和selinux type 3.在xxx(相关文件).te添加或赋予权限。

1)简单的

<36>[   59.591056] type=1400 audit(177553.350:39): avc: denied { read write } for pid=410 comm="[email protected]" name="brightness" dev="sysfs" ino=25578 scontext=u:r:hal_light_default:s0 tcontext=u:object_r:led_control:s0 tclass=file permissive=0

<规则1>定义selinux type 通常qcom或mtk平台给出

type hal_light_default, domain;
hal_server_domain(hal_light_default, hal_light)
type hal_light_default_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(hal_light_default)

-----> system/sepolicy/vendor/hal_light_default.te

<规则2> .绑定文件和selinux type

/sys/devices/soc/soc:flashlight/leds/flashlight/brightness          u:object_r:led_control:s0
/sys/class/leds/flashlight/brightness                               u:object_r:led_control:s0
/sys/devices/soc/[a-z0-9]+.i2c/i2c-[0-9]/[0-9]-[a-z0-9]+/input/input[0-9]/apds9930-light(/.*)?           u:object_r:sysfs_sensors:s0
/sys/devices/soc/[a-z0-9]+.i2c/i2c-[0-9]/[0-9]-[a-z0-9]+/input/input[0-9]/ltr553-light(/.*)?             u:object_r:sysfs_sensors:s0

------->device/qcom/sepolicy/common/file_contexts

<规则3> allow <scontext> <tcontext>:<tclass> <operation>

allow hal_light_default sysfs:file { write r_file_perms  };
allow hal_light_default led_control:file rw_file_perms;

  -----> system/sepolicy/vendor/hal_light_default.te

2)规则限制的

domain.te 该策略文件会限制一些特征文件的权限,如

#
# Only system_app and system_server should be creating or writing
# their files. The proper way to share files is to setup
# type transitions to a more specific type or assigning a type
# to its parent directory via a file_contexts entry.
# Example type transition:
#  mydomain.te:file_type_auto_trans(mydomain, system_data_file, new_file_type)
#
neverallow {
  domain
  -system_server
  -system_app
  -init
  -installd # for relabelfrom and unlink, check for this in explicit neverallow
  with_asan(`-asan_extract')
system_data_file:file no_w_file_perms;

audit(107998.269:51): avc: denied { create } for pid=3519 comm=4173796E635461736B202331 name="libnfc-nxpConfigState.bin" scontext=u:r:nfc:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=0

此时规则是不能给system_data_file添加写权限的。

查找工程源码 ---------> phNxpConfig.cpp

const char config_timestamp_path[] =
        "/data/vendor/nfc/libnfc-nxpConfigState.bin";

修改该路径可以避免各种报错,类似情况,找准原因,类似修改。

2.Android提供了很多方法可以查看验证修改的权限

1)直接查看编译目录out/target/product/xxx/obj/ETC/sepolicy_intermediates/policy.conf

2) 烧录镜像文件,查看串口或者kernel的打印信息

3.查看进程pid和文件sepolicy属性

查看进程pid

查看文件 sepolicy属性

4.推荐一篇比较专业全面关于SEAndoid/SELinux(共三部分)的分析博客:

Addr:https://blog.csdn.net/innost/article/details/19299937

猜你喜欢

转载自blog.csdn.net/Tree_in_sea/article/details/81129604