SELinux - Annalyser les problèmes

SELinux fonctionne avec des politiques ciblées qui définissent les actions autorisées. Les politiques décrivent les types de processus, les contextes de fichier ou de port, en posant des étiquettes sur les processus et les ressources et déterminent l’action à autoriser.

S’il n’existe pas d’entrée pour un processus, une ressource ou une action spécifique, l’action demandée sera refusée et la tentative sera écrite dans le fichier logs /var/log/audit/audit.log sous forme de message AVC (Access Vector Cache) et un résumé sera également écrit dans /var/log/messages.

Compréhension des problèmes liés à SELinux

/!\ Avant de modifier la configuration de SELinux il faut savoir que la pluparts des problèmes d’accès surviennent quand des services sont incorrectement implémentés ou quand des applications comportent des politiques incomplètes.

AVC - Access Vector Cache

Lorsque qu’une action est refusée par SELinux, un message AVC (Access Vector Cache) est écrit dans /var/log/audit/audit.log et un résumé AVC est également écrit dans /var/log/messages.

Afficher des évènements

Afficher tous les évènements. Tous les AVC deny sont des refus de SELinux :

# sealert -a /var/log/audit/audit.log
# sealert -a /var/log/audit/audit.log | grep denied
# cat /var/log/audit/audit.log | grep AVC

Trouver l’UUID de l’AVC et avoir une suggestion de résolution :

# tail /var/log/messages
# tail /var/log/messages | grep SELinux

Afficher le détail de l’AVC à l’aide de son UUID :

# sealert -l UUID

La commande ausearch permet de directement rechercher dans /var/log/audit/audit.log et permet aussi d’avoir une suggestion de résolution. L’option -m précise le type de message à rechercher et -ts une unité de temps :

# ausearch -m AVC -ts today

Aide de sealert

Extrait de l’aide :

Usage: sealert [options]

Options:
-h, --help show this help message and exit
-b, --browser Launch the browser
-s, --service Start sealert as a dbus service
-S, --noservice Start sealert without dbus service as stand alone app
-l UUID, --lookupid=UUID Lookup alert by id, id may be wildcard * to lookup all alerts
-a FILE, --analyze=FILE Scan a log file, analyze its AVCs
-u USER, --user=USER logon user name
-p PASWD, --password=PASWD logon user password
-P PLUGIN, --plugin=PLUGIN Plugin Name Required for -f
-f FIX, --fix=FIX fix avc with the given uuid, requires plugin

Régler les problèmes liés à SELinux

Le détail des commandes sealert -l, ausearch -m et des fichiers /var/log/audit/audit.log et /var/log/messages donnent des indications pour résoudre les problèmes liés à SELinux.

Exemple

Il est impossible d’acceder à la page index.html d’un site hébergé sur Apache :

# tail /var/log/audit/audit.log

[...]
type=AVC msg=audit(1955631589.067:212): avc: denied { getattr } for pid=1337 comm="httpd" path="/var/www/html/index.html" dev="vda4" ino=9564266 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:admin_home_t:s0 tclass=file permissive=0
[...]

On cherche l’UUID de l’AVC qui pose problème.

# tail /var/log/messages

[...]
Jul 14 12:23:35 host setroubleshoot[4589]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html.
For complete SELinux messages run: sealert -l 94b82g17-5a83-24df-94bd-ef56ca5b7eb7
[...]

La commande sealert décrit l’événement et précise :

  • le processus concerné
  • le fichier consulté
  • l’action tentée et refusée
  • Des conseils pour corriger l’étiquette du fichier ou comment générer une nouvelle politique pour autoriser l’action refusée
  • La partie Raw Audit Messages donne des informations extraites de /var/log/audit/audit.log
# sealert -l 94b82g17-5a83-24df-94bd-ef56ca5b7eb7

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

***** Plugin catchall_labels (83.8 confidence) suggests *******************

If you want to allow httpd to have getattr access on the index.html file
Then you need to change the label on /my_website/index.html
Do
# semanage fcontext -a -t FILE_TYPE '/my_website/index.html'
where FILE_TYPE is one of the following: NetworkManager_exec_t, NetworkManager_log_t,
NetworkManager_tmp_t, abrt_dump_oops_exec_t, abrt_etc_t, abrt_exec_t, abrt_handle_event_exec_t,
abrt_helper_exec_t, abrt_retrace_coredump_exec_t, abrt_retrace_spool_t, abrt_retrace_worker_exec_t,
abrt_tmp_t, abrt_upload_watch_tmp_t, abrt_var_cache_t, abrt_var_log_t, abrt_var_run_t,
accountsd_exec_t, acct_data_t, acct_exec_t, admin_crontab_tmp_t, admin_passwd_exec_t,
afs_logfile_t, aide_exec_t, aide_log_t, alsa_exec_t, alsa_tmp_t, amanda_exec_t, amanda_log_t,
amanda_recover_exec_t, amanda_tmp_t, amtu_exec_t, anacron_exec_t, anon_inodefs_t

[...]

Additional Information:
Source Context system_u:system_r:httpd_t:s0
Target Context unconfined_u:object_r:default_t:s0
Target Objects /my_website/index.html [ file ]
Source httpd
Source Path /usr/sbin/httpd
Port 443
Host mywebserver1337.com
Source RPM Packages httpd-2.4.51-7.el9_0.x86_64
Target RPM Packages
SELinux Policy RPM selinux-policy-targeted-34.1.27-1.el9.noarch
Local Policy RPM selinux-policy-targeted-34.1.27-1.el9.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name mywebserver1337.com
Platform Linux mywebserver1337.com
5.14.0-70.2.1.el9_0.x86_64 #1 SMP PREEMPT Wed Mar
16 14:18:21 EDT 2023 x86_64 x86_64
Alert Count 4
First Seen 2023-08-14 08:31:18 EDT
Last Seen 2023-08-14 08:42:56 EDT
Local ID 8b56254b-127d-5814-7f1b-8e32a8fc567c

Raw Audit Messages
type=AVC msg=audit(1784622344.312:1206): avc: denied { getattr } for
pid=21278 comm="httpd" path="/my_website/index.html" dev="vda4" ino=24103718
scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0
tclass=file permissive=0

Autre exemple, ici il est précisé le bon contexte à appliquer (httpd_sys_content_t) :

# sealert -l 32a6b565-58942-dadf-5687-cf55bcuaa159

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. The access attempt may have been stopped due to
insufficient permissions to access a parent directory in which case try to change
the following command accordingly.
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:
# ausearch -c 'httpd' --raw | audit2allow -M my-httpd
# semodule -X 300 -i my-httpd.pp
[...]

La commande ausearch permet de directement rechercher dans /var/log/audit/audit.log. L’option -m précise le type de message à rechercher et -ts temps.

Cette commande nous précise l’origine de l’alerte :

  • processus : Web Apache httpd
  • fichier : /my_website/index.html
  • contexte : system_r:httpd_t
# ausearch -m AVC -ts today

[...]

----
time->Thu Apr 7 04:52:13 2023
type=PROCTITLE msg=audit(1784622344.312:1206):
proctitle=2F7573722F7362696E2F6874747064002D44464F524547524F554E44

type=SYSCALL msg=audit(1784622344.312:1206): arch=c000003e syscall=262 success=no
exit=-13 a0=ffffff9c a1=7fefc403d850 a2=7fefc89bc830 a3=100 items=0 ppid=20461
pid=21278 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48
sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd"
subj=system_u:system_r:httpd_t:s0 key=(null)

type=AVC msg=audit(1784622344.312:1206): avc: denied
{ getattr } for pid=21278 comm="httpd" path="/my_website/index.html"
dev="vda4" ino=24103718 scontext=system_u:system_r:httpd_t:s0
tcontext=unconfined_u:object_r:default_t:s0 tclass=file permissive=0

Vérifier en listant le contexte :

# ls -ldZ /my_website/index.html
drwxr-xr-x. 2 root root system_u:object_r:default_t:s0 6 Mar 21 11:47 /my_website/index.html

Application du bon contexte httpd_sys_content_t :

# semanage fcontext -a -t httpd_sys_content_t '/my_website(/.*)?'

Et en restituant le contexte :

# restorecon -Rv /my_website
Relabeled /my_website from unconfined_u:object_r:default_t:s0 to
unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /my_website/index.html from unconfined_u:object_r:default_t:s0 to
unconfined_u:object_r:httpd_sys_content_t:s0

Vérifier en listant le contexte :

# ls -ldZ /my_website/index.html
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 6 Mar 21 11:47 /my_website/index.html

/!\ Résumé des commandes

Commandes

# sealert
# sealert -a /var/log/audit/audit.log // Afficher tous les évènements
# sealert -l <IDError> // Afficher un AVC
# ausearch // Recherche dans le fichier /var/log/audit/audit.log
# ausearch -m AVC -ts today // Recherche les messages (-m) de type AVC today (-ts)

Fichiers

/var/log/audit/audit.log   // logs SELinux (audit : message AVC (Access Vector Cache))
/var/log/messages // avec grep SELinux

Paquets

setroubleshootserver

Cas pratique

Analyse et résolution des problèmes liés à SELinux

La page Web http://SERVER01/index.html renvoie un message d’erreur : Forbidden.

Forbidden

You don't have permission to access this resource.

Afficher les occurences de sealert avec less dans le fichier /var/log/messages. (Symbole / pour faire une recherche, n pour next et q pour quitter) :

[root@SERVER01 ~]# less /var/log/messages
[...]
Apr 7 04:52:18 SERVER01 setroubleshoot[22879]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /MyBlog/index.html.
For complete SELinux messages run: sealert -l 9a96294a-264a-5972-2c6e-59f5ec64d88
[...]

Exécution de la commande sealert suggérée, elle précise que l’étiquette de contexte du fichier /MyBlog/index.html est incorrecte :

[root@SERVER01 ~]# sealert -l 9a96294a-264a-5972-2c6e-59f5ec64d88

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

***** Plugin catchall_experimentsels (83.8 confidence) suggests *******************

If you want to allow httpd to have getattr access on the index.html file
Then you need to change the experimentsel on /MyBlog/index.html
Do
# semanage fcontext -a -t FILE_TYPE '/MyBlog/index.html'
where FILE_TYPE is one of the following: NetworkManager_exec_t,
NetworkManager_log_t, NetworkManager_tmp_t, abrt_dump_oops_exec_t,
abrt_etc_t, abrt_exec_t, abrt_handle_event_exec_t, abrt_helper_exec_t,
abrt_retrace_coredump_exec_t, abrt_retrace_spool_t, abrt_retrace_worker_exec_t,
abrt_tmp_t, abrt_upload_watch_tmp_t, abrt_var_cache_t, abrt_var_log_t,
abrt_var_run_t, accountsd_exec_t, acct_data_t, acct_exec_t, admin_crontab_tmp_t,
admin_passwd_exec_t, afs_logfile_t, aide_exec_t, aide_log_t, alsa_exec_t,
alsa_tmp_t, amanda_exec_t, amanda_log_t, amanda_recover_exec_t, amanda_tmp_t,
[...]

Additional Information:
Source Context system_u:system_r:httpd_t:s0
Target Context unconfined_u:object_r:default_t:s0
Target Objects /MyBlog/index.html [ file ]
Source httpd
Source Path /usr/sbin/httpd
Port <Unknown>
Host SERVER01.my.compagny.org
Source RPM Packages httpd-2.4.51-7.el9_0.x86_64
Target RPM Packages
SELinux Policy RPM selinux-policy-targeted-34.1.27-1.el9.noarch
Local Policy RPM selinux-policy-targeted-34.1.27-1.el9.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name SERVER01.my.compagny.org
Platform Linux SERVER01.my.compagny.org
5.14.0-70.2.1.el9_0.x86_64 #1 SMP PREEMPT Wed Mar
16 18:15:38 EDT 2024 x86_64 x86_64
Alert Count 4
First Seen 2024-09-11 19:58:38 EDT
Last Seen 2024-09-11 19:59:13 EDT
Local ID 9a96294a-264a-5972-2c6e-59f5ec64d88

Raw Audit Messages
type=AVC msg=audit(1649321533.406:1024): avc: denied { getattr } for
pid=20464 comm="httpd" path="/MyBlog/index.html" dev="vda4" ino=25571802
scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0
tclass=file permissive=0
[...]

La section Raw Audit Messages de sealert extrait ses informations de /var/log/audit/audit.log. La commande ausearch permet d’y faire une recherche.
Les options de ausearch :

  • m : recherche sur le type de message
  • ts : précise une date

Ici le processus en cause est le serveur Web Apache httpd, le fichier est /MyBlog/index.html et le contexte est system_r:httpd_t :

[root@SERVER01 ~]# ausearch -m AVC -ts today
[...]
----
time->Thu Apr 7 04:52:13 2022
type=PROCTITLE msg=audit(1649321533.406:1024):
proctitle=2F7573722F7362696E2F6874747064002D44464F524547524F554E44
type=SYSCALL msg=audit(1649321533.406:1024): arch=c000003e syscall=262 success=no
exit=-13 a0=ffffff9c a1=7fefc403d850 a2=7fefc89bc830 a3=100 items=0 ppid=20461
pid=20464 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48
sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd"
subj=system_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1649321533.406:1024): avc: denied // Type AVC pour les messages
{ getattr } for pid=20464 comm="httpd" path="/MyBlog/index.html" // Fichier problématique
dev="vda4" ino=25571802 scontext=system_u:system_r:httpd_t:s0 // Contexte bloquant
tcontext=unconfined_u:object_r:default_t:s0 tclass=file permissive=0

Comparaison du contexte SELinux du répertoire par défaut /var/www/html du service httpd car le contexte httpd_sys_content_t conviendra pour /MyBlog/index.html :

[root@SERVER01 ~]# ls -ldZ /var/www/html
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 6 Sep 11 21:12 /var/www/html

Application du contexte httpd_sys_content_t :

[root@SERVER01 ~]# semanage fcontext -a -t httpd_sys_content_t '/MyBlog(/.*)?'
[root@SERVER01 ~]# restorecon -Rv /MyBlog
Reexperimentseled /MyBlog from unconfined_u:object_r:default_t:s0 to
unconfined_u:object_r:httpd_sys_content_t:s0
Reexperimentseled /MyBlog/index.html from unconfined_u:object_r:default_t:s0 to
unconfined_u:object_r:httpd_sys_content_t:s0

Il sera maintenant possible d’accéder à http://SERVER01/index.html.

Cat's Awesome Page

Hello Cats ! Cats rule the WWW !!! (And #FreePaulWatson)

Gestion de la sécurité avec SELinux

La page Web http://SERVER01/experiments.html. renvoie un message d’erreur : Forbidden.

Forbidden

You don't have permission to access this resource.

Afficher les occurences de sealert avec less dans le fichier /var/log/messages. (Symbole / pour faire une recherche, n pour next et q pour quitter) :

[root@SERVER01 ~]# less /var/log/messages
[...]
Sep 11 22:10:35 SERVER01 setroubleshoot[26509]: failed to retrieve rpm info for /experiments-content/lab.html
Sep 11 22:10:37 SERVER01 setroubleshoot[26509]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /experiments-content/experiments.html.
For complete SELinux messages run: sealert -l 35c9e452-2552-4ca3-8217-493b72ba6d0b
Sep 11 22:10:37 SERVER01 setroubleshoot[26509]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /experiments-content/experiments.html
[...]

Exécution de la commande sealert suggérée, elle précise que l’étiquette de contexte du fichier /MyBlog/index.html est incorrecte :

[root@SERVER01 ~]# sealert -l 35c9e452-2552-4ca3-8217-493b72ba6d0b

SELinux is preventing /usr/sbin/httpd from getattr access on the file /experimentscontent/experiments.html.

***** Plugin catchall_experimentsels (83.8 confidence) suggests *******************
If you want to allow httpd to have getattr access on the experiments.html file
Then you need to change the experimentsel on /experiments-content/experiments.html
Do
# semanage fcontext -a -t FILE_TYPE '/experiments-content/experiments.html'
where FILE_TYPE is one of the following:
[...]

Additional Information:
Source Context system_u:system_r:httpd_t:s0
Target Context unconfined_u:object_r:default_t:s0
Target Objects /experiments-content/experiments.html [ file ]
Source httpd
Source Path /usr/sbin/httpd
Port <Unknown>
Host SERVER01.my.compagny.org
Source RPM Packages httpd-2.4.51-7.el9_0.x86_64
Target RPM Packages
SELinux Policy RPM selinux-policy-targeted-34.1.27-1.el9.noarch
Local Policy RPM selinux-policy-targeted-34.1.27-1.el9.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name SERVER01.my.compagny.org
Platform Linux SERVER01.my.compagny.org
5.14.0-70.2.1.el9_0.x86_64 #1 SMP PREEMPT Wed Mar
16 18:15:38 EDT 2024 x86_64 x86_64
Alert Count 8
First Seen 2024-09-11 22:41:16 EDT
Last Seen 2024-09-11 22:41:19 EDT
Local ID 35c9e452-2552-4ca3-8217-493b72ba6d0b

Raw Audit Messages
type=AVC msg=audit(1649326572.86:407): avc: denied { getattr } for
pid=10731 comm="httpd" path="/experiments-content/experiments.html" dev="vda4" ino=18192752
scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0
tclass=file permissive=0
type=SYSCALL msg=audit(1649326572.86:407): arch=x86_64 syscall=newfstatat
success=no exit=EACCES a0=ffffff9c a1=7f7c8c0457c0 a2=7f7c887f7830 a3=100 items=0
ppid=10641 pid=10731 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48
egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm=httpd exe=/usr/sbin/httpd
subj=system_u:system_r:httpd_t:s0 key=(null)
Hash: httpd,httpd_t,default_t,file,getattr

La section Raw Audit Messages de sealert extrait ses informations de /var/log/audit/audit.log. La commande ausearch permet d’y faire une recherche.
Les options de ausearch :

  • m : recherche sur le type de message
  • ts : précise une date

Ici le processus en cause est le serveur Web Apache httpd, le fichier est /MyBlog/index.html et le contexte est system_r:httpd_t :

[root@SERVER01 ~]# ausearch -m AVC -ts recent
[...]
----
time->Thu Sep 11 22:10:32 2022
type=PROCTITLE msg=audit(1649326572.086:407):
proctitle=2F7573722F7362696E2F6874747064002D44464F524547524F554E44
type=SYSCALL msg=audit(1649326572.086:407): arch=c000003e syscall=262 success=no
exit=-13 a0=ffffff9c a1=7f7c8c0457c0 a2=7f7c887f7830 a3=100 items=0 ppid=10641
pid=10731 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48
sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd"
subj=system_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1649326572.086:407): avc: denied // Type AVC pour les messages
{ getattr } for pid=10731 comm="httpd" path="/experiments-content/experiments.html"// Fichier problématique
dev="vda4" ino=18192752 scontext=system_u:system_r:httpd_t:s0 // Contexte bloquant
tcontext=unconfined_u:object_r:default_t:s0 tclass=file permissive=0

Comparaison du contexte SELinux du répertoire par défaut /var/www/html du service httpd car le contexte httpd_sys_content_t conviendra pour /experiments-content :

[root@SERVER01 ~]# ls -dZ /experiments-content /var/www/html
unconfined_u:object_r:default_t:s0 /experiments-content
system_u:object_r:httpd_sys_content_t:s0 /var/www/html

Application du contexte httpd_sys_content_t :

[root@SERVER01 ~]# semanage fcontext -a -t httpd_sys_content_t '/experiments-content(/.*)?'

[root@SERVER01 ~]# restorecon -R /experiments-content/

Il sera maintenant possible d’accéder à http://SERVER01/experiments.html :

Experiments

Put water on the fire and wait for it to boil. What happens? The fire goes out.

Documentation

man mandb
man sealert
man ausearch
apropos _selinux

> Partager <