CentOS 6.X의 SELinux 업로드

15364 단어 selinux
SELinux는 Security-Enhanced Linux의 약칭으로 보안이 강화된 linux이다.전통적인 linux 권한은 파일과 디렉터리의 owner, 그룹과other의 rwx를 제어하는 것이고 SELinux는 위임식 접근 제어를 사용한다. 즉, 하나의 프로세스가 구체적인 파일 시스템 위의 파일과 디렉터리에 대한 접근을 제어하는 것이다. SELinux는 많은 규칙을 규정하여 어떤 프로세스가 어떤 파일과 디렉터리에 접근할 수 있는지 결정한다.
Linux는 프로세스나 파일의 보안 context에 따라 프로세스가 파일 시스템에 접근할 수 있는지 여부를 결정합니다. 보안 context는 Identify:role:type 세 부분으로 구성되어 있으며,selinux의 형식이 SELINUXTYPE=targeted일 때 보안 context의 type만 유용합니다.selinux의 프로필은 여기 있습니다:/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 - SELinux is fully disabled.

SELINUX=enforcing

# SELINUXTYPE= type of policy in use. Possible values are:

#       targeted - Only targeted network daemons are protected.

#       strict - Full SELinux protection.

SELINUXTYPE=targeted


필요한 SELinux 관련 도구를 설치해야 합니다.
yum install policycoreutils-python setools-console setroubleshoot setroubleshoot-server

현재 SELinux의 상태를 보려면 다음과 같이 하십시오.
[root@centos ~]# getenforce

Enforcing

상태가 Enforcing이면 SELinux가 열려 있고 disabled나permissive는 다음 명령을 사용해야 합니다 (permissive는 SELinux의 권한 제어에 부합되지 않으면 Warning 알림 정보가 나타나며 실제 Block 프로세스에 접근하지 않습니다).
SELinux를 Enforcing으로 설정하려면:
[root@centos ~]# setenforce 1

그런 다음 컴퓨터를 재부팅하고 SELinux 관련 파일이 작성될 때까지 기다립니다.
프로세스의 보안 context를 보려면 다음과 같이 하십시오.
[root@centos ~]# ps aux -Z | grep httpd

unconfined_u:system_r:httpd_t:s0 root     6056  0.0  0.3  11672  3504 ?        Ss   15:31   0:03 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6061  0.0  0.2  11804  2664 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6062  0.0  0.2  11672  2132 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6063  0.0  0.2  11804  2664 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6064  0.0  0.2  11804  2780 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6065  0.0  0.2  11672  2132 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6066  0.0  0.2  11672  2132 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6067  0.0  0.2  11672  2132 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache   6068  0.0  0.2  11672  2132 ?        S    15:31   0:00 /usr/sbin/httpd

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 6845 0.0  0.0 4340 752 pts/0 S+ 18:16   0:00 grep httpd

디렉토리의 보안 context를 보려면 다음과 같이 하십시오.
[root@centos ~]# ls -dZ /var/www/

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

보안 context의 사용자, role, type, 그리고 규칙 (Booleans) 을 포함한 SELinux의 통계 정보를 보여 줍니다.
[root@centos ~]# seinfo



Statistics for policy file: /etc/selinux/targeted/policy/policy.24

Policy Version & Type: v.24 (binary, mls)



   Classes:            81    Permissions:       235

   Sensitivities:       1    Categories:       1024

   Types: 3488    Attributes:        273

   Users: 9    Roles: 12

   Booleans: 187    Cond. Expr.:       222

   Allow:          273920    Neverallow:          0

   Auditallow:         96    Dontaudit:      199904

   Type_trans:      23469    Type_change:        38

   Type_member:        48    Role allow:         20

   Role_trans:        291    Range_trans:      3993

   Constraints:        87    Validatetrans:       0

   Initial SIDs:       27    Fs_use:             22

   Genfscon:           81    Portcon:           426

   Netifcon:            0    Nodecon:             0

   Permissives:        59    Polcap:              2


모든 규칙 이름을 보려면 다음과 같이 하십시오.
[root@centos ~]# seinfo -b

Conditional Booleans: 187

   allow_domain_fd_use

   allow_ftpd_full_access

   allow_sysadm_exec_content

   allow_user_exec_content

   allow_zebra_write_config

   cdrecord_read_content

   fcron_crond

   httpd_manage_ipa

   httpd_use_openstack

   mmap_low_allowed

   samba_share_fusefs

   sepgsql_enable_users_ddl

   abrt_handle_event

   allow_ftpd_use_cifs

   allow_httpd_mod_auth_pam

   allow_java_execstack

   cron_can_relabel

.......


규칙이 열려 있는지 확인하려면 다음과 같이 하십시오.
[root@centos ~]# getsebool httpd_enable_homedirs

httpd_enable_homedirs --> off


규칙을 엽니다.
[root@centos ~]# setsebool -P httpd_enable_homedirs=1

[root@centos ~]# getsebool httpd_enable_homedirs

httpd_enable_homedirs --> on


규칙의 구체적인 정보 (즉 allow 또는 deny 프로세스의 보안 context type이 파일 시스템의 보안 context type에 접근하는 것) 를 보십시오.
[root@centos ~]# sesearch -b httpd_enable_homedirs --all

ERROR: Cannot get avrules: Neverallow rules requested but not available

Found 46 semantic av rules:

   allow httpd_sys_script_t home_root_t : dir { getattr search open } ; 

   allow httpd_sys_script_t home_root_t : lnk_file { read getattr } ; 

   allow httpd_suexec_t user_home_dir_t : dir { getattr search open } ; 

   allow httpd_suexec_t user_home_dir_t : lnk_file { read getattr } ; 

   allow httpd_suexec_t autofs_t : dir { ioctl read getattr lock search open } ; 

   allow httpd_suexec_t cifs_t : file { ioctl read getattr lock execute execute_no_trans open } ; 

   allow httpd_suexec_t cifs_t : dir { ioctl read getattr lock search open } ; 

   allow httpd_suexec_t cifs_t : lnk_file { read getattr } ; 

   allow httpd_suexec_t nfs_t : file { ioctl read getattr lock execute execute_no_trans open } ; 

   allow httpd_suexec_t nfs_t : dir { ioctl read getattr lock search open } ; 

   allow httpd_suexec_t nfs_t : lnk_file { read getattr } ; 

   allow httpd_t user_home_t : file { ioctl read getattr lock open } ; 

.............


selinux는 파일 시스템에 많은 기본 설정이 존재합니다. semanage를 통해 시스템의 모든 디렉터리의 기본 보안 context를 볼 수 있습니다.
[root@centos ~]# semanage fcontext -l

SELinux fcontext                                   type               Context



/                                                  directory          system_u:object_r:root_t:s0 

/.*                                                all files          system_u:object_r:default_t:s0 

/[^/]+                                             regular file       system_u:object_r:etc_runtime_t:s0 

/\.autofsck                                        regular file       system_u:object_r:etc_runtime_t:s0 

/\.autorelabel                                     regular file       system_u:object_r:etc_runtime_t:s0 

/\.journal                                         all files          <>

/\.suspended                                       regular file       system_u:object_r:etc_runtime_t:s0 

/a?quota\.(user|group)                             regular file       system_u:object_r:quota_db_t:s0 

/afs                                               directory          system_u:object_r:mnt_t:s0 

/bin                                               directory          system_u:object_r:bin_t:s0 

/bin/.*                                            all files          system_u:object_r:bin_t:s0 

/bin/alsaunmute                                    regular file       system_u:object_r:alsa_exec_t:s0 

/bin/bash                                          regular file       system_u:object_r:shell_exec_t:s0 

...............


디렉토리에 기본 보안 context를 추가하려면:
[root@centos ~]# semanage fcontext -a -t public_content_t "/srv/test(/.*)?"


위의 명령은/srv/test의 기본 보안 context type을public 로 설정합니다content_t
파일의 보안 context를 변경하려면:
[root@centos ~]# chcon -t var_t /var/www/html/index.html 

[root@centos ~]# ll -Z /var/www/html/index.html 

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


위의 명령은/var/www/html/index입니다.html type에서 vart
restorecon 명령을 사용하여 파일을 디렉토리에 있는 기본 보안 context로 복원할 수 있습니다:/var/www의 기본 context가 무엇인지 확인하십시오:
[root@centos ~]# semanage fcontext -l | grep /var/www

/var/www(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0 

/var/www/[^/]*/cgi-bin(/.*)?                       all files          system_u:object_r:httpd_sys_script_exec_t:s0 

/var/www/apcupsd/multimon\.cgi                     regular file       system_u:object_r:httpd_apcupsd_cgi_script_exec_t:s0 

/var/www/apcupsd/upsfstats\.cgi                    regular file       system_u:object_r:httpd_apcupsd_cgi_script_exec_t:s0 

.............


restorecon을 사용하여/var/www의 모든 파일과 디렉터리를 기본값으로 복원합니다.
[root@centos ~]# restorecon -Rv /var/www

restorecon reset /var/www/html/index.html context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0


--------------------selinux는 로그 파일을 제공하여 오류 정보를 기록합니다. 오류 정보는/var/log/messages와/var/log/setroubleshoot/*에 기록되어 있으며,auditd 서비스를 다시 시작하여selinux의log 서비스를 켜야 합니다:
[root@centos ~]# /etc/init.d/auditd restart


selinux의 오류 정보를 보려면 다음과 같이 하십시오.
[root@centos ~]# cat /var/log/messages | grep setroubleshoot

Aug  9 17:46:47 centos yum[6590]: Installed: setroubleshoot-plugins-3.0.40-1.el6.noarch

Aug  9 17:46:50 centos yum[6590]: Installed: setroubleshoot-server-3.0.47-3.el6_3.i686

Aug  9 17:46:54 centos yum[6590]: Installed: setroubleshoot-3.0.47-3.el6_3.i686

Aug  9 17:58:57 centos setroubleshoot: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages. run sealert -l c7a436a1-a114-4659-91a9-4155b1003dd7

Aug  9 17:58:58 centos setroubleshoot: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages. run sealert -l c7a436a1-a114-4659-91a9-4155b1003dd7

Aug  9 18:00:35 centos setroubleshoot: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages. run sealert -l c7a436a1-a114-4659-91a9-4155b1003dd7

Aug  9 18:00:36 centos setroubleshoot: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages. run sealert -l c7a436a1-a114-4659-91a9-4155b1003dd7


sealert를 실행하여 구체적인 해결 방법을 보려면 다음과 같이 하십시오.
[root@centos ~]# sealert -l c7a436a1-a114-4659-91a9-4155b1003dd7

SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html.



*****  Plugin restorecon (99.5 confidence) suggests  *************************



If you want to fix the label. 

/var/www/html/index.html default label should be httpd_sys_content_t.

Then you can run restorecon.

Do

# /sbin/restorecon -v /var/www/html/index.html



*****  Plugin catchall (1.49 confidence) suggests  ***************************



If you believe that httpd should be allowed getattr access on the index.html file by default.

Then you should report this as a bug.

You can generate a local policy module to allow this access.

Do

allow this access for now by executing:

# grep httpd /var/log/audit/audit.log | audit2allow -M mypol

# semodule -i mypol.pp




위의 정보는 구체적인 해결 방법을 제시했는데, 바로restorecon을 사용하여 index를 사용하는 것이다.html이 기본값으로 복원됩니다.
--------------------다음은 httpd라는 www 서비스로 간단하게 실험해 보겠습니다. httpd를 설치하지 않은 것은 먼저 설치하세요.
[root@centos ~]# yum install httpd

Loaded plugins: fastestmirror, refresh-packagekit, security

Loading mirror speeds from cached hostfile

 * base: mirrors.ta139.com

 * extras: mirrors.ta139.com

 * updates: mirrors.ta139.com

Setting up Install Process

Package httpd-2.2.15-15.el6.centos.1.i686 already installed and latest version

Nothing to do


httpd 서비스를 시작하려면:
[root@centos ~]# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]


시스템이 80 포트 스니핑을 시작했는지 확인하려면:
[root@centos ~]# netstat -tupln | grep httpd

tcp        0      0 :::80                       :::*                        LISTEN      9587/httpd     


루트의 홈 디렉터리에 index를 만듭니다.html 파일:
[root@centos ~]# echo "Test for selinux" > index.html 


context를 보려면 다음과 같이 하십시오.
[root@centos ~]# ls -Z index.html 

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


이 type이 admin인 것을 보실 수 있습니다home_t,/root 이 디렉터리의 보안 context를 계승합니다.
/var/www/html에 복사합니다.
[root@centos ~]# cp -a index.html  /var/www/html/


원본 파일을 보존한 보안 context:
[root@centos ~]# ll -Z /var/www/html/index.html 

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


링크스로 이 페이지에 접근하기:
[root@centos ~]# links http://localhost/index.html -dump

                                   Forbidden



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



   --------------------------------------------------------------------------



    Apache/2.2.15 (CentOS) Server at localhost Port 80


예상대로permission deny가 생겼습니다. 이럴 때는/var/log/messages를 직접 보고 문제를 해결할 수 있습니다.
먼저 httpd 프로세스의 context 를 살펴보겠습니다.
[root@centos ~]# ps aux -Z | grep httpd

unconfined_u:system_r:httpd_t:s0 apache   9590  0.0  0.2  11804  2852 ?        S    10:38   0:00 /usr/sbin/httpd


httpd 접근 type이admin 인지 다시 확인하십시오home_t 파일:
[root@centos ~]# sesearch --all | grep "allow httpd_t admin_home_t"

ERROR: Cannot get avrules: Neverallow rules requested but not available


검색 결과 이rule가 없어서 httpd 프로세스가 Block되었습니다.
httpd 액세스 type이 httpd 인 경우 확인sys_content_t 파일:
root@centos ~]# sesearch --all | grep "allow httpd_t httpd_sys_content_t"

ERROR: Cannot get avrules: Neverallow rules requested but not available

   allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ; 

   allow httpd_t httpd_sys_content_t : dir { ioctl read getattr lock search open } ; 

   allow httpd_t httpd_sys_content_t : lnk_file { read getattr } ; 

   allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ; 

   allow httpd_t httpd_sys_content_t : dir { ioctl read getattr lock search open } ; 

   allow httpd_t httpd_sys_content_t : dir { ioctl read write getattr lock add_name remove_name search open } ; 

   allow httpd_t httpd_sys_content_t : lnk_file { read getattr } ; 


자, 회복/var/www/html/index.html:
[root@centos ~]# restorecon -Rv /var/www/html/

restorecon reset /var/www/html/index.html context unconfined_u:object_r:admin_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0


한 번 더 액세스:
[root@centos ~]# links http://localhost/index.html -dump

   Test for selinux


오케이.

좋은 웹페이지 즐겨찾기