#!/bin/bash

# Copyright (c) 2022  Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

VERSION="1.1"

# Warning! Be sure to download the latest version of this script from its primary source:

BULLETIN="https://access.redhat.com/security/vulnerabilities/RHSB-2022-002"

# DO NOT blindly trust any internet sources and NEVER do `curl something | bash`!

# This script is meant for simple detection of the vulnerability. Feel free to modify it for your
# environment or needs. For more advanced detection, consider Red Hat Insights:
# https://access.redhat.com/products/red-hat-insights#getstarted

# Checking against the list of vulnerable packages is necessary because of the way how features
# are back-ported to older versions of packages in various channels.


VULNERABLE_VERSIONS_KERNEL=(
    '4.18.0-80.1.2.el8_0'
    '4.18.0-80.4.2.el8_0'
    '4.18.0-80.7.1.el8_0'
    '4.18.0-80.7.2.el8_0'
    '4.18.0-80.11.1.el8_0'
    '4.18.0-80.11.2.el8_0'
    '4.18.0-80.15.1.el8_0'
    '4.18.0-80.16.1.el8_0'
    '4.18.0-80.18.1.el8_0'
    '4.18.0-80.23.2.el8_0'
    '4.18.0-80.27.1.el8_0'
    '4.18.0-80.27.2.el8_0'
    '4.18.0-80.29.1.el8_0'
    '4.18.0-80.30.1.el8_0'
    '4.18.0-80.31.1.el8_0'
    '4.18.0-80.el8'
    '4.18.0-147.0.2.el8_1'
    '4.18.0-147.0.3.el8_1'
    '4.18.0-147.3.1.el8_1'
    '4.18.0-147.5.1.el8_1'
    '4.18.0-147.8.1.el8_1'
    '4.18.0-147.13.2.el8_1'
    '4.18.0-147.20.1.el8_1'
    '4.18.0-147.24.2.el8_1'
    '4.18.0-147.27.1.el8_1'
    '4.18.0-147.32.1.el8_1'
    '4.18.0-147.34.1.el8_1'
    '4.18.0-147.38.1.el8_1'
    '4.18.0-147.43.1.el8_1'
    '4.18.0-147.44.1.el8_1'
    '4.18.0-147.48.1.el8_1'
    '4.18.0-147.51.1.el8_1'
    '4.18.0-147.51.2.el8_1'
    '4.18.0-147.52.1.el8_1'
    '4.18.0-147.54.2.el8_1'
    '4.18.0-147.56.1.el8_1'
    '4.18.0-147.57.1.el8_1'
    '4.18.0-147.58.1.el8_1'
    '4.18.0-147.59.1.el8_1'
    '4.18.0-147.el8'
    '4.18.0-193.1.2.el8_2'
    '4.18.0-193.6.3.el8_2'
    '4.18.0-193.12.1.el8_2'
    '4.18.0-193.13.2.el8_2'
    '4.18.0-193.14.3.el8_2'
    '4.18.0-193.19.1.el8_2'
    '4.18.0-193.23.1.el8_2'
    '4.18.0-193.24.1.el8_2.dt1'
    '4.18.0-193.28.1.el8_2'
    '4.18.0-193.29.1.el8_2'
    '4.18.0-193.37.1.el8_2'
    '4.18.0-193.40.1.el8_2'
    '4.18.0-193.41.1.el8_2'
    '4.18.0-193.46.1.el8_2'
    '4.18.0-193.47.1.el8_2'
    '4.18.0-193.51.1.el8_2'
    '4.18.0-193.56.1.el8_2'
    '4.18.0-193.60.2.el8_2'
    '4.18.0-193.64.1.el8_2'
    '4.18.0-193.65.2.el8_2'
    '4.18.0-193.68.1.el8_2'
    '4.18.0-193.70.1.el8_2'
    '4.18.0-193.71.1.el8_2'
    '4.18.0-193.75.1.el8_2'
    '4.18.0-193.el8'
    '4.18.0-221.el8'
    '4.18.0-232.el8'
    '4.18.0-234.el8'
    '4.18.0-235.el8'
    '4.18.0-240.1.1.el8_3'
    '4.18.0-240.8.1.el8_3'
    '4.18.0-240.10.1.el8_3'
    '4.18.0-240.15.1.el8_3'
    '4.18.0-240.22.1.el8_3'
    '4.18.0-240.23.2.el8_3'
    '4.18.0-240.el8'
    '4.18.0-293.el8'
    '4.18.0-304.el8'
    '4.18.0-305.3.1.el8_4'
    '4.18.0-305.7.1.el8_4'
    '4.18.0-305.10.2.el8_4'
    '4.18.0-305.12.1.el8_4'
    '4.18.0-305.17.1.el8_4'
    '4.18.0-305.19.1.el8_4'
    '4.18.0-305.25.1.el8_4'
    '4.18.0-305.28.1.el8_4'
    '4.18.0-305.30.1.el8_4'
    '4.18.0-305.34.2.el8_4'
    '4.18.0-305.el8'
    '4.18.0-339.el8'
    '4.18.0-348.2.1.el8_5'
    '4.18.0-348.7.1.el8_5'
    '4.18.0-348.12.2.el8_5'
    '4.18.0-348.el8'

    '4.18.0-80.1.2.rt9.145.el8_0'
    '4.18.0-80.4.2.rt9.152.el8_0'
    '4.18.0-80.7.1.rt9.153.el8_0'
    '4.18.0-80.7.2.rt9.154.el8_0'
    '4.18.0-80.11.1.rt9.156.el8_0'
    '4.18.0-80.11.2.rt9.157.el8_0'
    '4.18.0-80.rt9.138.el8'
    '4.18.0-147.0.2.rt24.94.el8_1'
    '4.18.0-147.0.3.rt24.95.el8_1'
    '4.18.0-147.3.1.rt24.96.el8_1'
    '4.18.0-147.5.1.rt24.98.el8_1'
    '4.18.0-147.8.1.rt24.101.el8_1'
    '4.18.0-147.rt24.93.el8'
    '4.18.0-193.1.2.rt13.53.el8_2'
    '4.18.0-193.6.3.rt13.59.el8_2'
    '4.18.0-193.12.1.rt13.63.el8_2'
    '4.18.0-193.13.2.rt13.65.el8_2'
    '4.18.0-193.14.3.rt13.67.el8_2'
    '4.18.0-193.19.1.rt13.70.el8_2'
    '4.18.0-193.24.1.rt13.74.el8_2.dt1'
    '4.18.0-193.28.1.rt13.77.el8_2'
    '4.18.0-193.29.1.rt13.78.el8_2'
    '4.18.0-193.37.1.rt13.87.el8_2'
    '4.18.0-193.40.1.rt13.90.el8_2'
    '4.18.0-193.41.1.rt13.91.el8_2'
    '4.18.0-193.46.1.rt13.96.el8_2'
    '4.18.0-193.47.1.rt13.97.el8_2'
    '4.18.0-193.51.1.rt13.101.el8_2'
    '4.18.0-193.56.1.rt13.106.el8_2'
    '4.18.0-193.60.2.rt13.112.el8_2'
    '4.18.0-193.64.1.rt13.115.el8_2'
    '4.18.0-193.65.2.rt13.117.el8_2'
    '4.18.0-193.68.1.rt13.118.el8_2'
    '4.18.0-193.70.1.rt13.120.el8_2'
    '4.18.0-193.71.1.rt13.121.el8_2'
    '4.18.0-193.75.1.rt13.125.el8_2'
    '4.18.0-193.rt13.51.el8'
    '4.18.0-221.rt7.33.el8'
    '4.18.0-232.rt7.44.el8'
    '4.18.0-234.rt7.46.el8'
    '4.18.0-235.rt7.48.el8'
    '4.18.0-240.1.1.rt7.55.el8_3'
    '4.18.0-240.8.1.rt7.62.el8_3'
    '4.18.0-240.10.1.rt7.64.el8_3'
    '4.18.0-240.15.1.rt7.69.el8_3'
    '4.18.0-240.22.1.rt7.77.el8_3'
    '4.18.0-240.23.2.rt7.79.el8_3'
    '4.18.0-240.rt7.54.el8'
    '4.18.0-293.rt7.59.el8'
    '4.18.0-304.rt7.71.el8'
    '4.18.0-305.3.1.rt7.75.el8_4'
    '4.18.0-305.7.1.rt7.79.el8_4'
    '4.18.0-305.10.2.rt7.83.el8_4'
    '4.18.0-305.12.1.rt7.84.el8_4'
    '4.18.0-305.17.1.rt7.89.el8_4'
    '4.18.0-305.19.1.rt7.91.el8_4'
    '4.18.0-305.25.1.rt7.97.el8_4'
    '4.18.0-305.28.1.rt7.100.el8_4'
    '4.18.0-305.30.1.rt7.102.el8_4'
    '4.18.0-305.34.2.rt7.107.el8_4'
    '4.18.0-305.rt7.72.el8'
    '4.18.0-339.rt7.120.el8'
    '4.18.0-348.2.1.rt7.132.el8_5'
    '4.18.0-348.7.1.rt7.137.el8_5'
    '4.18.0-348.12.2.rt7.143.el8_5'
    '4.18.0-348.rt7.130.el8'
)

KPATCH_MODULE_NAMES=(

)



basic_args() {
    # Parses basic commandline arguments and sets basic environment.
    #
    # Args:
    #     parameters - an array of commandline arguments
    #
    # Side effects:
    #     Exits if --help parameters is used
    #     Sets COLOR constants and debug variable

    local parameters=( "$@" )

    RED="\\033[1;31m"
    YELLOW="\\033[1;33m"
    GREEN="\\033[1;32m"
    BOLD="\\033[1m"
    RESET="\\033[0m"
    for parameter in "${parameters[@]}"; do
        if [[ "$parameter" == "-h" || "$parameter" == "--help" ]]; then
            echo "Usage: $( basename "$0" ) [-n | --no-colors] [-d | --debug]"
            exit 1
        elif [[ "$parameter" == "-n" || "$parameter" == "--no-colors" ]]; then
            RED=""
            # shellcheck disable=SC2034
            YELLOW=""
            GREEN=""
            BOLD=""
            RESET=""
        elif [[ "$parameter" == "-d" || "$parameter" == "--debug" ]]; then
            debug=true
        fi
    done
}


basic_reqs() {
    # Prints common disclaimer and checks basic requirements.
    #
    # Args:
    #     CVE - string printed in the disclaimer
    #
    # Side effects:
    #     Exits when 'rpm' command is not available

    local CVE="$1"

    # Disclaimer
    echo
    echo -e "${BOLD}This script (v$VERSION) is primarily designed to detect $CVE on supported"
    echo -e "Red Hat Enterprise Linux systems and kernel packages."
    echo -e "Result may be inaccurate for other RPM based systems."
    echo -e "Result may be inaccurate for affected RPM packages not compiled by Red Hat.${RESET}"
    echo

    # RPM is required
    if ! command -v rpm &> /dev/null; then
        echo "'rpm' command is required, but not installed. Exiting."
        exit 1
    fi
}


check_supported_kernel() {
    # Checks if running kernel is supported.
    #
    # Args:
    #     running_kernel - kernel string as returned by 'uname -r'
    #
    # Side effects:
    #     Exits when running kernel is obviously not supported

    local running_kernel="$1"

    # Check supported platform
    if [[ "$running_kernel" != *".el"[6-8]* ]]; then
        echo -e "${RED}This script is meant to be used only on RHEL 6, 7 and 8.${RESET}"
        echo
        echo -e "Follow $BULLETIN for advice."
        exit 1
    fi
}


get_rhel() {
    # Gets RHEL number.
    #
    # Args:
    #     running_kernel - kernel string as returned by 'uname -r'
    #
    # Prints:
    #     RHEL number, e.g. '5', '6', '7', or '8'

    local running_kernel="$1"

    local rhel
    rhel=$( sed -r -n 's/^.*el([[:digit:]]).*$/\1/p' <<< "$running_kernel" )
    echo "$rhel"
}


check_kernel() {
    # Checks kernel if it is in list of vulnerable kernels.
    #
    # Args:
    #     running_kernel - kernel string as returned by 'uname -r'
    #     vulnerable_versions - an array of vulnerable versions
    #
    # Prints:
    #     Vulnerable kernel string as returned by 'uname -r', or nothing

    local running_kernel="$1"
    shift
    local vulnerable_versions=( "$@" )

    for tested_kernel in "${vulnerable_versions[@]}"; do
        if [[ "$running_kernel" == *"$tested_kernel"* ]]; then
            echo "$running_kernel"
            break
        fi
    done
}


check_kpatch() {
    # Checks if specific kpatch listed in a kpatch list is applied.
    #
    # Args:
    #     kpatch_module_names - an array of kpatches
    #
    # Prints:
    #     Found kpatch, or nothing

    local kpatch_module_names=( "$@" )
    local modules

    # Get loaded kernel modules
    modules=$( lsmod )

    # Check if kpatch is installed
    for tested_kpatch in "${kpatch_module_names[@]}"; do
        if [[ "$modules" == *"$tested_kpatch"* ]]; then
            echo "$tested_kpatch"
            break
        fi
    done
}


set_default_values() {
    result=0
}


parse_facts() {
    # Gathers all available information and stores it in global variables. Only store facts and
    # do not draw conclusion in this function for better maintainability.
    #
    # Side effects:
    #     Sets many global boolean flags and content variables
    :
}

draw_conclusions() {
    # Draws conclusions based on available system data.
    #
    # Side effects:
    #     Sets many global boolean flags and content variables

    kpatch_installed=$( check_kpatch "${KPATCH_MODULE_NAMES[@]}" )
    vulnerable_kernel=$( check_kernel "$running_kernel" "${VULNERABLE_VERSIONS_KERNEL[@]}" )


    if [[ ! "$kpatch_installed" && "$vulnerable_kernel" ]]; then
        (( result |= 2 ))
    fi
}


debug_print() {
    # Prints selected variables when debugging is enabled.

    variables=( running_kernel rhel kpatch_installed vulnerable_kernel )
    for variable in "${variables[@]}"; do
        echo "$variable = *${!variable}*"
    done
    echo
}


if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
    basic_args "$@"
    basic_reqs "CVE-2022-0847"
    running_kernel=$( uname -r )
    check_supported_kernel "$running_kernel"

    rhel=$( get_rhel "$running_kernel" )
    if (( rhel == 5 )); then
        export PATH="/sbin:/usr/sbin:$PATH"
    fi

    set_default_values
    parse_facts
    draw_conclusions

    # Debug prints
    if [[ "$debug" ]]; then
        debug_print
    fi

    # Results
    echo -e "This system is running kernel ${BOLD}${running_kernel}${RESET}"
    if [[ "$kpatch_installed" ]]; then
        echo -e "Detected kpatch: ${BOLD}${kpatch_installed}${RESET}"
    fi
    echo

    if (( result != 0 )); then
        echo -e "${RED}This system is affected by CVE-2022-0847.${RESET}"
    else
        echo -e "${GREEN}This system is NOT affected by CVE-2022-0847.${RESET}"
    fi

    exit "$result"
fi
