SELinux

您所在的位置:网站首页 selinux怎么处于严格模式 SELinux

SELinux

2023-06-14 10:06| 来源: 网络整理| 查看: 265

一、简介

        SELinux是一种强制访问控制(MAC)机制,在许多GNU/Linux发行版中都是内置的。SELinux起源于FLASK框架(Flux Advanced Security Kernel),该框架由犹他大学Flux团队和美国国防部合作开发。后来美国国家安全局对此做了进一步开发,并作为开源软件发布。

二、概念 1、SELinux上下文

        每个进程和系统资源都有一个特殊的安全标签,称为 SELinux 上下文。SELinux 上下文有时称为 SELinux 标签。

        SELinux 上下文包括以下字段: user(用户)、role(角色)、type(类型)和 security level(安全级别)。在 SELinux 策略中,SELinux 类型信息可能是最重要的。这是因为,最常用的、用于定义允许在进程和系统资源间进行的交互的策略规则会使用 SELinux 类型而不是 SELinux 的完整上下文。SELinux 类型通常以 _t 结尾。

        SELinux 策略在一系列规则中使用这些上下文,它们定义进程如何相互交互以及与各种系统资源进行交互。默认情况下,策略不允许任何交互,除非规则明确授予了相应的权限。

2、Type Enforcement

         TE 是SELinux的一种重要的访问控制机制,它基于源(source)和目标(target)的类型确定是否允许访问。源和目标也被称为主体(subject)和客体(object)。主体是一个活动实体(比如一个进程),用于执行一次访问。客体(比如一个文件、路径或其它进程)是一个被访问的实体。例如,vim打开并编辑一个文件,主体是vim进程,客体是这个文件。

         类型是一个安全属性。系统中具有相同安全属性的所有主体和客体都应该具有相同类型。例如,系统中所有的共享库有相同的类型lib_t,因为从安全角度来说,它们是等价的。

         SELinux安全策略包含了TE规则,用于描述哪些访问是被允许的。SELinux策略是非常灵活的,很多安全目标可以通过策略来实现,比如完整性和分离。

3、MLS Enforcement

        多级别安全(Multi-Level Security,简称 MLS)策略使用许可级别的概念,这个概念首先由美国国防人员设计。MLS 满足一组非常严格的安全要求,这基于在严格控制的环境中管理的信息(如军事)。

        使用 MLS 非常复杂,不适合于一般用例场景。

        多级别安全(MLS)技术使用信息安全级别将数据分为分级分类,例如:

                [Low] 非保密

                [low] 保密

                [high] 机密

                [Highest] 顶端机密

        默认情况下,MLS SELinux 策略使用 16 敏感度级别:

                s0 是最低敏感级。

                s15 是最高敏感级。

        MLS 使用特定的术语来代表敏感度级别:

        用户和进程称为主体(subjects),其敏感度级别被称为安全权限(clearance)。

        系统文件、设备和其他被动组件称为客体(objects),其敏感度级别被称为安全级别

(classification)。

        为了实施 MLS,SELinux 使用 Bell-La Padula Model (BLP)模型。这个模型根据附加到每个主体和客体的标签指定系统中如何进行信息流。

        BLP 的基本原则是"不能从上面读取,不能向下面写入”。这意味着用户只能读取自己的敏感度级别及更低级别的文件,数据只能从较低级别流入到更高级别,且不会从高级别流向低级别。

        MLS SELinux 策略(RHEL 中的 MLS 实现)应用一个名为 Bell-La Padula with write equality 的经过修改的原则。这意味着,用户可以在自己的敏感度级别和更低级别中读取文件,但只能在自己的级别上写入。例如,这可以防止不明确的用户将内容写入 top-secret 文件中。

4、MCS Enforcement

        Multi-Category Security (MCS)是一个访问控制机制,它使用分配给进程和文件的类别。然后,文件只能由分配到相同类别的进程访问。MCS 的目的是维护您系统上的数据保密性。

        MCS 类别由 c0 到 c1023 的值定义,但您也可以为每个类别或类别组合定义一个文本标签,如"Personnel"、"ProjectX"或"ProjectX.Personnel"。MCS 转换服务(mcstrans)随后将 category 值替换为系统输入和输出中的相应标签,以便用户可以使用这些标签而不是 category 值。

        当用户分配给类别时,他们可以为他们分配的任何类别标记其任何文件。

        MCS 适用于一个简单的原则:要访问文件,必须将用户分配给该文件的所有类别。MCS 检查在常规 Linux Discretionary Access Control(DAC)和 SELinux Type Enforcement(TE)规则后应用,因此它只能进一步限制现有的安全配置。

        MLS中的 MCS:您可以将 MCS 用作非层次系统,也可以将其与多级别安全(MLS)结合使用,作为分层系统中的非层次结构层。

三、框架 1、High Level Core SELinux Components

 2、High Level SELinux Architecture

3、Processing a System Call

 四、使用场景

1、启用/禁用SELinux

SELinux 可以被启用或禁用。

启用后,SELinux 有两个模式: enforcing和 permissive。

使用 getenforce 或 sestatus 命令检查 SELinux 的运行模式。

getenforce 命令返回Enforcing、Permissive 或 Disabled。

sestatus 命令返回 SELinux 状态以及正在使用的 SELinux 策略。

打开 /etc/selinux/config 文件,配置 SELINUX选项,

SELINUX=disabled 或 permissive 或 enforcing

保存并重启系统

2、查询SELinux上下文 (1)查询用户的上下文

[root@localhost ~]# id -Z

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

(2)查询进程的上下文

[root@localhost ~]# ps auxZ

LABEL                      USER  PID  %CPU  %MEM    VSZ   RSS  TTY      STAT  START    TIME  COMMAND

system_u:system_r:kernel_t:s0  root   2    0.0    0.0       0     0    ?        S     5月27   0:00  [kthreadd]

(3)查询文件上下文

[root@localhost ~]# ll -Z

-rw-r--r--.  Root  root  unconfined_u:object_r:admin_home_t:s0  file1

3、更改SELinux上下文 (1)临时更改:chcon

[root@localhost ~]# chcon -t httpd_sys_content_t file1

[root@localhost ~]# ll -Z

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

(2)持久性更改:semanage fcontext

[root@localhost ~]#  semanage fcontext -a -t samba_share_t /root/file1  // 全路径,写入文件 /etc/selinux/targeted/contexts/files/file_contexts.local

[root@localhost ~]# ll -Z    // 查看文件上下文,尚未应用更改

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file1

[root@localhost ~]# semanage fcontext -C –l  // 查看新建的文件上下文

SELinux fcontext                                   类型                 上下文

/root/file1                                        all files          system_u:object_r:samba_share_t:s0

[root@localhost ~]# restorecon -v file1  // 应用上下文的更改

restorecon reset /root/file1 context unconfined_u:object_r:admin_home_t:s0->unconfined_u:object_r:samba_share_t:s0

[root@localhost ~]# ll -Z             // 上下文已更改

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 file1

4、查看SELinux拒绝信息

        SELinux 决策(如允许或禁止访问)会被缓存。这个缓存被称为 Access Vector Cache(AVC)。当SELinux 拒绝访问时,会记录拒绝消息。这些拒绝也称为 "AVC 拒绝",并记录到不同的位置,取决于正在运行的守护进程:

Daemon: auditd on

日志位置: /var/log/audit/audit.log

Daemon: auditd off; rsyslogd on

日志位置: /var/log/messages

Daemon: setroubleshootd、rsyslogd 和 auditd on

日志位置: /var/log/audit/audit.log,易于读取的拒绝消息也发送到 /var/log/messages

        如果 DAC 规则(标准 Linux 权限)允许访问,请分别检查 /var/log/messages 和

/var/log/audit/audit.log 的 "SELinux is prevent" 和" denied" 错误。

        以 root 用户身份运行以下命令来完成:

~]# grep "SELinux is preventing" /var/log/messages

~]# grep "denied" /var/log/audit/audit.log

5、修复SELinux拒绝问题 (1)检查标准 Linux 权限

如果访问被拒绝,且没有记录 SELinux 拒绝,查看标准 Linux 权限

[root@localhost ~]# ll file1

-rw-r--r--.  1  root  root  0  5月  19  14:13  file1

使用 chown 命令更改所有者和组

[root@localhost ~]# chown apache:apache file1

[root@localhost ~]# ll file1

-rw-r--r--. 1 apache apache 0 5月  19 14:13 file

(2)静默拒绝的原因

        由于 dontaudit 可防止审计记录出现于日志内,有可能部分 AVC 或 USER_AVC 不被显示。

        若要避免 dontaudit 规则隐藏这些信息,管理员可执行 semodule -DB 来重建不包含 dontaudit 规则的 SELinux 策略。重建后,日志内将会有更多信息,还有一些与问题无关的记录(noatsecure、rlimitinh 及 siginh 是执行程序时必检查的,因此一般可忽略)。

        检查完被 dontaudit 隐藏的记录后,请执行 semodule -B 重建含有 dontaudit 规则的策略。

         获得 dontaudit 规则的完整列表,请运行 sesearch --dontaudit 命令。使用 -s 域选项和 grep 命令缩小搜索范围:

[root@localhost ~]# sesearch --dontaudit | more

Found 10082 semantic av rules:

   dontaudit  chrome_sandbox_t  sandbox_web_type  :  netlink_generic_socket  getattr ;

   dontaudit  denyhosts_t  denyhosts_t  :  udp_socket  listen ;

[root@localhost ~]# sesearch --dontaudit -s smbd_t | grep squid

(3)SELinux为服务提供的man page

        selinux-policy-doc 软件包提供此类 man page,以 service-name_selinux格式命名。

        例如: httpd_selinux(8) 手册页包含有关为给定情况使用哪些文件类型的信息,以及布尔值以允许脚本、共享文件、访问用户主目录中的目录等。

        man page 中的信息有助于配置正确的文件类型和布尔值,从而防止 SELinux 拒绝访问。

五、工具

https://github.com/SELinuxProject/selinux/wiki/Tools

audit2allow(1)

Generates policy allow rules from an audit log file.

audit2why(8)

Describes audit log messages and why access was denied.

avcstat(8)

Displays the AVC statistics.

chcat(8)

Change or remove a catergory from a file or user.

chcon(1)

Changes the security context of a file.

checkmodule(8)

Compiles base and loadable modules from source.

checkpolicy(8)

Compiles a monolithic policy from source.

fixfiles(8)

Update / correct the security context of for filesystems that use extended attributes.

genhomedircon(8)

Generates file configuration entries for users home directories. This command has also been built into

semanage(8), therefore when using the policy store / loadable modules this does not need to be used.

getenforce(1)

Shows the current enforcement state.

getsebool(8)

Shows the state of the booleans.

load_policy(8)

Loads a new policy into the kernel. Not required when using semanage(8) / semodule(8) commands.

matchpathcon(8)

Show a files path and security context.

The SELinux Notebook

Page 416

newrole(1)

Allows users to change roles - runs a new shell with the new security context.

restorecon(8)

Sets the security context on one or more files.

run_init(8)

Runs an init script under the correct context.

runcon(1)

Runs a command with the specified context.

selinuxenabled(1)

Shows whether SELinux is enabled or not.

semanage(8)

Used to configure various areas of a policy within a policy store.

semodule(8)

Used to manage the installation, upgrading etc. of policy modules.

semodule_expand(8)

Manually expand a base policy package into a kernel binary policy file.

semodule_link(8)

Manually link a set of module packages.

semodule_package(8)

Create a module package with various configuration files (file context etc.)

sestatus(8)

Show the current status of SELinux and the loaded policy.

setenforce(1)

Sets / unsets enforcement mode.

setfiles(8)

Initialise the extended attributes of filesystems.

setsebool(8)

Sets the state of a boolean to on or off persistently across reboots or for this session only.

六、配置路径 1、Global Configuration Files

/etc/selinux/config

/etc/selinux/semanage.conf

/etc/selinux/restorecond.conf

restorecond-user.conf

/etc/selinux/newrole_pam.conf

/etc/sestatus.conf

/etc/security/sepermit.conf

2、Policy Store Configuration Files

NOTE:

Files in this area are private and subject to change, they should only be modified using the semodule(8) and semanage(8) commands.

SELinux userspace library

versions < 2.4   /etc/selinux//modules

versions >= 2.4  /var/lib/selinux/

active/modules Directory Contents

tmp Policy Store (build failure)

active/commit_num

active/policy.kern

active/policy.linked

active/seusers.linked

active/seusers_extra.linked

active/booleans.local

disable_dontaudit

active/file_contexts

Building the File Labeling Support Files

active/file_contexts.local

active/homedir_template

active/file_contexts.homedirs

active/seusers

active/seusers.local

active/users_extra

active/users_extra.local

active/users.local

active/interfaces.local

active/nodes.local

active/ports.local

Set domain permissive mode

3、Policy Configuration Files

Each file discussed in this section is relative to the policy name as follows:

/etc/selinux/

setrans.conf

secolor.conf

policy/policy.

contexts/customizable_types

contexts/default_contexts

contexts/dbus_contexts

contexts/default_type

contexts/failsafe_context

contexts/initrc_context

contexts/lxc_contexts

contexts/netfilter_contexts - Obsolete

contexts/openrc_contexts

contexts/openssh_contexts

contexts/removable_context

contexts/sepgsql_contexts

contexts/snapperd_contexts

contexts/securetty_types

contexts/systemd_contexts

contexts/userhelper_context

contexts/virtual_domain_context

contexts/virtual_image_context

contexts/x_contexts

contexts/files/file_contexts

contexts/files/file_contexts.local

contexts/files/file_contexts.homedirs

contexts/files/file_contexts.subs

contexts/files/file_contexts.subs_dist

contexts/files/media

contexts/users/[seuser_id]

logins/

users/local.users

七、参考

SELinux 用户和管理员指南 Red Hat Enterprise Linux 7 | Red Hat Customer Portal

使用 SELinux Red Hat Enterprise Linux 9 | Red Hat Customer Portal

https://github.com/SELinuxProject/selinux-notebook

Your visual how-to guide for SELinux policy enforcement | Opensource.com



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3