#!/bin/posh
# shellcheck disable=1003,1091,2006,2016,2034,2039
# vim: set ts=2 sw=2 sts=2 fdm=marker fmr=#(,#) et:
#
# doc:
#
#  Copy this file to a new one with the same name of the cve to test, all in
# lowercase (i.e.: cve-2014–6271.sh).
#  Then add the code for the functions shown here. **ALL** functions must appear
# in the new created file, however the ones marked as 'optional' can be left
# with the same code than in 'skel.sh'. Inside the function, declare all the
# variables as 'local' (i.e.: local vuln_version="1.2.3")
#
#  NOTE: You can use here, functions and variables implemented in 'lse.sh':
#   * lse_get_pkg_version: Get package version supplying package name
#   * lse_is_version_bigger: Check if version in $1 is bigger than the $2
#   * $lse_arch: System architecture
#   * $lse_distro_codename: The linux distribution code name (ubuntu, debian,
#      opsuse, centos, redhat, fedora)
#   * $lse_linux: Kernel version
#   * Colors
#  XXX: Check the definitions in 'lse.sh' to better understand what they do and
#       how they work
#
################################################################################
## RULES:
##  * Do NOT cause any harm with the tests
##  * Try to be as accurate as possible, trying to detect patched versions from
##    distro package versions. Try to minimize false positives.
##  * The script must be POSIX compliant. Test it with 'posh' shell.
################################################################################


# lse_cve_level: 0 if leads to a privilege escalation; 1 for other CVEs
lse_cve_level=0

# lse_cve_id: CVE id in lowercase (i.e.: cve-2014–6271)
lse_cve_id="cve-2019-5736"

# lse_cve_description: Short. Not more than 52 characters long.
#__________________="vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
lse_cve_description="Escalate in some types of docker containers"

# Code retrieved with 'declare -f' by the packaging bash script
lse_cve_test() { #(
  local vulnerable=false
  local runc_version
  local package_version

  runc_version=$(runc --version | grep -i "runc version" | cut -d" " -f3)
  package_version=$(lse_get_pkg_version runc)

  if [ -n "$runc_version" ] && lse_is_version_bigger "0.1.1" "$runc_version"; then
    vulnerable=true

    case "$lse_distro_codename" in
      ubuntu|debian)
        [ -r "/etc/os-release" ] && distro_release=$(grep -E '^VERSION_CODENAME=' /etc/os-release | cut -f2 -d=)
        case "$distro_release" in
          bionic)
            package_fixed="1.0.0~rc4+dfsg1-6ubuntu0.18.04.1"
            ;;
          cosmic)
            package_fixed="1.0.0~rc4+dfsg1-6ubuntu0.18.10.1"
            ;;
          xenial)
            package_fixed="1.0.0~rc2+docker1.13.1-0ubuntu1~16.04.1"
            ;;
          stretch)
            package_fixed="0.1.1+dfsg1-2+deb9u1"
            ;;
          *) # Other releases not vulnerable
            package_fixed="0"
            ;;
        esac
        ;;
      centos)
        [ -r "/etc/os-release" ] && distro_release=$(grep -E '^VERSION_ID=' /etc/os-release | cut -f2 -d=)
        case "$distro_release" in
          7*)
            package_fixed="1.0.0-60.dev.git2abd837.el7"
            ;;
        esac
        ;;
      opsuse)
        package_version=$(lse_get_pkg_version docker-runc)
        [ -r "/etc/os-release" ] && distro_release=$(grep -E '^VERSION_ID=' /etc/os-release | cut -f2 -d=)
        case "$distro_release" in
          15*)
            package_fixed="1.0.0rc8+gitr3826_425e105d5a03"
            ;;
        esac
        ;;
    esac
    if [ -n "$package_fixed" ] && [ -n "$package_version" ] && ! lse_is_version_bigger "$package_fixed" "$package_version"; then
      # Not Vulnerable
      exit 1
    fi
  fi
  $vulnerable && runc --version
} #)

# Uncomment this line for testing the lse_cve_test function
#lse_NO_EXEC=true . ../lse.sh ; lse_cve_test
