
(Part 1 - SELinux on RedHat based Linux distributions)
Created: 2021-09-21 Tue 18:25


Welcome to part one of our BIND 9 security webinar series














You can check the available Linux security modules available in
your system in the file /sys/kernel/security/lsm:
# cat /sys/kernel/security/lsm lockdown,capability,yama,tomoyo,bpf






DAC is implemented via the Unix file permissions (read-write-execute for owner/group/world)
drwxr-xr-x 147 user admin 4704 Sep 5 22:04 src








unconfined (normal Unix permissions apply)







The command sestatus gives information about the SELinux function of the system
# sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Memory protection checking: actual (secure) Max kernel policy version: 32


disabled: The SELinux modules and policies are not loaded and
not enforced, SELinux labels are not created on new filespermissive: The SELinux modules and policies are loaded, but
they are not enforced. Policy violations will be logged through
the audit subsystem. New files and processes will get SELinux
labelsenforcing: The SELinux system is fully loaded and the policy
will be enforced. New files and processes will get SELinux labels



setenforce 1 will set SELinux into enforcing modesetenforce 0 will set SELinux into permissive mode (turn SELinux security off)


selinux=0 will disable SELinux completely
(requires reboot)/etc/selinux/config# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing [...]


List all SELinux modules of a running system:
# semodule -l abrt accountsd acct afs aiccu [...] bind [...]


To disable just the BIND 9 SELinux module
semodule -d bind
To enable the BIND 9 SELinux module
# semodule -ve bind Attempting to enable module 'bind': Ok: return value of 0. Committing changes: Ok: transaction number 6.


dnf install -y selinux-policy-devel sepolicy manpage -a -p /usr/share/man/man8
bind, the manpage is called
named_selinux. This manpage documents the named process types,
which besides BIND 9 is also used for the Unbound resolver:man named_selinux




named_conf_t are for BIND 9 configuration
files and can only be read by the BIND 9 processes# ls -lZ /etc/named.conf -rw-r-----. 1 root named system_u:object_r:named_conf_t:s0 1705 May 27 20:49 /etc/named.conf # ls -lZ /etc/named.rfc1912.zones -rw-r-----. 1 root named system_u:object_r:named_conf_t:s0 1029 May 27 20:49 /etc/named.rfc1912.zones
etc_t (general Linux configuration files under
/etc) can also be read[root@bind9-selinux ~]# ls -lZ /etc/named.root.key -rw-r--r--. 1 root named system_u:object_r:etc_t:s0 1070 May 27 20:49 /etc/named.root.key


named_zone_t. By default,
the BIND 9 processes can read and write these files.
named_write_master_zones (see below) write
access to these files can be forbidden# ls -lZ /var/named/ total 16 drwxrwx---. 2 named named system_u:object_r:named_cache_t:s0 23 Sep 16 18:39 data drwxrwx---. 2 named named system_u:object_r:named_cache_t:s0 60 Sep 16 18:40 dynamic -rw-r-----. 1 root named system_u:object_r:named_conf_t:s0 2253 May 27 20:49 named.ca -rw-r-----. 1 root named system_u:object_r:named_zone_t:s0 152 May 27 20:49 named.empty -rw-r-----. 1 root named system_u:object_r:named_zone_t:s0 152 May 27 20:49 named.localhost -rw-r-----. 1 root named system_u:object_r:named_zone_t:s0 168 May 27 20:49 named.loopback drwxrwx---. 2 named named system_u:object_r:named_cache_t:s0 6 May 27 20:49 slaves


named_cache_t are dynamic files and can be read
and written by the BIND 9 processes# ls -lZ /var/named/data/ total 4 -rw-r--r--. 1 named named system_u:object_r:named_cache_t:s0 443 Sep 16 18:39 named.run
# ls -lZ /var/named/dynamic/ total 8 -rw-r--r--. 1 named named system_u:object_r:named_cache_t:s0 821 Sep 16 18:40 managed-keys.bind -rw-r--r--. 1 named named system_u:object_r:named_cache_t:s0 512 Sep 16 18:40 managed-keys.bind.jnl


named process is labeled
as named_t:
# ps auxZ | grep named
system_u:system_r:named_t:s0 named 62221 0.0 7.0 265260 58248 ?
Ssl Sep16 0:00 /usr/sbin/named -u named -c /etc/named.conf


named process is
running as type unlabeled_t:
# ps auxZ | grep named
system_u:object_r:unlabeled_t:s0 named 62221 0.0 7.1 265260 59316 ?
Ssl Sep16 0:00 /usr/sbin/named -u named -c /etc/named.conf


/opt/bind/sbin/named is labeled unconfined_t
# ps auxZ | grep named
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 named 111228 0.8 2.3 183672 19736 ?
Ssl 03:53 0:00 /opt/bind/sbin/named -u named -c /etc/named.conf




ausearch can be used to list the policy violation of
a specific process
-m avc list LSM policy violations-x /usr/sbin/named filter for violations of this process-i (interpret) print the data in human readable form
# ausearch -m avc -x /usr/sbin/named -i
----
type=PROCTITLE msg=audit(09/17/2021 04:06:05.836:2278) : proctitle=/usr/sbin/named -u named -c /etc/named.conf
type=SYSCALL msg=audit(09/17/2021 04:06:05.836:2278) : arch=x86_64 syscall=bind success=no exit=EACCES(Permission denied) a0=0x15 a1=0x7f5183d24660 a2=0x10 a3=0x7f5183d244fc items=0 ppid=111613 pid=111615 auid=unset uid=named gid=named euid=named suid=named fsuid=named egid=named sgid=named fsgid=named tty=(none) ses=unset comm=isc-worker0000 exe=/usr/sbin/named subj=system_u:system_r:named_t:s0 key=(null)
type=AVC msg=audit(09/17/2021 04:06:05.836:2278) : avc: denied { name_bind } for pid=111615 comm=isc-worker0000 src=8053 scontext=system_u:system_r:named_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0


/etc and /var/named)

matchpathcon (Match Path Context) will report files
where the file label does not match the SELinux policy
# matchpathcon -V /var/named/named.localhost /var/named/named.localhost has context system_u:object_r:etc_t:s0, should be system_u:object_r:named_zone_t:s0


chcon (change SELinux context) can be used to set the
file label type:chcon --type named_cache_t /var/named/zonefile.db


restorecon will adjust the label on a file so that it
matches the label expected by the SELinux policy# restorecon -v /var/named/named.localhost Relabeled /var/named/named.localhost from system_u:object_r:etc_t:s0 to system_u:object_r:named_zone_t:s0


semanage fcontext -a will add a file context label
to the SELinux policy
restrorecon to relabel the files# semanage fcontext -a -t named_zone_t /srv/bind/zones/primary/example.com.db # restorecon -vr /srv/bind/zones Relabeled /srv/bind/zones/primary/example.com.db from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:named_zone_t:s0


# semanage fcontext -a -t named_zone_t --ftype f "/srv/bind/zones(/.*)?" # semanage fcontext -a -t named_zone_t --ftype d "/srv/bind/zones(/.*)?" # semanage fcontext -a -t named_cont_t --ftype f "/srv/bind/conf(/.*)?" # semanage fcontext -a -t named_conf_t --ftype d "/srv/bind/conf(/.*)?"


# getsebool -a abrt_anon_write --> off abrt_handle_event --> off [...] named_tcp_bind_http_port --> off named_write_master_zones --> on [...]


named.conf
statistics-channels {
inet 192.0.2.0 port 8053 allow { localnets; };
};


8053
# ausearch -m avc -ts recent -i
----
type=PROCTITLE msg=audit(09/17/2021 04:06:05.836:2278) : proctitle=/usr/sbin/named -u named -c /etc/named.conf
type=SYSCALL msg=audit(09/17/2021 04:06:05.836:2278) : arch=x86_64 syscall=bind success=no
exit=EACCES(Permission denied) a0=0x15 a1=0x7f5183d24660 a2=0x10 a3=0x7f5183d244fc
items=0 ppid=111613 pid=111615 auid=unset uid=named gid=named euid=named suid=named
fsuid=named egid=named sgid=named fsgid=named tty=(none) ses=unset
comm=isc-worker0000 exe=/usr/sbin/named subj=system_u:system_r:named_t:s0 key=(null)
type=AVC msg=audit(09/17/2021 04:06:05.836:2278) : avc: denied { name_bind } for pid=111615
comm=isc-worker0000 src=8053 scontext=system_u:system_r:named_t:s0
tcontext=system_u:object_r:unreserved_port_t:s0
tclass=tcp_socket permissive=0


named_tcp_bind_http_port will allow
access to ports that are defined for type http_port_t# setsebool named_tcp_bind_http_port=on
8053 is not among these ports# semanage port -l | grep http_port_t http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000


http_port_t
statistics-channels {
inet * port 8008 allow { any; };
};
http_port_t# semanage port -a -t http_port_t -p tcp 8053 # semanage port -l | grep http_port_t http_port_t tcp 8053, 80, 81, 443, 488, 8008, 8009, 8443, 9000


named_write_master_zones controls if
the BIND 9 processes are permitted to write zone files (files with
the context label named_zone_t)
on by default, writing to zone files is
enabled

# setsebool named_write_master_zones=off


named_selinux Manual page
https://linux.die.net/man/8/named_selinux (the man page on your
system is likely more up-to-date)






