Linux之SELinux
发布日期:2021-06-28 19:42:25 浏览次数:3 分类:技术文章

本文共 4730 字,大约阅读时间需要 15 分钟。

Linux之SELinux

  • 这是维基百科的说明。

接触SELinux的原因

  • 在使用docker拉取gitlab镜像进行配置,在启动gitlab的时候出现了小插曲。启动gitlab的命令如下:
sudo docker run --detach \--hostname 192.168.1.14 \--publish 10443:443 --publish 10080:80 --publish 10022:22 \--name gitlab \--restart always \--volume ~/gitlab/config:/etc/gitlab \--volume ~/gitlab/logs:/var/log/gitlab \--volume ~/gitlab/data:/var/opt/gitlab \gitlab/gitlab-ce:latest
  • 然后出现一直无法创建成功的状态,通过docker logs -f gitlab查看日志发现如下图的错误效果:

error

  • cp: cannot create regular file '/etc/gitlab/gitlab.rb': Permission denied
  • 刚开始以为是用户权限问题,但是回想centos启动的时候就配置了一个root的超级用户权限,如下所示:
[root@localhost ~]# whoamiroot[root@localhost ~]#
  • 排除了用户权限的原因,就想到了SELinux的服务是否影响了docker的启动效果,所以开始尝试去关闭SELinux的服务。

SELinux服务

简介

  • SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC(Media Address Control)机制。
  • Security-Enhanced Linux(SELinux)由以下两部分组成:
    • Kernel SELinux模块(/kernel/security/selinux)
    • 用户态工具
  • SELinux是一个安全体系结构,它通过LSM(Linux Security Modules)框架被集成到Linux Kernel 2.6.x中。它是NSA(United States National Security Agency)和SELinux社区的联合项目。
  • SELinux提供了一种灵活的强制访问控制(MAC)系统,且内嵌于Linux Kernel中。SELinux定义了系统中每个用户进程应用文件的访问和转变的权限,然后它使用一个安全策略来控制这些实体(用户、进程、应用和文件)之间的交互,安全策略指定如何严格或者宽松地进行检查。
  • SELinux对系统用户(system users)是透明的,只有系统管理员需要考虑在他的服务器中如何制定严格的策略。策略可以根据需要是严格或者是宽松的。
  • 只有同时满足了标准Linux访问控制SELinux访问控制时,主题才能访问客体。

DAC和MAC的关键区别(root用户)

  • 安全增强型Linux(SELinux) 开始是由NSA(国家安全局)启动并加入到Linux系统中的一套核心组件及用户工具,可以让应用程序运行在其所需的最低权限上。未经修改过的Linux系统是使用自主访问控制的,用户可以自己请求更高的权限,由此恶意软件几乎可以访问任何它想访问的文件,而如果你授予其root权限,那它就无所不能了。
  • 在SELinux中没有root这个概念,安全策略是由管理员来定义的,任何软件都无法取代它。这意味着那些潜在的恶意软件所能造成的损害可以被控制在最小。一般情况下只有非常注重数据安全的企业级用户才会使用SELinux
  • 操作系统有两类访问控制:自主访问控制(DAC)强制访问控制(MAC)。标准Linux安全是一种DAC,SELinux为Linux增加了一个灵活的和可配置的MAC。
  • 所有DAC机制都有一个共同的弱点,就是它们不能识别自然人与计算机程序之间最基本的区别。简单点说就是,如果一个用户被授予允许访问,意味着程序也被授予访问,如果程序被授予访问,那么恶意程序也将有同样的访问权。DAC最根本的弱点是主体容易受到多种多样的恶意软件的攻击,MAC就是避免这些攻击的出路,大多数MAC特性组成了多层安全模型。
  • SELinux实现了一个更灵活的MAC形式,叫做类型强制(Type Enforcement)和一个非强制的多层安全形式(Multi-Level Security)。
  • 在Android4.2中,SELinux是个可选项,谷歌并没有直接取消root权限或者其他功能。这是一个为企业级用户或者是对隐私数据极为重视的用户提供的选项,普通消费者则完全可以关闭它。

SELinux的运行机制

  • SELinux决策过程如下图所示:

SELinux

  • 当一个subject(如: 一个应用)试图访问一个object(如: 一个文件),Kernel中的策略执行服务器将检查AVC(Access Vertor Cache),在AVC中,subject和object的权限被缓存(cached)。如果基于AVC中的数据不能做出决定,则请求安全服务器,安全服务器在一个矩阵中查找"应用+文件"的安全环境。然后根据查询结果允许或者拒绝访问,拒绝消息细节位于/var/log/messages中。

SELinux伪文件系统

  • /selinux/伪文件系统kernel子系统通常使用的命令,它类似于/proc伪文件系统。系统管理员和用户不需要操作这部分。/selinux/目录举例如下:
[root@localhost ~]# cd /sys/fs/selinux/[root@localhost selinux]# lltotal 0-rw-rw-rw-.  1 root root    0 Sep 25 10:09 accessdr-xr-xr-x.  2 root root    0 Sep 25 10:09 avcdr-xr-xr-x.  2 root root    0 Sep 25 10:09 booleans-rw-r--r--.  1 root root    0 Sep 25 10:09 checkreqprotdr-xr-xr-x. 99 root root    0 Sep 25 10:09 class--w-------.  1 root root    0 Sep 25 10:09 commit_pending_bools-rw-rw-rw-.  1 root root    0 Sep 25 10:09 context-rw-rw-rw-.  1 root root    0 Sep 25 10:09 create-r--r--r--.  1 root root    0 Sep 25 10:09 deny_unknown--w-------.  1 root root    0 Sep 25 10:09 disable-rw-r--r--.  1 root root    0 Sep 25 10:09 enforcedr-xr-xr-x.  2 root root    0 Sep 25 10:09 initial_contexts-rw-------.  1 root root    0 Sep 25 10:09 load-rw-rw-rw-.  1 root root    0 Sep 25 10:09 member-r--r--r--.  1 root root    0 Sep 25 10:09 mlscrw-rw-rw-.  1 root root 1, 3 Sep 25 10:09 null-r--r--r--.  1 root root    0 Sep 25 10:09 policydr-xr-xr-x.  2 root root    0 Sep 25 10:09 policy_capabilities-r--r--r--.  1 root root    0 Sep 25 10:09 policyvers-r--r--r--.  1 root root    0 Sep 25 10:09 reject_unknown-rw-rw-rw-.  1 root root    0 Sep 25 10:09 relabel-r--r--r--.  1 root root    0 Sep 25 10:09 status-rw-rw-rw-.  1 root root    0 Sep 25 10:09 user[root@localhost selinux]#
  • cat enforce其值可能如下:
[root@localhost selinux]# cat enforce 0[root@localhost selinux]#
  • 1: enforcing mode
  • 0: permissive mode

SELinux配置文件

  • SELinux配置文件(configuration)或者策略文件(policy)位于/etc/目录下。

/etc/sysconfig/selinux配置文件

  • /etc/sysconfig/selinux是一个符号链接,真正的配置文件为: /etc/sysconfig/config
[root@localhost ~]# cd /etc/sysconfig/[root@localhost sysconfig]# ll...lrwxrwxrwx. 1 root root   17 Sep 19 09:48 selinux -> ../selinux/config...[root@localhost sysconfig]#
  • 配置SELinux有如下两种方式:
    • 1、使用配置工具:Security Level Configuration Tool(system-config-selinux)
    • 2、编辑配置文件(/etc/sysconfig/selinux)
  • /etc/sysconfig/selinux中包含如下配置选项:
    • 1、打开或者关闭SELinux
    • 2、设置系统执行哪一个策略(policy)
    • 3、设置系统如何执行策略(policy)

配置文件选项

SELinux

  • SELINUX=enforcing|permissive|disabled —— 定义SELinux的高级状态
    • enforcing —— The SELinux security policy is enforced.
    • permissive —— The SELinux system prints warnings but does not enforce policy.
    • disabled —— SELinux is fully disabled. SELinux hooks are disengaged from the kernel and the pseudo-file system is unregistered.

SELINUXTYPE(安全策略)

  • SELINUXTYPE=targeted|strict —— 指定SELinux执行哪一个策略。
    • targeted —— 只有目标网络daemons保护。每个daemon是否执行策略,可通过system-config-selinux进行配置。保护常见的网络服务,为SELinux默认值。

转载地址:https://blog.csdn.net/XXJ19950917/article/details/82843110 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ManageIQ之部署与安装
下一篇:Linux报错之ping: www.baidu.com: Name or service not known

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月25日 04时43分16秒