Ensure AppArmor Is Not Disabled at Boot

Description

AppArmor must not be disabled on the kernel command line: no boot entry should carry apparmor=0. Check that the line

GRUB_CMDLINE_LINUX="..."

within /etc/default/grub doesn’t contain the argument apparmor=0. Run the following command to update command line for already installed kernels:

# grubby --update-kernel=ALL --remove-args="apparmor=0"

Rationale

With apparmor=0 on the kernel command line, the kernel boots without AppArmor and loads none of its profiles, so the confinement they describe never applies.

Remediation

Shell script

The following script can be run on the host to remediate the issue.

#!/bin/bash

# Remediation is applicable only in certain platforms
if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then

if /bin/false ; then
    KARGS_DIR="/usr/lib/bootc/kargs.d/"
    find "$KARGS_DIR" -maxdepth 1 -type f -name '*.toml' -exec sed -i -E -e "s/^(\s*kargs\s*=\s*\[.*)\"apparmor=0(=[^\"]*)?\"[,[:space:]]*(.*]\s*)/\1\3/" -e "s/^(\s*kargs.*),\s*\]$/\1\]/" {} +
else


# Correct the form of default kernel command line in GRUB
if grep -q -E '^GRUB_CMDLINE_LINUX=.*apparmor=0=?.*"'  '/etc/default/grub' ; then
       sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)apparmor=0=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
fi
if grep -q -E '^GRUB_CMDLINE_LINUX_DEFAULT=.*apparmor=0=?.*"'  '/etc/default/grub' ; then
       sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)apparmor=0=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
fi
update-grub 

fi

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi