Enable Randomized Layout of Virtual Address Space
Description
To set the runtime status of the kernel.randomize_va_space
kernel parameter, run the following command:
$ sudo sysctl -w kernel.randomize_va_space=2
To make sure that the setting is persistent, add the following line to a file in the directory /etc/sysctl.d
:
kernel.randomize_va_space = 2
Rationale
Address space layout randomization (ASLR) makes it more difficult for an
attacker to predict the location of attack code they have introduced into a
process’s address space during an attempt at exploitation. Additionally,
ASLR makes it more difficult for an attacker to know the location of
existing code in order to re-purpose it using return oriented programming
(ROP) techniques.
Shell script
The following script can be run on the host to remediate the issue.
# Remediation is applicable only in certain platforms
if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
# Comment out any occurrences of kernel.randomize\_va\_space from /etc/sysctl.d/\*.conf files
for f in /etc/sysctl.d/\*.conf /run/sysctl.d/\*.conf /usr/local/lib/sysctl.d/\*.conf /usr/lib/sysctl.d/\*.conf; do
matching\_list=$(grep -P '^(?!#).\*[\s]\*kernel.randomize\_va\_space.\*$' $f | uniq )
if ! test -z "$matching\_list"; then
while IFS= read -r entry; do
escaped\_entry=$(sed -e 's|/|\\/|g' <<< "$entry")
# comment out "kernel.randomize\_va\_space" matches to preserve user data
sed -i "s/^${escaped\_entry}$/# &/g" $f
done <<< "$matching\_list"
fi
done
#
# Set sysctl config file which to save the desired value
#
SYSCONFIG\_FILE="/etc/sysctl.conf"
#
# Set runtime for kernel.randomize\_va\_space
#
/sbin/sysctl -q -n -w kernel.randomize\_va\_space="2"
#
# If kernel.randomize\_va\_space present in /etc/sysctl.conf, change value to "2"
# else, add "kernel.randomize\_va\_space = 2" 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.randomize\_va\_space")
# shellcheck disable=SC2059
printf -v formatted\_output "%s = %s" "$stripped\_key" "2"
# 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.randomize\_va\_space\\>" "${SYSCONFIG\_FILE}"; then
escaped\_formatted\_output=$(sed -e 's|/|\\/|g' <<< "$formatted\_output")
LC\_ALL=C sed -i --follow-symlinks "s/^kernel.randomize\_va\_space\\>.\*/$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: List /etc/sysctl.d/\*.conf files
find:
paths:
- /etc/sysctl.d/
- /run/sysctl.d/
- /usr/local/lib/sysctl.d/
- /usr/lib/sysctl.d/
contains: ^[\s]\*kernel.randomize\_va\_space.\*$
patterns: '\*.conf'
file\_type: any
register: find\_sysctl\_d
when: ansible\_virtualization\_type not in ["docker", "lxc", "openvz", "podman", "container"]
tags:
- DISA-STIG-UBTU-20-010448
- NIST-800-171-3.1.7
- NIST-800-53-CM-6(a)
- NIST-800-53-SC-30
- NIST-800-53-SC-30(2)
- PCI-DSS-Req-2.2.1
- PCI-DSSv4-2.2.3
- disable\_strategy
- low\_complexity
- medium\_disruption
- medium\_severity
- reboot\_required
- sysctl\_kernel\_randomize\_va\_space
- name: Comment out any occurrences of kernel.randomize\_va\_space from config files
replace:
path: '{{ item.path }}'
regexp: ^[\s]\*kernel.randomize\_va\_space
replace: '#kernel.randomize\_va\_space'
loop: '{{ find\_sysctl\_d.files }}'
when: ansible\_virtualization\_type not in ["docker", "lxc", "openvz", "podman", "container"]
tags:
- DISA-STIG-UBTU-20-010448
- NIST-800-171-3.1.7
- NIST-800-53-CM-6(a)
- NIST-800-53-SC-30
- NIST-800-53-SC-30(2)
- PCI-DSS-Req-2.2.1
- PCI-DSSv4-2.2.3
- disable\_strategy
- low\_complexity
- medium\_disruption
- medium\_severity
- reboot\_required
- sysctl\_kernel\_randomize\_va\_space
- name: Ensure sysctl kernel.randomize\_va\_space is set to 2
sysctl:
name: kernel.randomize\_va\_space
value: '2'
sysctl\_file: /etc/sysctl.conf
state: present
reload: true
when: ansible\_virtualization\_type not in ["docker", "lxc", "openvz", "podman", "container"]
tags:
- DISA-STIG-UBTU-20-010448
- NIST-800-171-3.1.7
- NIST-800-53-CM-6(a)
- NIST-800-53-SC-30
- NIST-800-53-SC-30(2)
- PCI-DSS-Req-2.2.1
- PCI-DSSv4-2.2.3
- disable\_strategy
- low\_complexity
- medium\_disruption
- medium\_severity
- reboot\_required
- sysctl\_kernel\_randomize\_va\_space