O objetivo deste post é ajudar as pessoas a manter o SELinux habilitado, resolvendo os problemas com o mesmo.

O exemplo é um LAMP com Red Hat EL 5.3 com tudo funcionando, porém um daemon vsftpd que não quer subir quando o SELinux está rodando.

Para verificar se o problema é de fato o SELinux, basta desabilita-lo temporariamente:

[root@selinux tmp]# setenforce Permissive
 
[root@selinux tmp]# /etc/init.d/vsftpd start
Starting vsftpd for vsftpd:                                             [  OK  ]

Sem o SELinux, o VSFTP funciona normalmente, porém basta ativa-lo que mensagens estranhas começam a aparecer:

[root@selinux tmp]# setenforce enforcing
 
[root@selinux tmp]# /etc/init.d/vsftpd start
Starting vsftpd for vsftpd: /usr/sbin/vsftpd: error while loading shared libraries: libssl.so.6: failed to map segment from shared object: Permission denied
                                                          [FAILED]
[root@selinux ~] # tail /var/log/messages
Jul 27 15:01:44 selinux kernel: type=1107 audit(1248717704.011:1569): user pid=25449 uid=28 auid=4294967295 subj=system_u:system_r:init_t:s0 msg='avc:  denied  { shmempwd } for  scontext=system_u:system_r:initrc_t:s0 tcontext=system_u:system_r:init_t:s0 tclass=nscd
Jul 27 15:01:44 selinux kernel: : exe="?" (sauid=28, hostname=?, addr=?, terminal=?)'
Jul 27 15:01:44 selinux kernel: type=1107 audit(1248717704.012:1570): user pid=25449 uid=28 auid=4294967295 subj=system_u:system_r:init_t:s0 msg='avc:  denied  { getpwd } for  scontext=system_u:system_r:initrc_t:s0 tcontext=system_u:system_r:init_t:s0 tclass=nscd
Jul 27 15:01:44 selinux kernel: : exe="?" (sauid=28, hostname=?, addr=?, terminal=?)'
Jul 27 15:01:44 selinux kernel: type=1107 audit(1248717704.012:1571): user pid=25449 uid=28 auid=4294967295 subj=system_u:system_r:init_t:s0 msg='avc:  denied  { shmemgrp } for  scontext=system_u:system_r:initrc_t:s0 tcontext=system_u:system_r:init_t:s0 tclass=nscd
Jul 27 15:01:44 selinux kernel: : exe="?" (sauid=28, hostname=?, addr=?, terminal=?)'
Jul 27 15:01:44 selinux kernel: type=1107 audit(1248717704.012:1572): user pid=25449 uid=28 auid=4294967295 subj=system_u:system_r:init_t:s0 msg='avc:  denied  { getgrp } for  scontext=system_u:system_r:initrc_t:s0 tcontext=system_u:system_r:init_t:s0 tclass=nscd
Jul 27 15:01:44 selinux kernel: : exe="?" (sauid=28, hostname=?, addr=?, terminal=?)'
Jul 27 15:01:44 selinux kernel: type=1400 audit(1248717704.028:1573): avc:  denied  { execute } for  pid=25644 comm="vsftpd" path="/lib64/libssl.so.0.9.8e" dev=sda3 ino=1488241 scontext=system_u:system_r:ftpd_t:s0 tcontext=system_u:object_r:file_t:s0 tclass=file

Você pode observar que vários destes erros são referentes ao cache NSCD, e somente a última é de fato referente ao VSFTPd e suas bibiotecas compartilhadas.

Para ficar mais visível, vamos parar o o nscd e tratar como dois problemas distintos:

[root@selinux selinux]# /etc/init.d/nscd stop
Stopping nscd:                                             [  OK  ]
 
[root@selinux selinux]# /etc/init.d/vsftpd start
Starting vsftpd for vsftpd: /usr/sbin/vsftpd: error while loading shared libraries: libssl.so.6: failed to map segment from shared object: Permission denied
                                                          [FAILED]
[root@selinux selinux]# tail /var/log/messages
Jul 27 15:01:52 selinux kernel: type=1400 audit(1248717712.742:1574): avc:  denied  { execute } for  pid=25662 comm="vsftpd" path="/lib64/libssl.so.0.9.8e" dev=sda3 ino=1488241 scontext=system_u:system_r:ftpd_t:s0 tcontext=system_u:object_r:file_t:s0 tclass=file

Uma das coisas mais legais que foram adicionados ao RHEL5, é o daemon setroubleshoot que ajuda a traduzir esses erros monstros do selinux para algo que um humano consiga entender.

A ferramenta audit2allow permite pegar trechos de logs monstros que estão barrando em algo e converter para o mundo do Selinux, eliminando assim o problema em que você está preso:

[root@selinux tcruz]# grep vsftpd /var/log/messages | audit2allow -M vsftpd

Será gerado dois arquivos:
vsftpd.te = texto
vsftpd.pp = binário

O vsftpd.te conterá algo como:

module vsftp 1.0;
 
require {
	type ftpd_t;
	type file_t;
	class dir search;
	class file execute;
}
 
#============= ftpd_t ==============
allow ftpd_t file_t:dir search;
allow ftpd_t file_t:file execute;

Enquanto o nscd.te mostraria algo assim:

module nscd 1.0;
 
require {
	type init_t;
	type initrc_t;
	class nscd { shmemhost shmempwd getpwd shmemgrp gethost getgrp };
}
 
#============= initrc_t ==============
allow initrc_t init_t:nscd { shmemgrp getgrp shmempwd getpwd gethost shmemhost };

Se você estiver satisfeito com o resultado, poderá simplesmente importar o arquivo binário para junto das regras atuais do seu sistema:

[root@selinux tcruz]# semodule -i vsftpd.pp

Você pode fazer isso com o vsftpd e com nscd, até que seu problema seja de fato resolvido!

Pronto! Simples assim :)

Um outro comando interessante, é o getsebool que pode alterar várias variáveis boolean (on ou off, 0 ou 1, ligado ou desligado) pré-definidas como:

[root@selinux log]# setsebool -P ftp_home_dir 1
[root@selinux log]# setsebool -P allow_ftpd_full_access=1
 
[root@selinux log]# getsebool -a | grep ftp
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> on
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
allow_tftp_anon_write --> off
ftp_home_dir --> on
ftpd_disable_trans --> off
ftpd_is_daemon --> on
httpd_enable_ftp_server --> off
tftpd_disable_trans --> off

Links Úteis para saber mais sobre o SELinux:

http://jczucco.blogspot.com/2009/07/apresentacao-sobre-selinux-no-fisl-10.html
http://magazine.redhat.com/2007/05/04/whats-new-in-selinux-for-red-hat-enterprise-linux-5/
http://fedoraproject.org/wiki/SELinux
http://docs.fedoraproject.org/selinux-faq-fc5/
http://fedoraproject.org/wiki/SELinux/Understanding

3 Responses to “Resolvendo problemas com o SELinux”

  1. FloostNo Gravatar says:

    I really like your blog and i respect your work. I’ll be a frequent visitor.

  2. JohnNo Gravatar says:

    Thank you! You often write very interesting articles. You improved my mood.

  3. Jeronimo Zucco (zucco) 's status on Monday, 17-Aug-09 14:06:32 UTC - Identi.ca says:

    [...] http://everlinux.com/blog/2009/07/28/resolvendo-problemas-com-o-selinux [...]

Leave a Reply