Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
Description
To set the runtime status of the net.ipv4.conf.default.route_localnet kernel parameter, run the following command:
$ sudo sysctl -w net.ipv4.conf.default.route_localnet=0
To make sure that the setting is persistent, add the following line to a file in the directory /etc/sysctl.d:
net.ipv4.conf.default.route_localnet = 0
Rationale
Refuse the routing of packets whose source or destination address is the local loopback
on interfaces created after boot, which take their settings from the default.
This prohibits the use of network 127/8 for local routing purposes.
Enabling route_localnet can expose applications listening on localhost to external traffic.
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 rpm --quiet -q kernel-core; then
#
# Set sysctl config file which to save the desired value
#
SYSCONFIG_FILE='/etc/sysctl.d/net_ipv4_conf_default_route_localnet.conf'
# Comment out any occurrences of net.ipv4.conf.default.route_localnet from /etc/sysctl.d/*.conf files
for f in /etc/sysctl.d/*.conf /run/sysctl.d/*.conf /usr/local/lib/sysctl.d/*.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 net.ipv4.conf.default.route_localnet 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 '^(?!#)(?!net.ipv4.conf.default.route_localnet\b).*[\s]*net.ipv4.conf.default.route_localnet.*$' $f | uniq )
else
matching_list=$(grep -P '^(?!#).*[\s]*net.ipv4.conf.default.route_localnet.*$' $f | uniq )
fi
if ! test -z "$matching_list"; then
while IFS= read -r entry; do
escaped_entry=$(sed -e 's|/|\\/|g' <<< "$entry")
# comment out "net.ipv4.conf.default.route_localnet" matches to preserve user data
sed -i --follow-symlinks "s/^${escaped_entry}$/# &/g" $f
done <<< "$matching_list"
fi
done
#
# Set runtime for net.ipv4.conf.default.route_localnet
#
if ! { rpm --quiet -q kernel bootc && ! rpm --quiet -q openshift-kubelet && { [ -f "/run/.containerenv" ] || [ -f "/.containerenv" ]; }; } ; then
/sbin/sysctl -q -n -w net.ipv4.conf.default.route_localnet="0"
fi
#
# If net.ipv4.conf.default.route_localnet present in /etc/sysctl.conf, change value to "0"
# else, add "net.ipv4.conf.default.route_localnet = 0" to /etc/sysctl.conf
#
if [ -f /etc/sysctl.conf ]; then
sed -i "/^net.ipv4.conf.default.route_localnet/d" /etc/sysctl.conf
fi
# 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' <<< "^net.ipv4.conf.default.route_localnet")
# shellcheck disable=SC2059
printf -v formatted_output "%s = %s" "$stripped_key" "0"
# 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 "^net.ipv4.conf.default.route_localnet\\>" "${SYSCONFIG_FILE}"; then
escaped_formatted_output=$(sed -e 's|/|\\/|g' <<< "$formatted_output")
LC_ALL=C sed -i --follow-symlinks "s/^net.ipv4.conf.default.route_localnet\\>.*/$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
cce="CCE-87291-1"
printf '# Per %s: Set %s in %s\n' "${cce}" "${formatted_output}" "${SYSCONFIG_FILE}" >> "${SYSCONFIG_FILE}"
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:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Set fact for sysctl paths
ansible.builtin.set_fact:
sysctl_paths:
- /etc/sysctl.d/
- /run/sysctl.d/
- /usr/local/lib/sysctl.d/
when: '"kernel-core" in ansible_facts.packages'
tags:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Find all files that contain net.ipv4.conf.default.route_localnet
ansible.builtin.shell:
cmd: |-
set -o pipefail
find -L {{ sysctl_paths | join(" ") }} -type f -name '*.conf' | xargs grep -HP '^\s*net.ipv4.conf.default.route_localnet\s*=\s*.*$'
executable: /bin/bash
register: find_all_values
check_mode: false
changed_when: false
failed_when: false
when: '"kernel-core" in ansible_facts.packages'
tags:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Find all files that set net.ipv4.conf.default.route_localnet to correct value
ansible.builtin.shell:
cmd: |-
set -o pipefail
find -L {{ sysctl_paths | join(" ") }} -type f -name '*.conf' | xargs grep -HP '^\s*net.ipv4.conf.default.route_localnet\s*=\s*0$'
executable: /bin/bash
register: find_correct_value
check_mode: false
changed_when: false
failed_when: false
when: '"kernel-core" in ansible_facts.packages'
tags:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Comment out any occurrences of net.ipv4.conf.default.route_localnet from config
files
ansible.builtin.replace:
path: '{{ item | split(":") | first }}'
regexp: ^[\s]*net.ipv4.conf.default.route_localnet
replace: '#net.ipv4.conf.default.route_localnet'
loop: '{{ find_all_values.stdout_lines }}'
when:
- '"kernel-core" 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:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Check if /etc/sysctl.conf exists
ansible.builtin.stat:
path: /etc/sysctl.conf
register: sysctl_conf_stat
when: '"kernel-core" in ansible_facts.packages'
tags:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Comment out any occurrences of net.ipv4.conf.default.route_localnet from /etc/sysctl.conf
ansible.builtin.replace:
path: /etc/sysctl.conf
regexp: ^[\s]*net.ipv4.conf.default.route_localnet
replace: '#net.ipv4.conf.default.route_localnet'
when:
- '"kernel-core" in ansible_facts.packages'
- sysctl_conf_stat.stat.exists
tags:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet
- name: Prevent Routing External Traffic to Local Loopback on IPv4 Interfaces by Default
- Ensure sysctl net.ipv4.conf.default.route_localnet is set to 0
ansible.posix.sysctl:
name: net.ipv4.conf.default.route_localnet
value: '0'
sysctl_file: /etc/sysctl.d/net_ipv4_conf_default_route_localnet.conf
state: present
reload: true
when: '"kernel-core" in ansible_facts.packages'
tags:
- CCE-87291-1
- disable_strategy
- low_complexity
- medium_disruption
- medium_severity
- reboot_required
- sysctl_net_ipv4_conf_default_route_localnet