Eigenes Profil erstellen SELinux: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
| (12 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt) | |||
| Zeile 24: | Zeile 24: | ||
*cat /etc/systemd/system/mydaemon.service | *cat /etc/systemd/system/mydaemon.service | ||
<pre> | <pre> | ||
| − | |||
[Unit] | [Unit] | ||
Description=Simple testing daemon | Description=Simple testing daemon | ||
| Zeile 34: | Zeile 33: | ||
[Install] | [Install] | ||
WantedBy=multi-user.target | WantedBy=multi-user.target | ||
| + | |||
| + | </pre> | ||
| + | |||
| + | ==Temporäre Änderung des Kontexts== | ||
| + | chcon -u system_u -t systemd_unit_t mydaemon.service | ||
| + | |||
| + | ==Permanente Änderung des Kontexts== | ||
| + | *semanage fcontext -a -t systemd_unit_t mydaemon.service | ||
| + | *restorecon -v mydaemon.service | ||
| + | |||
| + | =Systemctl status= | ||
| + | *systemctl start mydaemon | ||
| + | *systemctl status mydaemon | ||
| + | =Check that the new daemon is confined by SELinux= | ||
| + | *ps -efZ | grep mydaemon | ||
| + | system_u:system_r:unconfined_service_t:s0 root 5812 1 0 15:41 ? 00:00:00 /usr/local/bin/mydaemon | ||
| + | |||
| + | =Generieren Sie eine benutzerdefinierte Richtlinie für den Daemon= | ||
| + | *sepolicy generate --init /usr/local/bin/mydaemon | ||
| + | Created the following files: | ||
| + | /etc/systemd/system/mydaemon.te # Type Enforcement file | ||
| + | /etc/systemd/system/mydaemon.if # Interface file | ||
| + | /etc/systemd/system/mydaemon.fc # File Contexts file | ||
| + | /etc/systemd/system/mydaemon_selinux.spec # Spec file | ||
| + | /etc/systemd/system/mydaemon.sh # Setup Script | ||
| + | =Erstellen Sie die Systemrichtlinie mit dem neuen Richtlinienmodul mithilfe des mit dem vorherigen Befehl erstellten Setup-Skripts neu= | ||
| + | *./mydaemon.sh | ||
| + | <pre> | ||
| + | Building and Loading Policy | ||
| + | + make -f /usr/share/selinux/devel/Makefile mydaemon.pp | ||
| + | Compiling targeted mydaemon module | ||
| + | Creating targeted mydaemon.pp policy package | ||
| + | rm tmp/mydaemon.mod tmp/mydaemon.mod.fc | ||
| + | + /usr/sbin/semodule -i mydaemon.pp | ||
| + | + sepolicy manpage -p . -d mydaemon_t | ||
| + | ./mydaemon_selinux.8 | ||
| + | + /sbin/restorecon -F -R -v /usr/local/bin/mydaemon | ||
| + | Relabeled /usr/local/bin/mydaemon from unconfined_u:object_r:bin_t:s0 to system_u:object_r:mydaemon_exec_t:s0 | ||
| + | ... | ||
</pre> | </pre> | ||
| + | =Starten Sie den Daemon neu und überprüfen Sie, ob er jetzt von SELinux eingeschränkt ausgeführt wird= | ||
| + | *systemctl restart mydaemon | ||
| + | *ps -efZ | grep mydaemon | ||
| + | =Da der Daemon jetzt von SELinux eingeschränkt wird, verhindert SELinux auch den Zugriff auf /var/log/messages. Zeigen Sie die entsprechende Ablehnungsnachricht an= | ||
| + | *ausearch -m AVC -ts recent | ||
| + | =Weiter Informationen= | ||
| + | *sealert -l "*" | ||
| + | =Vorgeschlagene Änderungen= | ||
| + | *ausearch -m AVC -ts recent | audit2allow -R | ||
| + | =Da die von audit2allow vorgeschlagenen Regeln in bestimmten Fällen falsch sein können, verwenden Sie nur einen Teil der Ausgabe, um die entsprechende Richtlinienschnittstelle zu finden= | ||
| + | *grep -r "logging_write_generic_logs" /usr/share/selinux/devel/include/ | grep .if | ||
| + | /usr/share/selinux/devel/include/system/logging.if:interface(`logging_write_generic_logs', | ||
=Links= | =Links= | ||
*https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_selinux/writing-a-custom-selinux-policy_using-selinux | *https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_selinux/writing-a-custom-selinux-policy_using-selinux | ||
Aktuelle Version vom 23. November 2022, 09:26 Uhr
Eigenes Programm
- cat ~/mydaemon.c
#include <unistd.h>
#include <stdio.h>
FILE *f;
int main(void)
{
while(1) {
f = fopen("/var/log/messages","w");
sleep(5);
fclose(f);
}
}
Kompilieren
- gcc -o mydaemon mydaemon.c
Kopieren
- cp mydaemon /usr/local/sbin
System Dienst
- cat /etc/systemd/system/mydaemon.service
[Unit] Description=Simple testing daemon [Service] Type=simple ExecStart=/usr/local/bin/mydaemon [Install] WantedBy=multi-user.target
Temporäre Änderung des Kontexts
chcon -u system_u -t systemd_unit_t mydaemon.service
Permanente Änderung des Kontexts
- semanage fcontext -a -t systemd_unit_t mydaemon.service
- restorecon -v mydaemon.service
Systemctl status
- systemctl start mydaemon
- systemctl status mydaemon
Check that the new daemon is confined by SELinux
- ps -efZ | grep mydaemon
system_u:system_r:unconfined_service_t:s0 root 5812 1 0 15:41 ? 00:00:00 /usr/local/bin/mydaemon
Generieren Sie eine benutzerdefinierte Richtlinie für den Daemon
- sepolicy generate --init /usr/local/bin/mydaemon
Created the following files: /etc/systemd/system/mydaemon.te # Type Enforcement file /etc/systemd/system/mydaemon.if # Interface file /etc/systemd/system/mydaemon.fc # File Contexts file /etc/systemd/system/mydaemon_selinux.spec # Spec file /etc/systemd/system/mydaemon.sh # Setup Script
Erstellen Sie die Systemrichtlinie mit dem neuen Richtlinienmodul mithilfe des mit dem vorherigen Befehl erstellten Setup-Skripts neu
- ./mydaemon.sh
Building and Loading Policy + make -f /usr/share/selinux/devel/Makefile mydaemon.pp Compiling targeted mydaemon module Creating targeted mydaemon.pp policy package rm tmp/mydaemon.mod tmp/mydaemon.mod.fc + /usr/sbin/semodule -i mydaemon.pp + sepolicy manpage -p . -d mydaemon_t ./mydaemon_selinux.8 + /sbin/restorecon -F -R -v /usr/local/bin/mydaemon Relabeled /usr/local/bin/mydaemon from unconfined_u:object_r:bin_t:s0 to system_u:object_r:mydaemon_exec_t:s0 ...
Starten Sie den Daemon neu und überprüfen Sie, ob er jetzt von SELinux eingeschränkt ausgeführt wird
- systemctl restart mydaemon
- ps -efZ | grep mydaemon
Da der Daemon jetzt von SELinux eingeschränkt wird, verhindert SELinux auch den Zugriff auf /var/log/messages. Zeigen Sie die entsprechende Ablehnungsnachricht an
- ausearch -m AVC -ts recent
Weiter Informationen
- sealert -l "*"
Vorgeschlagene Änderungen
- ausearch -m AVC -ts recent | audit2allow -R
Da die von audit2allow vorgeschlagenen Regeln in bestimmten Fällen falsch sein können, verwenden Sie nur einen Teil der Ausgabe, um die entsprechende Richtlinienschnittstelle zu finden
- grep -r "logging_write_generic_logs" /usr/share/selinux/devel/include/ | grep .if
/usr/share/selinux/devel/include/system/logging.if:interface(`logging_write_generic_logs',