2
# Copyright (C) 2009 Canonical, Ltd.
4
# Author: Kees Cook <kees@ubuntu.com>
6
# Attempts to determine if the running x86-based CPU has NX capapbilities
7
# (regardless of it being disabled by the BIOS). If the CPU is NX-capable
8
# but the nx bit is missing from flags, exit 1 (i.e. "BIOS settings need
9
# changing"), otherwise exit 0 (i.e. "nothing wrong with BIOS")
14
# cpu family > 6 && cpu family < 15
15
# cpu family == 6, model <= 12
16
# pae, cpu family == 6, model == 13 (excepting some sSpec?)
17
# http://processorfinder.intel.com/List.aspx?ParentRadio=All&ProcFam=942&SearchKey=
19
# http://processorfinder.intel.com/Default.aspx
20
# pae, cpu family == 6, model >= 14
21
# pae, cpu family == 15, model >= 3
22
# pae, cpu family > 15
27
parser = optparse.OptionParser()
28
parser.add_option("--verbose", action='store_true',
29
help="Explain in detail what has been detected")
30
(opt, args) = parser.parse_args()
32
arch = os.environ.get('CHECK_BIOS_NX_MACHINE',os.uname()[4])
33
if not re.match('(i.86|x86_64)$', arch):
35
print >>sys.stderr, "This script is currently only useful on x86-based CPUs"
41
for line in file(os.environ.get('CHECK_BIOS_NX_CPUINFO','/proc/cpuinfo')):
43
if line.startswith('cpu family\t'):
44
family = int(line.split().pop())
45
elif line.startswith('model\t'):
46
model = int(line.split().pop())
47
elif line.startswith('flags\t'):
48
flags = line.split(':',1)[1].strip().split()
49
if model != None and family != None and len(flags) > 0:
53
# No flags found (?!), fail open
55
print >>sys.stderr, "No 'flags' were found for this CPU. Check /proc/cpuinfo"
58
# If it's in the flags, it's not being disabled by the BIOS; rejoice.
61
print >>sys.stderr, "This CPU has 'nx' in the flags, so the BIOS is not disabling it."
65
if model == None or family == None:
66
# Cannot identify CPU, fail open
68
print >>sys.stderr, "No 'model' or 'family' were found for this CPU. Check /proc/cpuinfo"
70
if (family == 6 and model >= 14) or \
71
(family == 15 and model >= 3) or \
73
# NX should be available in CPU, but missing from flags
75
print >>sys.stderr, '''This CPU is family %d, model %d, and has NX capabilities but is unable to
76
use these protective features because the BIOS is configured to disable
77
the capability. Please enable this in your BIOS. For more details, see:
78
''' % (family, model) + \
79
'https://wiki.ubuntu.com/Security/CPUFeatures'
82
# NX not available in CPU
84
print >>sys.stderr, '''This CPU is family %d, model %d, and does not have NX capabilities.''' % (family, model)
88
print >>sys.stderr, "This CPU is not PAE capable, so it does not have NX."