Blame SOURCES/check_caveats

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