3

#创作者激励# #跟着小白一起学鸿蒙# SELinux简析-开源基础软件社区-51CTO.COM

 1 year ago
source link: https://ost.51cto.com/posts/22384
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#创作者激励# #跟着小白一起学鸿蒙# SELinux简析 原创 精华

SELinux

#创作者激励# #跟着小白一起学鸿蒙# SELinux简析-开源基础软件社区

SELinux说明

SELinux(Security-Enhanced Linux) 是[美国国家安全局](NSA)对于[强制访问控制]的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 主要作用就是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。

SELinux基本概念

  • 主体Subjects:可以完全等同于进程。

  • 目标Objects:被主体访问的资源。可以是文件、目录、端口、设备等。

  • 策略Policy:哪些进程需要管制、要怎么管制是由政策决定。规则是模块化、可扩展的。在安装新的应用程序时,应用程序可通过添加新的模块来添加规则。用户也可以手动地增减规则。

  • 模式Mode:默认有三种模式:

    • Enforcing 强制— SELinux 策略强制执行,基于 SELinux 策略规则授予或拒绝主体对目标的访问
    • Permissive 宽容— SELinux 策略不强制执行,不实际拒绝访问,但会有拒绝信息写入日志
    • Disabled 禁用— 完全禁用SELinux

OpenHarmony SELinux使用介绍

#创作者激励# #跟着小白一起学鸿蒙# SELinux简析-开源基础软件社区

OpenHarmony里的selinux部件负责对文件,属性,服务等系统资源提供强制访问控制保护。提供neverallow规则限制系统中的高危操作,减少系统安全风险。

  1. 文件结构(distributedatamgr)

    ├── BUILD.gn
    ├── bundle.json
    ├── config								# 板侧		三方库配置文件
    │   ├── config.enforce
    │   └── config.permissive
    ├── docs								# 文档
    ├── interfaces
    │   ├── policycoreutils					# 板侧		libload_policy.so, librestorecon.so	
    │   │   ├── include
    │   │   └── src
    │   └── tools							# 板侧		load_policy, restorecon
    │       ├── hap_restorecon
    │       ├── load_policy
    │       ├── param_check
    │       ├── restorecon
    │       └── service_check
    ├── LICENSE
    ├── OAT.xml
    ├── README-en.md
    ├── README.md
    ├── scripts								# 编译侧		策略编译脚本
    ├── selinux.gni
    ├── sepolicy							# 编译侧		策略文件
    │   ├── base
    │   │   ├── public
    │   │   ├── system
    │   │   └── te
    │   └── ohos_policy
    │       ├── ability
    │       ├── ark
    │       ├── arkXtest
    │       ├── barrierfree
    │       ├── bundlemanager
    │       ├── cloud
    │       ├── communication
    │       ├── customization
    │       ├── developtools
    │       ├── deviceprofile
    │       ├── distributeddatamgr
    │       ├── distributedhardware
    │       ├── distributedschedule
    │       ├── drivers
    │       ├── dsoftbus
    │       ├── filemanagement
    │       ├── global
    │       ├── graphic
    │       ├── hiviewdfx
    │       ├── iam
    │       ├── kernel
    │       ├── location
    │       ├── misc
    │       ├── miscservices
    │       ├── msdp
    │       ├── multimedia
    │       ├── multimodalinput
    │       ├── notification
    │       ├── os_account
    │       ├── powermgr
    │       ├── print
    │       ├── resourceschedule
    │       ├── security
    │       ├── sensors
    │       ├── startup
    │       ├── telephony
    │       ├── test_framework
    │       ├── update
    │       ├── usb
    │       ├── useriam
    │       ├── web
    │       ├── window
    │       └── xts
    └── test
    
    • 将镜像烧录到开发板上,开机,hdc_std shell登录板子 ,在其中执行:
    ls -lZ /         # 查看文件标签
    ls -lLZ /        # 查看link源文件标签
    ps -eZ           # 查看进程标签
    setenforce 1     # 使能selinux强制模式
    setenforce 0     # 是能selinux宽容模式,当前默认宽容模式
    getenforce       # 获取selinux工作模式
    
    • 策略文件:/etc/selinux/targeted/policy/policy.31

    • 文件标签规则 /etc/selinux/targeted/policy/file_contexts

    • selinux模式开关 /etc/selinux/config

  2. //使用dmsg查看日志
    audit: type=1400 audit(1502458430.566:4): avc:  denied  { open } for  pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1
    
    //关键字:avc: denied
    
    //日志解读
    open                                #操作为open
    pid=1658                            #访问主体进程号为1658
    comm="setenforce"                   #访问主体进程名为setenforce
    path="/sys/fs/selinux/enforce"      #被访问客体为/sys/fs/selinux/enforce
    dev="selinuxfs"                     #被访问文件属于selinuxfs这一文件系统
    ino=4                               #文件节点编号为4
    scontext=u:r:hdcd:s0                #访问主体selinux标签为u:r:hdcd:s0
    tcontext=u:object_r:selinuxfs:s0   #被访问客体selinux标签为u:object_r:selinuxfs:s0
    tclass=file                         #当前告警属于file类型的操作
    permissive=1                        #当前selinux处于宽容模式,只告警不做访问拦截。强制模式时,做拦截, permissive=0
    
    //分析:
    缺少啥权限:denied  { open }
    谁缺少权限:scontext=u:r:hdcd:s0
    对哪个文件缺少:tcontext=u:object_r:selinuxfs:s0
    什么类型:tclass=file
    通用公式为:all {scontext} {tcontext}:{tclass} {denied权限}
    
  3. 根据avc告警,获取访问信息
    如:
    audit: type=1400 audit(1502458430.566:4): avc:  denied  { open } for  pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1
    对应规则为
    allow hdcd selinuxfs:file open;
    
    • 仓名:OpenHarmony / security_selinux
    • 位置:按照部件分类,比如wifi为sepolicy/ohos_policy/communication/wifi/system/xxx.te
    • 加具体规则进对应te文件

SELinux是3.2增加的安全保护措施,所以如果我们开发了新服务,新框架,很有可能会碰到程序不能运行的问题,简单的判断方法有三种:

  1. 找到这个文件:vendor/hihope/rk3568/config.json (不同的开发板文件不同哦)
    改build_selinux为false
    这样镜像就不支持selinux了,然后应该就能运行了,但是不推荐,破坏安全环境
    
  2. setenforce 0
    使用此命令后,会关闭selinux判断,但是下次重启会恢复,此方法用于临时验证问题
    
  3. 增加te规则(如上解释)

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2023-3-24 17:26:50修改

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK