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