文章目录
- 简要概述
- sepolicy/file_contexts
- sepolicy/property_contexts
- sepolicy/system_app.te
- sepolicy/sandroidlog.te
- init.tbox.rc
- 验证过程
简要概述
通过rc 文件服务启动SAndroidLog二进制程序,读取persist.tbox.log属性配置日志存储路径,通过 SELinux 权限管控文件 / 设备访问,最终将 Tbox 日志写入指定路径。
以下是关于通过ctl.start的方式启动SAndroidLog的过程目录结构
sepolicy/file_contexts
配置SAndroidLog的文件上下文,给SAndroidLog二进制文件打 SELinux 标签,体现在sandroidlog.te中
python
# SAndroidLog 执行文件上下文
/system/bin/SAndroidLog u:object_r:sandroidlog_exec:s0
配置好了之后,如果通过ls -Z /system/bin/SAndroidLog查询到的结果不是u:object_r:sandroidlog_exec:s0时,进入该文件目录,执行下restorecon SAndroidLog,此时文件就会重新加载。
sepolicy/property_contexts
定义一个property属性值,然后在sandroidlog.te和system_app.te中进行配置
python
persist.tbox.log u:object_r:system_sandroidlog_prop:s0
sepolicy/system_app.te
设置在property_contexts中定义的system_sandroidlog_prop属性值,让system组的app拥有处理persist.tbox.log这个property的权限
cpp
set_prop(system_app, system_sandroidlog_prop)
get_prop(system_app, system_sandroidlog_prop)
sepolicy/sandroidlog.te
sandroidlog会在rc的启动文件中进行配置,seclabel u:r:sandroidlog:s0,以下是配置te文件所需要的代码。
python
# 定义域和执行文件类型(基础必备,解决 transition/execute_no_trans 报错)
type sandroidlog, coredomain, domain, system_executes_vendor_violators;
type sandroidlog_exec,system_file_type,exec_type,file_type;
# 注册为init托管的守护进程
init_daemon_domain(sandroidlog)
# 属性权限:读 persist.tbox.android.log
get_prop(sandroidlog, system_sandroidlog_prop)
# USB/串口设备权限(Tbox通信必备)
allow sandroidlog sysfs:dir { read open };
allow sandroidlog sys_soc_hcd_usb:dir { search read open};
allow sandroidlog sys_soc_hcd_usb:file { read open getattr };
allow sandroidlog sys_devices_soc_usb:dir search;
...
# 存储权限(USB/内部存储写日志)
allow sandroidlog mnt_user_file:dir search;
allow sandroidlog fuse:dir { search write add_name };
allow sandroidlog fuse:file { create read write open };
如果不配置sandroidlog.te文件,rc中使用默认提示的seclabel u:object_r:system_file:s0进行配置,执行SAndroidLog进程时,有以下的报错信息
avc: denied { execute_no_trans }
avc: denied { transition }
这表明必须单独配置 te 文件。对于位于 /system/bin/ 目录下的可执行文件,最基本的配置示例如下:
python
type sandroidlog, coredomain, domain, system_executes_vendor_violators;
type sandroidlog_exec,system_file_type,exec_type,file_type;
init_daemon_domain(sandroidlog)
针对get_prop,是为了在配置的rc脚本中获取到persist.tbox.log的属性值
python
get_prop(sandroidlog, system_sandroidlog_prop)
针对allow 开头的一些字段,是需要在运行时,看有什么具体的报错信息来进行配置的。
init.tbox.rc
关于SAndroidLog进程的rc文件配置信息
为了不影响其他的rc文件,此时采用新建一个init.tbox.rc文件
python
# Tbox Android日志服务
// vendor/device/etc/init/init.tbox.rc
service tobx_android_log /system/bin/SAndroidLog -s ${persist.tbox.log} -f /system/bin/default.cfg -m 100
# 归属主服务类
class main
# 运行用户+用户组(解决dac_override、设备访问权限)
user system
group system bluetooth media_rw everybody
# SELinux域(必须与te文件一致)
seclabel u:r:sandroidlog:s0
# 默认不自动启动,通过ctl.start手动启动
disabled
# 只执行一次
oneshot
SAndroidLog文件目录:
vendor/device/etc/tbox/SAndroidLog
default.cfg文件目录:
vendor/device/etc/tbox/default.cfg
无论是rc文件,还是提供的bin文件,最终都是要集成到车机中去的
需要加入以下代码进行处理
在产品device.mk或product.mk中添加:
python
# tbox SAndroidLog
PRODUCT_COPY_FILES += \
# 二进制文件到system/bin
vendor/device/etc/tbox/SAndroidLog:/system/bin/SAndroidLog \
# 配置文件到system/bin
vendor/device/etc/tbox/default.cfg:/system/bin/default.cfg \
# rc服务文件到vendor/etc/init(系统自动加载)
vendor/device/etc/init/init.tbox.rc:/vendor/etc/init/init.tbox.rc
如果在执行tobx_android_log服务时,出现了以下的报错
python
avc: denied { dac_override } for capability=1 scontext=u:r:sandroidlog:s0 tcontext=u:r:sandroidlog:s0 tclass=capability permissive=0
此时采用allow sandroidlog sandroidlog:capability dac_override;这种方式是不行的。
此时需要知道访问了哪个节点或者哪个文件目录,这个需要被告知,无法通过报错信息来识别到的。
表示不同组之间的域无法直接访问,此时就需要配置group组的权限。
bluetooth:表示访问的是dev/ttyUSB下的权限配置。
media_rw everybody:表示访问的是storage/usb0下的权限配置。
验证过程
文件上下文不生效(ls -Z 标签不对)
cpp
# 进入目录重置上下文
cd /system/bin
restorecon QAndroidLog
检查二进制文件权限:chmod 0755 /system/bin/QAndroidLog
检查 SELinux 标签:ls -Z /system/bin/QAndroidLog
检查属性值:getprop persist.tbox.android.log(不能为空)
make命令
make selinux_policy
编译好了后需要替换selinux相关路径文件,路径如下所示。替换好文件后,adb shell sync,之后重启。如果没有生效,可以先进入机器目录看下文件是否被替换成功了。
python
adb push out/target/product/xxx/system/etc/selinux /system/etc/
adb push out/target/product/xxx/vendor/etc/selinux /vendor/etc/
adb push out/target/product/xxx/system_ext/etc/selinux /system_ext/etc/
adb push out/target/product/xxx/product/etc/selinux /product/etc/
# 同步并重启
adb shell sync
adb reboot
关键信息查询
python
# 存储tbox log的文件路径,这里是个传参
persist.tbox.log
# 通过adb的方式来start stop tobx_android_log服务
# 启动服务
setprop ctl.start tobx_android_log
# 停止服务
setprop ctl.stop tobx_android_log
# 查看服务状态
getprop init.svc.tobx_android_log
running # 运行中
stopped # 已停止
restarting # 重启
# 关键字过滤 SAndroidLog主要是用于过滤avc相关日志,tobx_android_log主要是用于看服务的start、stop
logcat | grep -E "SAndroidLog|tobx_android_log"