|
|
6009e6 |
#!/bin/python3
|
|
|
6009e6 |
# Copyright (C) 2017 Red Hat
|
|
|
6009e6 |
# Authors:
|
|
|
6009e6 |
# - Patrick Uiterwijk <puiterwijk@redhat.com>
|
|
|
6009e6 |
# - Kashyap Chamarthy <kchamart@redhat.com>
|
|
|
6009e6 |
#
|
|
|
6009e6 |
# Licensed under MIT License, for full text see LICENSE
|
|
|
6009e6 |
#
|
|
|
6009e6 |
# Purpose: Launch a QEMU guest and enroll ithe UEFI keys into an OVMF
|
|
|
6009e6 |
# variables ("VARS") file. Then boot a Linux kernel with QEMU.
|
|
|
6009e6 |
# Finally, perform a check to verify if Secure Boot
|
|
|
6009e6 |
# is enabled.
|
|
|
6009e6 |
|
|
|
6009e6 |
from __future__ import print_function
|
|
|
6009e6 |
|
|
|
6009e6 |
import argparse
|
|
|
6009e6 |
import os
|
|
|
6009e6 |
import logging
|
|
|
6009e6 |
import tempfile
|
|
|
6009e6 |
import shutil
|
|
|
6009e6 |
import string
|
|
|
6009e6 |
import subprocess
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def strip_special(line):
|
|
|
6009e6 |
return ''.join([c for c in str(line) if c in string.printable])
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def generate_qemu_cmd(args, readonly, *extra_args):
|
|
|
6009e6 |
if args.disable_smm:
|
|
|
6009e6 |
machinetype = 'pc'
|
|
|
6009e6 |
else:
|
|
|
6009e6 |
machinetype = 'q35,smm=on'
|
|
|
6009e6 |
machinetype += ',accel=%s' % ('kvm' if args.enable_kvm else 'tcg')
|
|
|
6009e6 |
|
|
|
6009e6 |
if args.oem_string is None:
|
|
|
6009e6 |
oemstrings = []
|
|
|
6009e6 |
else:
|
|
|
6009e6 |
oemstring_values = [
|
|
|
6009e6 |
",value=" + s.replace(",", ",,") for s in args.oem_string ]
|
|
|
6009e6 |
oemstrings = [
|
|
|
6009e6 |
'-smbios',
|
|
|
6009e6 |
"type=11" + ''.join(oemstring_values) ]
|
|
|
6009e6 |
|
|
|
6009e6 |
return [
|
|
|
6009e6 |
args.qemu_binary,
|
|
|
6009e6 |
'-machine', machinetype,
|
|
|
6009e6 |
'-display', 'none',
|
|
|
6009e6 |
'-no-user-config',
|
|
|
6009e6 |
'-nodefaults',
|
|
|
6009e6 |
'-m', '768',
|
|
|
6009e6 |
'-smp', '2,sockets=2,cores=1,threads=1',
|
|
|
6009e6 |
'-chardev', 'pty,id=charserial1',
|
|
|
6009e6 |
'-device', 'isa-serial,chardev=charserial1,id=serial1',
|
|
|
6009e6 |
'-global', 'driver=cfi.pflash01,property=secure,value=%s' % (
|
|
|
6009e6 |
'off' if args.disable_smm else 'on'),
|
|
|
6009e6 |
'-drive',
|
|
|
6009e6 |
'file=%s,if=pflash,format=raw,unit=0,readonly=on' % (
|
|
|
6009e6 |
args.ovmf_binary),
|
|
|
6009e6 |
'-drive',
|
|
|
6009e6 |
'file=%s,if=pflash,format=raw,unit=1,readonly=%s' % (
|
|
|
6009e6 |
args.out_temp, 'on' if readonly else 'off'),
|
|
|
6009e6 |
'-serial', 'stdio'] + oemstrings + list(extra_args)
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def download(url, target, suffix, no_download):
|
|
|
6009e6 |
istemp = False
|
|
|
6009e6 |
if target and os.path.exists(target):
|
|
|
6009e6 |
return target, istemp
|
|
|
6009e6 |
if not target:
|
|
|
6009e6 |
temped = tempfile.mkstemp(prefix='qosb.', suffix='.%s' % suffix)
|
|
|
6009e6 |
os.close(temped[0])
|
|
|
6009e6 |
target = temped[1]
|
|
|
6009e6 |
istemp = True
|
|
|
6009e6 |
if no_download:
|
|
|
6009e6 |
raise Exception('%s did not exist, but downloading was disabled' %
|
|
|
6009e6 |
target)
|
|
|
6009e6 |
import requests
|
|
|
6009e6 |
logging.debug('Downloading %s to %s', url, target)
|
|
|
6009e6 |
r = requests.get(url, stream=True)
|
|
|
6009e6 |
with open(target, 'wb') as f:
|
|
|
6009e6 |
for chunk in r.iter_content(chunk_size=1024):
|
|
|
6009e6 |
if chunk:
|
|
|
6009e6 |
f.write(chunk)
|
|
|
6009e6 |
return target, istemp
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def enroll_keys(args):
|
|
|
6009e6 |
shutil.copy(args.ovmf_template_vars, args.out_temp)
|
|
|
6009e6 |
|
|
|
6009e6 |
logging.info('Starting enrollment')
|
|
|
6009e6 |
|
|
|
6009e6 |
cmd = generate_qemu_cmd(
|
|
|
6009e6 |
args,
|
|
|
6009e6 |
False,
|
|
|
6009e6 |
'-drive',
|
|
|
6009e6 |
'file=%s,format=raw,if=none,media=cdrom,id=drive-cd1,'
|
|
|
6009e6 |
'readonly=on' % args.uefi_shell_iso,
|
|
|
6009e6 |
'-device',
|
|
|
6009e6 |
'ide-cd,drive=drive-cd1,id=cd1,'
|
|
|
6009e6 |
'bootindex=1')
|
|
|
6009e6 |
p = subprocess.Popen(cmd,
|
|
|
6009e6 |
stdin=subprocess.PIPE,
|
|
|
6009e6 |
stdout=subprocess.PIPE,
|
|
|
6009e6 |
stderr=subprocess.STDOUT)
|
|
|
6009e6 |
logging.info('Performing enrollment')
|
|
|
6009e6 |
# Wait until the UEFI shell starts (first line is printed)
|
|
|
6009e6 |
read = p.stdout.readline()
|
|
|
6009e6 |
if b'char device redirected' in read:
|
|
|
6009e6 |
read = p.stdout.readline()
|
|
|
6009e6 |
# Skip passed QEMU warnings, like the following one we see in Ubuntu:
|
|
|
6009e6 |
# qemu-system-x86_64: warning: TCG doesn't support requested feature: CPUID.01H:ECX.vmx [bit 5]
|
|
|
6009e6 |
while b'qemu-system-x86_64: warning:' in read:
|
|
|
6009e6 |
read = p.stdout.readline()
|
|
|
6009e6 |
if args.print_output:
|
|
|
6009e6 |
print(strip_special(read), end='')
|
|
|
6009e6 |
print()
|
|
|
6009e6 |
# Send the escape char to enter the UEFI shell early
|
|
|
6009e6 |
p.stdin.write(b'\x1b')
|
|
|
6009e6 |
p.stdin.flush()
|
|
|
6009e6 |
# And then run the following three commands from the UEFI shell:
|
|
|
6009e6 |
# change into the first file system device; install the default
|
|
|
6009e6 |
# keys and certificates, and reboot
|
|
|
6009e6 |
p.stdin.write(b'fs0:\r\n')
|
|
|
6009e6 |
p.stdin.write(b'EnrollDefaultKeys.efi\r\n')
|
|
|
6009e6 |
p.stdin.write(b'reset -s\r\n')
|
|
|
6009e6 |
p.stdin.flush()
|
|
|
6009e6 |
while True:
|
|
|
6009e6 |
read = p.stdout.readline()
|
|
|
6009e6 |
if args.print_output:
|
|
|
6009e6 |
print('OUT: %s' % strip_special(read), end='')
|
|
|
6009e6 |
print()
|
|
|
6009e6 |
if b'info: success' in read:
|
|
|
6009e6 |
break
|
|
|
6009e6 |
p.wait()
|
|
|
6009e6 |
if args.print_output:
|
|
|
6009e6 |
print(strip_special(p.stdout.read()), end='')
|
|
|
6009e6 |
logging.info('Finished enrollment')
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def test_keys(args):
|
|
|
6009e6 |
logging.info('Grabbing test kernel')
|
|
|
6009e6 |
kernel, kerneltemp = download(args.kernel_url, args.kernel_path,
|
|
|
6009e6 |
'kernel', args.no_download)
|
|
|
6009e6 |
|
|
|
6009e6 |
logging.info('Starting verification')
|
|
|
6009e6 |
try:
|
|
|
6009e6 |
cmd = generate_qemu_cmd(
|
|
|
6009e6 |
args,
|
|
|
6009e6 |
True,
|
|
|
6009e6 |
'-append', 'console=tty0 console=ttyS0,115200n8',
|
|
|
6009e6 |
'-kernel', kernel)
|
|
|
6009e6 |
p = subprocess.Popen(cmd,
|
|
|
6009e6 |
stdin=subprocess.PIPE,
|
|
|
6009e6 |
stdout=subprocess.PIPE,
|
|
|
6009e6 |
stderr=subprocess.STDOUT)
|
|
|
6009e6 |
logging.info('Performing verification')
|
|
|
6009e6 |
while True:
|
|
|
6009e6 |
read = p.stdout.readline()
|
|
|
6009e6 |
if args.print_output:
|
|
|
6009e6 |
print('OUT: %s' % strip_special(read), end='')
|
|
|
6009e6 |
print()
|
|
|
6009e6 |
if b'Secure boot disabled' in read:
|
|
|
6009e6 |
raise Exception('Secure Boot was disabled')
|
|
|
6009e6 |
elif b'Secure boot enabled' in read:
|
|
|
6009e6 |
logging.info('Confirmed: Secure Boot is enabled')
|
|
|
6009e6 |
break
|
|
|
6009e6 |
elif b'Kernel is locked down from EFI secure boot' in read:
|
|
|
6009e6 |
logging.info('Confirmed: Secure Boot is enabled')
|
|
|
6009e6 |
break
|
|
|
6009e6 |
p.kill()
|
|
|
6009e6 |
if args.print_output:
|
|
|
6009e6 |
print(strip_special(p.stdout.read()), end='')
|
|
|
6009e6 |
logging.info('Finished verification')
|
|
|
6009e6 |
finally:
|
|
|
6009e6 |
if kerneltemp:
|
|
|
6009e6 |
os.remove(kernel)
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def parse_args():
|
|
|
6009e6 |
parser = argparse.ArgumentParser()
|
|
|
6009e6 |
parser.add_argument('output', help='Filename for output vars file')
|
|
|
6009e6 |
parser.add_argument('--out-temp', help=argparse.SUPPRESS)
|
|
|
6009e6 |
parser.add_argument('--force', help='Overwrite existing output file',
|
|
|
6009e6 |
action='store_true')
|
|
|
6009e6 |
parser.add_argument('--print-output', help='Print the QEMU guest output',
|
|
|
6009e6 |
action='store_true')
|
|
|
6009e6 |
parser.add_argument('--verbose', '-v', help='Increase verbosity',
|
|
|
6009e6 |
action='count')
|
|
|
6009e6 |
parser.add_argument('--quiet', '-q', help='Decrease verbosity',
|
|
|
6009e6 |
action='count')
|
|
|
6009e6 |
parser.add_argument('--qemu-binary', help='QEMU binary path',
|
|
|
6009e6 |
default='/usr/bin/qemu-system-x86_64')
|
|
|
6009e6 |
parser.add_argument('--enable-kvm', help='Enable KVM acceleration',
|
|
|
6009e6 |
action='store_true')
|
|
|
6009e6 |
parser.add_argument('--ovmf-binary', help='OVMF secureboot code file',
|
|
|
6009e6 |
default='/usr/share/edk2/ovmf/OVMF_CODE.secboot.fd')
|
|
|
6009e6 |
parser.add_argument('--ovmf-template-vars', help='OVMF empty vars file',
|
|
|
6009e6 |
default='/usr/share/edk2/ovmf/OVMF_VARS.fd')
|
|
|
6009e6 |
parser.add_argument('--uefi-shell-iso', help='Path to uefi shell iso',
|
|
|
6009e6 |
default='/usr/share/edk2/ovmf/UefiShell.iso')
|
|
|
6009e6 |
parser.add_argument('--skip-enrollment',
|
|
|
6009e6 |
help='Skip enrollment, only test', action='store_true')
|
|
|
6009e6 |
parser.add_argument('--skip-testing',
|
|
|
6009e6 |
help='Skip testing generated "VARS" file',
|
|
|
6009e6 |
action='store_true')
|
|
|
6009e6 |
parser.add_argument('--kernel-path',
|
|
|
6009e6 |
help='Specify a consistent path for kernel')
|
|
|
6009e6 |
parser.add_argument('--no-download', action='store_true',
|
|
|
6009e6 |
help='Never download a kernel')
|
|
|
6009e6 |
parser.add_argument('--fedora-version',
|
|
|
6009e6 |
help='Fedora version to get kernel for checking',
|
|
|
6009e6 |
default='27')
|
|
|
6009e6 |
parser.add_argument('--kernel-url', help='Kernel URL',
|
|
|
6009e6 |
default='https://download.fedoraproject.org/pub/fedora'
|
|
|
6009e6 |
'/linux/releases/%(version)s/Everything/x86_64'
|
|
|
6009e6 |
'/os/images/pxeboot/vmlinuz')
|
|
|
6009e6 |
parser.add_argument('--disable-smm',
|
|
|
6009e6 |
help=('Don\'t restrict varstore pflash writes to '
|
|
|
6009e6 |
'guest code that executes in SMM. Use this '
|
|
|
6009e6 |
'option only if your OVMF binary doesn\'t have '
|
|
|
6009e6 |
'the edk2 SMM driver stack built into it '
|
|
|
6009e6 |
'(possibly because your QEMU binary lacks SMM '
|
|
|
6009e6 |
'emulation). Note that without restricting '
|
|
|
6009e6 |
'varstore pflash writes to guest code that '
|
|
|
6009e6 |
'executes in SMM, a malicious guest kernel, '
|
|
|
6009e6 |
'used for testing, could undermine Secure '
|
|
|
6009e6 |
'Boot.'),
|
|
|
6009e6 |
action='store_true')
|
|
|
6009e6 |
parser.add_argument('--oem-string',
|
|
|
6009e6 |
help=('Pass the argument to the guest as a string in '
|
|
|
6009e6 |
'the SMBIOS Type 11 (OEM Strings) table. '
|
|
|
6009e6 |
'Multiple occurrences of this option are '
|
|
|
6009e6 |
'collected into a single SMBIOS Type 11 table. '
|
|
|
6009e6 |
'A pure ASCII string argument is strongly '
|
|
|
6009e6 |
'suggested.'),
|
|
|
6009e6 |
action='append')
|
|
|
6009e6 |
args = parser.parse_args()
|
|
|
6009e6 |
args.kernel_url = args.kernel_url % {'version': args.fedora_version}
|
|
|
6009e6 |
|
|
|
6009e6 |
validate_args(args)
|
|
|
6009e6 |
return args
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def validate_args(args):
|
|
|
6009e6 |
if (os.path.exists(args.output)
|
|
|
6009e6 |
and not args.force
|
|
|
6009e6 |
and not args.skip_enrollment):
|
|
|
6009e6 |
raise Exception('%s already exists' % args.output)
|
|
|
6009e6 |
|
|
|
6009e6 |
if args.skip_enrollment and not os.path.exists(args.output):
|
|
|
6009e6 |
raise Exception('%s does not yet exist' % args.output)
|
|
|
6009e6 |
|
|
|
6009e6 |
verbosity = (args.verbose or 1) - (args.quiet or 0)
|
|
|
6009e6 |
if verbosity >= 2:
|
|
|
6009e6 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
6009e6 |
elif verbosity == 1:
|
|
|
6009e6 |
logging.basicConfig(level=logging.INFO)
|
|
|
6009e6 |
elif verbosity < 0:
|
|
|
6009e6 |
logging.basicConfig(level=logging.ERROR)
|
|
|
6009e6 |
else:
|
|
|
6009e6 |
logging.basicConfig(level=logging.WARN)
|
|
|
6009e6 |
|
|
|
6009e6 |
if args.skip_enrollment:
|
|
|
6009e6 |
args.out_temp = args.output
|
|
|
6009e6 |
else:
|
|
|
6009e6 |
temped = tempfile.mkstemp(prefix='qosb.', suffix='.vars')
|
|
|
6009e6 |
os.close(temped[0])
|
|
|
6009e6 |
args.out_temp = temped[1]
|
|
|
6009e6 |
logging.debug('Temp output: %s', args.out_temp)
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def move_to_dest(args):
|
|
|
6009e6 |
shutil.copy(args.out_temp, args.output)
|
|
|
6009e6 |
os.remove(args.out_temp)
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
def main():
|
|
|
6009e6 |
args = parse_args()
|
|
|
6009e6 |
if not args.skip_enrollment:
|
|
|
6009e6 |
enroll_keys(args)
|
|
|
6009e6 |
if not args.skip_testing:
|
|
|
6009e6 |
test_keys(args)
|
|
|
6009e6 |
if not args.skip_enrollment:
|
|
|
6009e6 |
move_to_dest(args)
|
|
|
6009e6 |
if args.skip_testing:
|
|
|
6009e6 |
logging.info('Created %s' % args.output)
|
|
|
6009e6 |
else:
|
|
|
6009e6 |
logging.info('Created and verified %s' % args.output)
|
|
|
6009e6 |
else:
|
|
|
6009e6 |
logging.info('Verified %s', args.output)
|
|
|
6009e6 |
|
|
|
6009e6 |
|
|
|
6009e6 |
if __name__ == '__main__':
|
|
|
6009e6 |
main()
|