~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to debian/cpu-checker/check-bios-nx

  • Committer: Kees Cook
  • Date: 2010-03-11 08:13:20 UTC
  • Revision ID: kees@outflux.net-20100311081320-2bas7tauws9fxha1
debian/control, data/update-motd-cpu-checker: move motd updater into
the data/ tree and add to build system now that check-bios-nx is part
of the external cpu-checker package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# Copyright (C) 2009 Canonical, Ltd.
3
 
# License: GPLv3
4
 
# Author: Kees Cook <kees@ubuntu.com>
5
 
#
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")
10
 
#
11
 
# lacks NX:
12
 
#     not pae:
13
 
#         cpu family <= 5
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=
18
 
# has NX:
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
23
 
 
24
 
import os, sys, re
25
 
import optparse
26
 
 
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()
31
 
 
32
 
arch = os.environ.get('CHECK_BIOS_NX_MACHINE',os.uname()[4])
33
 
if not re.match('(i.86|x86_64)$', arch):
34
 
    if opt.verbose:
35
 
        print >>sys.stderr, "This script is currently only useful on x86-based CPUs"
36
 
    sys.exit(0)
37
 
 
38
 
family = None
39
 
model = None
40
 
flags = []
41
 
for line in file(os.environ.get('CHECK_BIOS_NX_CPUINFO','/proc/cpuinfo')):
42
 
    line = line.strip()
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:
50
 
        break
51
 
 
52
 
if len(flags) == 0:
53
 
    # No flags found (?!), fail open
54
 
    if opt.verbose:
55
 
        print >>sys.stderr, "No 'flags' were found for this CPU.  Check /proc/cpuinfo"
56
 
    sys.exit(1)
57
 
 
58
 
# If it's in the flags, it's not being disabled by the BIOS; rejoice.
59
 
if 'nx' in flags:
60
 
    if opt.verbose:
61
 
        print >>sys.stderr, "This CPU has 'nx' in the flags, so the BIOS is not disabling it."
62
 
    sys.exit(0)
63
 
 
64
 
if 'pae' in flags:
65
 
    if model == None or family == None:
66
 
        # Cannot identify CPU, fail open
67
 
        if opt.verbose:
68
 
            print >>sys.stderr, "No 'model' or 'family' were found for this CPU.  Check /proc/cpuinfo"
69
 
        sys.exit(1)
70
 
    if (family == 6 and model >= 14) or \
71
 
       (family == 15 and model >= 3) or \
72
 
       (family > 15):
73
 
        # NX should be available in CPU, but missing from flags
74
 
        if opt.verbose:
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'
80
 
        sys.exit(1)
81
 
    else:
82
 
        # NX not available in CPU
83
 
        if opt.verbose:
84
 
            print >>sys.stderr, '''This CPU is family %d, model %d, and does not have NX capabilities.''' % (family, model)
85
 
        sys.exit(0)
86
 
 
87
 
if opt.verbose:
88
 
    print >>sys.stderr, "This CPU is not PAE capable, so it does not have NX."
89
 
sys.exit(0)