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 :
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 :
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) :
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 :
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)
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
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 :
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 ~]# 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
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 :
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
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 :
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