1.课程目标

 

  • 了解什么是SELinux

  • 掌握如何改变SELinux的模式;

  • 掌握怎样修改文件安全上下文(Context)

  • 查看和维护SELinuxBool值;

  • 故障排解SELinux引起的问题

 

2.SELinux简介

 

2.1.是什么

SELinux(Security Enhanced Linux),是由美国国家安全局(NationalSecurity AgencyNSA)领导开发的GPL项目。SELinux拥有一个灵活而强制性的     访问控制结构,旨在提高Linux系统的安全性,提供强建的安全保证,可防御未知***。据称相当于A1级别的军事安全性能。比MS NT所谓的C2等高得多。

 

按照NSASELinux小组的定义:SELinux是一组给Linux内核的修补程序,并提供一些更强、更安全的强制存取控制架构来与内核的主要子系统共同运作。基于机密及完整性原则,它提供了一个架构来强制信息的分离,以对付***的威胁或任何企图略过安全架构的应用程序。借此限制恶意或设计不良的程序可能造成的破坏。

 

2.2.作用

应用SELinux后,可以减轻恶意***或恶意软件带来的灾难,并提供对机密性和完整性有很高要求的信息很高的安全保障。SELinux VS Linux,普通Linux安全和传统Unix系统一样,基于自主存取控制方法---DAC,只要符合规定的权限(如规定的所有者和文件属性等),就可以存取资源。在传统的安全机制下,一些通过Setuid/setgid的程序就产生了很严重的安全隐患,甚至一些错误的配置就可引发巨大的漏洞,被轻易***。

 

SELinux则基于强制存取控制方法---MAC,透过强制性的安全策略,应用程序或用户必须同时符合DAC及对应SELinuxMAC才能进行正常操作,否则都将遭到拒绝或失败,而这些问题将不会影响其它正常运作的程序和应用,并保持它们的安全系统结构。

 

简单的说,SELinux的使用,有增加了系统的一层安全特性。使Linux系统更加安全,更加牢固。

 

2.3.与传统权限的区别

传统的Linux权限控制中主要存在一些缺陷,比如由用户身份决定权限而不是根据进程决定权限,无法针对进程本身提供颗粒化(更加细)的权限控制,root用户权限没有任何限制等。而SELinux可以为操作系统提供安全防护。SELinux实现了以策略为基础的命令访问       控制(后面实验中会有所体现,服务针对用户有所限制,要使用策略打开用户权限),进而达成更颗粒化的安全评估,对任意访问控制机制也有更大的弹性。

 

SELinux可以为每一个服务指定权限及其策略(Mandatory Access ControlMAC最大的特点由安全机制管理者来决定权限),而并非依赖于传统的usergroupotherroot账号的简单权限控制方式(Discretinoary Access ControlDAC最大的特点由资源的拥有者决定权限)。当进行特定操作时,Linux权限首先被检查,如果是允许操作,SELinux将会检查并且允许或拒绝使用者的使用。但是如果Linux不允许操作,就由Linux权限禁止,与SELinux检查无关(当有SELinu时,权限操作会首先由DAC检查,再由SELinux检查,必须两者都满足才能执行操作。若DAC检查不满足,则直接拒绝,不用再由SELinux检查MAC检查的前提是通过DAC的检查)

 

RHELSELinuxFTPSambaApache等一系列服务整合,使得其优点能够更轻易扩展。在要求极度安全环境,可以用于更多的应用程序上实现更广泛的SELinux功能,甚至为每一个服务指定严格的SELinux策略。比如传统的Linux系统若遭***侵入Web服务器,可能导致整个服务器瘫痪。但是有了SELinux的保护,虽然Web服务器被***,却可将受害范围减至最小。

 

2.4.SELinux访问控制

 

SELinux分为两种访问控制

  • a.MAC(Mandatory  Access  Control)。强制的访问控制,加了selinux后,就是强制的访问控制,此时就算给文件权限设置为777,如果加了selinux后,照样修改不了内容,即使是root用户,也一样修改不了。

  • b.DAC(Discretionary Access  Control):宽松的访问控制,比如:没有部署selinux,采用的就是宽松的访问控制,访问控制都是基于用户,组,拥有人,拥有组,其他人以及我们所用的权限像acl等等之类,通过这样来控制我们的系统安全

 

2.5.术语

 

  • DAC(Discretinoary Access Control),自主存取控制。DAC一句程序运行时的身份决定权限,是大部分操作系统的权限存取控制方式。也就是根据文件owngroupotherrwx权限进行限制。Root有最高权限无法限制;rwx权限换分不够颗粒化,无法针对不同的进程实现限制(即策略化限制)

  • MAC(Mandatory Access Control),轻质存取控制。MAC一句条件决定是否有存取权限。可以规范个别细致的项目进行存取控制,提供完整的彻底化规范限制。可以对文件、目录、网络、套接字等进行规范,所有动作必须先得到DAC授权,然后得到MAC授权才可以存取。

 

注:SELinux一旦打开,默认就是拒绝所有

 

3.SELinux运行装态配置

 

3.1.SELinux状态分类

 

SELinx运行状态有两种

  • enabled(开启)

  • disabled(关闭)

 

enabled有分两种模式

  • enforcing(强制模式,启用阻挡)

  • permissive(警告模式,不阻挡只显示提示信息)

 

3.2.SELinux状态设置与查看

 

3.2.1.查看SELinux当前状态

命令:getenforce

[root@server30 ~]# getenforce

Enforcing

 

3.2.2.SELinux状态修改

 

方法一:命令修改

命令:setenforce 0/1

参数:0permissive模式,一般不做默认模式,用来做排错用;

     1enforcing模式,默认模式;

例:把当前SELinux状态修改为警告模式

[root@ desktop30 ~]# setenforce 0

[root@ desktop30 ~]# getenforce 

Permissive

[root@ desktop30 ~]#

注:

  • 此方法为当前生效(临时状态),不会修改配置文件内容。下次重启还原为默认状态;

  • 此方法只能修改为enforcingpermissive,不能开启(enabled)或关闭(disabled)SELinux

 

方法二:修改配置文件

配置文件路径/etc/sysconfig/selinux

例:查看配置文件

[root@ desktop30 selinux]# vim  /etc/sysconfig/selinux

 

# 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,

#      minimum - Modification of targeted policy. Only selected processes are  pro

tected.

#      mls - Multi Level Security protection.

SELINUXTYPE=targeted

 

  • SELinux状态可以在此配置文件中修改,在“SELINUX=”后面写上对应的状态即可,并且为永久生效,需要reboot重启系统(缺省为enforcing)

  • SELinux类型,RHEL7之前系统中只有2中,但是在RHEL7系统中新增了一种”minimum“。默认为target(红帽开发的,仅保护我们的网络服务,对系统不会太严格,RHCE级别的),另外两种不做多的介绍,如果要使用,需要单独安装包。

 

状态、模式之间的切换

强制模式与警告模式互相转换,一般不需要重启机器(一般为测试用);但要是强制模式与关闭模式之间转换(enableddisabled之间转换),转换后要重启机器才能生效。因为当改为强制模式时,所有文件都有一个context值,当改为关闭模式时,所有的context值都要去掉。用ls  -Z可以查看。

 

4.bool

 

 

4.1.bool简介

SELinux策略中定义了许多boolean值,用于配置让SELinux开启保护某些程序或关闭对某些程序的某个项目的保护。这些boolean值的信息保存在/selinux/booleans目录中。

 

4.2.bool相关命令

  • getsebool:查看bool

例:查看ftp有关用户家目录的bool值开关状态

[root@ desktop30 ~]# getsebool -a |grep  ftp |grep home

ftp_home_dir --> off

//关闭状态

sftpd_enable_homedirs --> off

sftpd_write_ssh_home --> off

tftp_home_dir --> off

 

  • setsebool:设置bool

例:打开ftp用户家目录的bool

[root@ desktop30 ~]# setsebool -P  ftp_home_dir 1

注:

  • 后面跟的数字可以跟0&1

  • 0为关闭bool值,1为打开。

 

4.3.bool的使用案例

 

本实验我们以FTP服务为例。

首先查看此时系统的SELinux状态为什么模式

[root@ desktop30 ~]# getenforce

Enforcing

默认的强制状态。此状态也是我们在实际生产环境以及RHEL考试中必须设置为默认的状态。

 

安装ftp服务

[root@ desktop30 ~]# yum -y install  vsftpd

Loaded plugins: langpacks, product-id,  subscription-manager

This system is not registered to Red Hat  Subscription Management. You can use subscription-manager to register.

Resolving Dependencies

--> Running transaction check

---> Package vsftpd.x86_64 0:3.0.2-9.el7  will be installed

--> Finished Dependency Resolution

 

Dependencies Resolved

……

[root@ desktop30 ~]# systemctl restart  vsftpd

[root@ desktop30 ~]# systemctl enable  vsftpd

ln -s  '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'

 

安装lftp工具验证

[root@ desktop30  ~]# yum -y install lftp

Loaded plugins: langpacks, product-id,  subscription-manager

This system is not registered to Red Hat  Subscription Management. You can use subscription-manager to register.

Resolving Dependencies

--> Running transaction check

---> Package lftp.x86_64 0:4.4.8-3.el7  will be installed

--> Finished Dependency Resolution

 

Dependencies Resolved

……

匿名用户验证:

[root@desktop30 ~]# lftp 172.16.30.30

lftp 172.16.30.30:~> ls

drwxr-xr-x    2 0         0               6 Mar 07  2014 pub

lftp 172.16.30.30:/> cd /pub

lftp 172.16.30.30:/pub> ls

-rw-r--r--    1 0         0               0 May 08 06:55  redhat

lftp 172.16.30.30:/pub> get redhat

lftp 172.16.30.30:/pub> exit

[root@desktop30 ~]# ls

……   redhat  ……

lftp 172.16.30.30:/pub> put  /etc/passwd

put: Access failed: 550 Permission  denied. (passwd)

如上,默认情况下,匿名可以访问ftp,并下载文件,但是不能上传。

 

查看vsftp的配置文件

[root@desktop30 ~]# vim  /etc/vsftpd/vsftpd.conf

# Example config file  /etc/vsftpd/vsftpd.conf

#

# The default compiled in settings are  fairly paranoid. This sample file

# loosens things up a bit, to make the ftp  daemon more usable.

# Please see vsftpd.conf.5 for all  compiled in defaults.

#

# READ THIS: This example file is NOT an  exhaustive list of vsftpd options.

# Please read the vsftpd.conf.5 manual  page to get a full idea of vsftpd's

# capabilities.

#

# Allow anonymous FTP? (Beware - allowed  by default if you comment this out).

anonymous_enable=YES

//允许匿名登录

#

# Uncomment this to allow local users to  log in.

# When SELinux is enforcing check for SE  bool ftp_home_dir

local_enable=YES

//允许本地登录

#

# Uncomment this to enable any form of  FTP write command.

write_enable=YES

//允许写,前提为本地用户

#

# Default umask for local users is 077.  You may wish to change this to 022,

# if your users expect that (022 is used  by most other ftpd's)

local_umask=022

#

# Uncomment this to allow the anonymous  FTP user to upload files. This only

# has an effect if the above global write  enable is activated. Also, you will

# obviously need to create a directory  writable by the FTP user.

#anon_upload_enable=YES

#

# Uncomment this if you want the  anonymous FTP user to be able to create

# new directories.

#anon_mkdir_write_enable=YES

//匿名创建文件被注释,所以匿名用户不能创建文件

根据上面的配置文件可知,匿名是不能上传文件的(anon_mkdir_write_enable)。要本地用户登录才可以。

 

创建本地用户登录

[root@desktop30 ~]# useradd user1

[root@desktop30 ~]# cd /home/user1/

[root@desktop30 user1]# touch user1

//user1的家目录创建文件做测试用

[root@desktop30 ~]# lftp 172.16.30.30

lftp 172.16.30.30:~> user user1

Password:

lftp user1@172.16.30.30:~> ls

-rw-r--r--    1 0         0               0 May 08 09:05  user1

//登录成功后,默认是在自己的家目录

 

验证

登录user1之后,上传文件。为了更详细的看出效果,请登录系统图形化桌面上传

lftp user1@172.16.30.30:~> put  /etc/passwd

put: Access failed: 553 Could not create  file. (passwd)

注意,此时上传失败,但是如果在图形化桌面操作的,此时左面下方会弹出SELinux提示,点击【show】弹出下面的对话框:

                            spacer.gif

如果在上传失败时,屏幕下方没有弹出提示,请自行打开SELinuxTroubleshooter查看。点击屏幕上方的【Application--Sundry--SELinux Troubleshooter】,同样能弹出上面的对话框。

注:如果系统没有【SELinux Troubleshooter】功能,请安装此功能,使用命令[yum –y install setr*]

弹出了此框,也就证明了是SELinux的原因,不是服务本身的原因(服务的配置文件中,本地用户登录时可以上传的)。所以,此时我们可以修改SELinux策略来实现上传功能。

点击图上的【Troubleshoot

spacer.gif

此图为针对特定的SELinux提供的bool开关:

  • if you wantto ftp to home dir:如果要访问家目录,请使用右边的解决策略,开启bool开关,使用命令:setsebool –P  ftp _home_dir 1;

  • if you wantto allow ftpd to gull access:如果实现所有访问,同理,使用右面的命令开启bool开关:setsebool -P ftpd_full_access 1

  • if youbelive….:如果你相信vsftpd缺省应该被允许创建访问文件,请执行:

#grep vsftpd /var/log/audit/audit.log | audit2allow -Mmypol

# semodule -i mypol.pp

我们做的是向用户家目录上传文件,所以,应该执行第一项内容,如下:

[root@desktop30 user1]# setsebool -P  ftp_home_dir 1

[root@desktop30 ~]# getsebool -a |grep  ftp |grep home

ftp_home_dir  --> on

sftpd_enable_homedirs --> off

sftpd_write_ssh_home --> off

tftp_home_dir --> off

 

重启服务再次验证

[root@desktop30 user1]# systemctl restart  vsftpd.service

[root@desktop30 user1]#

[root@desktop30 ~]# lftp 172.16.30.30

lftp 172.16.30.30:~> user user1

Password:

lftp user1@172.16.30.30:~> ls

-rw-r--r--    1 0         0               0 May 08 09:05  user1

lftp user1@172.16.30.30:~> put  /etc/passwd

2318 bytes transferred                            

lftp user1@172.16.30.30:~> ls

-rw-r--r--    1 2012      2012         2318 May 08 10:06  passwd

-rw-r--r--    1 0         0               0 May 08 09:05  user1

//上传成功。

 

4.4.判断SELinux的影响

 

上个实验是在图形化桌面操作的,会弹出troubleshoot的提示。而一般的生产环境中,是不允许安装图形化桌面的,只能是CLI界面。那么,我们要如何判断是不是SELinux的影响呢?这个能力,实际生产环境中还是需要我们有的。我们可以通过修改SELinux的模式来判断是否是SELinux的影响。如下:

 

关闭bool开关

[root@desktop30 ~]# setsebool -P  ftp_home_dir 0

[root@desktop30 ~]# getsebool -a |grep  ftp |grep home

ftp_home_dir  --> off

sftpd_enable_homedirs --> off

sftpd_write_ssh_home --> off

tftp_home_dir --> off

 

调整SELinux模式,测试用的话,可以用setenforce命令切换enforcingpermissive状态

[root@desktop30 ~]# getenforce

Enforcing

[root@desktop30 ~]# setenforce 0

[root@desktop30 ~]# getenforce 

Permissive

[root@desktop30 ~]#

 

再次登录ftp测试

[root@desktop30 ~]# lftp 172.16.30.30

lftp 172.16.30.30:~> user user1

Password:

lftp user1@172.16.30.30:~> ls

-rw-r--r--    1 2012      2012         2318 May 08 10:06  passwd

-rw-r--r--    1 0         0               0 May 08 09:05  user1

lftp user1@172.16.30.30:~> put  /etc/shadow

1846 bytes transferred

lftp user1@172.16.30.30:~> ls

-rw-r--r--    1 2012      2012         2318 May 08 10:06  passwd

-rw-r--r--    1 2012      2012         1846 May 08 10:17  shadow

-rw-r--r--    1 0         0               0 May 08 09:05  user1

//上传成功,证明确实是SELinux的影响

 

 

SELinux模式改回enforcing

[root@desktop30 ~]# setenforce 1

[root@desktop30 ~]# getenforce

Enforcing

 

5.Context

 

5.1.简介

 

所有系统访问控制都是以关联的客体和主体的某种类型的访问控制属性为基础的。在SELinux中,访问控制属性叫安全上下文(context)。所有客体(文件、进程间通信通道、套接字、网络主机等)和主体(进程)都有与其关联的安全上下文(简单说:Linux中,所有的文件和进程都有一个安全的上下文关系)。一个安全上下文由用户、角色和类型标示符3部分组成。SELinux对一些命令做了修改以便于查看安全上下文(如:ls –Z/ps –Z/id –Z )。

SELinux的主要访问控制特性是类型强制,安全上下文中的类型标示符决定了访问权。类型标示符是安全上下文的主要组成部分,一个进程的类型通常被称为一个域(domain)域类型都表示相同的含义。

安全上下文中的用户和角色标示符除了对强制有一点约束之外,对类型强制访问控制策略没有任何影响。对于进程,用户和角色标识符显得更有意义,因为它们是用于控制类型和用户标识符的联合体,这样就会与linux用户账号关联起来。然而对于客体,用户和角色标识符很少使用,为了规范管理,客体的角色常常是object_r.客体的用户常常是创建客体的进程的用户标识符,它们在访问控制上没有什么用。

 

5.2.认识安全上下文(context)

 

Context以以下形式出现

用户:角色:类型()

例:

[root@desktop30 ~]# pwd

/root

[root@desktop30 ~]# ls -Zd

dr-xr-x---. root root system_u:object_r:admin_home_t:s0 .

 

  • system_u

用户:指的是SELinux用户。root表示root账户身份,user_u表示普通无特权用户,system_u表示系统进程。通过用户可以确认身份类型,一般搭配角色使用。身份和不同的角色搭配时有权限不同。账户之间切换时此用户身份不变。在targeted策略环境下用户标识没有实质作用。

 

  • object_r

角色:一般为文件目录的角色,system_r一般为进程的角色,在targeted策略环境中用户的角色一般为system_r,在strict策略中,用户的角色被细分为system_rsysadm_ruser_rstaff_r角色。用户的角色类似用户组的概念。不同的角色具有不同的身份权限。一个用户可以具备多个角色,但是同一时间只能使用一个角色。在targeted策略环境下,角色没有实质作用。在targeted策略环境中,所有的进程文件的角色都是system_r,在strict策略环境中,一般用户登录时角色是user_r,root登录时角色是staff_r,但是没有特权切换到sysadm_r角色才具备超级用户的权限,只有staff_r角色可以却换成sysadm_r角色。

 

  • admin_home_t

类型(进程的类型又可以成为域):文件和进程都有一个类型。SELinux依据类型的相关组合来限制存取权限。

 

5.3.文件的context

 

查看/root下文件的context/tmpcontext

[root@desktop30 tmp]# ls -Z /root

-rw-------. root root  system_u:object_r:admin_home_t:s0 anaconda-ks.cfg

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Desktop

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Documents

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Downloads

drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0  findfile

-rw-r--r--. root root  system_u:object_r:admin_home_t:s0 initial-setup-ks.cfg

-rw-r--r--.  root root unconfined_u:object_r:admin_home_t:s0 lines.txt

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Music

lrwxrwxrwx. root root  unconfined_u:object_r:admin_home_t:s0 ntpkey_host_desktop30.example.com ->  ntpkey_RSAhost_desktop30.example.com.3639994206

-rw-r--r--. root root  unconfined_u:object_r:admin_home_t:s0  ntpkey_RSAhost_desktop30.example.com.3639994206

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Pictures

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Public

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

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Templates

drwxr-xr-x. root root  unconfined_u:object_r:admin_home_t:s0 Videos

 

[root@desktop30 tmp]# ls -Zd

drwxrwxrwt. root root system_u:object_r:tmp_t:s0       .

 

/root/lines.txt为例,移动到/tmp下,再次查看context

[root@desktop30 tmp]# cp /root/lines.txt  .

[root@desktop30 tmp]# ls -Z

-rw-r--r--. root root  unconfined_u:object_r:user_tmp_t:s0  lines.txt

如上,移动之后,文件的context值与/tmp的一样,继承了/tmpcontext值。

再把此文件复制到/mnt下,再次查看context

[root@desktop30 tmp]# ls -Zd /mnt/

drwxr-xr-x. root root system_u:object_r:mnt_t:s0       /mnt/

[root@desktop30 tmp]# cp lines.txt /mnt/

[root@desktop30 tmp]# ls -Z /mnt/      

-rw-r--r--. root root  unconfined_u:object_r:mnt_t:s0   lines.txt

再次移动之后,此文件又继承了/mntcontext值。

由此我们可知,SELinux为开启状态(enabled)时,文件的context值会继承其所在目录的context值,但文件的权限不会变化,因为文件的权限是受umask值决定的。而文件的context值是受文件上一级目录决定的。

 

5.4.context值与包的关系

 

ftp服务为例,查看/vat/ftp/pub下的文件的context

[root@desktop30 ~]# ls -Z /var/ftp/pub/

-rw-r--r--. root root  unconfined_u:object_r:public_content_t:s0  redhat

 

依次向文件的上级目录查看

[root@desktop30 ~]# ls -Z /var/ftp/pub/

-rw-r--r--. root root  unconfined_u:object_r:public_content_t:s0  redhat

[root@desktop30 ~]# ls -Zd /var/ftp/pub/

drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/pub/

[root@desktop30 ~]# ls -Zd /var/ftp

drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp

[root@desktop30 ~]# ls -Zd /var

drwxr-xr-x. root root system_u:object_r:var_t:s0       /var

如上,到达/var时,context不再一样了。

 

查看/var/ftp&/var/ftp/pub属于那个包

[root@desktop30 ~]# rpm -qf /var/ftp/

vsftpd-3.0.2-9.el7.x86_64

[root@desktop30 ~]# rpm -qf /var/ftp/pub/

vsftpd-3.0.2-9.el7.x86_64

属于同一个包。

 

查看/var属于那个包

[root@desktop30 ~]# rpm -qf /var/

filesystem-3.2-18.el7.x86_64

如上,因为/var/ftp&/var/ftp/pub属于同一个包生成的文件,所以两者的context值一样。而/var与它们不是同一个包安装的,所以context不一样。

由上可知:

  目录的context值和安装包有关,是设计编包的人已把context定义了,当装了此包,会生成对应的context值。

 

5.5.文件context与服务的关系

 

安装http服务并启动

[root@desktop30 ~]# yum -y install httpd

Loaded plugins: langpacks, product-id,  subscription-manager

This system is not registered to Red Hat  Subscription Management. You can use subscription-manager to register.

Package httpd-2.4.6-17.el7.x86_64 already  installed and latest version

Nothing to do

//已经安装,直接启动

[root@desktop30 ~]# systemctl restart  httpd.service

[root@desktop30 ~]# systemctl enable  httpd.service 

 

创建索引文件并访问

[root@desktop30 ~]# cd /var/www/html/

 [root@desktop30 html]# vim index.html

[root@desktop30 html]# echo Hello! how  are you >index.html

[root@desktop30 html]# cat index.html

Hello!  how are you

wKiom1VUE6ixbGmPAABO5GbSyCc081.jpg

访问成功。

 

修改索引文件的context值,再次访问

[root@desktop30 html]# ls -Z

-rw-r--r--. root root  unconfined_u:object_r:httpd_sys_content_t:s0 index.html

[root@desktop30 html]# chcon -t tmp_t  index.html

[root@desktop30 html]# ls -Z

-rw-r--r--. root root  unconfined_u:object_r:tmp_t:s0    index.html

wKiom1VUE8HTVonpAACB5MKPujc102.jpg

访问失败。

 

注:

之所以能访问到我们要访问的文件,其实是有进程访问到的,进程访问文件的context值,若进程和文件的context值一致能访问,否则就访问不到要访问的文件。所以上面的例子修改了文件的context值就访问不到文件内容了。

 

5.6.恢复文件context

 

5.6.1.restorecon

 

上个实验中,网页的索引文件被我们手动修改了context值,所以不能访问了。手动修改之后的context值是不正常的。举个例子来说,就好比是一个外国人偷渡到中国,没有护照等证件,是不可见光的,否则会被抓。

那么,如何把经过修改的文件重新恢复到原来的context值呢?此处就要介绍一个命令restorecon,看下面的实例。

例:把http的索引文件的context值还原

[root@desktop30 html]# ls -Z

-rw-r--r--. root root  unconfined_u:object_r:tmp_t:s0    index.html

[root@desktop30 html]# restorecon -r  index.html

[root@desktop30 html]# ls -Z

-rw-r--r--. root root  unconfined_u:object_r:httpd_sys_content_t:s0  index.html

 

再次访问网页查看

wKiom1VUE-PC0JbQAAA6zySNgGY748.jpg

如上,context还原之后访问正常。

 

5.6.2.重打标签

 

现在,我们把整个的/var/www都删除,然后手动创建这一些列的文件/var/www/html/index.html.

[root@desktop30 ~]# rm -rf /var/www

[root@desktop30 ~]# mkdir -p  /var/www/html

[root@desktop30 ~]# vim  /var/www/html/index.html

Hello!how area you!

 

查看context

[root@desktop30 ~]# ls -Zd /var/www/

drwxr-xr-x. root root unconfined_u:object_r:var_t:s0   /var/www/

[root@desktop30 ~]# ls -Zd /var/www/html/

drwxr-xr-x. root root  unconfined_u:object_r:var_t:s0    /var/www/html/

[root@desktop30 ~]# ls -Zd  /var/www/html/index.html

-rw-r--r--. root root  unconfined_u:object_r:var_t:s0   /var/www/html/index.html

Context已经都不再是原来的了,都继承了/varcontext

现在访问网页查看

wKiom1VUFAOAdB8UAAG4Yw4P3Mw133.jpg

很明显,现在已经不能查看了。唯一的办法就是重新把context值改回httpd服务所使用的。

如果要使用restorecon命令,就要知道httpd进程的context是什么,然后用命令,如下:

[root@desktop30 ~]# restorecon -v -R  /var/www/html/index.html

 

如果不知道的话,可以使用重打标签技术,然后重启,让selinux重打所有文件的标签

[root@desktop30 ~]# touch /.autorelabel

[root@desktop30 ~]# reboot

[root@desktop30 ~]# ls -Zd /var/www/

drwxr-xr-x. root root  unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/

[root@desktop30 ~]# ls -Zd /var/www/html/

drwxr-xr-x. root root  unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/

[root@desktop30 ~]# ls -Zd  /var/www/html/index.html

-rw-r--r--. root root  unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html

如上,重打标签之后,手动改过的文件的context恢复到原来的了。

 

5.7.semanage

 

此命令是把文件的context值写进内核,写进内核之后,即使重打标签也不会更改。

例:现在我们要做http,但是把网页的根目录设置在/mydata/html

 

首先创建这样的文件,并查看context

[root@desktop30 ~]# mkdir -p /mydata/html

[root@desktop30 ~]# ls -Zd /mydata

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /mydata

[root@desktop30 ~]# ls -Zd /mydata/html/

drwxr-xr-x. root root  unconfined_u:object_r:default_t:s0  /mydata/html/

接下来要做的就是把这两个文件的context值改为httphttpd_sys_content_t,并写进内核,即使下次重打标签也不能更改。

[root@desktop30 ~]# semanage fcontext -a  -t httpd_sys_content_t '/mydata(/.*)?'

[root@desktop30 ~]# restorecon -R -v  /mydata/html/

restorecon reset /mydata/html context  unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

 

重打标签,恢复标签

[root@desktop30 ~]# ls -Zd /mydata    

drwxr-xr-x. root root  unconfined_u:object_r:httpd_sys_content_t:s0 /mydata

[root@desktop30 ~]# ls -Zd /mydata/html/

drwxr-xr-x. root root  unconfined_u:object_r:httpd_sys_content_t:s0 /mydata/html/

如上,context已经设置为http进程一致的,接下来就可以设置为http的根文档了。