{{tag>Brouillon Sécurité CA}} # Notes SELinux Voir : * SELinux coloring book * SELinux notebook * `/sys/kernel/security/lsm` * https://debian-handbook.info/browse/fr-FR/stable/sect.selinux.html * https://linuxhint.com/selinux_debian_10/ * https://blog.microlinux.fr/selinux/ * https://people.redhat.com/duffy/selinux/selinux-coloring-book_A4-Stapled.pdf * http://people.redhat.com/tcameron/summit2010/selinux/SELinuxMereMortals.pdf * http://people.redhat.com/tcameron/Summit2016/selinux/selinux_for_mere_mortals.pdf * ausearch * SEtroubleshoot * https://github.com/centreon/centreon-plugins/tree/develop/selinux * Ansible https://docs.ansible.com/ansible/2.9/modules/list_of_system_modules.html * https://web.archive.org/web/20170326131853/http://www.lurking-grue.org/writingselinuxpolicyHOWTO.html * setroubleshootd * system-config-selinux * https://hacktricks.wiki/en/linux-hardening/privilege-escalation/selinux.html ## Définition Security-Enhanced Linux (SELinux) is a Linux kernel mechanism that label sever y process and every file system object on the system. A SELinux policy defines the rules on how labeled processes interact with label objects. Source : https://edu.anarcho-copy.org/GNU%20Linux%20-%20Unix-Like/Podman/podman-action.pdf ## Autoriser avec audit2allow ~~~bash sudo cat /var/log/audit/audit.log | grep AVC | grep denied | audit2why > audit2why.txt sudo cat /var/log/audit/audit.log | grep AVC | grep denied | audit2allow > audit2allow.txt ~~~ ~~~bash /usr/bin/audit2allow -a -M plop ~~~ Copier le contexte par défaut d'une arborescence ~~~bash # Example - podman - additional image stores sudo semanage fcontext -a -e /var/lib/containers/storage /var/sharedstore sudo restorecon -r /var/sharedstore/ ~~~ ## Restaurer les autorisations SELinux ~~~bash # Voir les différences restorecon -nv /etc/passwd # Les remettre restorecon -v /etc/passwd # Exemple restorecon -v /usr/sbin/httpd restorecon -v /etc/ld.so.cache restorecon -v /etc/hosts ~~~ Au démarrage du système ~~~bash touch /.autorelabel ~~~ Réétiquetés les fichiers lors du prochain redémarrage \\ Cela crée le fichier `/.autorelabel` contenant l'option `-F` ~~~bash fixfiles -F onboot ~~~ Passez toujours en mode permissif avant d'entrer dans la commande fixfiles -F onboot. Cela permet d'éviter que le système ne démarre pas s'il contient des fichiers non étiquetés. Pour plus d'informations, voir [RHBZ#2021835](https://bugzilla.redhat.com/show_bug.cgi?id=2021835). Voir aussi `/etc/selinux/restorecond.conf` qui contient les chemins des fichiers à restaurer ## En vrac ~~~bash ls -z ps -eZ sestatus setenforce 0 getenforce semanage login -l chcon seinfo audit2allow ~~~ ~~~bash dnf install -y container-selinux ~~~ ~~~bash semodule --list-modules=full chcon system_u:object_r:bin_t /opt/nagios/agent/nrpe chcon -t sudo_exec_t /usr/bin/sudo ~~~ ~~~bash grep -w 'denied.*insights-client.*permissive=0' /var/log/audit/audit.log ~~~ Serveur Web ~~~bash setsebool -P httpd_can_network_connect 1 ~~~ ## Pb Exemple Err `Permission denied` Before: ~~~ # ls -lZ /opt/plop/plop/plop.jar -rwxrwxrwx. 1 app_plop appgrp unconfined_u:object_r:unlabeled_t:s0 61558236 Feb 26 2023 /opt/plop/plop/plop.jar ~~~ After: ~~~ # ls -lZ /opt/plop/plop/plop.jar -rwxrwxrwx. 1 app_plop appgrp unconfined_u:object_r:bin_t:s0 61558236 Feb 26 2023 /opt/plop/plop/plop.jar ~~~ ### Service SystemD ~~~bash chmod 640 /usr/local/lib/systemd/system/plop.service restorecon -v /usr/local/lib/systemd/system/plop.service systemctl daemon-reload chcon system_u:object_r:bin_t:s0 /tools/list/plop/ctmagt/ctm/scripts/start-ag chcon system_u:object_r:bin_t:s0 /tools/list/plop/ctmagt/ctm/scripts/shut-ag ~~~ ## Containers ~~~python DEFAULT_CONTAINER_RUN_OPTIONS = ['--network', 'slirp4netns:enable_ipv6=true', '--security-opt', 'label=disable'] ~~~ Voir : https://ansible.readthedocs.io/projects/awx/en/24.6.1/administration/instances.html ### Autres Checking /etc/selinux/targeted/contexts/files/file_contexts, I found out which additional selinux contexts I had to add to the newly created directories: ~~~bash sudo semanage fcontext --add --type container_ro_file_t '/var/lib/containers/user/[^/]+/storage/overlay(/.*)?' sudo semanage fcontext --add --type container_ro_file_t '/var/lib/containers/user/[^/]+/storage/overlay2(/.*)?' sudo semanage fcontext --add --type container_ro_file_t '/var/lib/containers/user/[^/]+/storage/overlay2-images(/.*)?' sudo semanage fcontext --add --type container_ro_file_t '/var/lib/containers/user/[^/]+/storage/overlay2-layers(/.*)?' sudo semanage fcontext --add --type container_ro_file_t '/var/lib/containers/user/[^/]+/storage/overlay-layers(/.*)?' sudo semanage fcontext --add --type container_ro_file_t '/var/lib/containers/user/[^/]+/storage/overlay-images(/.*)?' sudo semanage fcontext --add --type container_file_t '/var/lib/containers/user/[^/]+/storage/volumes/[^/]*/.* ~~~ then, reapply them to the system: ~~~bash sudo restorecon -RvF /var/lib/containers/user ~~~ Voir https://kcore.org/2023/12/13/adventures-with-rootless-containers/ ## Autres Voir : * sealert ~~~bash ausearch -c '(ssh)' --raw | audit2allow -M my-ssh semodule -X 300 -i my-ssh.pp ~~~ ~~~ Dec 11 18:23:34 srv1 setroubleshoot[13728]: SELinux interdit à bash d'utiliser l'accès read sur le fichier /usr/lib/x86_64-linux-gnu/libc.so.6. Pour des messages SELinux exhaustifs, lancez sealert -l 4c092563-ff9e-461c-a202-f8b30a83315f Dec 11 18:23:34 srv1 setroubleshoot[13728]: SELinux interdit à bash d'utiliser l'accès read sur le fichier /usr/lib/x86_64-linux-gnu/libc.so.6. ***** Le greffon restorecon (54.2 de confiance) suggère ******************* Si vous souhaitez corriger l'étiquette. L'étiquette par défaut de /usr/lib/x86_64-linux-gnu/libc.so.6 devrait être lib_t. Alors vous pouvez lancer restorecon. La tentative d’accès pourrait avoir été stoppée due à des permissions insuffisantes d’accès au dossier parent, auquel cas essayez de changer la commande suivante en conséquence. Faire # /sbin/restorecon -v /usr/lib/x86_64-linux-gnu/libc.so.6 ***** Le greffon file (16.6 de confiance) suggère ************************* Ceci est dû à la création d'un nouveau système de fichiers. Alors vous devez y ajouter des étiquettes. Faire /sbin/restorecon -R -v /usr/lib/x86_64-linux-gnu/libc.so.6 ***** Le greffon file (16.6 de confiance) suggère ************************* Si vous pensez que ceci est dû à une machine mal étiquetée. Alors vous devez complètement ré-étiqueter. Faire touch /.autorelabel; reboot ***** Le greffon catchall_labels (3.18 de confiance) suggère ************** Si vous souhaitez autoriser bash à accéder à read sur libc.so.6 file Alors l'étiquette sur /usr/lib/x86_64-linux-gnu/libc.so.6 doit être modifiée Faire # semanage fcontext -a -t FILE_TYPE '/usr/lib/x86_64-linux-gnu/libc.so.6' ~~~ ## SELinux PAM `/etc/pam.d/remote` ~~~ #%PAM-1.0 auth required pam_securetty.so auth include system-auth account required pam_nologin.so account include system-auth password include system-auth # pam_selinux.so close should be the first session rule session required pam_selinux.so close session optional pam_keyinit.so force revoke session required pam_loginuid.so session include system-auth session optional pam_console.so # pam_selinux.so open should only be followed by sessions to be executed in the user context session required pam_selinux.so open ~~~