SELinux – podstawy

SELinux to potężne narzędzie pozwalające zabezpieczyć serwer linuksowy.

Policy jest ładowane do jądra systemu i w jądrze systemu jest sprawdzane.

Contents

Tryby pracy SElinux

disabled – system SElinux jest wyłączony
permissive – SElinux pisze ostrzeżenia o przekroczeniu polityki bezpieczeństwa ale zezwala na wykonanie zadania
enforcing – SElinux blokuje wszystkie niedozwolone działania.

Polityki pracy SElinux

strict – SELinux chroni cały system
targeted – SElinux chroni wybrane elementy systemu

Kontekst, tożsamosc, status i typ

Kontekst posiada składnię: tożsamość:status:typ.

[root@localhost conf]# ls -Z
-rw-r--r--  root root system_u:object_r:httpd_config_t httpd.conf
-rw-r--r--  root root system_u:object_r:httpd_config_t magic

W przypadku pracy z systemem MLS (Multi Level Security) kontekst posiada składnię: tożsamość:status:typ:czulosc (ang. sensitivity,
domyślnie s0).

Standardowo:

  • Typ kończy się na t (np. httpd_config_t)
  • Status kończy się na r (np. object_r)
  • Tożsamość kończy się na u (np system_u)

Użytkownicy

Domyślne tożsamość

Domyślną tożsamością użytkownika jest user_u a jedyny status, do jakiego będzie ma dostęp, to user_r.

Kontekst dla użytkownika można sprawdzić korzystając z polecenia id (tylko bieżący użytkownik)

24:/usr/src/selinux-policy-refpolicy-src# id
uid=0(root) gid=0(root) grupy=0(root),1555(domadmin) context=user_u:system_r:unconfined_t:s0

Zmiana bieżącej roli (tożsamości)

[ziutus@kickstart_test12 ~]$ newrole -r sysadm_r
Password:
[ziutus@kickstart_test12 ~]$ id
uid=500(ziutus) gid=500(ziutus) groups=500(ziutus) context=staff_u:sysadm_r:sysadm_t:s0-s15:c0.c1023

Modyfikacja przynależności użytkownika do określonego kontekstu

Należy wykonać dwa kroki, najpierw określić jaki kontest a następnie poprawić pliki z jego katalogu domowego, aby mógł się on zalogować

semanage login -a -s staff_u ziutus
restorecon -R -F /home/ziutus

Zarządzanie  kontekstem i modułami policy

chcon – change security context

restorecon – restore file(s) default SELinux security contexts

semodule – Manage SELinux policy modules.

Przegląd konfiguracji

getsebool

polecenie getsebool z parametrem -a pokazuje wszystkie ustawienia związane z zmiennymi typu bool (czyli prawda lub falsz). jezeli chcemy sprawdzić ustawienia tylko jednej wartości, możemy podać ją jako parametr.

24:/var/log# getsebool ypserv_disable_trans
ypserv_disable_trans --> off

getenforce

Polecenie getenforcepozwala sprawdzić tryb, w którym pracuje selinux.

24:/var/log# getenforce
Enforcing

selinuxenabled

Narzędzie do wykorzystania w skryptach które zwraca „prawda” lub „fałsz” w zależności czy selinux jest włączony czy wyłączony.

matchpathcon

Polecenie matchpathcon pozwala określić kontekst dla danego pliku.

debian:~# matchpathcon /etc/passwd
/etc/passwd     system_u:object_r:etc_t:s0

dodatkowo można użyć narzędzi:

getfattr 

policyvers

24:/var/log# policyvers
20

sestatus

24:/var/log# sestatus

SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          permissive
Policy version:                 20

Policy from config file:        refpolicy-targeted

Praca z logami

audit2why pozwala okreslic dlaczego SELinux odmówił dostępu

przykład użycia:

audit2why < /var/log/messages

audit2allow - generate policy allow rules from logs of denied operations

Narzędzia monitorujące pracę SELinux

avcstat - Display SELinux AVC statistics

debian:~# avcstat
lookups       hits     misses     allocs   reclaims      frees
1869921    1869029        892       1749        368       1243

sestatus - SELinux status tool

Zmiana ustawien SELinux

semanage – SELinux Policy Management tool

chcon – change security context

fixfiles – fix file security contexts (his script is primarily used to correct the security context database (extended attributes) on filesystems).

setsebool

togglesebool

setenforce – modify the mode SELinux is running in

restorecon – set file security contexts.

setfiles – set file security contexts.

load_policy – load a new policy into the kernel

chcat – change file security category

Tworzenie nowej polityki SELinux

checkpolicy – SELinux policy compiler

checkmodule –

semodule_package

Inne narzedzia

newrole – run a shell with a new role

run_init – run an init script in the proper context

Konfiguratory SELinux

seedit (nierozwijany)

http://seedit.sourceforge.net/

slide

http://oss.tresys.com/projects/slide

Selinux w dystrybucjach

MLS w CentOS 6

1. Instalacja pakietu polityki mls (Multi Level Security)

yum install selinux-policy-mls

2. Zmiana SELINUXTYPE=mls w /etc/selinux/config
3. Wyłączenie trybu enforce

~]# setenforce 0
~]# getenforce
Permissive

4. Relabeling systemu plików (jako że mamy nową politykę)
Tworzymu plik /.autorelabel

touch /.autorelabel

i restartujemy system.

5. Poprawiamy ustawienia dla katalogu /root i katalogów domowych

~]# genhomedircon
~]# restorecon -R -v /root /home 

6. sprawdzmay logi czy występują jakieś błedy (/var/log/audit/audit.log)
7. Edytujemy /etc/selinux/config i zmieniamy tryb na enforcing.
8. Ostatni reboot.

Debian

Najpierw należy zainstalować niezbędne pakiety. Dokonany tego wykonując polecenie wajig install selinux-policy-refpolicy-strict.

debian:~# wajig install selinux-policy-refpolicy-strict

Następnie każdemu plikowi, katalogowi, urządzenia itp należy nadać kontekst. Dokonamy tego przy pomocy komendy fixfiles relabel.

debian:~# fixfiles relabel

Files in the /tmp directory may be labeled incorrectly, this command
can remove all files in /tmp.  If you choose to remove files from /tmp,
a reboot will be required after completion.
Do you wish to clean out the /tmp directory [N]?
/sbin/setfiles: labeling files under /
matchpathcon_filespec_eval: hash table stats: 21020 elements, 19185/65536 buckets used, longest chain length 2
/sbin/setfiles: labeling files under /boot
matchpathcon_filespec_eval: hash table stats: 18 elements, 18/65536 buckets used, longest chain length 1
/sbin/setfiles: Done.

Następnie włączamy SELinux podczas startu systemu, zrobimy to przez modyfikacje parametrów startowych systemu (edycja pliku /etc/selinux/config)

Usługi i SELinux

startowanie/zatrzymywanie/odpytywanie usług w CentOS 5/6

run_init service nfs start

SELinux i Apache

chcon -vR --type=httpd_sys_content_t /data/www/second/
semanage fcontext -a -e /var/www /srv/www
restorecon -R -v /srv/www

semanage fcontext  -a -t httpd_sys_content_t "/data/www/second(/.*)"
restorecon -R -v /data/www/second/

selinux i ftp

Dostęp do katalogów domowych

setsebool -P ftp_home_dir 1

selinux i samba

Samba i dostęp do katalogów domowych

setsebool -P samba_enable_home_dirs 1

SELinux i nfs

nfs i dostęp do katalogów domowych

setsebool -P use_nfs_home_dirs 1 

Ciekawe narzędzia

setroubleshoot

Poprawiając polityki bezpieczeństwa natknąłem się na ciekawe narzędzie o nazwie setroubleshoot.
Instalacja jest prosta:

yum install  setroubleshoot
/etc/init.d/rsyslog restart
/etc/init.d/auditd restart
sealert -a /var/log/audit/audit.log

Efekt pracy programu na przykładzie jednego komunikatu:

Summary:
SELinux is preventing httpd (httpd_t) "name_bind" to "Unknown" (httpd_t).
Detailed Description:
SELinux denied access requested by httpd. It is not expected that this access is
required by httpd and this access may signal an intrusion attempt. It is also
possible that the specific version or configuration of the application is
causing it to require additional access.
Allowing Access:
You can generate a local policy module to allow this access - see FAQ
(http://fedora.redhat.com/docs/selinux-faq-fc5/#id2961385) Or you can disable
SELinux protection altogether. Disabling SELinux protection is not recommended.
Please file a bug report (http://bugzilla.redhat.com/bugzilla/enter_bug.cgi)
against this package.
Additional Information:
Source Context                system_u:system_r:httpd_t
Target Context                system_u:object_r:httpd_t
Target Objects                None [ tcp_socket ]
Source                        httpd
Source Path                   /usr/sbin/httpd
Port                          80
Host                          "Unknown"
Source RPM Packages           httpd-2.2.3-43.el5.centos.3
Target RPM Packages
Policy RPM                    selinux-policy-2.4.6-279.el5_5.1
Selinux Enabled               True
Policy Type                   strict
MLS Enabled                   True
Enforcing Mode                Enforcing
Plugin Name                   catchall
Host Name                     centos.server.localnet
Platform                      Linux centos.server.localnet 2.6.18-194.3.1.el5 #1
SMP Thu May 13 13:09:10 EDT 2010 i686 i686
Alert Count                   6
First Seen                    Fri Oct  1 21:31:40 2010
Last Seen                     Fri Oct  1 21:37:40 2010
Local ID                      b5f4e8ef-6325-4ccb-ae7f-c898462b77df
Line Numbers                  17, 18, 19, 20, 23, 24, 25, 26, 37, 38, 39, 40
Raw Audit Messages
type=AVC msg=audit(1285961860.699:19487): avc:  denied  { name_bind } for  pid=8616 comm="httpd" src=80 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:httpd_t:s0 tclass=tcp_socket
type=SYSCALL msg=audit(1285961860.699:19487): arch=40000003 syscall=102 success=no exit=-13 a0=2 a1=bfa2ec80 a2=186988 a3=8917858 items=0 ppid=8615 pid=8616 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts1 ses=3181 comm="httpd" exe="/usr/sbin/httpd" subj=system_u:system_r:httpd_t:s0 key=(null)

Katalog typowych problemów

Brak możliwości logowania po SSH po skopiowaniu kluczy lokalnie

Przy kopiowaniu kluczy lokalnie kontekst nie jest zmieniany, należy więc ustawić poprawny kontekst:

[root@linux2 log]# restorecon -FRvv /root/.ssh
restorecon reset /root/.ssh context unconfined_u:object_r:admin_home_t:s0->system_u:object_r:ssh_home_t:s0
restorecon reset /root/.ssh/known_hosts context unconfined_u:object_r:admin_home_t:s0->system_u:object_r:ssh_home_t:s0
restorecon reset /root/.ssh/authorized_keys context unconfined_u:object_r:admin_home_t:s0->system_u:object_r:ssh_home_t:s0

SELinux – w której dystrybucji jest najłatwiej zaimplementować?

Niby SELinux jest ten sam zawsze ale jednak w jednych dystrybucjach jest łatwiej go ustawić, w innych trudniej.

Prosty przykład:
1. Ubuntu

ktos@ubuntu:~# man -k selinux | wc -l
34

2. CentOS

[root@centos sysconfig]# man -k selinux | wc -l
56

Gdzie jest różnica? Na przykład w stronach pomocy, mogających ustawić usługi:

[root@centos sysconfig]# man -k _selinux
ftpd_selinux         (8)  - Security-Enhanced Linux policy for ftp daemons
httpd_selinux        (8)  - Security Enhanced Linux Policy for the httpd daemon
kerberos_selinux     (8)  - Security Enhanced Linux Policy for Kerberos
named_selinux        (8)  - Security Enhanced Linux Policy for the Internet Name server (named) daemon
nfs_selinux          (8)  - Security Enhanced Linux Policy for NFS
pam_selinux          (8)  - PAM module to set the default security context
rsync_selinux        (8)  - Security Enhanced Linux Policy for the rsync daemon
samba_selinux        (8)  - Security Enhanced Linux Policy for Samba
ypbind_selinux       (8)  - Security Enhanced Linux Policy for NIS

CentOS ma także narzędzia wspomagające konfiugrację jak system-config-security lub system-config-selinux…

Linki w Internecie

Linki do oficjalnych stron dystrybucji poświęconych SELinux

Linki do oficjalnych stron dystrybucyjnych poświęconych SELinux:

Ciekawe artykuły

http://james-morris.livejournal.com/2005/09/16/ MLS and MCS

http://www.lurking-grue.org/gettingstarted_newselinuxHOWTO.html – Getting Started with SE Linux HOWTO: the new SE Linux

Todo

Konfiguracja kontekstu z serwera (sprawdzić)

w pliku semange.conf jest informacja o możliwości posiadania zdalnego serwera polityk. (sprawdzić jak to może działać) 

Dodaj komentarz