Enable kernel.apparmor_restrict_unprivileged_unconfined
Description
To set the runtime status of the kernel.apparmor_restrict_unprivileged_unconfined kernel parameter, run the following command:
$ sudo sysctl -w kernel.apparmor_restrict_unprivileged_unconfined=1
To make sure that the setting is persistent, add the following line to a file in the directory /etc/sysctl.d:
kernel.apparmor_restrict_unprivileged_unconfined = 1
Rationale
Restricting unprivileged unconfined processes with AppArmor reduces the
attack surface available to local users and helps enforce additional
kernel-level hardening.
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 dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$'; then
#
# Set sysctl config file which to save the desired value
#
SYSCONFIG_FILE="/etc/sysctl.conf"
# Comment out any occurrences of kernel.apparmor_restrict_unprivileged_unconfined from /etc/sysctl.d/*.conf files
for f in /etc/sysctl.d/*.conf /run/sysctl.d/*.conf /usr/local/lib/sysctl.d/*.conf /etc/ufw/sysctl.conf; do
# skip systemd-sysctl symlink (/etc/sysctl.d/99-sysctl.conf -> /etc/sysctl.conf)
if [[ "$(readlink -f "$f")" == "/etc/sysctl.conf" ]]; then continue; fi
if [[ "$(readlink -f "$f")" == "$(readlink -f "${SYSCONFIG_FILE}")" ]]; then
# entries starting with kernel.apparmor_restrict_unprivileged_unconfined in the file this remediation writes to
# are replaced in place below, commenting them out would make the remediation non-idempotent
matching_list=$(grep -P '^(?!#)(?!kernel.apparmor_restrict_unprivileged_unconfined\b).*[\s]*kernel.apparmor_restrict_unprivileged_unconfined.*$' $f | uniq )
else
matching_list=$(grep -P '^(?!#).*[\s]*kernel.apparmor_restrict_unprivileged_unconfined.*$' $f | uniq )
fi
if ! test -z "$matching_list"; then
while IFS= read -r entry; do
escaped_entry=$(sed -e 's|/|\\/|g' <<< "$entry")
# comment out "kernel.apparmor_restrict_unprivileged_unconfined" matches to preserve user data
sed -i --follow-symlinks "s/^${escaped_entry}$/# &/g" $f
done <<< "$matching_list"
fi
done
sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value='1'
#
# Set runtime for kernel.apparmor_restrict_unprivileged_unconfined
#
if ! /bin/false ; then
/sbin/sysctl -q -n -w kernel.apparmor_restrict_unprivileged_unconfined="$sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value"
fi
#
# If kernel.apparmor_restrict_unprivileged_unconfined present in /etc/sysctl.conf, change value to appropriate value
# else, add "kernel.apparmor_restrict_unprivileged_unconfined = value" to /etc/sysctl.conf
#
# Strip any search characters in the key arg so that the key can be replaced without
# adding any search characters to the config file.
stripped_key=$(sed 's/[\^=\$,;+]*//g' <<< "^kernel.apparmor_restrict_unprivileged_unconfined")
# shellcheck disable=SC2059
printf -v formatted_output "%s = %s" "$stripped_key" "$sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value"
# If the key exists, change it. Otherwise, add it to the config_file.
# We search for the key string followed by a word boundary (matched by \>),
# so if we search for 'setting', 'setting2' won't match.
if LC_ALL=C grep -q -m 1 -i -e "^kernel.apparmor_restrict_unprivileged_unconfined\\>" "${SYSCONFIG_FILE}"; then
escaped_formatted_output=$(sed -e 's|/|\\/|g' <<< "$formatted_output")
LC_ALL=C sed -i --follow-symlinks "s/^kernel.apparmor_restrict_unprivileged_unconfined\\>.*/$escaped_formatted_output/gi" "${SYSCONFIG_FILE}"
else
if [[ -s "${SYSCONFIG_FILE}" ]] && [[ -n "$(tail -c 1 -- "${SYSCONFIG_FILE}" || true)" ]]; then
LC_ALL=C sed -i --follow-symlinks '$a'\\ "${SYSCONFIG_FILE}"
fi
printf '%s\n' "$formatted_output" >> "${SYSCONFIG_FILE}"
fi
else
>&2 echo 'Remediation is not applicable, nothing was done'
fi
Ansible playbook
The following playbook can be run with Ansible to remediate the issue.
- name: Gather the package facts
package_facts:
manager: auto
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined
- name: XCCDF Value sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value # promote to variable
set_fact:
sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value: !!str 1
tags:
- always
- name: Enable kernel.apparmor_restrict_unprivileged_unconfined - Set fact for sysctl
paths
ansible.builtin.set_fact:
sysctl_paths:
- /etc/sysctl.d/
- /run/sysctl.d/
- /usr/local/lib/sysctl.d/
when: '"linux-base" in ansible_facts.packages'
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined
- name: Enable kernel.apparmor_restrict_unprivileged_unconfined - Find all files that
contain kernel.apparmor_restrict_unprivileged_unconfined
ansible.builtin.shell:
cmd: |-
set -o pipefail
find -L {{ sysctl_paths | join(" ") }} -type f -name '*.conf' | xargs grep -HP '^\s*kernel.apparmor_restrict_unprivileged_unconfined\s*=\s*.*$'
executable: /bin/bash
register: find_all_values
check_mode: false
changed_when: false
failed_when: false
when: '"linux-base" in ansible_facts.packages'
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined
- name: Enable kernel.apparmor_restrict_unprivileged_unconfined - Find all files that
set kernel.apparmor_restrict_unprivileged_unconfined to correct value
ansible.builtin.shell:
cmd: |-
set -o pipefail
find -L {{ sysctl_paths | join(" ") }} -type f -name '*.conf' | xargs grep -HP '^\s*kernel.apparmor_restrict_unprivileged_unconfined\s*=\s*{{ sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value }}$'
executable: /bin/bash
register: find_correct_value
check_mode: false
changed_when: false
failed_when: false
when: '"linux-base" in ansible_facts.packages'
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined
- name: Enable kernel.apparmor_restrict_unprivileged_unconfined - Comment out any
occurrences of kernel.apparmor_restrict_unprivileged_unconfined from config files
ansible.builtin.replace:
path: '{{ item | split(":") | first }}'
regexp: ^[\s]*kernel.apparmor_restrict_unprivileged_unconfined
replace: '#kernel.apparmor_restrict_unprivileged_unconfined'
loop: '{{ find_all_values.stdout_lines }}'
when:
- '"linux-base" in ansible_facts.packages'
- find_correct_value.stdout_lines | length == 0 or find_all_values.stdout_lines
| length > find_correct_value.stdout_lines | length
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined
- name: Enable kernel.apparmor_restrict_unprivileged_unconfined - Comment out any
occurrences of kernel.apparmor_restrict_unprivileged_unconfined from /etc/ufw/sysctl.conf
ansible.builtin.replace:
path: /etc/ufw/sysctl.conf
regexp: (^[\s]*kernel.apparmor_restrict_unprivileged_unconfined.*$)
replace: '# \1'
when: '"linux-base" in ansible_facts.packages'
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined
- name: Enable kernel.apparmor_restrict_unprivileged_unconfined - Ensure sysctl kernel.apparmor_restrict_unprivileged_unconfined
is set
ansible.posix.sysctl:
name: kernel.apparmor_restrict_unprivileged_unconfined
value: '{{ sysctl_kernel_apparmor_restrict_unprivileged_unconfined_value }}'
sysctl_file: /etc/sysctl.conf
state: present
reload: true
when: '"linux-base" in ansible_facts.packages'
tags:
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_kernel_apparmor_restrict_unprivileged_unconfined