Debian Codegraph @

check-bios-nx-heuristic

raw
1 #!/bin/sh
2 # Copyright (C) 2009-2010 Canonical, Ltd.
3 # License: GPLv3
4 # Authors:
5 # Kees Cook <[email protected]>
6 # Steve Beattie <[email protected]>
7 # Jamie Strandboge <[email protected]>
8 #
9 # Attempts to determine if the running x86-based CPU has NX capabilities
10 # (regardless of it being disabled by the BIOS). If the CPU is NX-capable
11 # but the nx bit is missing from flags, exit 1 (i.e. "BIOS settings need
12 # changing"), otherwise exit 0 (i.e. "nothing wrong with BIOS")
13 #
14 # lacks NX:
15 # not pae:
16 # cpu family <= 5
17 # cpu family > 6 && cpu family < 15
18 # cpu family == 6, model <= 12
19 # pae, cpu family == 6, model == 13 (excepting some sSpec?)
20 # http://processorfinder.intel.com/List.aspx?ParentRadio=All&ProcFam=942&SearchKey=
21 # has NX:
22 # http://processorfinder.intel.com/Default.aspx
23 # pae, cpu family == 6, model >= 14
24 # pae, cpu family == 15, model >= 4
25 # pae, cpu family > 15
26 #
27 # variable chance of NX:
28 # pae, cpu family == 15, model == 3 (and some ==4! arrgh)
29 # many of these seem to have NX, but there have been several
30 # that do not. For now, err on the side of warning about the lack
31 # of NX.
32 #
33 # Thanks to Steve and Jamie for the awk and shell overhaul!
34 set -e
35 export LANG=C
36
37 usage() {
38 echo "Usage: $0 [options]"
39 echo ""
40 echo "Options:"
41 echo " -h, --help show this help message and exit"
42 echo " --verbose Explain in detail what has been detected"
43 }
44
45 VERBOSE=
46
47 TEMP=$(getopt -o h --long verbose,help -n check-bios-nx -- "$@")
48 eval set -- "$TEMP"
49
50 while :; do
51 case "$1" in
52 -h|--help) usage ; exit 0 ;;
53 --verbose) VERBOSE=1; shift ;;
54 --) shift ; break ;;
55 *) usage >&2 ; exit 2 ;;
56 esac
57 done
58
59 export VERBOSE
60 awk -f - ${CHECK_BIOS_NX_CPUINFO:-/proc/cpuinfo} <<EOF
61 function verbose(message)
62 {
63 if (length(ENVIRON["VERBOSE"]) != 0)
64 print message > "/dev/stderr"
65 }
66
67 BEGIN {
68 arch = ENVIRON["CHECK_BIOS_NX_MACHINE"]
69 if (length(arch) == 0) {
70 "uname -m" | getline arch
71 }
72
73 if (match(arch, "^(i.86|x86_64)$") == 0) {
74 verbose("This script is currently only useful on x86-based CPUs")
75 skip_end = 1
76 exit(0)
77 }
78
79 FS=": "
80 family = -1
81 model = -1
82 flags = ""
83
84 has_nx = 0
85 has_pae = 0
86 }
87
88 family == -1 && /^cpu family\t/ { family = \$2 }
89 model == -1 && /^model\t/ { model = \$2 }
90 flags == "" && /^flags\t/ { flags = \$2 }
91
92 END {
93
94 if (skip_end == 1)
95 exit(0)
96
97 if (flags == "") {
98 # No flags found (?!), fail open
99 verbose("No 'flags' were found for this CPU. Check /proc/cpuinfo contents.")
100 exit(1)
101 }
102 split(flags, cpuflags, " ")
103 for (i in cpuflags) {
104 if (cpuflags[i] ~ /^pae$/)
105 has_pae = 1
106 if (cpuflags[i] ~ /^nx$/)
107 has_nx = 1
108 }
109
110 if (has_nx) {
111 # If it's in the flags, it's not being disabled by the BIOS; rejoice.
112 verbose("This CPU has nx in the flags, so the BIOS is not disabling it.")
113 exit(0)
114 }
115
116 if (has_pae == 0) {
117 # No PAE, correct to lack nx
118 verbose("This CPU is not PAE capable, so it does not have NX.")
119 exit(0)
120 }
121
122 if (family == -1 || model == -1) {
123 # Cannot identify CPU, fail open
124 verbose("No model or family were found for this CPU. Check /proc/cpuinfo")
125 exit(1)
126 }
127
128 if ((family == 6) && (model >= 14) ||
129 (family == 15) && (model >= 3) ||
130 (family > 15)) {
131 # NX should be available in CPU, but missing from flags
132 verbose(sprintf("This CPU is family %s, model %s, and has NX capabilities but is unable to", family, model))
133 verbose("use these protective features because the BIOS may be configured to disable")
134 verbose("the capability. Please enable this in your BIOS. For more details, see:")
135 verbose("https://wiki.ubuntu.com/Security/CPUFeatures")
136 exit 1
137 }
138
139 # NX not available in CPU
140 verbose(sprintf("This CPU is family %s, model %s, and does not have NX capabilities.", family, model))
141 exit(0)
142 }
143 EOF