操作系统安全—SELinux(嵌入linux内核中)

学习操作系统安全时候遇到的一个扩展tips,当做笔记来用,转载自简书

原文链接:https://www.jianshu.com/p/eb3b3b41b619

如有侵权请联系删除

linux的访问控制

  • Discretionary Access Control(DAC)

自主(自由)访问控制:这种访问控制下,一个进程如果被劫持,那这个进程就有了运行这个进程用户的所有访问权限。如:
一个httpd进程

[root@localhost ~]# ps aux |grep httpd
apache     3937  0.0  0.2 224060  2944 ?        S    18:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache     3938  0.0  0.2 224060  2944 ?        S    18:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache     3939  0.0  0.2 224060  2944 ?        S    18:37   0:00 /usr/sbin/httpd -DFOREGROUND
....

httpd进程的属于apche用户的,我们来看一下,/etc/passwd文件

[root@localhost ~]# ll /etc/passwd
-rw-r--r--. 1 root root 1204 Oct 26 18:37 /etc/passwd

这里可以看出/etc/passwd文件是root用户,root组的,但other用户是对这个文件是有r权限的,那如果有人劫持了httpd进程,那他就可以访问到/etc/passwd文件,这种不就知道了系统上有什么用户了,就可以有针对性的对用户进程密码破解。系统中other用户对很多的文件都有r权限,这是非常不安全的。

在自主访问控制下,没有办法将一个进程的访问限制在一个允许的范围内。如:

httpd进程就只能访问我们提供给用户可以访问的资源,而我们不想用户看到的资源,就不能访问。那这样如httpd进程被劫持了,那也就只能访问有限的资源,不可能看到其它的资源。

这能不能实现呢?

  • Mandatory Access Control(MAC)

在MAC(强制访问控制)这种模型里,管理员管理访问控制。管理员制定策略,用户不能改变它。策略定义了哪个主体能访问哪个对象。这种访问控制模型可以增加安全级别,因为它基于策略,任何没有被显式授权的操作都不能执行。MAC被开发和实现在最重视保密的系统中,如军事系统。主体获得清楚的标记,对象得到分类标记,或称安全级别。

在“强制访问控制”模型中,每一个进程能做什么都有明确的规则,而且遵循没有明确说明可以访问的,就不能访问,这样能在任何一个进程被劫持也只能访问我们定义规则中明确说明它能访问的资源,所能破坏的也在定义的范围内,(这就是最小访问原则

  • SELinux

selinux就是一种mac的访问控制,任何一个进程的访问都要经过selinux中定义的规则检查,通过才能进行访问

规则定义在什么地方呢?

  • policy(政策)

这就是规则库,一堆的规则(rule)集合在一起就成了政策(policy),那按照MAC的定义是不是要将系统上所有的程序能做什么都写成规则才能使用selinux呢?理论上是的,只有这样才能做到最小化访问原则,但每一个系统上运行的程序千千万万,光是写这个规则我想都没有人再去用selinux了。(而且规则不是普通用户能写的)
那怎么使用呢?还好在Centos 7 中为我们提供了有policy

[root@localhost ~]# ll /etc/sysconfig/selinux 
lrwxrwxrwx. 1 root root 17 Oct 10 19:24 /etc/sysconfig/selinux -> ../selinux/config

[root@localhost ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

/etc/selinux/conf中系统为我们提供了三种policy

  • targeted
    默认使用的policy,只能有限的程序受selinux的保护。
  • minimum
    这是targeted的修改版,仅选定的进程受保护。(用的不多)
  • mls
    多级安全保护。(用的不多)

像上面所有的如果要所有的程序都受selinux的控制,这将是一个非常庞大的工程,对用户来说基本没有用户体验,(我们不能做任何事,除非在policy中添加我们能做什么的rule,那光写rule就天荒地老了,还用什么?)所以只控制有限的程序是一个不错的选择


  • 安全上下文(Security Context)

在启动selinux后,系统中所有的资源都会被打标(label),这就是安全上下文(context)。selinux就是通过这些标记来辅助完成访问控制的。

规则中不可能写 httpd进程能访问/var/www/html/index.html文件,这样的具体的条目,这样改一下 httpd的文件名那所有的规则就都要重写,明显不能这么做,那又要怎么做呢?

先看一下安全上下文长什么样:

[root@localhost ~]# ls -Z /sbin/httpd 
-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /sbin/httpd
[root@localhost ~]# ps -eZ |grep httpd 
system_u:system_r:httpd_t:s0      3936 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3937 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3938 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3939 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3940 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0      3941 ?        00:00:00 httpd
[root@localhost ~]# ls -Z /var/www/html/
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

看到加上-Z选项后多出来的显示了吗?这就是context
system_u:system_r:httpd_t:s0
unconfined_u:object_r:httpd_sys_content_t:s0

这里有四段:
user:role:type:range

字段 说明
user SELinux用户身份。这可以与允许SELinux用户使用的一个或多个角色相关联。

使用SELinux策略将每个Linux用户映射到一个SELinux用户。这使Linux用户可以继承对SELinux用户的限制。映射的SELinux用户身份在SELinux上下文中用于该会话中的进程,以便定义他们可以输入哪些角色和级别。
role SELinux角色。这可以与SELinux用户被允许访问的一种或多种类型相关联。

SELinux的一部分是基于角色的访问控制(RBAC)安全模型。该角色是RBAC的属性。 SELinux用户被授权使用角色,角色被授权用于域。该角色充当域和SELinux用户之间的中介。可以输入的角色决定了可以输入哪些域。最终,它控制可以访问的对象类型。这有助于减少特权升级攻击的漏洞。
type 当类型与进程相关联时,它定义SELinux用户(subject)可以访问哪些进程(或domain)。
当类型与object相关联时,它定义SELinux用户对该object具有什么访问权限。

类型是类型强制的属性。该类型定义进程的域,并定义文件的类型。 SELinux策略规则定义类型如何相互访问,无论是访问类型的域还是访问另一个域的域。仅当存在允许访问的特定SELinux策略规则时,才允许访问。
range 此字段也可以称为级别,并且仅在策略支持MCS或MLS时才显示。该条目可以包括:
•单个安全级别,其中包含敏感级别和零个或多个类别
(例如s0,s1:c0,s7:c10.c15)
•由两个安全级别(低和高)组成的范围,两个安全级别之间用连字符分隔
(例如s0-s15:c0.c1023)。

上面的做个了解就好,在Centos的targeted中可以不去关心user、role、range,只关心type就好了

类型是类型强制的属性。该类型定义进程的域,并定义文件的类型。 SELinux策略规则定义类型如何相互访问,无论是访问类型的域还是访问另一个域的域。仅当存在允许访问的特定SELinux策略规则时,才允许访问。

type 这个字段,在进程和文件(linux上一切皆文件)上有不同的称呼
进程上,一般称作domain / 域
文件上,一般称作type / 类型

再来看一下上面的安全上下文
system_u:object_r:httpd_exec_t:s0 /sbin/httpd 文件
system_u:system_r:httpd_t:s0 3936 ? 00:00:00 httpd 进程
unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html 文件

我们知道在Centos中httpd服务的默认访问路径在/var/www/html/中,我们安装、启动一个httpd服务,再在/var/www/html/下创建的html文件,可以直接在浏览器中访问

现在来解释一下这个上下文,用户执行了httpd_exec_t类型的httpd程序,可以产生一个工作在httpd_t的进程,这个进程可以访问httpd_sys_content_t类型的文件/var/www/html/index.html

这些其实都是在policy中有明确的rule定义的

httpd_exec_t 类型(entrypoint)运行进入 httpd_t

[root@localhost ~]# sesearch -A -t httpd_exec_t |grep httpd_t
   allow httpd_t httpd_exec_t : file { ioctl read getattr lock map execute execute_no_trans entrypoint open } ; 
   ...

httpd_t 可以 访问 httpd_sys_content_t 类型的链接文件、目录、文件

[root@localhost ~]# sesearch -A -s httpd_t |grep httpd_sys_content_t
   allow httpd_t httpd_sys_content_t : lnk_file { read getattr } ; 
   allow httpd_t httpd_sys_content_t : dir { ioctl read getattr lock search open } ; 
   allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock map open } ; 
   allow httpd_t httpd_sys_content_t : dir { ioctl read write getattr lock add_name remove_name search open } ; 

这里要说明一个,在开启了selinux,并执行targeted 策略后,系统为我们定义了一堆的rule,来控制常见的网络服务,如:httpd,vsftp,samba等等

rule中没有明确定义允许的,就不能访问

常见举例1:如将/var/www/html/目录下的文件type改掉,那网站将不能访问

[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@localhost html]# chcon -t admin_home_t index.html 
[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html

这是因为运行在httpd_t域的httpd,不能read(读) admin_home_t类型的文件

常见举例2:如将http的DocumentRoot “/var/www/html/”修改位置,那网站将不能访问

[root@localhost html]# mkdir -pv /service/http/
mkdir: created directory ‘/service’
mkdir: created directory ‘/service/http/’
[root@localhost html]# ls -dZ /service/http/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /service/http/

这是因为policy中定义/var/www/html目录和其下的目录、文件的type为httpd_sys_content_t,这样运行在httpd_t域的httpd,就可以访问了,但将DocumentRoot重新指定位置后,在policy中就没有我们指定位置的相关定义,不会将指定的目录的context设置为typehttpd_sys_content_t,这样httpd进程也就不能正常访问了

以上都可以将httpd服务的网站 目录、文件(object)类型修改为httpd_sys_content_t就可以正常访问了


其实还有一种修改方法,但不建议在实际中使用

◈ 一直说targeted 只控制了部分的进程,那就将系统中的进程分为了confined(受控) 和 unconfined(不受控),我们是不是可以将httpd调整为 不受控 呢?

[root@localhost html]# systemctl stop httpd
[root@localhost html]# ls -Z /bin/cat
-rwxr-xr-x. root root system_u:object_r:bin_t:s0       /bin/cat
[root@localhost html]# ls -Z /sbin/httpd 
-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /sbin/httpd
[root@localhost html]# chcon -t bin_t /sbin/httpd
[root@localhost html]# ls -Z /sbin/httpd 
-rwxr-xr-x. root root system_u:object_r:bin_t:s0       /sbin/httpd
[root@localhost html]# systemctl start httpd
[root@localhost html]# ps -eZ |grep httpd
system_u:system_r:unconfined_service_t:s0 4517 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4518 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4519 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4520 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4521 ? 00:00:00 httpd
system_u:system_r:unconfined_service_t:s0 4522 ? 00:00:00 httpd

可以看到现在httpd进程的domain变为了unconfined_service_t,现在再来访问网站就不再受控于selinux了,你再像上面的例子调整网站文件的位置,type都可以正常访问了,(当然DAC还是要遵守的)。


  • BOOLEANS(布尔值)

在targeted中,关于context的规则我们没有能力去修改(能力有限啊,听说写规则是一项专业的技能),但有着一些boolean我们是可以去改变的

布尔值允许在运行时更改SELinux策略的某些部分,而无需任何SELinux策略编写知识。这允许进行更改,例如允许服务访问NFS卷,而无需重新加载或重新编译SELinux策略。

这个值一般是让我们可以改变服务的运行特性
如:

[root@localhost html]# semanage boolean -l
SELinux boolean                State  Default Description

httpd_enable_cgi               (on   ,   on)  Allow httpd to enable cgi
httpd_use_nfs                  (off  ,  off)  Allow httpd to use nfs
httpd_anon_write               (off  ,  off)  Allow httpd to anon write
...
ftpd_anon_write                (off  ,  off)  Allow ftpd to anon write
...
samba_enable_home_dirs         (off  ,  off)  Allow samba to enable home dirs
samba_create_home_dirs         (off  ,  off)  Allow samba to create home dirs
samba_share_nfs                (off  ,  off)  Allow samba to share nfs
...

这里定义了targeted认为不安全的一些服务运行特性,如:httpd的匿名写操作,ftpd的匿名写,samba的用户家目录,这些默认都是off,不允许的。但在实际中有可能我们又要用到这些功能,那可以在boolean中进行on,放行。


现在我们对Centos 7 上的targeted策略有了一定的了解,在使用中用到最多的:

  • type的修改
  • boolean的修改

至于:什么 "多级安全(MLS) " ,"限制用户"等等的诸多功能,我们就不讨论了,自行查看官方文档
SELinux User's and Administrator's Guide


下面来说一说selinux的相关配置和命令:

  • 1. SELinux 的开启,关闭

    • 关闭
      在/etc/selinux/config文件中修改SELINUX=disabled即可
    • 开启
      selinux的开启有两种模式:enforcingpermissive
      • enfocing
        这是强制模式,所有接受selinux控制的进程对object的访问都要在rule中有明确的定义,没有就不能访问,并记录日志
      • permissive
        这是宽容模式,所有接受selinux控制的进程对object的访问都要在rule中有明确的定义,没有也可以访问,但会记录到日志中(常用于排错中)
  • 2. SELinux 的日志

SELinux决定(例如允许或禁止访问)被缓存。此缓存称为访问向量缓存(AVC)。 SELinux拒绝访问时,将记录拒绝消息。这些拒绝也称为“ AVC拒绝”,并根据运行的守护程序记录到其他位置:

服务 Log 位置
auditd on /var/log/audit/audit.log
auditd off; rsyslogd on /var/log/messages
setroubleshootd, rsyslogd, and auditd on /var/log/audit/audit.log
易于阅读的拒绝消息也发送至 /var/log/messages
  • 3.SELinux 的命令

getenfoce
  查看当前的工作模式
setenfoce [0|1]
  改变当前的工作模式
  0: permissive
  1: enfocing

setenfoce可是在系统运行时临时切换开启状态的selinux的运行模式,但不能将selinux从disable(关闭),切换到0或1中来,selinux从关闭开启需要重启系统,而selinux在系统重启过程中将根据policy对所有文件(object)进行打标

[root@localhost html]# getenforce 
Enforcing

getsebool
  查看boolean设定
  -a 查看全部
  boolean 查看指定boolean的值
setsebool [ -P] boolean value
  设置boolean值[0|1] [off|on]
  如果指定了-P选项,则值都将写入磁盘上的策略文件。重新启动后保持不变。

[root@localhost html]# getsebool -a |grep tftp
tftp_anon_write --> off
tftp_home_dir --> off

chcon - 改变object上下文
   -u, --user=USER 在目标安全上下文中设置用户USER
   -r, --role=ROLE 在目标安全上下文中设置角色ROLE
   -t, --type=TYPE 在目标安全上下文中设置类型TYPE
   -R, --recursive 递归操作文件和目录

[root@localhost html]# chcon -t admin_home_t index.html 
[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html

restorecon pathname - 还原pathname本来的context
   -v 显示过程


 

sestatus [-v | -b]
  显示selinux的相当信息
  -v: 显示更多的信息
  -b: 显示boolean值信息

[root@localhost html]# sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

seinfo
  可以显示更多、更详细的信息,如:user, role, type
   -t 显示类型
   -r 显示角色
   -u 显示用户
   -b 显示布尔值

[root@localhost html]# seinfo 

Statistics for policy file: /sys/fs/selinux/policy
Policy Version & Type: v.31 (binary, mls)

   Classes:           129    Permissions:       267
   Sensitivities:       1    Categories:       1024
   Types:            4774    Attributes:        258
   Users:               8    Roles:              14
   Booleans:          315    Cond. Expr.:       361
   Allow:          106707    Neverallow:          0
   Auditallow:        155    Dontaudit:       10045
   Type_trans:      18058    Type_change:        74
   Type_member:        35    Role allow:         39
   Role_trans:        416    Range_trans:      5899
   Constraints:       143    Validatetrans:       0
   Initial SIDs:       27    Fs_use:             32
   Genfscon:          102    Portcon:           613
   Netifcon:            0    Nodecon:             0
   Permissives:         0    Polcap:              5

sesearch
  SELinux策略查询工具
sesearch [OPTIONS] RULE_TYPE [RULE_TYPE ...] [EXPRESSION] [POLICY ...]
  OPTIONS
  -A, --allow
    搜索允许规则。
  EXPRESSIONS
  -s NAME, --source=NAME
    查找以类型/属性名称为源的规则。(subject domain)
  -t NAME, --target=NAME
    查找以类型/属性NAME为目标的规则。(object type)

[root@localhost ~]# sesearch -A -t httpd_exec_t |grep httpd_t
   allow httpd_t httpd_exec_t : file { ioctl read getattr lock map execute execute_no_trans entrypoint open } ; 

semanage

[root@localhost html]# semanage -h
usage: semanage [-h]
                
                {import,export,login,user,port,ibpkey,ibendport,interface,module,node,fcontext,boolean,permissive,dontaudit}
                ...

semanage is used to configure certain elements of SELinux policy with-out
requiring modification to or recompilation from policy source.

positional arguments:
  {import,export,login,user,port,ibpkey,ibendport,interface,module,node,fcontext,boolean,permissive,dontaudit}
    import              导入本地自定义
    export              输出本地自定义
    login               管理linux用户和SELinux受限用户之间的登录映射
    user                管理SELinux受限用户(SELinux用户的角色和级别)
    port                管理网络端口类型定义
    ibpkey              Manage infiniband ibpkey type definitions
    ibendport           Manage infiniband end port type definitions
    interface           管理网络接口类型定义
    module              管理SELinux策略模块
    node                 管理网络节点类型定义
    fcontext             管理文件上下文映射定义
    boolean             管理布尔值以有选择地启用功能
    permissive          管理流程类型强制模式
    dontaudit           禁用/启用策略中的dontaudit规则

[root@localhost html]# semanage user -h  "使用-h 获取帮助"

查看时常用 "-l"来显示相关信息,如:
[root@localhost html]# semanage user -l

                Labeling   MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles

guest_u         user       s0         s0                             guest_r
root            user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
staff_u         user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
sysadm_u        user       s0         s0-s0:c0.c1023                 sysadm_r
system_u        user       s0         s0-s0:c0.c1023                 system_r unconfined_r
unconfined_u    user       s0         s0-s0:c0.c1023                 system_r unconfined_r
user_u          user       s0         s0                             user_r
xguest_u        user       s0         s0                             xguest_r



以下参考

=========================

May <subject> do <action> to <object>
May a web server access files in users' home directories?
<Web服务器>可以<访问><用户主目录中的文件>吗?

图1.1

图1.1 SELinux允许以httpd_t运行的Apache进程访问/var/www/html/目录,并且它拒绝同一进程访问/data/mysql/目录,因为对于httpd_t和mysqld_db_t类型上下文没有允许规则。另一方面,以mysqld_t身份运行的MariaDB进程能够访问/data/mysql/目录,而SELinux也正确拒绝mysqld_t类型的进程来访问标记为httpd_sys_content_t的/var/www/html/目录。

Linux Discretionary Access Control(DAC)
DAC主要的内容包括以下几个概念:主体、客体、权限(rwx)、所有权(ugo)。
 
在这个模型中,主体是用户的身份,客体是资源或者说是文件(切记:一切皆文件)。由客体的属主对自己的客体进行管理,由主体自己决定是否将自己的客体访问权限或部分访问权限授予其他主体,这种控制方式是自主的。也就是说,在自主访问控制下,用户可以按自己的意愿,有选择地与其他用户共享他的文件。
 
我们所定义的DAC系统有两个至关重要的标准:
 

  1. 文件的所有权:系统中的每个文件(一些特殊文件可能没有,如块设备文件等)都有所有者。在DAC系统中,文件的所有者是创建这个文件的计算机的使用者(或事件,或另一个文件)。那么此文件的自主访问控制权限由它的创建者来决定如何设置和分配;
     
  2. 访问权限:文件的所有者拥有访问权限,并且可以将访问权限分配给自己及其他用户。

上述两个标准说明:
 

  1. 文件的所有权的优先级高于访问权限
     
    1)文件的所有者即便没有任何权限,也可以在为自己分配权限之后获得访问文件的能力。
     
    2)非文件的所有者即便已经获得了访问权限,也可能会被所有者随时收回,从而导致无权访问该文件。
     
  2. 权限是文件访问的关键
     
    1)无论是不是文件的所有者,关系到使用者能否访问文件的最直接的因素是其所对应的用户是否获得了可访问该文件的权限
     
    2)使用者被分配的何种权限,就只能以该权限所规定的操作来访问文件,无法越权。

======================

配置文件 /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
#       targeted - Targeted processes are protected,
#       mls - Multi Level Security protection.
SELINUXTYPE=targeted

SELinux关闭

修改配置文件 SELINUX=disabled,并重启主机

SELinux启用后,可以有两种运行模式:强制(enforcing)或许可(permissive)。

状态查看

~]$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      30
  • Enforcing Mode
    当SELinux在强制模式下运行时,它将强制执行SELinux策略,并根据SELinux策略规则拒绝访问。在Red Hat Enterprise Linux中,当系统最初与SELinux一起安装时,默认情况下启用强制模式。

  • Permissive Mode
    当SELinux在许可模式下运行时,不会强制执行SELinux策略。系统保持运行状态,SELinux不会拒绝任何操作,而只会记录AVC消息,然后将其用于故障排除,调试和SELinux策略改进。在这种情况下,每个AVC仅记录一次。

改变selinux的Mode

~]# getenforce
Enforcing
~]# setenforce 0
~]# getenforce
Permissive
~]# setenforce 1
~]# getenforce
Enforcing

在启动时,您可以设置几个内核参数来更改SELinux的运行方式:
enforcing=0
设置此参数会使计算机以许可模式启动,这在解决问题时很有用。如果文件系统太损坏,则使用许可模式可能是检测问题的唯一选择。此外,在许可模式下,系统会继续正确创建标签。在此模式下创建的AVC消息可以与在强制模式下不同。在宽松模式下,仅报告第一个拒绝。但是,在强制模式下,您可能会拒绝读取目录,并且应用程序停止。在许可模式下,您将收到相同的AVC消息,但是应用程序将继续读取目录中的文件,此外,您还将获得每个拒绝的AVC。

selinux=0
此参数导致内核不加载SELinux基础结构的任何部分。初始化脚本会注意到系统使用selinux=0参数启动并touch /.autorelabel文件。这将导致系统在您下次启用SELinux时引导时自动重新标记。

强制系统重新标记,以下命令:

~]# touch /.autorelabel
~]# reboot

在运行SELinux的系统上,所有进程和文件都以表示安全相关信息的方式标记。此信息称为SELinux上下文。对于文件,可以使用ls -Z命令查看:

~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1

在此示例中,SELinux提供了一个用户(unconfined_u),一个角色(object_r),一个类型(user_home_t)和一个级别(s0)。此信息用于制定访问控制决策。在DAC系统上,访问是根据Linux用户和组ID控制的。在DAC规则之后检查SELinux策略规则。如果DAC规则首先拒绝访问,则不使用SELinux策略规则。

默认情况下,新创建的文件和目录继承其父目录的SELinux类型。例如,当在/ etc目录中创建一个标有etc_t类型的新文件时,新文件将继承相同的类型:

~]$ ls -dZ - /etc
drwxr-xr-x. root root system_u:object_r:etc_t:s0       /etc
~]# touch /etc/file1
~]# ls -lZ /etc/file1
-rw-r--r--. root root unconfined_u:object_r:etc_t:s0   /etc/file1
  • chcon
    Chcon命令更改文件的SELinux上下文。但是,使用chcon命令进行的更改,在重新标记或执行restorecon命令后将被重置,不能持续存在。
~]$ touch file1
~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1

将类型更改为samba_share_t。 -t选项仅更改类型。然后查看更改:

~]$ chcon -t samba_share_t file1
~]$ ls -Z file1 
-rw-rw-r--  user1 group1 unconfined_u:object_r:samba_share_t:s0 file1

恢复file1文件的SELinux上下文。使用-v选项查看更改:

~]$ restorecon -v file1
restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0

restorecon命令读取/etc/selinux/targeted/contexts/files/目录中的文件,以查看文件应具有的SELinux上下文。

更改目录及其内容类型

~]# mkdir /web
~]# touch /web/file{1,2,3}
~]# ls -dZ /web
drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
~]# ls -lZ /web
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
~]# chcon -R -t httpd_sys_content_t /web/
~]# ls -dZ /web/
drwxr-xr-x  root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/
~]# ls -lZ /web/
-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
  • semanage fcontext
    通过semanage fcontext进行的更改由以下实用程序使用。当重新标记文件系统并且restorecon实用工具恢复默认的SELinux上下文时,将使用setfiles实用工具。这意味着即使重新标记了文件系统,semanage fcontext所做的更改也将保持不变

要显示新创建的文件和目录的上下文,请以超级用户身份输入以下命令

~]# semanage fcontext -C -l

快速参考

要在文件系统中保留SELinux上下文更改,请重新标记:

  • 输入以下命令,记住要使用文件或目录的完整路径:
    ~]# semanage fcontext -a options file-name|directory-name
  • 使用restorecon实用工具应用上下文更改:
    ~]# restorecon -v file-name|directory-name

修改文件

~]# touch /etc/file1
~]$ ls -Z /etc/file1
-rw-r--r--  root root unconfined_u:object_r:etc_t:s0       /etc/file1
~]# semanage fcontext -a -t samba_share_t /etc/file1
~]# ls -Z /etc/file1
-rw-r--r--  root root unconfined_u:object_r:etc_t:s0       /etc/file1
~]$ semanage fcontext -C -l
/etc/file1    unconfined_u:object_r:samba_share_t:s0
~]# restorecon -v /etc/file1
restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0

更改目录及其内容类型

~]# mkdir /web
~]# touch /web/file{1,2,3}
~]# ls -dZ /web
drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
~]# ls -lZ /web
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
原文地址:https://www.cnblogs.com/halftion/p/13198090.html