Blame SOURCES/check_caveats

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