• Android SELinux


    ls -Z

    查看selinux的服务

    SELinux配置方法:

    参考官方文档:

    Security-Enhanced Linux in Android  |  Android Open Source Project

    在实际项目中,可以通过adb命令,查看自己的服务有哪些不满足selinux,一般利用dmesg | grep audit | grep 服务

    audit会列出SELinux相关,当有denied关键字表示需要配置。

    例如有如下:

    [ 627.377276] type=1400 audit(1629119715.869:5947): avc: denied { read write } for pid=465 comm="ppp@1.0-service" name="qseecom" dev="tmpfs" ino=12352 scontext=u:r:np_ppp_daemon:s0 tcontext=u:object_r:tee_device:s0 tclass=chr_file permissive=0 [ 634.428710] type=1400 audit(1629119722.919:6058): avc: denied { read write } for pid=465 comm="ppp@1.0-service" name="qseecom" dev="tmpfs" ino=12352 scontext=u:r:np_ppp_daemon:s0 tcontext=u:object_r:tee_device:s0 tclass=chr_file permissive=0

    上面代表服务ppp的权限问题,

    可以添加如下:

    allow np_ppp_daemon tee_device:chr_file { ioctl open read write };

    ioctl open read write参数根据avc:denied的项目添加

    注意:np_ppp_daemon定义

    /(vendor|system/vendor)/bin/hw/vendor\.cm\.ppp@1\.0-service          u:object_r:np_ppp_daemon_exec:s0

    vendor.ts.ppp::IPpp                      u:object_r:np_ppp_hwservice:s0

    type np_ppp_hwservice, hwservice_manager_type;

    vendor.ppp.                         u:object_r:vendor_ppp_prop:s0

    type vendor_ppp_prop, property_type;

    type np_ppp_daemon,domain;

    type np_ppp_daemon_exec,exec_type,vendor_file_type,file_type;

    init_daemon_domain(np_ppp_daemon)   注:开机启动需要加

    hwbinder_use(np_ppp_daemon);

    add_hwservice(np_ppp_daemon, np_ppp_hwservice)

    allow np_ppp_daemon hwservicemanager_prop:file r_file_perms;

    allow np_ppp_daemon tee_device:chr_file { ioctl open read write };

    allow np_ppp_daemon ion_device:chr_file { open ioctl read write };

    列子二:

    Line 2079: [    9.178500] type=1400 audit(258.273:17): avc: denied { create } for comm="test_abc" scontext=u:r:test_abc:s0 tcontext=u:r:test_abc:s0 tclass=tcp_socket permissive=0
        Line 2158: [    9.609626] type=1400 audit(258.273:17): avc: denied { create } for comm="test_abc" scontext=u:r:test_abc:s0 tcontext=u:r:test_abc:s0 tclass=tcp_socket permissive=0

    7、修改test_abc.te 增加test_abc的相关权限,编译版本,重新进行验证:

    #============= test_abc ==============

    allow test_abc self:capability dac_override;

    allow test_abc self:tcp_socket create;   (test_abc是他自己,所以是self)

    3.具体案例分析

    解决思路:缺少什么权限就补充什么权限。

    如何修改权限?需要修改sepolicy/*.te的权限文件。(不同源码可能路径不同)

    案例一:

     
    

    audit(0.0:67): avc: denied { write } for path="/dev/block/vold/93:96" dev="tmpfs" ino=/1263 scontext=u:r:kernel:s0 tcontext=u:object_r:block_device:s0 tclass=blk_file permissive=0

    分析:

    缺少的权限:{ write } 权限

    谁缺少该权限:scontext = u:r:kernel:s0

    对哪个文件缺少权限: tcontext = u:object_r:block_device

    什么类型的文件: tclass = blk_file

    完整意思:kernel进程对block_device类型的blk_file缺少write权限。

    解决办法:

    在SEpolicy目录下,找到kernel.te这个文件,加入以下内容:

     
    

    allow kernel block_device:blk_file write;

    案例二:

     
    

    audit(0.0:53): avc: denied { execute } for path="/data/data/com.mofing/qt-reserved-files/plugins/platforms/libgnustl_shared.so" dev="nandl" ino=115502 scontext=u:r:platform_app:s0 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=0

    分析过程:

    缺少什么权限: { excute } 权限

    谁缺少该权限: scontext = u:r:platform_app:s0

    对哪个文件缺少权限: tcontext = u:object_r:app_data_file

    什么类型的文件:tclass = file

    完整意思:platform_app进程对app_data_file类型的file缺少excute权限。

    解决办法:

    在SEpolicy目录下,找到platform_app.te这个文件,加入以下内容:

     
    

    allow platform_app app_data_file:file excute;

    4.audit2allow

    我们可以使用audit2allow工具来自动生成所需要的规则。

    (1)将avc deniedlog保存到avc文件中。

    (2)audit2allow -i avc -o avc.txt

    5.Tips

    有时候avc denied的log不是一次性暴露所有权限问题,要等解决一个权限问题之后才会暴露另一个权限问题。比如提示缺少某个目录的read权限,加入read后,才显示缺少write权限,要一次一次试,一次一次加,时间成本极大。

    针对dir缺少的任何权限,建议赋予creat_dir_perms,基本涵盖了对dir的所有权限,比如{ open search write read rename creat rmdir getaddr }等。

    针对file缺少的任何权限,建议赋予rwx_file_perms,基本涵盖了对file的所有权限,比如{ open read write execute getaddr creat ioctl }等。


    其他常用操作及常见问题说明

    SELinux的三种状态

    • Permissive:对违反SELinux的行为放行,但会生成相关log。
    • Enforcing:禁止违反SELinux的行为发生,并生成相关log。
    • Disable:关闭。

    查看SELinux状态

    (1)通过命令:getenforce

    (2)通过Log:avc denied log中含有permissive=0,代表SELinux开启。

    更改SELinux状态

    setenforce 0 设置SELinux为Permissive状态

    setenforce 1 设置SELinux为Enforcing状态

    文件操作与SELinux

    创建文件:新创建的文件的安全上下文和所在目录的安全上下文相同。

    拷贝文件:拷贝之后的文件的安全上下文会变为目的位置所在目录的安全上下文。如果要保证拷贝之后文件的安全上下文不发生变化,可以在拷贝时加上参数

    --preserve=context

    移动文件:将文件移动到目标位置,文件的安全上下文不变。

    打包文件:不会打包安全上下文信息,与解压到的目录安全上下文信息相同。

    类型转换

    type_transition source-type target-exec-type:class default-type;

    类型转换与域转换使用相同的规则语句,不同的是此时的class为dir。其作用是当source-type类型的进程在target-type的dir中创建文件时,创建出的文件的类型为default-type。当然没有该语句,则新文件的type继承所在目录的type。

    域转换

    type_transition source-type target-exec-type:class default-type;

    作用是当source-type类型的进程执行target-exec-type类型的可执行文件时,产生的新的进程的typedefault-type。如果没有此规则的话,子进程会从父进程那里继承类型。

    在SEAndroid中,在initrc中由init进程生成的子进程都需要有一个新的类型,即必须要有对应的域转换的规则。否则即使SElinux的状态为permissive,仍然会导致新的子进程启动失败。

    也可使用domain_auto_trans宏进行域转换。

    restorecon

    在调试程序SELinux时,有可能push到车机的文件上下文会改变,可以使用restorecon命令恢复上下文。

    在调试程序SELinux时,有可能push到车机的文件上下文会改变,可以使用restorecon命令恢复上下文。

     
    

    restorecon xxx

    neverallow

    neverallow可能出现在编译期间,代表绝对不允许的操作,会提示违反SELinuxneverallow的TE语句,可以根据提示信息进行修改。

    allowxperm

    增加ioctl权限规则的时候,若通过allow语句不生效,且avc log中包含ioctlcmd = xxxx字段的时候,则需要使用allowxperm

    查找对应的16进制代码,在te文件中增加

    allowxperm A B:class ioctl MACRO

    类型定义命名规范

    type定义格式:"np_${module}_xxx_xxx"

    “np_${module}”:以此格式开头;

    “_xxx”:模块研发可自由发挥;

    “_xxx”:特殊类型要求的结尾;

    以np开头是google建议:

    类型/属性命名空间

    规范一:类型的命名都应该以“np_${module}”开头,例:

    type np_ota_deamon domain;(关联到init deamon域)

    type np_ota domain; (关联到domain域)

    app_domain(np_ota_app) (关联到app damain域)

    一个模块可能既有普通的进程,也有服务、属性等。所以需要np_${module}进行绑定,${module}的

    来源是proprietary下的仓库名称。

    规范二:若类型关联到了property_type,需要以“__prop”作为后缀。例:

    type np_ota_prop, property_type;

    规范三:若类型关联到了service_manager_type,需要以“__xxx_service”作为后缀。例:

    type np_ota_service, service_manager_type;

    规范四:若类型关联到了hwservice_manager_type上,需要以“__xxx_hwservice”作为后缀。例:

    type np_ota_hwservice, hwservice_manager_type;

    规范五:若类型关联到了vndservice_manager_type,需要以“_vndservice”作为后缀。例:

    type np_ota_vndservice, vndservice_manager_type;

    规范六:若类型关联到了fs_type上,需要以“_fs”直接连着上一个单词并结尾。例:

    type np_ota_fs, fs_type;

    规范七:若类型关联到了sysfs_type上,需要以”_sysfs”为后缀。例:

    type np_ota_sysfs, fs_type, sysfs_type;

    规范八:若类型关联到了debugfs_type上,需要以”_debugfs”为后缀。例:

    type np_ota_debugfs, fs_type, debugfs_type;

    规范九:若类型关联到了exec_type,需要以”_exec”作为后缀。例:

    type np_ota_exec, file_type, exec_type;

    Android P vendor分区需要额外关联到vendor_file_type,例:type np_ota_exec, file_type, vendor_file_type, exec_type;

    Android Q 上vendor和Android P保持一致

    Android Q system分区需要额外关联到system_file_type,例:

    type np_ota_exec, file_type, system_file_type, exec_type;

    规范十:若类型用来标识/proc下的文件,需要以”_proc”为后缀。例:

    type np_ota_proc, fs_type;

    规范十一:若类型关联到了log_property_type上,需要以”_log_prop”作为后缀。例:

    type np_ota_log_prop, property_type, log_property_type;

    规范十二:若类型关联到了appdomain上,需要以”_app”作为后缀。例:

    type np_ota_app, domain, appdomain;

    规范十三:若类型关联到了data_file_type,需要以”_data_file”作为后缀。例:

    type np_ota_data_file, file_type, data_file_type;

    规范十四:若类型关联到了dev_type,需要以”_device”作为后缀。例:

    type np_ota_device, dev_type;

    关联属性规范

    规范一:所有的域都需要关联到domain上。例:

    type np_ota_deamon, domain;

    规范二:用来标识/vendor或/system/vendor开头的进程的域不能关联到coredomain。例:

    /(vendor|system/vendor)/bin/hw/vendor.ts.tbox@1.0-service

    u:object_r:np_tbox_deamon_exec:s0

    type np_tbox_deamon_exec, coredomain <-- 错误的

    规范三:用来标识app的域需要关联到appdomain。例:

    type np_ota_app, domain, appdomain;

    规范四:关联到exec_type的类型,需要关联到file_type。例:

    type np_ota_exec, file_type, exec_type;

    /vendor/bin下的 exec_type 需要关联到vendor_file_type

    规范五:用于标识/vendor下文件的类型,需要关联到vendor_file_type。例:

    type np_ota_file, file_type, vendor_file_type;

    规范六:关联到vendor_file_type的类型,需要关联到file_type。例:

    type np_ota_file, file_type, vendor_file_type;

    规范七:用于标识/system/data下文件的类型,需要关联到data_file_type。例:

    type np_ota_data_file, file_type, data_file_type;

    规范八:关联到data_file_type的类型,需要关联到file_type。例:

    type np_ota_data_file, file_type, data_file_type;

    规范九:关联到core_data_file_type的类型,需要关联到data_file_type。例:

    type np_ota_data_file, file_type, data_file_type, core_data_file_type;

    规范十:用于标识可执行文件的类型,需要关联到exec_type。例:

    type np_ota_exec, file_type, exec_type;规范十一:关联到sysfs_type的类型,需要关联到fs_type。例:

    type np_ota_sysfs, fs_type, sysfs_type;

    规范十二:关联到debugfs_type的类型,需要关联到fs_type。例:

    type np_ota_debugfs, file_type, debugfs_type;

    规范十七:用于标识debug以及trace相关文件的类型,需要关联到debugfs_type。例:

    type np_ota_debugfs, fs_type, debugfs_type;

    规范十八:用于标识/sys下文件的类型,需要关联到sysfs_type。例:

    type np_ota_sysfs, fs_type, sysfs_type;

    规范十九:用于标识/dev下文件的类型,需要关联到dev_type。例:

    type np_ota_device, dev_type;

    Android 中的安全增强型 Linux  |  Android 开源项目  |  Android Open Source Project

  • 相关阅读:
    【Arthas性能排查系列(一)】检查Java程序调用链路的耗时情况
    【Node】使用Node.js构建简单的静态页面生成器
    Taurus.MVC 微服务框架 入门开发教程:项目集成:6、微服务间的调用方式:Rpc.StartTaskAsync。
    OPC Expert 最新版 Crack-2022-12-05
    eunomia-bpf项目重磅开源!eBPF 轻量级开发框架来了
    MATLAB算法实战应用案例精讲-【人工智能】边缘计算(附python代码实现)
    【JAVA-QA】java注释的方式/格式/特点/作用知识点
    阿里云Nas文件存储的各种场景使用
    可持续建筑分论坛精彩回顾 | 第二届始祖数字化可持续发展峰会
    基于springboot实现乒乓球预约管理系统项目【项目源码】计算机毕业设计
  • 原文地址:https://blog.csdn.net/tengfeidx/article/details/119423073