(Part 2 - AppArmor, SecompBPF and Firejail)
Created: 2021-10-20 Wed 18:25
Welcome to part two of our BIND 9 security webinar series
named
process is monitored for restarts
aa-status
can be used to check if AppArmor is active
(example from a default Debian 11 install, the named
profile is
loaded and AppArmor is active)# aa-status apparmor module is loaded. 8 profiles are loaded. 8 profiles are in enforce mode. /usr/bin/man firejail-default lsb_release man_filter man_groff named nvidia_modprobe nvidia_modprobe//kmod 0 profiles are in complain mode. 0 processes have profiles defined. 0 processes are in enforce mode. 0 processes are in complain mode. 0 processes are unconfined but have a profile defined.
auditd
service installed and
running# apt install auditd
/var/log/audit/audit.log
ausearch
tool# grep -i denied /var/log/audit/audit.log type=AVC msg=audit(1634287113.597:118): apparmor="DENIED" operation="open" profile="named" name="/srv/bind/zones/example.com" pid=41687 comm="isc-worker0000" requested_mask="r" denied_mask="r" fsuid=106 ouid=0FSUID="bind" OUID="root"
-Z
parameter to the ps
command can be used to list
processes that are secured by AppArmor:# ps -efZ | grep -v unconfined LABEL UID PID PPID C STIME TTY TIME CMD named (enforce) bind 41687 1 0 10:38 ? 00:00:00 /usr/sbin/named -f -u bind
aa-unconfined
) can be found in
the Debian packet apparmor-utils
apt install apparmor-utils
aa-unconfined
will check for running processes
listening on a network socket and will print their AppArmor
security status:# aa-unconfined 451 /usr/sbin/dhclient (/sbin/dhclient) not confined 675 /usr/sbin/sshd (sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups) not confined 41687 /usr/sbin/named confined by 'named (enforce)'
aa-complain
, aa-enforce
, aa-disable
and aa-audit
can be used to switch the AppArmor mode on a program.
# aa-complain /usr/sbin/named Setting /usr/sbin/named to complain mode. Warning: profile named represents multiple programs # systemctl restart bind9
/etc/apparmor.d/usr.sbin.named
:# vim:syntax=apparmor # Last Modified: Fri Jun 1 16:43:22 2007 #include <tunables/global> profile named /usr/sbin/named flags=(attach_disconnected) { #include <abstractions/base> #include <abstractions/nameservice> capability net_bind_service, capability setgid, capability setuid, [...] # /etc/bind should be read-only for bind # /var/lib/bind is for dynamically updated zone (and journal) files. # /var/cache/bind is for slave/stub data, since we're not the origin of it. # See /usr/share/doc/bind9/README.Debian.gz /etc/bind/** r, /var/lib/bind/** rw, /var/lib/bind/ rw, /var/cache/bind/** lrw, /var/cache/bind/ rw, # Database file used by allow-new-zones /var/cache/bind/_default.nzd-lock rwk, [...]
named
and it is for the process started
from /usr/sbin/named
attach_disconnected
flag tells AppArmor how to handle file
access to disconnected files. Disconnected files are open files
where the application still has access to the file handle, but
the file cannot be looked up by name anymore.profile named /usr/sbin/named flags=(attach_disconnected) {
named
processcapability net_bind_service, # open a network socket capability setgid, # change the group ID of the process capability setuid, # change the user ID of the process capability sys_chroot, # use the chroot syscall capability sys_resource, # change resource restrictions on the process
/etc/bind/** r, # read access including sub-directories /var/lib/bind/** rw, # read/write access incl. sub-directories /var/lib/bind/ rw, # read/write access for this directory /var/cache/bind/** lrw, # read/write and link permission /var/cache/bind/ rw, # read/write access for this directory
/etc/apparmor.d/local/usr.sbin.named
. This file is
included at the end of the mail profile:#include <local/usr.sbin.named> }
aa-logprof
can be used to find AppArmor permission
issues and to create an extension to the AppArmor profile:# aa-logprof Reading log entries from /var/log/audit/audit.log. Updating AppArmor profiles in /etc/apparmor.d. Complain-mode changes: Profile: named Path: /srv/bind/zones/example.com New Mode: r Severity: 4 [1 - #include <abstractions/ubuntu-browsers.d/user-files>] 2 - /srv/bind/zones/example.com r, (A)llow / [(D)eny] / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Audi(t) / Abo(r)t / (F)inish
/etc/apparmor.d/local
Adding /srv/bind/zones/example.com r, to profile. Enforce-mode changes: = Changed Local Profiles = The following local profiles were changed. Would you like to save them? [1 - named] (S)ave Changes / Save Selec(t)ed Profile / [(V)iew Changes] / View Changes b/w (C)lean profiles / Abo(r)t
/etc/apparmor.d/local/usr.sbin.named
allows the
BIND 9 process named
to read files below /srv/bind/zones/
:/srv/bind/zones/** r,
/usr/local
or
/opt
aa-exec
can be used to start any binary
under the control of AppArmor# aa-exec -p named /opt/bind/sbin/named -f -u bind
aa-exec
# ps auxZ | grep named named (enforce) bind 102252 0.4 0.9 241200 19388 pts/9 Sl+ 12:39 0:00 /opt/bind/sbin/named -f -u bind unconfined root 102257 0.0 0.0 6256 720 pts/3 S+ 12:39 0:00 grep named
firejail
contains the Firejail command line
tools, firejail-profiles
contains the security profiles for
popular applications# apt install firejail firejail-profiles
/etc/firejail/firejail.config
/etc/firejail
# ls -l /etc/firejail/ | head total 4460 -rw-r--r-- 1 root root 1146 Feb 27 2021 0ad.profile -rw-r--r-- 1 root root 841 Feb 27 2021 2048-qt.profile -rw-r--r-- 1 root root 310 Feb 27 2021 7za.profile -rw-r--r-- 1 root root 310 Feb 27 2021 7z.profile -rw-r--r-- 1 root root 310 Feb 27 2021 7zr.profile -rw-r--r-- 1 root root 906 Feb 27 2021 abiword.profile -rw-r--r-- 1 root root 521 Feb 27 2021 abrowser.profile -rw-r--r-- 1 root root 258 Feb 27 2021 acat.profile -rw-r--r-- 1 root root 260 Feb 27 2021 adiff.profile
dig
, host
and nslookup
, but
not for the BIND 9 processfirejail --list
can be used to list all processes that are protected by Firejail# firejail --list 49757:root::firejail named
firejail --top
will display the resource usage of all
processes running under FirejailPID User RES(KiB) SHR(KiB) CPU% Prcs Uptime Command 49757 root 17740 8324 0.0 3 00:02:50 firejail named -f -u bind
firejail --netstats
will display network statistics
for all processes running under Firejailfiremon
can be used to monitor the execution of a
process under Firejail control:# firemon 12:25:10 exec 49413 (root) NEW SANDBOX: firejail /usr/sbin/named -g 12:25:10 fork 49413 (root) firejail /usr/sbin/named -g child 49414 firejail /usr/sbin/named -g 12:25:10 fork 49413 (root) firejail /usr/sbin/named -g child 49415 firejail /usr/sbin/named -g 12:25:10 exit 49415 (root) 12:25:10 fork 49414 (root) firejail /usr/sbin/named -g child 49416 firejail /usr/sbin/named -g 12:25:10 exec 49416 (root) /run/firejail/lib/fseccomp protocol build inet,inet6 /run/firejail/mnt/seccomp/seccomp.protocol 12:25:10 exit 49416 (root) 12:25:10 fork 49414 (root) firejail /usr/sbin/named -g child 49417 firejail /usr/sbin/named -g 12:25:10 exit 49417 (root) 12:25:10 fork 49414 (root) firejail /usr/sbin/named -g child 49418 firejail /usr/sbin/named -g 12:25:10 exec 49418 (root) /run/firejail/lib/fseccomp drop /run/firejail/mnt/seccomp/seccomp /run/firejail/mnt/seccomp/seccomp.postexec _sysctl,acct,add_key, ...
systemd
can apply many security measures to processes
started from Systemd unitssystemd-analyze security
can be used to check the
security score on a Systemd unit# systemd-analyze security bind9 NAME DESCRIPTION EXPOSURE ✗ PrivateNetwork= Service has access to the host's network 0.5 ✗ User=/DynamicUser= Service runs as root user 0.4 ✗ CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP) Service may change UID/GID identities/capabilities 0.3 ✗ CapabilityBoundingSet=~CAP_SYS_ADMIN Service has administrator privileges 0.3 ✗ CapabilityBoundingSet=~CAP_SYS_PTRACE Service has ptrace() debugging abilities 0.3 ✗ RestrictAddressFamilies=~AF_(INET|INET6) Service may allocate Internet sockets 0.3 ✗ RestrictNamespaces=~CLONE_NEWUSER Service may create user namespaces 0.3 ✗ RestrictAddressFamilies=~… Service may allocate exotic sockets 0.3 ✗ CapabilityBoundingSet=~CAP_(CHOWN|FSETID|SETFCAP) Service may change file ownership/access mode/capabilities unrestricted 0.2 [...] ✗ CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG Service may issue vhangup() 0.1 ✗ CapabilityBoundingSet=~CAP_WAKE_ALARM Service may program timers that wake up the system 0.1 ✗ RestrictAddressFamilies=~AF_UNIX Service may allocate local sockets 0.1 ✗ ProcSubset= Service has full access to non-process /proc files (/proc subset=) 0.1 → Overall exposure level for named.service: 9.6 UNSAFE 😨
The command systemd-cgtop
Control Group Tasks %CPU Memory Input/s Output/s / 104 100.4 1.6G 0B 290.5K system.slice 35 75.9 200.5M - - system.slice/named.service 5 29.0 30.1M - - system.slice/crowdsec.service 7 25.9 37.6M - - user.slice 14 23.3 1.3G - - user.slice/user-0.slice 14 23.3 1.3G - -