Blame SOURCES/check_caveats

c59b13
#! /bin/bash -eu
c59b13
c59b13
# Script for checking various microcode caveats
c59b13
#
c59b13
#
c59b13
# SPDX-License-Identifier: CC0-1.0
c59b13
c59b13
: ${MC_CAVEATS_DATA_DIR=/usr/share/microcode_ctl/ucode_with_caveats}
c59b13
: ${FW_DIR=/lib/firmware}
c59b13
: ${CFG_DIR=/etc/microcode_ctl/ucode_with_caveats}
c59b13
c59b13
usage() {
078ac8
	echo 'Usage: check_caveats [-d] [-e] [-k TARGET_KVER] [-c CONFIG]'
078ac8
	echo '                     [-m] [-v]'
c59b13
	echo
078ac8
	echo '   -d - enables disclaimer printing mode'
c59b13
	echo '   -e - check for early microcode load possibility (instead of'
c59b13
	echo '        late microcode load)'
c59b13
	echo '   -k - target version to check against, $(uname -r) is used'
c59b13
	echo '        otherwise'
c59b13
	echo '   -c - caveat config(s) to check, all configs are checked'
c59b13
	echo '        otherwise'
c59b13
	echo '   -m - check that caveats actually apply to the current model'
c59b13
	echo '   -v - verbose output'
c59b13
	echo
c59b13
	echo 'Environment:'
c59b13
	echo '  MC_CAVEATS_DATA_DIR - directory that contains caveats'
c59b13
	echo '                        configuration data'
c59b13
}
c59b13
c59b13
debug() { [ 0 = "$verbose" ] || echo "$*" >&2; }
c59b13
c59b13
# A simplified RPM version comparison that takes into account knowledge about
c59b13
# Y- and Z-streams (so it compares versions inside Y-stram or Z-stream if
c59b13
# the version against which comparison is performed has appropriate versioning
c59b13
# scheme).
c59b13
#
c59b13
# $1 - kernel version to check
c59b13
# $* - list of kernel versions to check against
c59b13
check_kver()
c59b13
{
c59b13
	local t_major= t_minor= t_patch= t_y= t_z1= t_z2= t_rest=
c59b13
	local m_major= m_minor= m_patch= m_y= m_z1= m_z2= m_rest=
c59b13
	local cmp_type=
c59b13
c59b13
	# IFS=.- read -r t_major t_minor t_patch t_y t_z1 t_z2 t_rest <<<"$1"
c59b13
	# "cannot create temp file for here-document: Read-only file system"
c59b13
	# that's why we can't have nice things.
c59b13
	t_major=${1%%.*}
c59b13
	t_rest=${1#${t_major}}
c59b13
	t_rest=${t_rest#.}
c59b13
	t_minor=${t_rest%%.*}
c59b13
	t_rest=${t_rest#${t_minor}}
c59b13
	t_rest=${t_rest#.}
c59b13
	t_patch=${t_rest%%-*}
c59b13
	t_rest=${t_rest#${t_patch}}
c59b13
	t_rest=${t_rest#-}
c59b13
	t_y=${t_rest%%.*}
c59b13
	t_rest=${t_rest#${t_y}}
c59b13
	t_rest=${t_rest#.}
c59b13
	t_z1=${t_rest%%.*}
c59b13
	t_rest=${t_rest#${t_z1}}
c59b13
	t_rest=${t_rest#.}
c59b13
	t_z2=${t_rest%%.*}
c59b13
c59b13
	# minor/major/patch/y should be numeric
c59b13
	[ -n "${t_major##*[!0-9]*}" ] || return 1
c59b13
	[ -n "${t_minor##*[!0-9]*}" ] || return 1
c59b13
	[ -n "${t_patch##*[!0-9]*}" ] || return 1
c59b13
	[ -n "${t_y##*[!0-9]*}" ] || return 1
c59b13
	# reset z1/z2 to zero if non-numeric
c59b13
	[ -n "${t_z1##*[!0-9]*}" ] || t_z1=0
c59b13
	[ -n "${t_z2##*[!0-9]*}" ] || t_z2=0
c59b13
c59b13
	while [ 1 -lt "$#" ]; do
c59b13
		cmp_type=upstream
c59b13
c59b13
		shift
c59b13
		m_major=${1%%.*}
c59b13
		m_rest=${1#${m_major}}
c59b13
		m_rest=${m_rest#.}
c59b13
		m_minor=${m_rest%%.*}
c59b13
		m_rest=${m_rest#${m_minor}}
c59b13
		m_rest=${m_rest#.}
c59b13
		m_patch=${m_rest%%-*}
c59b13
		m_rest=${m_rest#${m_patch}}
c59b13
		m_rest=${m_rest#-}
c59b13
		m_y=${m_rest%%.*}
c59b13
		m_rest=${m_rest#${m_y}}
c59b13
		m_rest=${m_rest#.}
c59b13
		m_z1=${m_rest%%.*}
c59b13
		m_rest=${m_rest#${m_z1}}
c59b13
		m_rest=${m_rest#.}
c59b13
		m_z2=${m_rest%%.*}
c59b13
c59b13
		# minor/major/patch should be numeric
c59b13
		[ -n "${m_major##*[!0-9]*}" ] || continue
c59b13
		[ -n "${m_minor##*[!0-9]*}" ] || continue
c59b13
		[ -n "${m_patch##*[!0-9]*}" ] || continue
c59b13
		# reset z1/z2 to zero if non-numeric
c59b13
		[ -n "${m_y##*[!0-9]*}" ] && cmp_type=y || m_y=0
c59b13
		[ -n "${m_z1##*[!0-9]*}" ] && cmp_type=z || m_z1=0
c59b13
		[ -n "${m_z2##*[!0-9]*}" ] && cmp_type=z || m_z2=0
c59b13
c59b13
		# Comparing versions
c59b13
		case "$cmp_type" in
c59b13
		upstream)
c59b13
			[ "$t_major" -ge "$m_major" ] || continue
c59b13
			[ "$t_minor" -ge "$m_minor" ] || continue
c59b13
			[ "$t_patch" -ge "$m_patch" ] || continue
c59b13
			return 0
c59b13
			;;
c59b13
		y)
c59b13
			[ "$t_major" -eq "$m_major" ] || continue
c59b13
			[ "$t_minor" -eq "$m_minor" ] || continue
c59b13
			[ "$t_patch" -eq "$m_patch" ] || continue
c59b13
			[ "$t_y" -ge "$m_y" ] || continue
c59b13
			return 0
c59b13
			;;
c59b13
		z)
c59b13
			[ "$t_major" -eq "$m_major" ] || continue
c59b13
			[ "$t_minor" -eq "$m_minor" ] || continue
c59b13
			[ "$t_patch" -eq "$m_patch" ] || continue
c59b13
			[ "$t_y" -eq "$m_y" ] || continue
c59b13
			[ "$t_z1" -ge "$m_z1" ] || continue
c59b13
			[ "$t_z2" -ge "$m_z2" ] || continue
c59b13
			return 0
c59b13
			;;
c59b13
		esac
c59b13
	done
c59b13
c59b13
	return 1
c59b13
}
c59b13
c59b13
# Provides model in format "VENDOR_ID FAMILY-MODEL-STEPPING"
c59b13
#
c59b13
# We check only the first processor as we don't expect non-symmetrical setups
c59b13
# with CPUs with caveats
c59b13
get_model_string()
c59b13
{
c59b13
	/usr/bin/printf "%s %02x-%02x-%02x" \
c59b13
		$(/bin/sed -rn '1,/^$/{
c59b13
			s/^vendor_id[[:space:]]*: (.*)$/\1/p;
c59b13
			s/^cpu family[[:space:]]*: (.*)$/\1/p;
c59b13
			s/^model[[:space:]]*: (.*)$/\1/p;
c59b13
			s/^stepping[[:space:]]*: (.*)$/\1/p;
c59b13
		}' /proc/cpuinfo)
c59b13
}
c59b13
c59b13
get_model_name()
c59b13
{
c59b13
	/bin/sed -rn '1,/^$/s/^model name[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
c59b13
}
c59b13
c59b13
get_vendor_id()
c59b13
{
c59b13
	/bin/sed -rn '1,/^$/s/^vendor_id[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
c59b13
}
c59b13
c59b13
get_mc_path()
c59b13
{
c59b13
	case "$1" in
c59b13
	GenuineIntel)
c59b13
		echo "intel-ucode/$2"
c59b13
		;;
c59b13
	AuthenticAMD)
c59b13
		echo "amd-ucode/$2"
c59b13
		;;
c59b13
	esac
c59b13
}
c59b13
c59b13
get_mc_ver()
c59b13
{
c59b13
	/bin/sed -rn '1,/^$/s/^microcode[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
c59b13
}
c59b13
c59b13
fail()
c59b13
{
c59b13
	ret=1
c59b13
c59b13
	fail_cfgs="$fail_cfgs $cfg"
c59b13
	fail_paths="$fail_paths $cfg_path"
078ac8
078ac8
	[ 0 -eq "$print_disclaimers" ] || [ ! -e "${dir}/disclaimer" ] \
078ac8
		|| cat "${dir}/disclaimer"
c59b13
}
c59b13
c59b13
#check_kver "$@"
c59b13
#get_model_name
c59b13
c59b13
match_model=0
c59b13
configs=
c59b13
kver=$(/bin/uname -r)
c59b13
verbose=0
c59b13
early_check=0
078ac8
print_disclaimers=0
c59b13
c59b13
ret=0
c59b13
078ac8
while getopts "dek:c:mv" opt; do
c59b13
	case "${opt}" in
078ac8
	d)
078ac8
		print_disclaimers=1
078ac8
		early_check=2
078ac8
		;;
c59b13
	e)
c59b13
		early_check=1
c59b13
		;;
c59b13
	k)
c59b13
		kver="$OPTARG"
c59b13
		;;
c59b13
	c)
c59b13
		configs="$configs $OPTARG"
c59b13
		;;
c59b13
	m)
c59b13
		match_model=1
c59b13
		;;
c59b13
	v)
c59b13
		verbose=1
c59b13
		;;
c59b13
	*)
c59b13
		usage
c59b13
		exit 1;
c59b13
		;;
c59b13
	esac
c59b13
done
c59b13
c59b13
: ${configs:=$(find "${MC_CAVEATS_DATA_DIR}" -maxdepth 1 -mindepth 1 -type d -printf "%f\n")}
c59b13
c59b13
cpu_model=$(get_model_string)
c59b13
cpu_model_name=$(get_model_name)
c59b13
cpu_vendor=$(get_vendor_id)
c59b13
c59b13
ret_paths=""
c59b13
ok_paths=""
c59b13
fail_paths=""
c59b13
c59b13
ret_cfgs=""
c59b13
ok_cfgs=""
c59b13
fail_cfgs=""
c59b13
c59b13
skip_cfgs=""
c59b13
c59b13
if [ 1 -eq "$early_check" ]; then
c59b13
	stage="early"
c59b13
else
c59b13
	stage="late"
c59b13
fi
c59b13
c59b13
c59b13
for cfg in $(echo "${configs}"); do
c59b13
	dir="$MC_CAVEATS_DATA_DIR/$cfg"
c59b13
c59b13
	# We add cfg to the skip list first and then, if we do not skip it,
c59b13
	# we remove the configuration from the list.
c59b13
	skip_cfgs="$skip_cfgs $cfg"
c59b13
c59b13
	[ -r "${dir}/readme" ] || {
c59b13
		debug "File 'readme' in ${dir} is not found, skipping"
c59b13
		continue
c59b13
	}
c59b13
c59b13
	[ -r "${dir}/config" ] || {
c59b13
		debug "File 'config' in ${dir} is not found, skipping"
c59b13
		continue
c59b13
	}
c59b13
c59b13
	cfg_model=
c59b13
	cfg_vendor=
c59b13
	cfg_path=
c59b13
	cfg_kvers=
c59b13
	cfg_kvers_early=
c59b13
	cfg_blacklist=
c59b13
	cfg_mc_min_ver_late=
c59b13
	cfg_disable=
c59b13
c59b13
	while read -r key value; do
c59b13
		case "$key" in
c59b13
		model)
c59b13
			cfg_model="$value"
c59b13
			;;
c59b13
		vendor)
c59b13
			cfg_vendor="$value"
c59b13
			;;
c59b13
		path)
c59b13
			cfg_path="$cfg_path $value"
c59b13
			;;
c59b13
		kernel)
c59b13
			cfg_kvers="$cfg_kvers $value"
c59b13
			;;
c59b13
		kernel_early)
c59b13
			cfg_kvers_early="$cfg_kvers_early $value"
c59b13
			;;
c59b13
		mc_min_ver_late)
c59b13
			cfg_mc_min_ver_late="$value"
c59b13
			;;
c59b13
		disable)
c59b13
			cfg_disable="$cfg_disable $value "
c59b13
			;;
c59b13
		blacklist)
c59b13
			cfg_blacklist=1
c59b13
			break
c59b13
			;;
c59b13
		esac
c59b13
	done < "${dir}/config"
c59b13
c59b13
	[ -z "${cfg_blacklist}" ] || \
c59b13
		cfg_blacklist=$(/bin/sed -n '/^blacklist$/,$p' "${dir}/config" |
c59b13
					/usr/bin/tail -n +2)
c59b13
c59b13
	debug "${cfg}: model '$cfg_model', path '$cfg_path', kvers '$cfg_kvers'"
c59b13
	debug "${cfg}: blacklist '$cfg_blacklist'"
c59b13
c59b13
	# Check for override files in the following order:
c59b13
	#  - disallow early/late specific caveat for specific kernel
c59b13
	#  - force early/late specific caveat for specific kernel
c59b13
	#  - disallow specific caveat for specific kernel
c59b13
	#  - force specific caveat for specific kernel
c59b13
	#
c59b13
	#  - disallow early/late specific caveat for any kernel
c59b13
	#  - disallow early/late any caveat for specific kernel
c59b13
	#  - force early/late specific caveat for any kernel
c59b13
	#  - force early/late any caveat for specific kernel
c59b13
	#  - disallow specific caveat for any kernel
c59b13
	#  - disallow any caveat for specific kernel
c59b13
	#  - force specific caveat for any kernel
c59b13
	#  - force any caveat for specific kernel
c59b13
	#
c59b13
	#  - disallow early/late everything
c59b13
	#  - force early/late everyhting
c59b13
	#  - disallow everything
c59b13
	#  - force everyhting
c59b13
	ignore_cfg=0
c59b13
	force_cfg=0
c59b13
	override_file=""
c59b13
	overrides="
c59b13
	0:$FW_DIR/$kver/disallow-$stage-$cfg
c59b13
	1:$FW_DIR/$kver/force-$stage-$cfg
c59b13
	0:$FW_DIR/$kver/disallow-$cfg
c59b13
	1:$FW_DIR/$kver/force-$cfg
c59b13
	0:$FW_DIR/$kver/disallow-$stage
c59b13
	0:$CFG_DIR/disallow-$stage-$cfg
c59b13
	1:$FW_DIR/$kver/force-$stage
c59b13
	1:$CFG_DIR/force-$stage-$cfg
c59b13
	0:$FW_DIR/$kver/disallow
c59b13
	0:$CFG_DIR/disallow-$cfg
c59b13
	1:$FW_DIR/$kver/force
c59b13
	1:$CFG_DIR/force-$cfg
c59b13
	0:$CFG_DIR/disallow-$stage
c59b13
	1:$CFG_DIR/force-$stage
c59b13
	0:$CFG_DIR/disallow
c59b13
	1:$CFG_DIR/force"
c59b13
	for o in $(echo "$overrides"); do
c59b13
		o_force=${o%%:*}
c59b13
		override_file=${o#$o_force:}
c59b13
c59b13
		[ -e "$override_file" ] || continue
c59b13
c59b13
		if [ 0 -eq "$o_force" ]; then
c59b13
			ignore_cfg=1
c59b13
		else
c59b13
			force_cfg=1
c59b13
		fi
c59b13
c59b13
		break
c59b13
	done
c59b13
c59b13
	[ 0 -eq "$ignore_cfg" ] || {
c59b13
		debug "Configuration \"$cfg\" is ignored due to presence of" \
c59b13
		      "\"$override_file\"."
c59b13
		continue
c59b13
	}
c59b13
c59b13
	# Check model if model filter is enabled
c59b13
	if [ 1 -eq "$match_model" -a  -n "$cfg_model" ]; then
c59b13
		[ "x$cpu_model" = "x$cfg_model" ] || {
c59b13
			debug "Current CPU model '$cpu_model' doesn't" \
c59b13
			      "match configuration CPU model '$cfg_model'," \
c59b13
			      "skipping"
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	# Check paths if model filter is enabled
c59b13
	if [ 1 -eq "$match_model" -a  -n "$cfg_path" ]; then
c59b13
		cpu_mc_path="$MC_CAVEATS_DATA_DIR/$cfg/$(get_mc_path \
c59b13
			"$cpu_vendor" "${cpu_model#* }")"
c59b13
		cfg_mc_present=0
c59b13
c59b13
		for p in $(printf "%s" "$cfg_path"); do
c59b13
			find "$MC_CAVEATS_DATA_DIR/$cfg" \
c59b13
				-path "$MC_CAVEATS_DATA_DIR/$cfg/$p" -print0 \
c59b13
			    | grep -zFxq "$cpu_mc_path" \
c59b13
			    || continue
c59b13
c59b13
			cfg_mc_present=1
c59b13
		done
c59b13
c59b13
		[ 1 = "$cfg_mc_present" ] || {
c59b13
			debug "No matching microcode files in '$cfg_path'" \
c59b13
			      "for CPU model '$cpu_model', skipping"
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	# Check vendor if model filter is enabled
c59b13
	if [ 1 -eq "$match_model" -a  -n "$cfg_vendor" ]; then
c59b13
		[ "x$cpu_vendor" = "x$cfg_vendor" ] || {
c59b13
			debug "Current CPU vendor '$cpu_vendor' doesn't" \
c59b13
			      "match configuration CPU vendor '$cfg_vendor'," \
c59b13
			      "skipping"
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	# Check configuration files
c59b13
c59b13
	ret_cfgs="$ret_cfgs $cfg"
c59b13
	ret_paths="$ret_paths $cfg_path"
c59b13
	skip_cfgs="${skip_cfgs% $cfg}"
c59b13
c59b13
	[ 0 -eq "$force_cfg" ] || {
c59b13
		debug "Checks for configuration \"$cfg\" are ignored due to" \
c59b13
		      "presence of \"$override_file\"."
c59b13
c59b13
		ok_cfgs="$ok_cfgs $cfg"
c59b13
		ok_paths="$ok_paths $cfg_path"
c59b13
c59b13
		continue
c59b13
	}
c59b13
c59b13
	[ "x${cfg_disable%%* $stage *}" = "x$cfg_disable" ] || {
c59b13
		debug "${cfg}: caveat is disabled in configuration"
c59b13
		fail
c59b13
		continue
c59b13
	}
c59b13
c59b13
	# Check late load kernel version
c59b13
	if [ 1 -ne "$early_check" -a -n "$cfg_kvers" ]; then
c59b13
		check_kver "$kver" $cfg_kvers || {
c59b13
			debug "${cfg}: late load kernel version check for" \
c59b13
			      " '$kver' against '$cfg_kvers' failed"
c59b13
			fail
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	# Check early load kernel version
c59b13
	if [ 0 -ne "$early_check" -a -n "$cfg_kvers_early" ]; then
c59b13
		check_kver "$kver" $cfg_kvers_early || {
c59b13
			debug "${cfg}: early load kernel version check for" \
c59b13
			      "'$kver' against '$cfg_kvers_early' failed"
c59b13
			fail
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	# Check model blacklist
c59b13
	if [ -n "$cfg_blacklist" ]; then
c59b13
		echo "$cfg_blacklist" | /bin/grep -vqFx "${cpu_model_name}" || {
c59b13
			debug "${cfg}: model '${cpu_model_name}' is blacklisted"
c59b13
			fail
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	# Check current microcode version for the late update
c59b13
	if [ -n "$cfg_mc_min_ver_late" -a 1 -ne "$early_check" -a \
c59b13
	   "x$cpu_model" = "x$cfg_model" ]; then
c59b13
		cpu_mc_ver="$(get_mc_ver)"
c59b13
c59b13
		[ 1 -eq $((cpu_mc_ver >= cfg_mc_min_ver_late)) ] || {
c59b13
			debug "${cfg}: CPU microcode version $cpu_mc_ver" \
c59b13
			      "failed check (should be at least" \
c59b13
			      "${cfg_mc_min_ver_late})"
c59b13
			fail
c59b13
			continue
c59b13
		}
c59b13
	fi
c59b13
c59b13
	ok_cfgs="$ok_cfgs $cfg"
c59b13
	ok_paths="$ok_paths $cfg_path"
c59b13
done
c59b13
078ac8
[ 0 -eq "$print_disclaimers" ] || exit 0
078ac8
c59b13
echo "cfgs$ret_cfgs"
c59b13
echo "skip_cfgs$skip_cfgs"
c59b13
echo "paths$ret_paths"
c59b13
echo "ok_cfgs$ok_cfgs"
c59b13
echo "ok_paths$ok_paths"
c59b13
echo "fail_cfgs$fail_cfgs"
c59b13
echo "fail_paths$fail_paths"
c59b13
c59b13
exit $ret