14 Commits
v0.17 ... v0.20

Author SHA1 Message Date
322f4efc8f fix broken logic of 68961f9, increment version to 0.20 2018-01-09 14:55:12 +01:00
19b01078c2 Merge pull request #32 from speed47/arm
adding known non-vulnerable ARM chips
2018-01-09 13:57:27 +01:00
68961f98c2 adding known non-vulnerable ARM chips 2018-01-09 13:11:48 +01:00
f0f2ea9b11 v0.19: introduce --no-color 2018-01-09 10:32:51 +01:00
6f1bdba1d9 bump to v0.18 to reflect changes 2018-01-09 09:21:42 +01:00
7b05105a54 Merge pull request #25 from Feandil/proc_config
When using /proc/config.gz, indicate it more clearly
2018-01-09 09:19:36 +01:00
8aed2d4086 Merge pull request #26 from Feandil/proc_kallsym
Use /proc/kallsyms to get symbols, if available
2018-01-09 09:17:18 +01:00
f4140a992a Use /proc/kallsyms to get symbols, if available 2018-01-09 08:58:09 +01:00
2c51b00a90 When using /proc/config.gz, indicate it more clearly 2018-01-09 08:54:07 +01:00
2d94514c07 adding mention of heuristic for variant 1 check 2018-01-09 08:43:52 +01:00
0e8f97afbc Merge pull request #24 from angus-p/Remove-extra-space
remove superfluous space from test line 315
2018-01-09 08:34:10 +01:00
70323a30da Merge pull request #23 from mradcliffe/issue-22
Increases tmp directory uniqueness to 6 characters to support Slackware
2018-01-09 08:33:32 +01:00
cc0b325383 remove superfluous space from test line 315
Extra space was causing non-existent variable to be tested resulting in 'YES' if running in live mode and IBRS compiled in
2018-01-09 03:47:25 +00:00
4454f03136 Increases tmp directory uniqueness to 6 characters to support Slackware 2018-01-08 22:28:55 -05:00

View File

@ -1,11 +1,21 @@
#! /bin/sh
# Spectre & Meltdown checker
#
# Check for the latest version at:
# https://github.com/speed47/spectre-meltdown-checker
# git clone https://github.com/speed47/spectre-meltdown-checker.git
# or wget https://raw.githubusercontent.com/speed47/spectre-meltdown-checker/master/spectre-meltdown-checker.sh
#
# Stephane Lesimple
VERSION=0.17
#
VERSION=0.20
# print status function
pstatus()
{
if [ "$opt_no_color" = 1 ]; then
_echo_nol "$2"
else
case "$1" in
red) col="\033[101m\033[30m";;
green) col="\033[102m\033[30m";;
@ -13,9 +23,10 @@ pstatus()
blue) col="\033[104m\033[30m";;
*) col="";;
esac
/bin/echo -ne "$col $2 \033[0m"
[ -n "$3" ] && /bin/echo -n " ($3)"
/bin/echo
_echo_nol "$col $2 \033[0m"
fi
[ -n "$3" ] && _echo_nol " ($3)"
_echo
}
# The 3 below functions are taken from the extract-linux script, available here:
@ -65,7 +76,7 @@ extract_vmlinux()
{
[ -n "$1" ] || return 1
# Prepare temp files:
vmlinuxtmp="$(mktemp /tmp/vmlinux-XXX)"
vmlinuxtmp="$(mktemp /tmp/vmlinux-XXXXXX)"
trap "rm -f $vmlinuxtmp" EXIT
# Initial attempt for uncompressed images or objects:
@ -90,13 +101,14 @@ show_usage()
{
cat <<EOF
Usage:
Live mode: $0
Offline mode: $0 [--kernel <vmlinux_file>] [--config <kernel_config>] [--map <kernel_map_file>]
Options:
Live mode: $0 [options] [--live]
Offline mode: $0 [options] [--kernel <vmlinux_file>] [--config <kernel_config>] [--map <kernel_map_file>]
Modes:
Two modes are available.
First mode is the "live" mode (default), it does its best to find information about the currently running kernel.
To run under this mode, just start the script without any option.
To run under this mode, just start the script without any option (you can also use --live explicitely)
Second mode is the "offline" mode, where you can inspect a non-running kernel.
You'll need to specify the location of the vmlinux file, and if possible, the corresponding config and System.map files:
@ -105,17 +117,95 @@ show_usage()
--config kernel_config Specify a kernel config file
--map kernel_map_file Specify a kernel System.map file
Options:
--no-color Don't use color codes
EOF
}
/bin/echo -e "\033[1;34mSpectre and Meltdown mitigation detection tool v$VERSION\033[0m"
/bin/echo
__echo()
{
opt="$1"
shift
msg="$@"
if [ "$opt_no_color" = 1 ] ; then
# strip ANSI color codes
msg=$(echo "$msg" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
fi
# explicitely call /bin/echo to avoid shell builtins that might not take options
/bin/echo $opt -e "$msg"
}
_echo()
{
__echo '' "$@"
}
_echo_nol()
{
__echo -n "$@"
}
is_cpu_vulnerable()
{
# param: 1, 2 or 3 (variant)
# returns 1 if vulnerable, 0 if not vulnerable, 255 on error
# by default, everything is vulnerable, we work in a "whitelist" logic here.
# usage: is_cpu_vulnerable 2 && do something if vulnerable
variant1=0
variant2=0
variant3=0
if grep -q AMD /proc/cpuinfo; then
variant1=0
variant2=1
variant3=1
elif grep -qi 'CPU implementer : 0x41' /proc/cpuinfo; then
# ARM
# reference: https://developer.arm.com/support/security-update
cpupart=$(awk '/CPU part :/ {print $4;exit}' /proc/cpuinfo)
cpuarch=$(awk '/CPU architecture:/ {print $3;exit}' /proc/cpuinfo)
if [ -n "$cpupart" -a -n "$cpuarch" ]; then
# Cortex-R7 and Cortex-R8 are real-time and only used in medical devices or such
# I can't find their CPU part number, but it's probably not that useful anyway
# model R7 R8 A9 A15 A17 A57 A72 A73 A75
# part ? ? 0xc09 0xc0f 0xc0e 0xd07 0xd08 0xd09 0xd0a
# arch 7? 7? 7 7 7 8 8 8 8
if [ "$cpuarch" = 7 ] && echo "$cpupart" | grep -Eq '^0x(c09|c0f|c0e)$'; then
# armv7 vulnerable chips
variant1=0
variant2=0
elif [ "$cpuarch" = 8 ] && echo "$cpupart" | grep -Eq '^0x(d07|d08|d09|d0a)$'; then
# armv8 vulnerable chips
variant1=0
variant2=0
else
variant1=1
variant2=1
fi
# for variant3, only A75 is vulnerable
if [ "$cpuarch" = 8 -a "$cpupart" = 0xd0a ]; then
variant3=0
else
variant3=1
fi
fi
fi
[ "$1" = 1 ] && return $variant1
[ "$1" = 2 ] && return $variant2
[ "$1" = 3 ] && return $variant3
return 255
}
_echo "\033[1;34mSpectre and Meltdown mitigation detection tool v$VERSION\033[0m"
_echo
# parse options
opt_kernel=''
opt_config=''
opt_map=''
opt_live_explicit=0
opt_live=1
opt_no_color=0
parse_opt_file()
{
@ -156,6 +246,12 @@ while [ -n "$1" ]; do
[ $? -ne 0 ] && exit $?
shift 2
opt_live=0
elif [ "$1" = "--live" ]; then
opt_live_explicit=1
shift
elif [ "$1" = "--no-color" ]; then
opt_no_color=1
shift
elif [ "$1" = "-h" -o "$1" = "--help" ]; then
show_usage
exit 0
@ -166,16 +262,25 @@ while [ -n "$1" ]; do
fi
done
# check for mode selection inconsistency
if [ "$opt_live_explicit" = 1 ]; then
if [ -n "$opt_kernel" -o -n "$opt_config" -o -n "$opt_map" ]; then
show_usage
echo "$0: error: incompatible modes specified, use either --live or --kernel/--config/--map"
exit 1
fi
fi
# root check (only for live mode, for offline mode, we already checked if we could read the files)
if [ "$opt_live" = 1 ]; then
if [ "$(id -u)" -ne 0 ]; then
/bin/echo -e "\033[31mNote that you should launch this script with root privileges to get accurate information.\033[0m"
/bin/echo -e "\033[31mWe'll proceed but you might see permission denied errors.\033[0m"
/bin/echo -e "\033[31mTo run it as root, you can try the following command: sudo $0\033[0m"
/bin/echo
_echo "\033[31mNote that you should launch this script with root privileges to get accurate information.\033[0m"
_echo "\033[31mWe'll proceed but you might see permission denied errors.\033[0m"
_echo "\033[31mTo run it as root, you can try the following command: sudo $0\033[0m"
_echo
fi
/bin/echo -e "Checking for vulnerabilities against live running kernel \033[35m"$(uname -s) $(uname -r) $(uname -v) $(uname -m)"\033[0m"
_echo "Checking for vulnerabilities against live running kernel \033[35m"$(uname -s) $(uname -r) $(uname -v) $(uname -m)"\033[0m"
# try to find the image of the current running kernel
[ -e /boot/vmlinuz-linux ] && opt_kernel=/boot/vmlinuz-linux
@ -186,11 +291,15 @@ if [ "$opt_live" = 1 ]; then
[ -e /boot/kernel-genkernel-$(uname -m)-$(uname -r) ] && opt_kernel=/boot/kernel-genkernel-$(uname -m)-$(uname -r)
# system.map
[ -e /boot/System.map-$(uname -r) ] && opt_map=/boot/System.map-$(uname -r)
if [ -e /proc/kallsyms ] ; then
opt_map="/proc/kallsyms"
elif [ -e /boot/System.map-$(uname -r) ] ; then
opt_map=/boot/System.map-$(uname -r)
fi
# config
if [ -e /proc/config.gz ] ; then
dumped_config="$(mktemp /tmp/config-XXX)"
dumped_config="$(mktemp /tmp/config-XXXXXX)"
gunzip -c /proc/config.gz > $dumped_config
# dumped_config will be deleted at the end of the script
opt_config=$dumped_config
@ -198,22 +307,24 @@ if [ "$opt_live" = 1 ]; then
opt_config=/boot/config-$(uname -r)
fi
else
/bin/echo "Checking for vulnerabilities against specified kernel"
_echo "Checking for vulnerabilities against specified kernel"
fi
if [ -n "$opt_kernel" ]; then
/bin/echo -e "Will use vmlinux image \033[35m$opt_kernel\033[0m"
_echo "Will use vmlinux image \033[35m$opt_kernel\033[0m"
else
/bin/echo "Will use no vmlinux image (accuracy might be reduced"
_echo "Will use no vmlinux image (accuracy might be reduced)"
fi
if [ -n "$opt_config" ]; then
/bin/echo -e "Will use kconfig \033[35m$opt_config\033[0m"
if [ -n "$dumped_config" ]; then
_echo "Will use kconfig \033[35m/proc/config.gz\033[0m"
elif [ -n "$opt_config" ]; then
_echo "Will use kconfig \033[35m$opt_config\033[0m"
else
/bin/echo "Will use no kconfig (accuracy might be reduced)"
_echo "Will use no kconfig (accuracy might be reduced)"
fi
if [ -n "$opt_map" ]; then
/bin/echo -e "Will use System.map file \033[35m$opt_map\033[0m"
_echo "Will use System.map file \033[35m$opt_map\033[0m"
else
/bin/echo "Will use no System.map file (accuracy might be reduced)"
_echo "Will use no System.map file (accuracy might be reduced)"
fi
if [ -e "$opt_kernel" ]; then
@ -229,12 +340,12 @@ if [ -z "$vmlinux" -o ! -r "$vmlinux" ]; then
[ -z "$vmlinux_err" ] && vmlinux_err="couldn't extract your kernel from $opt_kernel"
fi
/bin/echo
_echo
###########
# SPECTRE 1
/bin/echo -e "\033[1;34mCVE-2017-5753 [bounds check bypass] aka 'Spectre Variant 1'\033[0m"
/bin/echo -n "* Kernel compiled with LFENCE opcode inserted at the proper places: "
_echo "\033[1;34mCVE-2017-5753 [bounds check bypass] aka 'Spectre Variant 1'\033[0m"
_echo_nol "* Checking count of LFENCE opcodes in kernel: "
status=0
if [ -n "$vmlinux_err" ]; then
@ -260,17 +371,21 @@ else
fi
fi
/bin/echo -ne "> \033[46m\033[30mSTATUS:\033[0m "
[ "$status" = 0 ] && pstatus yellow UNKNOWN
[ "$status" = 1 ] && pstatus red VULNERABLE
[ "$status" = 2 ] && pstatus green 'NOT VULNERABLE'
_echo_nol "> \033[46m\033[30mSTATUS:\033[0m "
if ! is_cpu_vulnerable 1; then
pstatus green 'NOT VULNERABLE' "your CPU vendor reported your CPU model as not vulnerable"
else
[ "$status" = 0 ] && pstatus yellow UNKNOWN
[ "$status" = 1 ] && pstatus red 'VULNERABLE' 'heuristic to be improved when official patches become available'
[ "$status" = 2 ] && pstatus green 'NOT VULNERABLE' 'heuristic to be improved when official patches become available'
fi
###########
# VARIANT 2
/bin/echo
/bin/echo -e "\033[1;34mCVE-2017-5715 [branch target injection] aka 'Spectre Variant 2'\033[0m"
/bin/echo "* Mitigation 1"
/bin/echo -n "* Hardware (CPU microcode) support for mitigation: "
_echo
_echo "\033[1;34mCVE-2017-5715 [branch target injection] aka 'Spectre Variant 2'\033[0m"
_echo "* Mitigation 1"
_echo_nol "* Hardware (CPU microcode) support for mitigation: "
if [ ! -e /dev/cpu/0/msr ]; then
# try to load the module ourselves (and remember it so we can rmmod it afterwards)
modprobe msr 2>/dev/null && insmod_msr=1
@ -294,7 +409,7 @@ if [ "$insmod_msr" = 1 ]; then
rmmod msr 2>/dev/null
fi
/bin/echo -n "* Kernel support for IBRS: "
_echo_nol "* Kernel support for IBRS: "
if [ "$opt_live" = 1 ]; then
if [ ! -e /sys/kernel/debug/sched_features ]; then
# try to mount the debugfs hierarchy ourselves and remember it to umount afterwards
@ -312,7 +427,7 @@ if [ "$opt_live" = 1 ]; then
ibrs_enabled=$(cat /sys/kernel/debug/x86/ibrs_enabled 2>/dev/null)
fi
fi
if [ "$ibrs_supported " != 1 -a -n "$opt_map" ]; then
if [ "$ibrs_supported" != 1 -a -n "$opt_map" ]; then
if grep -q spec_ctrl "$opt_map"; then
pstatus green YES
ibrs_supported=1
@ -322,7 +437,7 @@ if [ "$ibrs_supported" != 1 ]; then
pstatus red NO
fi
/bin/echo -n "* IBRS enabled for Kernel space: "
_echo_nol "* IBRS enabled for Kernel space: "
if [ "$opt_live" = 1 ]; then
# 0 means disabled
# 1 is enabled only for kernel space
@ -337,7 +452,7 @@ else
pstatus blue N/A "not testable in offline mode"
fi
/bin/echo -n "* IBRS enabled for User space: "
_echo_nol "* IBRS enabled for User space: "
if [ "$opt_live" = 1 ]; then
case "$ibrs_enabled" in
"") [ "$ibrs_supported" = 1 ] && pstatus yellow UNKNOWN || pstatus red NO;;
@ -349,8 +464,8 @@ else
pstatus blue N/A "not testable in offline mode"
fi
/bin/echo "* Mitigation 2"
/bin/echo -n "* Kernel compiled with retpoline option: "
_echo "* Mitigation 2"
_echo_nol "* Kernel compiled with retpoline option: "
# We check the RETPOLINE kernel options
if [ -r "$opt_config" ]; then
if grep -q '^CONFIG_RETPOLINE=y' "$opt_config"; then
@ -363,7 +478,7 @@ else
pstatus yellow UNKNOWN "couldn't read your kernel configuration"
fi
/bin/echo -n "* Kernel compiled with a retpoline-aware compiler: "
_echo_nol "* Kernel compiled with a retpoline-aware compiler: "
# Now check if the compiler used to compile the kernel knows how to insert retpolines in generated asm
# For gcc, this is -mindirect-branch=thunk-extern (detected by the kernel makefiles)
# See gcc commit https://github.com/hjl-tools/gcc/commit/23b517d4a67c02d3ef80b6109218f2aadad7bd79
@ -399,9 +514,9 @@ else
pstatus yellow UNKNOWN "couldn't find your kernel image or System.map"
fi
/bin/echo -ne "> \033[46m\033[30mSTATUS:\033[0m "
if grep -q AMD /proc/cpuinfo; then
pstatus green "NOT VULNERABLE" "your CPU is not vulnerable as per the vendor"
_echo_nol "> \033[46m\033[30mSTATUS:\033[0m "
if ! is_cpu_vulnerable 2; then
pstatus green 'NOT VULNERABLE' "your CPU vendor reported your CPU model as not vulnerable"
elif [ "$retpoline" = 1 -a "$retpoline_compiler" = 1 ]; then
pstatus green "NOT VULNERABLE" "retpoline mitigate the vulnerability"
elif [ "$opt_live" = 1 ]; then
@ -420,9 +535,9 @@ fi
##########
# MELTDOWN
/bin/echo
/bin/echo -e "\033[1;34mCVE-2017-5754 [rogue data cache load] aka 'Meltdown' aka 'Variant 3'\033[0m"
/bin/echo -n "* Kernel supports Page Table Isolation (PTI): "
_echo
_echo "\033[1;34mCVE-2017-5754 [rogue data cache load] aka 'Meltdown' aka 'Variant 3'\033[0m"
_echo_nol "* Kernel supports Page Table Isolation (PTI): "
kpti_support=0
kpti_can_tell=0
if [ -n "$opt_config" ]; then
@ -460,7 +575,7 @@ else
pstatus yellow UNKNOWN "couldn't read your kernel configuration nor System.map file"
fi
/bin/echo -n "* PTI enabled and active: "
_echo_nol "* PTI enabled and active: "
if [ "$opt_live" = 1 ]; then
if grep ^flags /proc/cpuinfo | grep -qw pti; then
# vanilla PTI patch sets the 'pti' flag in cpuinfo
@ -491,9 +606,9 @@ if [ "$mounted_debugfs" = 1 ]; then
umount /sys/kernel/debug
fi
/bin/echo -ne "> \033[46m\033[30mSTATUS:\033[0m "
if grep -q AMD /proc/cpuinfo; then
pstatus green "NOT VULNERABLE" "your CPU is not vulnerable as per the vendor"
_echo_nol "> \033[46m\033[30mSTATUS:\033[0m "
if ! is_cpu_vulnerable 3; then
pstatus green 'NOT VULNERABLE' "your CPU vendor reported your CPU model as not vulnerable"
elif [ "$opt_live" = 1 ]; then
if [ "$kpti_enabled" = 1 ]; then
pstatus green "NOT VULNERABLE" "PTI mitigates the vulnerability"
@ -508,6 +623,6 @@ else
fi
fi
/bin/echo
_echo
[ -n "$dumped_config" ] && rm -f "$dumped_config"