SELinux 权限

如何根据log 信息修改SELinux 权限

当出现 SELinux 权限不足时,可根据LOG 信息提示进行相应的修改。下面介绍如何根据 kernel log 来查看信息

1. disable selinux

在终端下

su
setenforce 0

该操作主要是为了禁用系统SELinux,让权限暂时通过。但是依然是会输出 SELinux 校验时的LOG。后面根据输出的LOG 进行相应的处理。

2. 查看kernel log

复现问题,导出 kernel log.

或者 adb shell su 之后输入 cat /dev/kmsg | grep avc 在控制台输出关键信息

kernel 日志的输出在 /dev/kmsg 节点下,avcSELinux 打印的信息

如:

5,4978,791423968,-;audit: type=1400 audit(1556096333.959:26): avc:  denied  { write } for  pid=5106 comm="Log_LogStrategy" name="Logs_Collector" dev="mmcblk0p40" ino=16321 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
5,4979,791424019,-;audit: type=1400 audit(1556096333.959:27): avc:  denied  { add_name } for  pid=5106 comm="Log_LogStrategy" name="amefrktoukgokiph_20190424165853.zip" scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
5,4980,791424176,-;audit: type=1400 audit(1556096333.959:28): avc:  denied  { create } for  pid=5106 comm="Log_LogStrategy" name="amefrktoukgokiph_20190424165853.zip" scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1
5,4981,791425437,-;audit: type=1400 audit(1556096333.959:29): avc:  denied  { write } for  pid=5106 comm="Log_LogStrategy" path="/data/Logs_Collector/amefrktoukgokiph_20190424165853.zip" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1
5,4982,797691545,-;audit: type=1400 audit(1556096340.229:30): avc:  denied  { remove_name } for  pid=5086 comm="xtc.systemagent" name="amefrktoukgokiph_20190424165853.zip" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
5,4983,797691614,-;audit: type=1400 audit(1556096340.229:31): avc:  denied  { rename } for  pid=5086 comm="xtc.systemagent" name="amefrktoukgokiph_20190424165853.zip" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1
5,4984,797693717,-;audit: type=1400 audit(1556096340.229:32): avc:  denied  { unlink } for  pid=5086 comm="xtc.systemagent" name="1556096340234" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1

3. 修改te 文件

  • 3.1 找到对应的domain

如:

scontext=u:r:system_app:s0

domainsystem_app, 所以需要修改 system/sepolicy/system_app.te 文件

  • 3.2 找到 target 和 Object Class

如:

tcontext=u:object_r:system_data_file:s0 tclass=dir

targetsystem_data_file, object classdir

  • 3.3 找到缺失的权限

如:

denied  { add_name }

缺失的权限为: add_name

  • 3.4 最后整合整个缺失的权限,统一处理

最后针对上面的权限提示修改的 system/sepolicy/system_app.te 为,增加如下内容:

allow system_app system_data_file:dir { add_name remove_name rw_dir_perms };
allow system_app system_data_file:file { create rename unlink rw_file_perms };

格式(分号结尾)

allow scontext tcontext:tclass { perm };

4. 检查是否违反了 neverallow

可以先进行尝试编译,若有违反 neverallowSELinux 定义,会编译不通过,同时会提示哪个 te 文件中定义的 neverallow 冲突了。

如上面的修改会导致 system/sepolicy/app.te 文件中的 neverallow 规则有冲突。

原先冲突的 nerverallow 定义为:

# Write to system-owned parts of /data.
# This is the default type for anything under /data not otherwise
# specified in file_contexts.  Define a different type for portions
# that should be writable by apps.
neverallow { appdomain} system_data_file:dir_file_class_set
    { create write setattr relabelfrom relabelto append unlink link rename };

这里定义了不允许 system_data_filetarget 进行 create write unlink 等操作

此时解决方法是,需要过滤去除掉我们需要的 domain

进行修改如下:

neverallow { appdomain -system_app }
    system_data_file:dir_file_class_set
    { create write setattr relabelfrom relabelto append unlink link rename };

加上 -system_app 的排除语法。

5. 重编系统

至此根据 kernel log 信息提取权限完成,重新编译系统。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容