笔记 :SElinux基础知识

来源:互联网 发布:老子的道是指什么 知乎 编辑:程序博客网 时间:2024/06/06 03:22

 

SElinux体系简介   (详见百科)

背景:

RHEL6.2 

selinux基于默认的 targeted 策略

只关注安全上下文中的元素中的类型


selinux模式

1.Enforcing   //强制

2.Permissive  //警告   (但还是让你通过)

3.Disabled      //关闭

#cat /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,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

在此文件中可更改模式和策略,但只有重启才能生效并具有永久性。


在系统运行时: 以下命令只是临时生效。重启后会还原到上面文件中的模式及策略。

[root@adp225 ~]# getenforce //查看现在模式
Enforcing
[root@adp225 ~]# setenforce 0 //更改为警告模式
[root@adp225 ~]# getenforce 
Permissive
[root@adp225 ~]# setenforce 1 //更改为强制模式
[root@adp225 ~]# getenforce 
Enforcing

注:在开机时在内核传递参数 selinux=0  或  selinux=1 也可以改变这次启动后的模式 。。


selinux Security Context

元素:

用户:角色:类型:敏感度:类别

user_u:object_r:tmp_t:s0:c0

并非所有系统都将显示:s0:c0

默认targeted策略主要使用类型元素

[root@adp225 ~]# ls -Z install.log
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log

[root@adp225 ~]# ls -Zd Music/
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Music/

[root@adp225 ~]# ps -Z  aux  |grep httpd

unconfined_u:system_r:httpd_t:s0 root     2315  0.0  0.7 186220  3924 ?        Ss   17:20   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   2318  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2319  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2320  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2321  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2322  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2323  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2324  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd
unconfined_u:system_r:httpd_t:s0 apache   2325  0.0  0.4 186220  2464 ?        S    17:20   0:00 /usr/sbin/httpd

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2333 0.0  0.1 103304 856 pts/0 S+ 17:21   0:00 grep httpd

在selinux体系中 unconfined_u:system_r:httpd_t:s0 称之为context值

例如httpd服务中用户访问网页 ,首先用户连接到server中httpd进程再由进程去访问需要的文件

chient---------server  process-----------file

process的contest值必须与file的contest值对应 ,不对应则不能访问该文件。

[root@Aserver ~]# system-config-selinux//以图形方式显示所有的对应关系

[root@Aserver ~]# semanage fcontext -l  |less//查看所有的对应关系

[root@Aserver ~]# chcon -R -t public_content_t   /mydata/html                       //递归更改/mydata/html的类型为public_content_t 。此更改因某种原因重导context值时还是会变为数据库中的值

[root@Aserver ~]# semanage  fcontext  -a  -t  public_content_t '/mydata/html(/.*)'             //把刚更改写入数据库

一些服务相对应的contest值得帮助

[root@Aserver ~]# man httpd_selinux

[root@Aserver ~]# man ftp_selinux

[root@Aserver ~]# man mamba_selinux

 managing booleans

布尔值  :决定是否在运行时应用某些策略。off OR on

[root@Aserver ~]# getsebool -a     //查看所有策略

[root@Aserver ~]# getsebool -a |grep  httpd//httpd相关策略
allow_httpd_anon_write --> off
allow_httpd_mod_auth_ntlm_winbind --> off
allow_httpd_mod_auth_pam --> off
allow_httpd_sys_script_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> on
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_read_user_content --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_tmp_exec --> off
httpd_tty_comm --> on
httpd_unified --> on
httpd_use_cifs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off

[root@Aserver ~]# setsebool httpd_enable_ftp_server on//开启

[root@Aserver ~]# setsebool httpd_enable_ftp_server off//关闭

以上更改只是在内存中更改 ,如需永久更改加 -P

[root@Aserver ~]# setsebool  -P  httpd_enable_ftp_server on//永久开启


故障排除

[root@remote ~]# cat /var/log/messages

[root@remote ~]# man -k selinux

包:setroubleshootd   提供selinux问题的解决方案

 

 

原创粉丝点击