| 1 |
#!/bin/sh |
| 2 |
# Copyright 2010, Canonical, Ltd. |
| 3 |
# License: GPLv2 |
| 4 |
# Author: Kees Cook <[email protected]> |
| 5 |
set -e |
| 6 |
|
| 7 |
export LANG=C |
| 8 |
|
| 9 |
usage() { |
| 10 |
echo "Usage: $0 [options]" |
| 11 |
echo "" |
| 12 |
echo "Options:" |
| 13 |
echo " -h, --help show this help message and exit" |
| 14 |
echo " --verbose Explain in detail what has been detected" |
| 15 |
} |
| 16 |
|
| 17 |
report() { |
| 18 |
if [ -n "$VERBOSE" ]; then |
| 19 |
echo "$@" >/dev/stderr |
| 20 |
fi |
| 21 |
} |
| 22 |
|
| 23 |
VERBOSE= |
| 24 |
|
| 25 |
TEMP=$(getopt -o h --long verbose,help -n check-bios-nx -- "$@") |
| 26 |
eval set -- "$TEMP" |
| 27 |
|
| 28 |
while :; do |
| 29 |
case "$1" in |
| 30 |
-h|--help) usage ; exit 0 ;; |
| 31 |
--verbose) VERBOSE=1; shift ;; |
| 32 |
--) shift ; break ;; |
| 33 |
*) usage >&2 ; exit 2 ;; |
| 34 |
esac |
| 35 |
done |
| 36 |
|
| 37 |
export VERBOSE |
| 38 |
|
| 39 |
if ! uname -m | egrep -q '^(i.86|x86_64)$' ; then |
| 40 |
report "This script is currently only useful on x86-based CPUs" |
| 41 |
exit 0 |
| 42 |
fi |
| 43 |
|
| 44 |
# Prepare MSR access |
| 45 |
msr="/dev/cpu/0/msr" |
| 46 |
if [ ! -r "$msr" ]; then |
| 47 |
modprobe msr |
| 48 |
fi |
| 49 |
if [ ! -r "$msr" ]; then |
| 50 |
echo "$0: You must be root to run this check." >&2 |
| 51 |
exit 2 |
| 52 |
fi |
| 53 |
|
| 54 |
# Check MSR_IA32_MISC_ENABLE's bit 34, NX_DISABLE |
| 55 |
BIT=$(rdmsr --bitfield 34:34 0x1a0 2>/dev/null || true) |
| 56 |
if [ "$BIT" = "1" ]; then |
| 57 |
report "FAIL: the NX bit is being filtered by the BIOS on this CPU!" |
| 58 |
exit 1 |
| 59 |
else |
| 60 |
if egrep -m1 -q '^flags[[:blank:]]*:.*\<nx\>' /proc/cpuinfo ; then |
| 61 |
report "ok: the NX bit is operational on this CPU." |
| 62 |
else |
| 63 |
report "WARNING: the NX bit is not available for this CPU." |
| 64 |
fi |
| 65 |
fi |
| 66 |
exit 0 |