Ensure the System Wide Crypto Policy Disables CBC Ciphers for SSH

Description

The system wide crypto policy has to disable the Cipher Block Chaining (CBC) mode ciphers for SSH. The rule reads the resolved policy that update-crypto-policies writes to /etc/crypto-policies/state/CURRENT.pol when it applies a policy. In that file, the cipher@libssh, cipher@openssh, cipher@openssh-client and cipher@openssh-server lines give the ciphers of the SSH back-ends where they differ from the global cipher line. A cipher@openssh line covers both the OpenSSH client and server, and an SSH back-end that no SSH cipher line covers uses the global cipher line. The file has to exist, and either its global cipher line lists no CBC cipher, or its SSH cipher lines cover libssh, the OpenSSH client and the OpenSSH server and none of them lists a CBC cipher. To disable the CBC ciphers for SSH, add the following line to /etc/crypto-policies/policies/modules/NO-SSHCBC.pmod:

cipher@SSH = -*-CBC

Then add the NO-SSHCBC subpolicy to the system wide crypto policy, together with the subpolicies already in use, for example:

$ sudo update-crypto-policies --set DEFAULT:NO-SSHCBC

Rationale

SSH messages encrypted with a cipher in CBC mode are vulnerable to an attack that recovers up to 32 bits of plaintext from an arbitrary block of ciphertext (CVE-2008-5161).

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 crypto-policies; then

current_pol=/etc/crypto-policies/state/CURRENT.pol
ssh_scope='^[[:space:]]*cipher@(lib|open)ssh(-server|-client)?[[:space:]]*='

# cipher@openssh covers both the OpenSSH client and server, and an SSH
# back-end that no SSH cipher line covers uses the global cipher line.
if [ ! -f "$current_pol" ] ||
    grep -Eiq "${ssh_scope}[^#]*-CBC\b" "$current_pol" ||
    { grep -Eiq '^[[:space:]]*cipher[[:space:]]*=[^#]*-CBC\b' "$current_pol" &&
        { ! grep -Eiq '^[[:space:]]*cipher@libssh[[:space:]]*=' "$current_pol" ||
            ! grep -Eiq '^[[:space:]]*cipher@openssh(-client)?[[:space:]]*=' "$current_pol" ||
            ! grep -Eiq '^[[:space:]]*cipher@openssh(-server)?[[:space:]]*=' "$current_pol"; }; }; then
    module=/etc/crypto-policies/policies/modules/NO-SSHCBC.pmod
    # End the module with a newline, so that the line appended below starts
    # a line of its own.
    [ -z "$(tail -c 1 "$module" 2>/dev/null)" ] || echo >>"$module"
    grep -qsxF -- 'cipher@SSH = -*-CBC' "$module" || echo 'cipher@SSH = -*-CBC' >>"$module"
    chmod 0644 "$module"
    # Apply NO-SSHCBC last, after any subpolicy that enables what it disables.
    crypto_policy=$(update-crypto-policies --show | sed -e 's/$/:/' -e 's/:NO-SSHCBC:/:/g' -e 's/:$//')
    update-crypto-policies --set "$crypto_policy:NO-SSHCBC"
fi

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