Verify Permissions on SSH Server Private *_key Key Files for CIS Benchmark

Description

SSH server private keys, the files that match the /etc/ssh/*_key glob, have to have restricted permissions, as the CIS Benchmarks require. If those files are owned by the root user and the root group, they have to have the 0600 permission or stricter. If they are owned by the root user and the dedicated group ssh_keys, they can have the 0640 permission or stricter.

Rationale

If an unauthorized user obtains the private SSH host key file, the host could be impersonated.

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 rpm --quiet -q kernel-core; then

for keyfile in /etc/ssh/*_key; do
    test -f "$keyfile" || continue
    case "$(stat -c "%U:%G" "$keyfile")" in
    root:root)
        chmod u-xs,g-xwrs,o-xwrt "$keyfile"
        ;;
    root:ssh_keys)
        chmod u-xs,g-xws,o-xwrt "$keyfile"
        ;;
    *)
        echo "Key-like file '$keyfile' is owned by an unexpected user:group combination"
        ;;
    esac
done

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-89887-4
  - configure_strategy
  - file_permissions_sshd_private_key_cis
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Verify Permissions on SSH Server Private *_key Key Files for CIS Benchmark
    - Find root:root-owned keys
  ansible.builtin.command: find -H /etc/ssh/ -maxdepth 1 -user root -regex ".*_key$"
    -type f -group root -perm /u+xs,g+xwrs,o+xwrt
  register: root_owned_keys
  changed_when: false
  failed_when: false
  check_mode: false
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-89887-4
  - configure_strategy
  - file_permissions_sshd_private_key_cis
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Verify Permissions on SSH Server Private *_key Key Files for CIS Benchmark
    - Set permissions for root:root-owned keys
  ansible.builtin.file:
    path: '{{ item }}'
    mode: u-xs,g-xwrs,o-xwrt
    state: file
  with_items:
  - '{{ root_owned_keys.stdout_lines }}'
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-89887-4
  - configure_strategy
  - file_permissions_sshd_private_key_cis
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Verify Permissions on SSH Server Private *_key Key Files for CIS Benchmark
    - Find root:ssh_keys-owned keys
  ansible.builtin.command: find -H /etc/ssh/ -maxdepth 1 -user root -regex ".*_key$"
    -type f -group ssh_keys -perm /u+xs,g+xws,o+xwrt
  register: dedicated_group_owned_keys
  changed_when: false
  failed_when: false
  check_mode: false
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-89887-4
  - configure_strategy
  - file_permissions_sshd_private_key_cis
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Verify Permissions on SSH Server Private *_key Key Files for CIS Benchmark
    - Set permissions for root:ssh_keys-owned keys
  ansible.builtin.file:
    path: '{{ item }}'
    mode: u-xs,g-xws,o-xwrt
    state: file
  with_items:
  - '{{ dedicated_group_owned_keys.stdout_lines }}'
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-89887-4
  - configure_strategy
  - file_permissions_sshd_private_key_cis
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

Warning

Remediation is not possible at bootable container build time because SSH host keys are generated post-deployment.