linux 下selinux nginx模块安装后能够访问非httpd_sys_content_t type的文件

作者&投稿:丘黄 (若有异议请与网页底部的电邮联系)
如何解决SELinux问题~

  首先需要确认SELinux处于激活状态,可以使用getenforce命令:
  shell> getenforce
Enforcing

  或者使用sestatus命令:
  shell> sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted

  注:关于SELinux的基础知识介绍请参考鸟哥的Linux私房菜中相关的介绍。
  我们还需要确认系统已经安装并启动了Apache,没有的话就YUM装一个,这很简单,就不多说了,接着在root目录创建一个测试文件test.html,如下:
  shell> cat /root/test.html
hello, world.

  然后把这个测试文件拷贝到Apache的DocumentRoot目录,我的Apache是通过YUM安装的话,缺省是/var/www/html目录,如下:
  shell> cp /root/test.html /var/www/html

  接着浏览一下,如果没出什么幺蛾子,应该一切都在意料之中,如下:
  shell> curl http://localhost/test.html
hello, world.

  看到这,你可能觉得我废话连篇,别着急,下面就是见证奇迹的时候了:
  同样还是那个测试文件test.html,不过这次不再是拷贝,而是移动,如下:
  shell> mv /root/test.html /var/www/html

  接着浏览一下,怎么样,结果很出人意料吧,竟然提示权限错误,如下:
  shell> curl http://localhost/test.html


403 Forbidden

Forbidden
You don't have permission to access /test.html
on this server.


  当然,我们现在知道这个问题是由于SELinux引起的,但还不知其所以然,实际上问题的原因此时已经被audit进程记录到了相应的日志里,可以这样查看:
  shell> audit2why < /var/log/audit/audit.log

  如果看不懂的话,推荐安装setroubleshoot套件:
  shell> yum install setroubleshoot

  它本身是一个GUI套件,不过其中包含的一个sealert命令对我们命令行用户很有用:
  shell> sealert -a /var/log/audit/audit.log
Summary:

SELinux is preventing /usr/sbin/httpd "getattr" access to
/var/www/html/test.html.

Detailed Description:

SELinux denied access requested by httpd. /var/www/html/test.html may be a
mislabeled. /var/www/html/test.html default SELinux type is httpd_sys_content_t,
but its current type is admin_home_t. Changing this file back to the default
type, may fix your problem.

File contexts can be assigned to a file in the following ways.

* Files created in a directory receive the file context of the parent
directory by default.
* The SELinux policy might override the default label inherited from the
parent directory by specifying a process running in context A which creates
a file in a directory labeled B will instead create the file with label C.
An example of this would be the dhcp client running with the dhclient_t type
and creating a file in the directory /etc. This file would normally receive
the etc_t type due to parental inheritance but instead the file is labeled
with the net_conf_t type because the SELinux policy specifies this.
* Users can change the file context on a file using tools such as chcon, or
restorecon.

This file could have been mislabeled either by user error, or if an normally
confined application was run under the wrong domain.

However, this might also indicate a bug in SELinux because the file should not
have been labeled with this type.

If you believe this is a bug, please file a bug report against this package.

Allowing Access:

You can restore the default system context to this file by executing the
restorecon command. restorecon '/var/www/html/test.html', if this file is a
directory, you can recursively restore using restorecon -R
'/var/www/html/test.html'.

Fix Command:

/sbin/restorecon '/var/www/html/test.html'

  这次应该看懂了吧!原因是说Apache下文件上下文类型应该是httpd_sys_content_t,但是现在是admin_home_t,所以权限错误,并且在结尾处给出了修复命令。
  可httpd_sys_content_t,admin_home_t都怎么看啊?很简单,借助ls命令的-Z参数即可:
  shell> ls -Z /path

  回到问题的开始,拷贝之所以没出现问题,是因为cp自动修改上下文属性,而移动之所以出现问题是因为mv保留原文件的上下文属性。
  注:关于SELinux和Apache的详细介绍,可以参考『man httpd_selinux』。
  知道了如何解决SELinux问题,以后如果遇到类似的情况不要急着武断的关闭SELinux。

特点1:MAC(Mandatory Access Control)―――对访问的控制彻底化
对于所有的文件,目录,端口这类的资源的访问,都可以是基于策略设定的,这些策略是由管理员定制的、一般用户是没有权限更改的。
对于进程只赋予最小的权限
特点2:TE (Type Enforcement)――― 对于进程只赋予最小的权限
Te概念在 SELinux里非常的重要。它的特点是对所有的文件都赋予一个叫type的文件类型标签,对于所有的进程也赋予各自的一个叫 domain的 标签。Domain标签能够执行的操作也是由access vector在策略里定好的。
我们熟悉的apache服务器,httpd进程只能在httpd_t 里运行,这个httpd_t
的domain能执行的操作,比如能读网页内容文件赋予httpd_sys_content_t,密码文件赋予shadow_t,TCP的80端口赋予
http_port_t等等。如果在access vector里我们不允许
http_t来对http_port_t进行操作的话,Apache启动都启动不了。反过来说,我们只允许80端口,只允许读取被标为
httpd_sys_content_t的文件,httpd_t就不能用别的端口,也不能更改那些被标为httpd_sys_content_t的文件(read
only)。


防止权限升级
特点3:domain迁移 ―― 防止权限升级

在用户环境里运行点对点下载软件azureus,你当前的domain是fu_t,但是,你考虑到安全问题,你打算让他在azureus_t里运行,你要是在terminal里用命令启动azureus的话,它的进程的domain就会默认继承你实行的shell的fu_t。
有了domain迁移的话,我们就可以让azureus在我们指定的azureus_t里运行,在安全上面,这种做法更可取,它不会影响到你的fu_t。
下面是domain迁移指示的例子:
domain_auto_trans(fu_t,azureus_exec_t,azureus_t)
意思就是,当在 fu_t domain里,实行了 被标为 azureus_exec_t的文件时,domain 从fu_t迁移到
azureus_t。下面是Apache启动的迁移图。注意了,因为从哪一个domain能迁移到httpd_t是在策略里定好了,所以要是我们手动
(/etc/init.d/httpd
start)启动apache的话,可能仍然留在sysadm_t里,这样就不能完成正确的迁移。要用run_init命令来手动启动。
对于用户只赋予最小的权限
特点4:RBAC(role base access control) ――――― 对于用户只赋予最小的权限
对于用户来说,被划分成一些ROLE,即使是ROOT用户,你要是不在sysadm_r里,也还是不能实行sysadm_t管理操作的。因为,那些ROLE可以执行那些domain也是在策略里设定的。ROLE也是可以迁移的,但是也只能按策略规定的迁移。

暂不清楚原因,可以先重启试试,之后再尝试一下看看 selinux的日志,或者messages 找找原因。

光从目的上说 你可以用 chcon 来更改 上下文描述类型,也可以从777 644 等权限上间接达到你的要求试试。

模块是你自己写的,还是从网上下的,

你可以仔细阅读一下规则。


娄底市19261617423: 如何关闭Linux里边的selinux -
龙戴二十: 首先我们可以用命令来查看selinux的状态 getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的.还有一个命令也可以查看出selinux的状态. sestatus -v还有一个setenforce 命令可以设置selinux的状态, 具体可以查看 ...

娄底市19261617423: selinux是什么 -
龙戴二十: SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux® 上最杰出的新安全子系统.NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件.SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到. 外文名称:Security-Enhanced Linux 简 称:SELinux 制造单位:美国国家安全局 地 位:Linux历史上最杰出的新安全系统 性 质:强制访问控制(MAC)安全系统

娄底市19261617423: linux怎么关闭selinux -
龙戴二十: 以根用户登录Linux系统,然后在提示符后输入命令sestatus,可以看出目前SELinux功能是处于开启状态的.在提示符后输入vi /etc/sysconfig/selinux命令,进入SElinux的配置文件.在SELinux配置文件中,将SELINUX=enforcing,改为SELINUX=disabled,然后保存退出.重新启动Linux系统.以根用户登录后,输入sestatus命令,可以看出SELinux功能已经关闭.关于更多Linux的学习,请查阅书籍《linux就该这么学》.

娄底市19261617423: 如何关闭linux系统如何关闭selinux -
龙戴二十: 一、查看SELinux状态命令:1、/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: enabled2、getenforce ##也可以用这个命令检查 二、关闭SELinux方法:1、临时关闭(不用重启机器):setenforce 0 #设置SELinux 成为permissive模式#setenforce 1 设置SELinux 成为enforcing模式2、修改配置文件需要重启机器:修改/etc/selinux/config 文件 将SELINUX=enforcing改为SELINUX=disabled 重启机器即可.

娄底市19261617423: SELinux是什么意思,如何关闭?Linux下的防火墙用什么命令打开? -
龙戴二十: 关闭用setenforce 0或者修改文件vim /etc/sysconfig/selinux 把SELINUX=enforcing 改为 SELINUX=disabled. linux防火墙用chkconfig iptables on 开机启动 service iptables start 打开防火墙规则.

娄底市19261617423: linux如何关闭selinux?
龙戴二十: /usr/bin/setenforce 是用来修改SELinux的实时运行模式的 临时打开: setenforce 1 ----设置SELinux 成为enforcing模式 临时关闭: setenforce 0 ----设置SELinux 成为permissive模式 如果要彻底禁用SELinux 需要在/etc/sysconfig/selinux中设置参数selinux=0 ,或者在/etc/grub.conf中添加这个参数 /usr/bin/setstatus -v

娄底市19261617423: SELinux介绍 -
龙戴二十: 在Redhat Enterprise Linux 4.0或Fedora Core 2 Linux以上版本的Linux中,有不少用户经常会遇到诸如apache的Permission denied,X windows打不开等等问题,抛开一些常规配置错误外,很大一部分原因是因为激活了SELinux的缘故.什么是...

娄底市19261617423: selinux常用参数
龙戴二十: 1)ls命令 在命令后加个 -Z 或者加 –context [root@python azureus]# ls -Z -rwxr-xr-x fu fu user_u:object_r:user_home_t azureus -rw-r--r-- fu fu user_u:object_r:user_home_t Azureus2.jar -rw-r--r-- fu fu user_u:object_r:user_home_t Azureus.png 2)chcon 更...

娄底市19261617423: 如何在开启selinux前提下解决此问题6969 -
龙戴二十: 以下介绍一下SELinux相关e69da5e6ba90e799bee5baa631333337613739的工具 /usr/bin/setenforce 修改SELinux的实时运行模式 setenforce 1 设置SELinux 成为enforcing模式 setenforce 0 设置SELinux 成为permissive模式 如果要彻底禁用...

娄底市19261617423: ubuntu16.04安装SElinux以后无法开机 -
龙戴二十: 按电源键反复开关机试试,放一段时间试试,确实不可以就重装系统吧,如果自己重装不了,花30元到维修那里找维修的人帮助您.只要注意自己的电脑不卡机、蓝屏、突然关机,开机就不会这样了.有问题请您追问我.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网