Debian Codegraph @

src/ukify/ukify.py

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 #
4 # This file is part of systemd.
5 #
6 # systemd is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; either version 2.1 of the License, or
9 # (at your option) any later version.
10 #
11 # systemd is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with systemd; If not, see <https://www.gnu.org/licenses/>.
18
19 # pylint: disable=import-outside-toplevel,consider-using-with,unused-argument
20 # pylint: disable=unnecessary-lambda-assignment
21
22 import argparse
23 import builtins
24 import collections
25 import configparser
26 import contextlib
27 import dataclasses
28 import datetime
29 import fnmatch
30 import inspect
31 import itertools
32 import json
33 import os
34 import pprint
35 import pydoc
36 import re
37 import shlex
38 import shutil
39 import socket
40 import struct
41 import subprocess
42 import sys
43 import tempfile
44 import textwrap
45 import uuid
46 from collections.abc import Iterable, Iterator, Sequence
47 from hashlib import sha256
48 from pathlib import Path
49 from types import ModuleType
50 from typing import (
51 IO,
52 Any,
53 Callable,
54 Literal,
55 Optional,
56 TypeVar,
57 Union,
58 cast,
59 )
60
61 import pefile # type: ignore
62
63 __version__ = '{{PROJECT_VERSION}} ({{VERSION_TAG}})'
64
65 EFI_ARCH_MAP = {
66 # host_arch glob : [efi_arch, 32_bit_efi_arch if mixed mode is supported]
67 'x86_64': ['x64', 'ia32'],
68 'i[3456]86': ['ia32'],
69 'aarch64': ['aa64'],
70 'armv[45678]*l': ['arm'],
71 'loongarch32': ['loongarch32'],
72 'loongarch64': ['loongarch64'],
73 'riscv32': ['riscv32'],
74 'riscv64': ['riscv64'],
75 } # fmt: skip
76 EFI_ARCHES: list[str] = sum(EFI_ARCH_MAP.values(), [])
77
78 # Default configuration directories and file name.
79 # When the user does not specify one, the directories are searched in this order and the first file found is
80 # used.
81 DEFAULT_CONFIG_DIRS = ['/etc/systemd', '/run/systemd', '/usr/local/lib/systemd', '/usr/lib/systemd']
82 DEFAULT_CONFIG_FILE = 'ukify.conf'
83
84
85 class Style:
86 bold = '\033[0;1;39m' if sys.stderr.isatty() else ''
87 gray = '\033[0;38;5;245m' if sys.stderr.isatty() else ''
88 red = '\033[31;1m' if sys.stderr.isatty() else ''
89 yellow = '\033[33;1m' if sys.stderr.isatty() else ''
90 reset = '\033[0m' if sys.stderr.isatty() else ''
91
92
93 def guess_efi_arch() -> str:
94 arch = os.uname().machine
95
96 for glob, mapping in EFI_ARCH_MAP.items():
97 if fnmatch.fnmatch(arch, glob):
98 efi_arch, *fallback = mapping
99 break
100 else:
101 raise ValueError(f'Unsupported architecture {arch}')
102
103 # This makes sense only on some architectures, but it also probably doesn't
104 # hurt on others, so let's just apply the check everywhere.
105 if fallback:
106 fw_platform_size = Path('/sys/firmware/efi/fw_platform_size')
107 try:
108 size = fw_platform_size.read_text().strip()
109 except FileNotFoundError:
110 pass
111 else:
112 if int(size) == 32:
113 efi_arch = fallback[0]
114
115 # print(f'Host arch {arch!r}, EFI arch {efi_arch!r}')
116 return efi_arch
117
118
119 def page(text: str, enabled: Optional[bool]) -> None:
120 if enabled:
121 # Initialize less options from $SYSTEMD_LESS or provide a suitable fallback.
122 os.environ['LESS'] = os.getenv('SYSTEMD_LESS', 'FRSXMK')
123 pydoc.pager(text)
124 else:
125 print(text)
126
127
128 def shell_join(cmd: list[Union[str, Path]]) -> str:
129 # TODO: drop in favour of shlex.join once shlex.join supports Path.
130 return ' '.join(shlex.quote(str(x)) for x in cmd)
131
132
133 def round_up(x: int, blocksize: int = 4096) -> int:
134 return (x + blocksize - 1) // blocksize * blocksize
135
136
137 def try_import(modname: str, name: Optional[str] = None) -> ModuleType:
138 try:
139 return __import__(modname)
140 except ImportError as e:
141 raise ValueError(f'Kernel is compressed with {name or modname}, but module unavailable') from e
142
143
144 def read_env_file(text: str) -> dict[str, str]:
145 result = {}
146
147 for line in text.splitlines():
148 line = line.rstrip()
149 if not line or line.startswith('#'):
150 continue
151 if m := re.match(r'([A-Z][A-Z_0-9]+)=(.*)', line):
152 name, val = m.groups()
153 if val and val[0] in '"\'':
154 val = next(shlex.shlex(val, posix=True))
155
156 result[name] = val
157 else:
158 print(f'bad line {line!r}', file=sys.stderr)
159
160 return result
161
162
163 def get_zboot_kernel(f: IO[bytes]) -> bytes:
164 """Decompress zboot efistub kernel if compressed. Return contents."""
165 # See linux/drivers/firmware/efi/libstub/Makefile.zboot
166 # and linux/drivers/firmware/efi/libstub/zboot-header.S
167
168 # 4 bytes at offset 0x08 contain the starting offset of compressed data
169 f.seek(8)
170 _start = f.read(4)
171 start = struct.unpack('<i', _start)[0]
172
173 # Reading 4 bytes from address 0x0c is the size of compressed data,
174 # but it needs to be corrected according to the compressed type.
175 f.seek(0xC)
176 _sizes = f.read(4)
177 size = struct.unpack('<i', _sizes)[0]
178
179 # Read 6 bytes from address 0x18, which is a nul-terminated
180 # string representing the compressed type.
181 f.seek(0x18)
182 comp_type = f.read(6)
183 f.seek(start)
184 if comp_type.startswith(b'gzip'):
185 gzip = try_import('gzip')
186 data = f.read(size)
187 return cast(bytes, gzip.decompress(data))
188 elif comp_type.startswith(b'lz4'):
189 lz4 = try_import('lz4.frame', 'lz4')
190 data = f.read(size)
191 return cast(bytes, lz4.frame.decompress(data))
192 elif comp_type.startswith(b'lzma'):
193 lzma = try_import('lzma')
194 data = f.read(size)
195 return cast(bytes, lzma.decompress(data))
196 elif comp_type.startswith(b'lzo'):
197 raise NotImplementedError('lzo decompression not implemented')
198 elif comp_type.startswith(b'xzkern'):
199 raise NotImplementedError('xzkern decompression not implemented')
200 elif comp_type.startswith(b'zstd'):
201 try:
202 zstd = try_import('compression.zstd')
203 data = f.read(size)
204 return cast(bytes, zstd.zstd.ZstdDecompressor().decompress(data))
205 except ValueError:
206 zstd = try_import('zstandard')
207 data = f.read(size)
208 return cast(bytes, zstd.ZstdDecompressor().stream_reader(data).read())
209
210 raise NotImplementedError(f'unknown compressed type: {comp_type!r}')
211
212
213 def maybe_decompress(filename: Union[str, Path]) -> bytes:
214 """Decompress file if compressed. Return contents."""
215 f = open(filename, 'rb')
216 start = f.read(4)
217 f.seek(0)
218
219 if start.startswith(b'\x7fELF'):
220 # not compressed
221 return f.read()
222
223 if start.startswith(b'MZ'):
224 f.seek(4)
225 img_type = f.read(4)
226 if img_type.startswith(b'zimg'):
227 # zboot efistub kernel
228 return get_zboot_kernel(f)
229 else:
230 # not compressed aarch64 and riscv64
231 return f.read()
232
233 if start.startswith(b'\x1f\x8b'):
234 gzip = try_import('gzip')
235 return cast(bytes, gzip.open(f).read())
236
237 if start.startswith(b'\x28\xb5\x2f\xfd'):
238 try:
239 zstd = try_import('compression.zstd')
240 return cast(bytes, zstd.zstd.ZstdDecompressor().decompress(f.read()))
241 except ValueError:
242 zstd = try_import('zstandard')
243 return cast(bytes, zstd.ZstdDecompressor().stream_reader(f.read()).read())
244
245 if start.startswith(b'\x02\x21\x4c\x18'):
246 lz4 = try_import('lz4.frame', 'lz4')
247 return cast(bytes, lz4.frame.decompress(f.read()))
248
249 if start.startswith(b'\x04\x22\x4d\x18'):
250 print('Newer lz4 stream format detected! This may not boot!', file=sys.stderr)
251 lz4 = try_import('lz4.frame', 'lz4')
252 return cast(bytes, lz4.frame.decompress(f.read()))
253
254 if start.startswith(b'\x89LZO'):
255 # python3-lzo is not packaged for Fedora
256 raise NotImplementedError('lzo decompression not implemented')
257
258 if start.startswith(b'BZh'):
259 bz2 = try_import('bz2', 'bzip2')
260 return cast(bytes, bz2.open(f).read())
261
262 if start.startswith(b'\x5d\x00\x00'):
263 lzma = try_import('lzma')
264 return cast(bytes, lzma.open(f).read())
265
266 raise NotImplementedError(f'unknown file format (starts with {start!r})')
267
268
269 @dataclasses.dataclass
270 class UkifyConfig:
271 all: bool
272 cmdline: Union[str, Path, None]
273 devicetree: Path
274 devicetree_auto: list[Path]
275 efi_arch: str
276 hwids: Union[str, Path, None]
277 initrd: list[Path]
278 efifw: list[Path]
279 join_profiles: list[Path]
280 sign_profiles: list[str]
281 json: Union[Literal['pretty'], Literal['short'], Literal['off']]
282 linux: Optional[Path]
283 measure: bool
284 microcode: Path
285 os_release: Union[str, Path, None]
286 output: Optional[str]
287 pcr_banks: list[str]
288 pcr_private_keys: list[str]
289 pcr_public_keys: list[str]
290 pcr_certificates: list[str]
291 pcrpkey: Optional[Path]
292 pcrsig: Union[str, Path, None]
293 join_pcrsig: Optional[Path]
294 phase_path_groups: Optional[list[str]]
295 policy_digest: bool
296 profile: Optional[str]
297 sb_cert: Union[str, Path, None]
298 sb_cert_name: Optional[str]
299 sb_cert_validity: int
300 sb_certdir: Path
301 sb_key: Union[str, Path, None]
302 sbat: Optional[list[str]]
303 sections: list['Section']
304 sections_by_name: dict[str, 'Section']
305 sign_kernel: Optional[bool]
306 signing_engine: Optional[str]
307 signing_provider: Optional[str]
308 certificate_provider: Optional[str]
309 signtool: Optional[str]
310 splash: Optional[Path]
311 stub: Path
312 summary: bool
313 tools: list[Path]
314 uname: Optional[str]
315 verb: str
316 files: list[str] = dataclasses.field(default_factory=list)
317
318 @classmethod
319 def from_namespace(cls, ns: argparse.Namespace) -> 'UkifyConfig':
320 return cls(**{k: v for k, v in vars(ns).items() if k in inspect.signature(cls).parameters})
321
322
323 class Uname:
324 # This class is here purely as a namespace for the functions
325
326 VERSION_PATTERN = r'(?P<version>[a-z0-9._+-]+) \([^ )]+\) (?:#.*)'
327
328 NOTES_PATTERN = r'^\s+Linux\s+0x[0-9a-f]+\s+OPEN\n\s+description data: (?P<version>[0-9a-f ]+)\s*$'
329
330 # Linux version 6.0.8-300.fc37.ppc64le ([email protected])
331 # (gcc (GCC) 12.2.1 20220819 (Red Hat 12.2.1-2), GNU ld version 2.38-24.fc37)
332 # #1 SMP Fri Nov 11 14:39:11 UTC 2022
333 TEXT_PATTERN = rb'Linux version (?P<version>\d\.\S+) \('
334
335 @classmethod
336 def scrape_x86(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> str:
337 # Based on https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L136
338 # and https://docs.kernel.org/arch/x86/boot.html#the-real-mode-kernel-header
339 with open(filename, 'rb') as f:
340 f.seek(0x202)
341 magic = f.read(4)
342 if magic != b'HdrS':
343 raise ValueError('Real-Mode Kernel Header magic not found')
344 f.seek(0x20E)
345 offset = f.read(1)[0] + f.read(1)[0] * 256 # Pointer to kernel version string
346 f.seek(0x200 + offset)
347 text = f.read(128)
348 text = text.split(b'\0', maxsplit=1)[0]
349 decoded = text.decode()
350
351 if not (m := re.match(cls.VERSION_PATTERN, decoded)):
352 raise ValueError(f'Cannot parse version-host-release uname string: {text!r}')
353 return m.group('version')
354
355 @classmethod
356 def scrape_elf(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> str:
357 readelf = find_tool('readelf', opts=opts)
358
359 cmd = [
360 readelf,
361 '--notes',
362 filename,
363 ]
364
365 print('+', shell_join(cmd), file=sys.stderr)
366 try:
367 notes = subprocess.check_output(cmd, stderr=subprocess.PIPE, text=True)
368 except subprocess.CalledProcessError as e:
369 raise ValueError(e.stderr.strip()) from e
370
371 if not (m := re.search(cls.NOTES_PATTERN, notes, re.MULTILINE)):
372 raise ValueError('Cannot find Linux version note')
373
374 text = ''.join(chr(int(c, 16)) for c in m.group('version').split())
375 return text.rstrip('\0')
376
377 @classmethod
378 def scrape_generic(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> str:
379 # import libarchive
380 # libarchive-c fails with
381 # ArchiveError: Unrecognized archive format (errno=84, retcode=-30, archive_p=94705420454656)
382
383 # Based on https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L209
384
385 text = maybe_decompress(filename)
386 if not (m := re.search(cls.TEXT_PATTERN, text)):
387 raise ValueError(f'Cannot find {cls.TEXT_PATTERN!r} in {filename}')
388
389 return m.group('version').decode()
390
391 @classmethod
392 def scrape(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> Optional[str]:
393 for func in (cls.scrape_x86, cls.scrape_elf, cls.scrape_generic):
394 try:
395 version = func(filename, opts=opts)
396 print(f'Found uname version: {version}', file=sys.stderr)
397 return version
398 except ValueError as e:
399 print(str(e), file=sys.stderr)
400 return None
401
402
403 DEFAULT_SECTIONS_TO_SHOW = {
404 '.linux': 'binary',
405 '.initrd': 'binary',
406 '.ucode': 'binary',
407 '.splash': 'binary',
408 '.dtb': 'binary',
409 '.dtbauto': 'binary',
410 '.hwids': 'binary',
411 '.efifw': 'binary',
412 '.cmdline': 'text',
413 '.osrel': 'text',
414 '.uname': 'text',
415 '.pcrpkey': 'text',
416 '.pcrsig': 'text',
417 '.sbat': 'text',
418 '.sbom': 'binary',
419 '.profile': 'text',
420 } # fmt: skip
421
422
423 @dataclasses.dataclass
424 class Section:
425 name: str
426 content: Optional[Path]
427 tmpfile: Optional[IO[Any]] = None
428 measure: bool = False
429 output_mode: Optional[str] = None
430
431 @classmethod
432 def create(cls, name: str, contents: Union[str, bytes, Path, None], **kwargs: Any) -> 'Section':
433 if isinstance(contents, (str, bytes)):
434 mode = 'wt' if isinstance(contents, str) else 'wb'
435 tmp = tempfile.NamedTemporaryFile(mode=mode, prefix=f'tmp{name}')
436 tmp.write(contents)
437 tmp.flush()
438 contents = Path(tmp.name)
439 else:
440 tmp = None
441
442 return cls(name, contents, tmpfile=tmp, **kwargs)
443
444 @classmethod
445 def parse_input(cls, s: str) -> 'Section':
446 try:
447 name, contents, *rest = s.split(':')
448 except ValueError as e:
449 raise ValueError(f'Cannot parse section spec (name or contents missing): {s!r}') from e
450 if rest:
451 raise ValueError(f'Cannot parse section spec (extraneous parameters): {s!r}')
452
453 if contents.startswith('@'):
454 sec = cls.create(name, Path(contents[1:]))
455 else:
456 sec = cls.create(name, contents)
457
458 sec.check_name()
459 return sec
460
461 @classmethod
462 def parse_output(cls, s: str) -> 'Section':
463 if not (m := re.match(r'([a-zA-Z0-9_.]+):(text|binary)(?:@(.+))?', s)):
464 raise ValueError(f'Cannot parse section spec: {s!r}')
465
466 name, ttype, out = m.groups()
467 out = Path(out) if out else None
468
469 return cls.create(name, out, output_mode=ttype)
470
471 def check_name(self) -> None:
472 # PE section names with more than 8 characters are legal, but our stub does
473 # not support them.
474 if not self.name.isascii() or not self.name.isprintable():
475 raise ValueError(f'Bad section name: {self.name!r}')
476 if len(self.name) > 8:
477 raise ValueError(f'Section name too long: {self.name!r}')
478
479
480 @dataclasses.dataclass
481 class UKI:
482 executable: Path
483 sections: list[Section] = dataclasses.field(default_factory=list, init=False)
484
485 def add_section(self, section: Section) -> None:
486 start = 0
487
488 # Start search at last .profile section, if there is one
489 for i, s in enumerate(self.sections):
490 if s.name == '.profile':
491 start = i + 1
492
493 multiple_allowed_sections = ['.dtbauto', '.efifw']
494 if any(
495 section.name == s.name for s in self.sections[start:] if s.name not in multiple_allowed_sections
496 ):
497 raise ValueError(f'Duplicate section {section.name}')
498
499 self.sections += [section]
500
501
502 class SignTool:
503 @staticmethod
504 def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
505 raise NotImplementedError
506
507 @staticmethod
508 def verify(input_f: Path, opts: UkifyConfig) -> bool:
509 raise NotImplementedError
510
511 @staticmethod
512 def from_string(name: str) -> type['SignTool']:
513 if name == 'pesign':
514 return PeSign
515 elif name == 'sbsign':
516 return SbSign
517 elif name == 'systemd-sbsign':
518 return SystemdSbSign
519 else:
520 raise ValueError(f'Invalid sign tool: {name!r}')
521
522
523 class PeSign(SignTool):
524 @staticmethod
525 def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
526 assert opts.sb_certdir is not None
527 assert opts.sb_cert_name is not None
528
529 tool = find_tool('pesign', opts=opts, msg='pesign, required for signing, is not installed')
530 cmd = [
531 tool,
532 '-s',
533 '--force',
534 '-n', opts.sb_certdir,
535 '-c', opts.sb_cert_name,
536 '-i', input_f,
537 '-o', output_f,
538 ] # fmt: skip
539
540 print('+', shell_join(cmd), file=sys.stderr)
541 subprocess.check_call(cmd)
542
543 @staticmethod
544 def verify(input_f: Path, opts: UkifyConfig) -> bool:
545 assert input_f is not None
546
547 tool = find_tool('pesign', opts=opts)
548 cmd = [tool, '-i', input_f, '-S']
549
550 print('+', shell_join(cmd), file=sys.stderr)
551 info = subprocess.check_output(cmd, text=True)
552
553 return 'No signatures found.' in info
554
555
556 class SbSign(SignTool):
557 @staticmethod
558 def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
559 assert opts.sb_key is not None
560 assert opts.sb_cert is not None
561
562 tool = find_tool('sbsign', opts=opts, msg='sbsign, required for signing, is not installed')
563 cmd = [
564 tool,
565 '--key', opts.sb_key,
566 '--cert', opts.sb_cert,
567 *(['--engine', opts.signing_engine] if opts.signing_engine is not None else []),
568 input_f,
569 '--output', output_f,
570 ] # fmt: skip
571
572 print('+', shell_join(cmd), file=sys.stderr)
573 subprocess.check_call(cmd)
574
575 @staticmethod
576 def verify(input_f: Path, opts: UkifyConfig) -> bool:
577 assert input_f is not None
578
579 tool = find_tool('sbverify', opts=opts)
580 cmd = [tool, '--list', input_f]
581
582 print('+', shell_join(cmd), file=sys.stderr)
583 info = subprocess.check_output(cmd, text=True)
584
585 return 'No signature table present' in info
586
587
588 class SystemdSbSign(SignTool):
589 @staticmethod
590 def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
591 assert opts.sb_key is not None
592 assert opts.sb_cert is not None
593
594 tool = find_tool(
595 'systemd-sbsign',
596 '/usr/lib/systemd/systemd-sbsign',
597 opts=opts,
598 msg='systemd-sbsign, required for signing, is not installed',
599 )
600 cmd = [
601 tool,
602 'sign',
603 '--private-key', opts.sb_key,
604 '--certificate', opts.sb_cert,
605 *(
606 ['--private-key-source', f'engine:{opts.signing_engine}']
607 if opts.signing_engine is not None
608 else []
609 ),
610 *(
611 ['--private-key-source', f'provider:{opts.signing_provider}']
612 if opts.signing_provider is not None
613 else []
614 ),
615 *(
616 ['--certificate-source', f'provider:{opts.certificate_provider}']
617 if opts.certificate_provider is not None
618 else []
619 ),
620 input_f,
621 '--output', output_f,
622 ] # fmt: skip
623
624 print('+', shell_join(cmd), file=sys.stderr)
625 subprocess.check_call(cmd)
626
627 @staticmethod
628 def verify(input_f: Path, opts: UkifyConfig) -> bool:
629 raise NotImplementedError('systemd-sbsign cannot yet verify if existing PE binaries are signed')
630
631
632 def parse_banks(s: str) -> list[str]:
633 banks = re.split(r',|\s+', s)
634 # TODO: do some sanity checking here
635 return banks
636
637
638 KNOWN_PHASES = (
639 'enter-initrd',
640 'leave-initrd',
641 'sysinit',
642 'ready',
643 'shutdown',
644 'final',
645 )
646
647
648 def parse_phase_paths(s: str) -> list[str]:
649 # Split on commas or whitespace here. Commas might be hard to parse visually.
650 paths = re.split(r',|\s+', s)
651
652 for path in paths:
653 for phase in path.split(':'):
654 if phase not in KNOWN_PHASES:
655 raise argparse.ArgumentTypeError(f'Unknown boot phase {phase!r} ({path=})')
656
657 return paths
658
659
660 def check_splash(filename: Optional[Path]) -> None:
661 if filename is None:
662 return
663
664 # import is delayed, to avoid import when the splash image is not used
665 try:
666 from PIL import Image
667 except ImportError:
668 return
669
670 img = Image.open(filename, formats=['BMP'])
671 print(f'Splash image {filename} is {img.width}×{img.height} pixels', file=sys.stderr)
672
673
674 def check_inputs(opts: UkifyConfig) -> None:
675 for name, value in vars(opts).items():
676 if name in {'output', 'tools'}:
677 continue
678
679 if isinstance(value, Path):
680 # Check that we can open the directory or file, or generate and exception
681 if value.is_dir():
682 value.iterdir()
683 else:
684 value.open().close()
685 elif isinstance(value, list):
686 for item in value:
687 if isinstance(item, Path):
688 if item.is_dir():
689 item.iterdir()
690 else:
691 item.open().close()
692
693 check_splash(opts.splash)
694
695
696 def check_cert_and_keys_nonexistent(opts: UkifyConfig) -> None:
697 # Raise if any of the keys and certs are found on disk
698 paths: Iterator[Union[str, Path, None]] = itertools.chain(
699 (opts.sb_key, opts.sb_cert),
700 *((priv_key, pub_key, cert) for priv_key, pub_key, cert, _ in key_path_groups(opts)),
701 )
702 for path in paths:
703 if path and Path(path).exists():
704 raise ValueError(f'{path} is present')
705
706
707 def find_tool(
708 name: str,
709 fallback: Optional[str] = None,
710 opts: Optional[UkifyConfig] = None,
711 msg: str = 'Tool {name} not installed!',
712 ) -> Union[str, Path]:
713 if opts and opts.tools:
714 for d in opts.tools:
715 tool = d / name
716 if tool.exists():
717 return tool
718
719 if shutil.which(name) is not None:
720 return name
721
722 if fallback is None:
723 raise ValueError(msg.format(name=name))
724
725 return fallback
726
727
728 def combine_signatures(pcrsigs: list[dict[str, str]]) -> str:
729 combined: collections.defaultdict[str, list[str]] = collections.defaultdict(list)
730 for pcrsig in pcrsigs:
731 for bank, sigs in pcrsig.items():
732 for sig in sigs:
733 if sig not in combined[bank]:
734 combined[bank] += [sig]
735 return json.dumps(combined)
736
737
738 def key_path_groups(opts: UkifyConfig) -> Iterator[tuple[str, Optional[str], Optional[str], Optional[str]]]:
739 if not opts.pcr_private_keys:
740 return
741
742 n_priv = len(opts.pcr_private_keys)
743 pub_keys = opts.pcr_public_keys or []
744 certs = opts.pcr_certificates or []
745 pp_groups = opts.phase_path_groups or []
746
747 yield from itertools.zip_longest(
748 opts.pcr_private_keys,
749 pub_keys[:n_priv],
750 certs[:n_priv],
751 pp_groups[:n_priv],
752 fillvalue=None,
753 )
754
755
756 def pe_strip_section_name(name: bytes) -> str:
757 return name.rstrip(b'\x00').decode()
758
759
760 def pe_section_size(section: pefile.SectionStructure) -> int:
761 return cast(int, min(section.Misc_VirtualSize, section.SizeOfRawData))
762
763
764 def call_systemd_measure(uki: UKI, opts: UkifyConfig, profile_start: int = 0) -> str:
765 measure_tool = find_tool(
766 'systemd-measure',
767 '/usr/lib/systemd/systemd-measure',
768 opts=opts,
769 )
770 combined = ''
771
772 banks = opts.pcr_banks or ()
773
774 # PCR measurement
775
776 # First, pick up either the base sections or the profile specific sections we shall measure now
777 unique_to_measure = {
778 s.name: s for s in uki.sections[profile_start:] if s.measure and s.name != '.dtbauto'
779 }
780
781 dtbauto_to_measure: list[Optional[Section]] = [
782 s for s in uki.sections[profile_start:] if s.measure and s.name == '.dtbauto'
783 ]
784
785 if len(dtbauto_to_measure) == 0:
786 dtbauto_to_measure = [None]
787
788 # Then, if we're measuring a profile, lookup the missing sections from the base image.
789 if profile_start != 0:
790 for section in uki.sections:
791 # If we reach the first .profile section the base is over
792 if section.name == '.profile':
793 break
794
795 # Only some sections are measured
796 if not section.measure:
797 continue
798
799 # Check if this is a section we already covered above
800 if section.name in unique_to_measure:
801 continue
802
803 unique_to_measure[section.name] = section
804
805 if opts.measure or opts.policy_digest:
806 pcrsigs = []
807 to_measure = unique_to_measure.copy()
808
809 for dtbauto in dtbauto_to_measure:
810 if dtbauto is not None:
811 to_measure[dtbauto.name] = dtbauto
812
813 pp_groups = opts.phase_path_groups or []
814
815 cmd = [
816 measure_tool,
817 'calculate' if opts.measure else 'policy-digest',
818 '--json',
819 opts.json,
820 *(f'--{s.name.removeprefix(".")}={s.content}' for s in to_measure.values()),
821 *(f'--bank={bank}' for bank in banks),
822 # For measurement, the keys are not relevant, so we can lump all the phase paths
823 # into one call to systemd-measure calculate.
824 *(f'--phase={phase_path}' for phase_path in itertools.chain.from_iterable(pp_groups)),
825 ]
826
827 # The JSON object will be used for offline signing, include the public key
828 # so that the fingerprint is included too. In case a certificate is passed, use the
829 # right parameter so that systemd-measure can extract the public key from it.
830 if opts.policy_digest:
831 if opts.pcr_public_keys:
832 cmd += ['--public-key', opts.pcr_public_keys[0]]
833 elif opts.pcr_certificates:
834 cmd += ['--certificate', opts.pcr_certificates[0]]
835 if opts.certificate_provider:
836 cmd += ['--certificate-source', f'provider:{opts.certificate_provider}']
837
838 print('+', shell_join(cmd), file=sys.stderr)
839 output = subprocess.check_output(cmd, text=True) # type: ignore
840
841 if opts.policy_digest:
842 pcrsig = json.loads(output)
843 pcrsigs += [pcrsig]
844 else:
845 print(output)
846
847 if opts.policy_digest:
848 combined = combine_signatures(pcrsigs)
849 # We need to ensure the section has space for signatures, that will be added separately later,
850 # so add some whitespace to pad the section. At most we'll need 4kb per digest (rsa4096).
851 # We might even check the key type given we have it to know the precise length, but don't
852 # bother for now.
853 combined += ' ' * 1024 * combined.count('"pol":')
854 uki.add_section(Section.create('.pcrsig', combined))
855
856 # PCR signing
857
858 if opts.pcr_private_keys:
859 pcrsigs = []
860 to_measure = unique_to_measure.copy()
861
862 for dtbauto in dtbauto_to_measure:
863 if dtbauto is not None:
864 to_measure[dtbauto.name] = dtbauto
865
866 cmd = [
867 measure_tool,
868 'sign',
869 *(f'--{s.name.removeprefix(".")}={s.content}' for s in to_measure.values()),
870 *(f'--bank={bank}' for bank in banks),
871 ]
872
873 for priv_key, pub_key, cert, group in key_path_groups(opts):
874 extra = [f'--private-key={priv_key}']
875 if opts.signing_engine is not None:
876 assert pub_key or cert
877 # Backward compatibility, we used to pass the public key as the certificate
878 # as there was no --pcr-certificate= parameter
879 extra += [
880 f'--private-key-source=engine:{opts.signing_engine}',
881 f'--certificate={pub_key or cert}',
882 ]
883 elif opts.signing_provider is not None:
884 assert pub_key or cert
885 extra += [
886 f'--private-key-source=provider:{opts.signing_provider}',
887 f'--certificate={pub_key or cert}',
888 ]
889 elif cert:
890 extra += [f'--certificate={cert}']
891 elif pub_key:
892 extra += [f'--public-key={pub_key}']
893
894 if opts.certificate_provider is not None:
895 extra += [f'--certificate-source=provider:{opts.certificate_provider}']
896
897 extra += [f'--phase={phase_path}' for phase_path in group or ()]
898
899 print('+', shell_join(cmd + extra), file=sys.stderr) # type: ignore
900 output = subprocess.check_output(cmd + extra, text=True) # type: ignore
901 pcrsig = json.loads(output)
902 pcrsigs += [pcrsig]
903
904 combined = combine_signatures(pcrsigs)
905 uki.add_section(Section.create('.pcrsig', combined))
906
907 return combined
908
909
910 def join_initrds(initrds: list[Path]) -> Union[Path, bytes, None]:
911 if not initrds:
912 return None
913 if len(initrds) == 1:
914 return initrds[0]
915
916 seq = []
917 for file in initrds:
918 initrd = file.read_bytes()
919 n = len(initrd)
920 padding = b'\0' * (round_up(n, 4) - n) # pad to 32 bit alignment
921 seq += [initrd, padding]
922
923 return b''.join(seq)
924
925
926 T = TypeVar('T')
927
928
929 def pairwise(iterable: Iterable[T]) -> Iterator[tuple[T, Optional[T]]]:
930 a, b = itertools.tee(iterable)
931 next(b, None)
932 return zip(a, b)
933
934
935 class PEError(Exception):
936 pass
937
938
939 def pe_add_sections(opts: UkifyConfig, uki: UKI, output: str) -> None:
940 pe = pefile.PE(uki.executable, fast_load=True)
941
942 # Old stubs do not have the symbol/string table stripped, even though image files should not have one.
943 if symbol_table := pe.FILE_HEADER.PointerToSymbolTable:
944 symbol_table_size = 18 * pe.FILE_HEADER.NumberOfSymbols
945 if string_table_size := pe.get_dword_from_offset(symbol_table + symbol_table_size):
946 symbol_table_size += string_table_size
947
948 # Let's be safe and only strip it if it's at the end of the file.
949 if symbol_table + symbol_table_size == len(pe.__data__):
950 pe.__data__ = pe.__data__[:symbol_table]
951 pe.FILE_HEADER.PointerToSymbolTable = 0
952 pe.FILE_HEADER.NumberOfSymbols = 0
953 pe.FILE_HEADER.IMAGE_FILE_LOCAL_SYMS_STRIPPED = True
954
955 # pylint thinks that Structure doesn't have various members that it has…
956 # pylint: disable=no-member
957
958 # Old stubs might have been stripped, leading to unaligned raw data values, so let's fix them up here.
959 for i, section in enumerate(pe.sections):
960 oldp = section.PointerToRawData
961 oldsz = section.SizeOfRawData
962 section.PointerToRawData = round_up(oldp, pe.OPTIONAL_HEADER.FileAlignment)
963 section.SizeOfRawData = round_up(oldsz, pe.OPTIONAL_HEADER.FileAlignment)
964 padp = section.PointerToRawData - oldp
965 padsz = section.SizeOfRawData - oldsz
966
967 for later_section in pe.sections[i + 1 :]:
968 later_section.PointerToRawData += padp + padsz
969
970 pe.__data__ = (
971 pe.__data__[:oldp]
972 + bytes(padp)
973 + pe.__data__[oldp : oldp + oldsz]
974 + bytes(padsz)
975 + pe.__data__[oldp + oldsz :]
976 )
977
978 # We might not have any space to add new sections. Let's try our best to make some space by padding the
979 # SizeOfHeaders to a multiple of the file alignment. This is safe because the first section's data starts
980 # at a multiple of the file alignment, so all space before that is unused.
981 pe.OPTIONAL_HEADER.SizeOfHeaders = round_up(
982 pe.OPTIONAL_HEADER.SizeOfHeaders, pe.OPTIONAL_HEADER.FileAlignment
983 )
984 pe = pefile.PE(data=pe.write(), fast_load=True)
985
986 # pefile has an hardcoded limit of 256MB, which is not enough when building an initrd with large firmware
987 # files and all kernel modules. See: https://github.com/erocarrera/pefile/issues/396
988 warnings = pe.get_warnings()
989 for w in warnings:
990 if 'VirtualSize is extremely large' in w:
991 continue
992 if 'VirtualAddress is beyond' in w:
993 continue
994 raise PEError(f'pefile warnings treated as errors: {warnings}')
995
996 # When attaching signatures we are operating on an existing UKI which might be signed
997 if not opts.pcrsig:
998 security = pe.OPTIONAL_HEADER.DATA_DIRECTORY[
999 pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']
1000 ]
1001 if security.VirtualAddress != 0:
1002 # We could strip the signatures, but why would anyone sign the stub?
1003 raise PEError('Stub image is signed, refusing')
1004
1005 # Remember how many sections originate from systemd-stub
1006 n_original_sections = len(pe.sections)
1007
1008 for section in uki.sections:
1009 new_section = pefile.SectionStructure(pe.__IMAGE_SECTION_HEADER_format__, pe=pe)
1010 new_section.__unpack__(b'\0' * new_section.sizeof())
1011
1012 offset = pe.sections[-1].get_file_offset() + new_section.sizeof()
1013 if offset + new_section.sizeof() > pe.OPTIONAL_HEADER.SizeOfHeaders:
1014 raise PEError(f'Not enough header space to add section {section.name}.')
1015
1016 assert section.content
1017 data = section.content.read_bytes()
1018
1019 new_section.set_file_offset(offset)
1020 new_section.Name = section.name.encode()
1021 new_section.Misc_VirtualSize = len(data)
1022 # Non-stripped stubs might still have an unaligned symbol table at the end, making their size
1023 # unaligned, so we make sure to explicitly pad the pointer to new sections to an aligned offset.
1024 new_section.PointerToRawData = round_up(len(pe.__data__), pe.OPTIONAL_HEADER.FileAlignment)
1025 new_section.SizeOfRawData = round_up(len(data), pe.OPTIONAL_HEADER.FileAlignment)
1026 new_section.VirtualAddress = round_up(
1027 pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize,
1028 pe.OPTIONAL_HEADER.SectionAlignment,
1029 )
1030
1031 new_section.IMAGE_SCN_MEM_READ = True
1032 if section.name == '.linux':
1033 # Old kernels that use EFI handover protocol will be executed inline.
1034 new_section.IMAGE_SCN_CNT_CODE = True
1035
1036 # Check if the kernel PE has the NX_COMPAT flag set, if not strip it from the UKI as they need
1037 # to have the same value, otherwise when firmwares start enforcing it, booting will fail.
1038 # https://microsoft.github.io/mu/WhatAndWhy/enhancedmemoryprotection/
1039 # https://www.kraxel.org/blog/2023/12/uefi-nx-linux-boot/
1040 try:
1041 inner_pe = pefile.PE(data=data, fast_load=True)
1042 nxbit = pefile.DLL_CHARACTERISTICS['IMAGE_DLLCHARACTERISTICS_NX_COMPAT']
1043 if not inner_pe.OPTIONAL_HEADER.DllCharacteristics & nxbit:
1044 pe.OPTIONAL_HEADER.DllCharacteristics &= ~nxbit
1045 except pefile.PEFormatError:
1046 # Unit tests build images with bogus data
1047 print(f'{section.name} in {uki.executable} is not a valid PE, ignoring', file=sys.stderr)
1048 else:
1049 new_section.IMAGE_SCN_CNT_INITIALIZED_DATA = True
1050
1051 # Special case, mostly for .sbat: the stub will already have a .sbat section, but we want to append
1052 # the one from the kernel to it. It should be small enough to fit in the existing section, so just
1053 # swap the data.
1054 for i, s in enumerate(pe.sections[:n_original_sections]):
1055 if pe_strip_section_name(s.Name) == section.name and section.name != '.dtbauto':
1056 if new_section.Misc_VirtualSize > s.SizeOfRawData:
1057 raise PEError(
1058 f'Not enough space in existing section {section.name} to append new data'
1059 f' (need {new_section.Misc_VirtualSize}, have {s.SizeOfRawData})'
1060 )
1061
1062 padding = bytes(s.SizeOfRawData - new_section.Misc_VirtualSize)
1063 pe.__data__ = (
1064 pe.__data__[: s.PointerToRawData]
1065 + data
1066 + padding
1067 + pe.__data__[pe.sections[i + 1].PointerToRawData :]
1068 )
1069 s.Misc_VirtualSize = new_section.Misc_VirtualSize
1070 break
1071 else:
1072 pe.__data__ = (
1073 pe.__data__[:]
1074 + bytes(new_section.PointerToRawData - len(pe.__data__))
1075 + data
1076 + bytes(new_section.SizeOfRawData - len(data))
1077 )
1078
1079 pe.FILE_HEADER.NumberOfSections += 1
1080 pe.OPTIONAL_HEADER.SizeOfInitializedData += new_section.Misc_VirtualSize
1081 pe.__structures__.append(new_section)
1082 pe.sections.append(new_section)
1083
1084 # If there is a pre-signed JSON blob, we need to update the existing JSON, by appending the signature to
1085 # each corresponding digest object. We have built the unsigned UKI with enough space to fit the .sig
1086 # objects, so we can just replace the new signed JSON in the existing sections.
1087 if opts.pcrsig:
1088 signatures = json.loads(str(opts.pcrsig))
1089 for i, section in enumerate(pe.sections):
1090 if pe_strip_section_name(section.Name) == '.pcrsig':
1091 j = json.loads(
1092 bytes(
1093 pe.__data__[
1094 section.PointerToRawData : section.PointerToRawData + section.SizeOfRawData
1095 ]
1096 )
1097 .rstrip(b'\x00')
1098 .decode()
1099 )
1100 for (bank, sigs), (input_bank, input_sigs) in itertools.product(
1101 j.items(), signatures.items()
1102 ):
1103 if input_bank != bank:
1104 continue
1105 for sig, input_sig in itertools.product(sigs, input_sigs):
1106 if sig['pol'] == input_sig['pol']:
1107 sig['sig'] = input_sig['sig']
1108
1109 encoded = json.dumps(j).encode()
1110 if len(encoded) > section.SizeOfRawData:
1111 raise PEError(
1112 f'Not enough space in existing section .pcrsig of size {section.SizeOfRawData} to append new data of size {len(encoded)}'
1113 )
1114 section.Misc_VirtualSize = len(encoded)
1115 # bytes(n) results in an array of n zeroes
1116 padding = bytes(section.SizeOfRawData - len(encoded))
1117 pe.__data__ = (
1118 pe.__data__[: section.PointerToRawData]
1119 + encoded
1120 + padding
1121 + pe.__data__[section.PointerToRawData + section.SizeOfRawData :]
1122 )
1123
1124 pe.OPTIONAL_HEADER.CheckSum = 0
1125 pe.OPTIONAL_HEADER.SizeOfImage = round_up(
1126 pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize,
1127 pe.OPTIONAL_HEADER.SectionAlignment,
1128 )
1129
1130 pe.write(output)
1131
1132
1133 def merge_sbat(input_pe: list[Path], input_text: list[str]) -> str:
1134 sbat = []
1135
1136 for f in input_pe:
1137 try:
1138 pe = pefile.PE(f, fast_load=True)
1139 except pefile.PEFormatError:
1140 print(f'{f} is not a valid PE file, not extracting SBAT section.', file=sys.stderr)
1141 continue
1142
1143 for section in pe.sections:
1144 if pe_strip_section_name(section.Name) == '.sbat':
1145 split = section.get_data().rstrip(b'\x00').decode().splitlines()
1146 if not split[0].startswith('sbat,'):
1147 print(f'{f} does not contain a valid SBAT section, skipping.', file=sys.stderr)
1148 continue
1149 # Filter out the sbat line, we'll add it back later, there needs to be only one and it
1150 # needs to be first.
1151 sbat += split[1:]
1152
1153 for t in input_text:
1154 if t.startswith('@'):
1155 t = Path(t[1:]).read_text()
1156 split = t.splitlines()
1157 if not split[0].startswith('sbat,'):
1158 print(f'{t} does not contain a valid SBAT section, skipping.', file=sys.stderr)
1159 continue
1160 sbat += split[1:]
1161
1162 return '\n'.join(
1163 (
1164 'sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md',
1165 *sbat,
1166 '', # an empty line so that we end up with a newline at the end
1167 )
1168 )
1169
1170
1171 # Keep in sync with Device from src/boot/chid.h
1172 # uint32_t descriptor, EFI_GUID chid, uint32_t name_offset, uint32_t compatible_offset
1173 DEVICE_STRUCT_SIZE = 4 + 16 + 4 + 4
1174 NULL_DEVICE = b'\0' * DEVICE_STRUCT_SIZE
1175 DEVICE_TYPE_DEVICETREE = 1
1176 DEVICE_TYPE_UEFI_FW = 2
1177
1178 # Keep in sync with efifirmware.h
1179 FWHEADERMAGIC = 'feeddead'
1180 EFIFW_HEADER_SIZE = 4 + 4 + 4 + 4
1181
1182
1183 def device_make_descriptor(device_type: int, size: int) -> int:
1184 return (size) | (device_type << 28)
1185
1186
1187 def pack_device(
1188 offsets: dict[str, int], devtype: int, name: str, compatible_or_fwid: str, chids: set[uuid.UUID]
1189 ) -> bytes:
1190 data = b''
1191 descriptor = device_make_descriptor(devtype, DEVICE_STRUCT_SIZE)
1192 for chid in sorted(chids):
1193 data += struct.pack('<I', descriptor)
1194 data += chid.bytes_le
1195 data += struct.pack('<II', offsets[name], offsets[compatible_or_fwid])
1196
1197 assert len(data) == DEVICE_STRUCT_SIZE * len(chids)
1198 return data
1199
1200
1201 def pack_strings(strings: set[str], base: int) -> tuple[bytes, dict[str, int]]:
1202 blob = b''
1203 offsets = {}
1204
1205 for string in sorted(strings):
1206 offsets[string] = base + len(blob)
1207 blob += string.encode('utf-8') + b'\00'
1208
1209 return (blob, offsets)
1210
1211
1212 def parse_hwid_dir(path: Path) -> bytes:
1213 hwid_files = sorted(path.rglob('*.json'))
1214 devstr_to_type: dict[str, int] = {
1215 'devicetree': DEVICE_TYPE_DEVICETREE,
1216 'uefi-fw': DEVICE_TYPE_UEFI_FW,
1217 }
1218
1219 # all attributes in the mandatory attributes list must be present
1220 mandatory_attribute = ['type', 'name', 'hwids']
1221
1222 # at least one of the following attributes must be present
1223 one_of = ['compatible', 'fwid']
1224
1225 one_of_key_to_devtype: dict[str, int] = {
1226 'compatible': DEVICE_TYPE_DEVICETREE,
1227 'fwid': DEVICE_TYPE_UEFI_FW,
1228 }
1229
1230 strings: set[str] = set()
1231 devices: collections.defaultdict[tuple[int, str, str], set[uuid.UUID]] = collections.defaultdict(set)
1232
1233 for hwid_file in hwid_files:
1234 data = json.loads(hwid_file.read_text(encoding='UTF-8'))
1235
1236 for k in mandatory_attribute:
1237 if k not in data:
1238 raise ValueError(f'hwid description file "{hwid_file}" does not contain "{k}"')
1239
1240 if not any(key in data for key in one_of):
1241 required_keys = ','.join(one_of)
1242 raise ValueError(f'hwid description file "{hwid_file}" must contain one of {required_keys}')
1243
1244 # (devtype, name, compatible/fwid) pair uniquely identifies the device
1245 devtype = devstr_to_type[data['type']]
1246
1247 for k in one_of:
1248 if k in data:
1249 if one_of_key_to_devtype[k] != devtype:
1250 raise ValueError(
1251 f'wrong attribute "{k}" for hwid description file "{hwid_file}", '
1252 'device type: "%s"' % devtype
1253 )
1254 strings |= {data['name'], data[k]}
1255 devices[(devtype, data['name'], data[k])] |= {uuid.UUID(u) for u in data['hwids']}
1256
1257 total_device_structs = 1
1258 for dev, uuids in devices.items():
1259 total_device_structs += len(uuids)
1260
1261 strings_blob, offsets = pack_strings(strings, total_device_structs * DEVICE_STRUCT_SIZE)
1262
1263 devices_blob = b''
1264 for (devtype, name, compatible_or_fwid), uuids in devices.items():
1265 devices_blob += pack_device(offsets, devtype, name, compatible_or_fwid, uuids)
1266
1267 devices_blob += NULL_DEVICE
1268
1269 return devices_blob + strings_blob
1270
1271
1272 def parse_efifw_dir(path: Path) -> bytes:
1273 if not path.is_dir():
1274 raise ValueError(f'{path} is not a directory or it does not exist')
1275
1276 # only one firmware image must be present in the directory
1277 # to uniquely identify that firmware with its ID.
1278 if len(list(path.glob('*'))) != 1:
1279 raise ValueError(f'{path} must contain exactly one firmware image file')
1280
1281 payload_blob = b''
1282 for fw in path.iterdir():
1283 payload_blob += fw.read_bytes()
1284
1285 payload_len = len(payload_blob)
1286 if payload_len == 0:
1287 raise ValueError(f'{fw} is a zero byte file!')
1288
1289 dirname = path.parts[-1]
1290 # firmware id is the name of the directory the firmware bundle is in,
1291 # terminated by NULL.
1292 fwid = b'' + dirname.encode() + b'\0'
1293 fwid_len = len(fwid)
1294 magic = bytes.fromhex(FWHEADERMAGIC)
1295
1296 efifw_header_blob = b''
1297 efifw_header_blob += struct.pack('<p', magic)
1298 efifw_header_blob += struct.pack('<I', EFIFW_HEADER_SIZE)
1299 efifw_header_blob += struct.pack('<I', fwid_len)
1300 efifw_header_blob += struct.pack('<I', payload_len)
1301
1302 efifw_blob = b''
1303 efifw_blob += efifw_header_blob + fwid + payload_blob
1304
1305 return efifw_blob
1306
1307
1308 STUB_SBAT = '''\
1309 sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md
1310 uki,1,UKI,uki,1,https://uapi-group.org/specifications/specs/unified_kernel_image/
1311 '''
1312
1313 ADDON_SBAT = '''\
1314 sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md
1315 uki-addon,1,UKI Addon,addon,1,https://www.freedesktop.org/software/systemd/man/latest/systemd-stub.html
1316 '''
1317
1318
1319 def make_uki(opts: UkifyConfig) -> None:
1320 assert opts.output is not None
1321
1322 # kernel payload signing
1323
1324 sign_args_present = opts.sb_key or opts.sb_cert_name
1325 sign_kernel = opts.sign_kernel
1326 linux = opts.linux
1327 combined_sigs = '{}'
1328
1329 # On some distros, on some architectures, the vmlinuz is a gzip file, so we need to decompress it
1330 # if it's not a valid PE file, as it will fail to be booted by the firmware.
1331 if linux:
1332 try:
1333 pefile.PE(linux, fast_load=True)
1334 except pefile.PEFormatError:
1335 try:
1336 decompressed = maybe_decompress(linux)
1337 except NotImplementedError:
1338 print(f'{linux} is not a valid PE file and cannot be decompressed either', file=sys.stderr)
1339 else:
1340 print(f'{linux} is compressed and cannot be loaded by UEFI, decompressing', file=sys.stderr)
1341 linux = Path(tempfile.NamedTemporaryFile(prefix='linux-decompressed').name)
1342 linux.write_bytes(decompressed)
1343
1344 if linux and sign_args_present:
1345 assert opts.signtool is not None
1346 signtool = SignTool.from_string(opts.signtool)
1347
1348 if sign_kernel is None:
1349 # figure out if we should sign the kernel
1350 sign_kernel = signtool.verify(linux, opts)
1351
1352 if sign_kernel:
1353 linux_signed = tempfile.NamedTemporaryFile(prefix='linux-signed')
1354 signtool.sign(os.fspath(linux), os.fspath(Path(linux_signed.name)), opts=opts)
1355 linux = Path(linux_signed.name)
1356
1357 if opts.uname is None and linux is not None:
1358 print('Kernel version not specified, starting autodetection 😖.', file=sys.stderr)
1359 opts.uname = Uname.scrape(linux, opts=opts)
1360
1361 uki = UKI(opts.join_pcrsig if opts.join_pcrsig else opts.stub)
1362 initrd = join_initrds(opts.initrd)
1363
1364 pcrpkey: Union[bytes, Path, None] = opts.pcrpkey
1365 if pcrpkey is None:
1366 keyutil_tool = find_tool('systemd-keyutil', '/usr/lib/systemd/systemd-keyutil')
1367 cmd = [keyutil_tool, 'extract-public']
1368
1369 if opts.pcr_public_keys and len(opts.pcr_public_keys) == 1:
1370 # If we're using an engine or provider, the public key will be an X.509 certificate.
1371 if opts.signing_engine or opts.signing_provider:
1372 cmd += ['--certificate', opts.pcr_public_keys[0]]
1373 if opts.certificate_provider:
1374 cmd += ['--certificate-source', f'provider:{opts.certificate_provider}']
1375
1376 print('+', shell_join(cmd), file=sys.stderr)
1377 pcrpkey = subprocess.check_output(cmd)
1378 else:
1379 pcrpkey = Path(opts.pcr_public_keys[0])
1380 elif opts.pcr_certificates and len(opts.pcr_certificates) == 1:
1381 cmd += ['--certificate', opts.pcr_certificates[0]]
1382 if opts.certificate_provider:
1383 cmd += ['--certificate-source', f'provider:{opts.certificate_provider}']
1384
1385 print('+', shell_join(cmd), file=sys.stderr)
1386 pcrpkey = subprocess.check_output(cmd)
1387 elif opts.pcr_private_keys and len(opts.pcr_private_keys) == 1:
1388 cmd += ['--private-key', Path(opts.pcr_private_keys[0])]
1389
1390 if opts.signing_engine:
1391 cmd += ['--private-key-source', f'engine:{opts.signing_engine}']
1392 if opts.signing_provider:
1393 cmd += ['--private-key-source', f'provider:{opts.signing_provider}']
1394
1395 print('+', shell_join(cmd), file=sys.stderr)
1396 pcrpkey = subprocess.check_output(cmd)
1397
1398 hwids = None
1399
1400 if opts.hwids != '':
1401 if opts.hwids is not None:
1402 hwids = parse_hwid_dir(Path(opts.hwids))
1403 else:
1404 hwids_dir = Path(f'/usr/lib/systemd/boot/hwids/{opts.efi_arch}')
1405 if hwids_dir.is_dir():
1406 print(f'Automatically building .hwids section from {hwids_dir}', file=sys.stderr)
1407 hwids = parse_hwid_dir(hwids_dir)
1408
1409 sections = [
1410 # name, content, measure?
1411 ('.osrel', opts.os_release, True),
1412 ('.cmdline', opts.cmdline, True),
1413 ('.dtb', opts.devicetree, True),
1414 *(('.dtbauto', dtb, True) for dtb in opts.devicetree_auto),
1415 ('.hwids', hwids, True),
1416 ('.uname', opts.uname, True),
1417 ('.splash', opts.splash, True),
1418 ('.pcrpkey', pcrpkey, True),
1419 ('.linux', linux, True),
1420 ('.initrd', initrd, True),
1421 *(('.efifw', parse_efifw_dir(fw), False) for fw in opts.efifw),
1422 ('.ucode', opts.microcode, True),
1423 ] # fmt: skip
1424
1425 # If we're building a PE profile binary, the ".profile" section has to be the first one.
1426 if opts.profile and not opts.join_profiles:
1427 uki.add_section(Section.create('.profile', opts.profile, measure=True))
1428
1429 for name, content, measure in sections:
1430 if content:
1431 uki.add_section(Section.create(name, content, measure=measure))
1432
1433 # systemd-measure doesn't know about those extra sections
1434 for section in opts.sections:
1435 uki.add_section(section)
1436
1437 # Don't add an .sbat section to profile PE binaries.
1438 if (opts.join_profiles or not opts.profile) and not opts.pcrsig:
1439 if linux is not None:
1440 # Merge the .sbat sections from stub, kernel, and parameter, so
1441 # that revocation can be done on either.
1442 input_pes = [opts.stub, linux]
1443 if not opts.sbat:
1444 opts.sbat = [STUB_SBAT]
1445 else:
1446 # Addons don't use the stub so we add SBAT manually
1447 input_pes = []
1448 if not opts.sbat:
1449 opts.sbat = [ADDON_SBAT]
1450 uki.add_section(Section.create('.sbat', merge_sbat(input_pes, opts.sbat), measure=linux is not None))
1451
1452 # If we're building a UKI with additional profiles, the .profile section for the base profile has to be
1453 # the last one so that everything before it is shared between profiles. The only thing we don't share
1454 # between profiles is the .pcrsig section which is appended later and doesn't make sense to share.
1455 if opts.profile and opts.join_profiles:
1456 uki.add_section(Section.create('.profile', opts.profile, measure=True))
1457
1458 # PCR measurement and signing
1459
1460 if (
1461 not opts.pcrsig
1462 and (opts.join_profiles or not opts.profile)
1463 and (
1464 not opts.sign_profiles
1465 or (opts.profile and read_env_file(opts.profile).get('ID') in opts.sign_profiles)
1466 )
1467 ):
1468 combined_sigs = call_systemd_measure(uki, opts=opts)
1469
1470 # UKI profiles
1471
1472 to_import = {
1473 '.linux',
1474 '.osrel',
1475 '.cmdline',
1476 '.initrd',
1477 '.efifw',
1478 '.ucode',
1479 '.splash',
1480 '.dtb',
1481 '.uname',
1482 '.sbat',
1483 '.profile',
1484 }
1485
1486 if not opts.os_release:
1487 to_import.remove('.osrel')
1488
1489 for profile in opts.join_profiles:
1490 pe = pefile.PE(profile, fast_load=True)
1491 prev_len = len(uki.sections)
1492
1493 names = [pe_strip_section_name(s.Name) for s in pe.sections]
1494 names = [n for n in names if n in to_import]
1495
1496 if len(names) == 0:
1497 raise ValueError(f'Found no valid sections in PE profile binary {profile}')
1498
1499 if names[0] != '.profile':
1500 raise ValueError(
1501 f'Expected .profile section as first valid section in PE profile binary {profile} but got {names[0]}'
1502 )
1503
1504 if names.count('.profile') > 1:
1505 raise ValueError(f'Profile PE binary {profile} contains multiple .profile sections')
1506
1507 for pesection in pe.sections:
1508 n = pe_strip_section_name(pesection.Name)
1509
1510 if n not in to_import:
1511 continue
1512
1513 print(
1514 f"Copying section '{n}' from '{profile}': {pe_section_size(pesection)} bytes",
1515 file=sys.stderr,
1516 )
1517 uki.add_section(
1518 Section.create(n, pesection.get_data(length=pe_section_size(pesection)), measure=True)
1519 )
1520
1521 if opts.sign_profiles:
1522 pesection = next(s for s in pe.sections if pe_strip_section_name(s.Name) == '.profile')
1523 id = read_env_file(pesection.get_data(length=pe_section_size(pesection)).decode()).get('ID')
1524 if not id or id not in opts.sign_profiles:
1525 print(f'Not signing expected PCR measurements for "{id}" profile', file=sys.stderr)
1526 continue
1527
1528 s = call_systemd_measure(uki, opts=opts, profile_start=prev_len)
1529 if s:
1530 combined_sigs = combine_signatures([json.loads(combined_sigs), json.loads(s)])
1531
1532 # UKI creation
1533
1534 if sign_args_present:
1535 unsigned = tempfile.NamedTemporaryFile(prefix='uki')
1536 unsigned_output = unsigned.name
1537 else:
1538 unsigned_output = opts.output
1539
1540 pe_add_sections(opts, uki, unsigned_output)
1541
1542 # UKI signing
1543
1544 if sign_args_present:
1545 assert opts.signtool is not None
1546 signtool = SignTool.from_string(opts.signtool)
1547
1548 signtool.sign(os.fspath(unsigned_output), os.fspath(opts.output), opts)
1549
1550 # We end up with no executable bits, let's reapply them
1551 os.umask(umask := os.umask(0))
1552 os.chmod(opts.output, 0o777 & ~umask)
1553
1554 print(f'Wrote {"signed" if sign_args_present else "unsigned"} {opts.output}', file=sys.stderr)
1555 if opts.policy_digest:
1556 print(combined_sigs)
1557
1558
1559 @contextlib.contextmanager
1560 def temporary_umask(mask: int) -> Iterator[None]:
1561 # Drop <mask> bits from umask
1562 old = os.umask(0)
1563 os.umask(old | mask)
1564 try:
1565 yield
1566 finally:
1567 os.umask(old)
1568
1569
1570 def generate_key_cert_pair(
1571 common_name: str,
1572 valid_days: int,
1573 keylength: int = 2048,
1574 ) -> tuple[bytes, bytes]:
1575 from cryptography import x509
1576 from cryptography.hazmat.primitives import hashes, serialization
1577 from cryptography.hazmat.primitives.asymmetric import rsa
1578
1579 # We use a keylength of 2048 bits. That is what Microsoft documents as
1580 # supported/expected:
1581 # https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/windows-secure-boot-key-creation-and-management-guidance?view=windows-11#12-public-key-cryptography
1582
1583 now = datetime.datetime.now(datetime.timezone.utc)
1584
1585 key = rsa.generate_private_key(
1586 public_exponent=65537,
1587 key_size=keylength,
1588 )
1589 cert = (
1590 x509.CertificateBuilder()
1591 .subject_name(
1592 x509.Name([x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, common_name)]),
1593 )
1594 .issuer_name(
1595 x509.Name([x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, common_name)]),
1596 )
1597 .not_valid_before(
1598 now,
1599 )
1600 .not_valid_after(
1601 now + datetime.timedelta(days=valid_days),
1602 )
1603 .serial_number(
1604 x509.random_serial_number(),
1605 )
1606 .public_key(
1607 key.public_key(),
1608 )
1609 .add_extension(
1610 x509.BasicConstraints(ca=False, path_length=None),
1611 critical=True,
1612 )
1613 .sign(
1614 private_key=key,
1615 algorithm=hashes.SHA256(),
1616 )
1617 )
1618
1619 cert_pem = cert.public_bytes(
1620 encoding=serialization.Encoding.PEM,
1621 )
1622 key_pem = key.private_bytes(
1623 encoding=serialization.Encoding.PEM,
1624 format=serialization.PrivateFormat.TraditionalOpenSSL,
1625 encryption_algorithm=serialization.NoEncryption(),
1626 )
1627
1628 return key_pem, cert_pem
1629
1630
1631 def generate_priv_pub_key_pair(keylength: int = 2048) -> tuple[bytes, bytes]:
1632 from cryptography.hazmat.primitives import serialization
1633 from cryptography.hazmat.primitives.asymmetric import rsa
1634
1635 key = rsa.generate_private_key(
1636 public_exponent=65537,
1637 key_size=keylength,
1638 )
1639 priv_key_pem = key.private_bytes(
1640 encoding=serialization.Encoding.PEM,
1641 format=serialization.PrivateFormat.TraditionalOpenSSL,
1642 encryption_algorithm=serialization.NoEncryption(),
1643 )
1644 pub_key_pem = key.public_key().public_bytes(
1645 encoding=serialization.Encoding.PEM,
1646 format=serialization.PublicFormat.SubjectPublicKeyInfo,
1647 )
1648
1649 return priv_key_pem, pub_key_pem
1650
1651
1652 def generate_keys(opts: UkifyConfig) -> None:
1653 work = False
1654
1655 # This will generate keys and certificates and write them to the paths that
1656 # are specified as input paths.
1657 if opts.sb_key and opts.sb_cert:
1658 fqdn = socket.getfqdn()
1659
1660 cn = f'SecureBoot signing key on host {fqdn}'
1661 if len(cn) > 64:
1662 # The length of CN must not exceed 64 bytes
1663 cn = cn[:61] + '...'
1664
1665 key_pem, cert_pem = generate_key_cert_pair(
1666 common_name=cn,
1667 valid_days=opts.sb_cert_validity,
1668 )
1669 print(f'Writing SecureBoot private key to {opts.sb_key}', file=sys.stderr)
1670 with temporary_umask(0o077):
1671 Path(opts.sb_key).write_bytes(key_pem)
1672 print(f'Writing SecureBoot certificate to {opts.sb_cert}', file=sys.stderr)
1673 Path(opts.sb_cert).write_bytes(cert_pem)
1674
1675 work = True
1676
1677 for priv_key, pub_key, _, _ in key_path_groups(opts):
1678 priv_key_pem, pub_key_pem = generate_priv_pub_key_pair()
1679
1680 print(f'Writing private key for PCR signing to {priv_key}', file=sys.stderr)
1681 with temporary_umask(0o077):
1682 Path(priv_key).write_bytes(priv_key_pem)
1683 if pub_key:
1684 print(f'Writing public key for PCR signing to {pub_key}', file=sys.stderr)
1685 Path(pub_key).write_bytes(pub_key_pem)
1686
1687 work = True
1688
1689 if not work:
1690 raise ValueError(
1691 'genkey: --secureboot-private-key=/--secureboot-certificate= or --pcr-private-key/--pcr-public-key must be specified'
1692 )
1693
1694
1695 def inspect_section(
1696 opts: UkifyConfig,
1697 section: pefile.SectionStructure,
1698 ) -> tuple[str, Optional[dict[str, Union[int, str]]]]:
1699 name = pe_strip_section_name(section.Name)
1700
1701 # find the config for this section in opts and whether to show it
1702 config = opts.sections_by_name.get(name, None)
1703 show = config or opts.all or (name in DEFAULT_SECTIONS_TO_SHOW and not opts.sections)
1704 if not show:
1705 return name, None
1706
1707 ttype = config.output_mode if config else DEFAULT_SECTIONS_TO_SHOW.get(name, 'binary')
1708
1709 size = pe_section_size(section)
1710 data = section.get_data(length=size)
1711 digest = sha256(data).hexdigest()
1712
1713 struct: dict[str, Union[int, str]] = {
1714 'size': size,
1715 'sha256': digest,
1716 }
1717
1718 if ttype == 'text':
1719 try:
1720 struct['text'] = data.rstrip(b'\0').replace(b'\0', b'\\0').decode()
1721 except UnicodeDecodeError as e:
1722 print(f'Section {name!r} is not valid text: {e}', file=sys.stderr)
1723 struct['text'] = '(not valid UTF-8)'
1724
1725 if config and config.content:
1726 assert isinstance(config.content, Path)
1727 config.content.write_bytes(data)
1728
1729 if opts.json == 'off':
1730 print(f'{name}:\n size: {size} bytes\n sha256: {digest}')
1731 if ttype == 'text':
1732 text = textwrap.indent(cast(str, struct['text']).rstrip(), ' ' * 4)
1733 print(f' text:\n{text}')
1734
1735 return name, struct
1736
1737
1738 def inspect_sections(opts: UkifyConfig) -> None:
1739 indent = 4 if opts.json == 'pretty' else None
1740
1741 for file in opts.files:
1742 pe = pefile.PE(file, fast_load=True)
1743 gen = (inspect_section(opts, section) for section in pe.sections)
1744 descs = {key: val for (key, val) in gen if val}
1745 if opts.json != 'off':
1746 json.dump(descs, sys.stdout, indent=indent)
1747
1748
1749 @dataclasses.dataclass(frozen=True)
1750 class ConfigItem:
1751 @staticmethod
1752 def config_list_prepend(
1753 namespace: argparse.Namespace,
1754 group: Optional[str],
1755 dest: str,
1756 value: Any,
1757 ) -> None:
1758 "Prepend value to namespace.<dest>"
1759
1760 assert not group
1761
1762 old = getattr(namespace, dest, [])
1763 if old is None:
1764 old = []
1765 setattr(namespace, dest, value + old)
1766
1767 @staticmethod
1768 def config_set_if_unset(
1769 namespace: argparse.Namespace,
1770 group: Optional[str],
1771 dest: str,
1772 value: Any,
1773 ) -> None:
1774 "Set namespace.<dest> to value only if it was None"
1775
1776 assert not group
1777
1778 if getattr(namespace, dest) is None:
1779 setattr(namespace, dest, value)
1780
1781 @staticmethod
1782 def config_set(
1783 namespace: argparse.Namespace,
1784 group: Optional[str],
1785 dest: str,
1786 value: Any,
1787 ) -> None:
1788 "Set namespace.<dest> to value only if it was None"
1789
1790 assert not group
1791
1792 setattr(namespace, dest, value)
1793
1794 @staticmethod
1795 def config_set_group(
1796 namespace: argparse.Namespace,
1797 group: Optional[str],
1798 dest: str,
1799 value: Any,
1800 ) -> None:
1801 "Set namespace.<dest>[idx] to value, with idx derived from group"
1802
1803 # pylint: disable=protected-access
1804 if group not in namespace._groups:
1805 namespace._groups += [group]
1806 idx = namespace._groups.index(group)
1807
1808 old = getattr(namespace, dest, None)
1809 if old is None:
1810 old = []
1811 setattr(
1812 namespace,
1813 dest,
1814 old + ([None] * (idx - len(old))) + [value],
1815 )
1816
1817 @staticmethod
1818 def parse_boolean(s: str) -> bool:
1819 "Parse 1/true/yes/y/t/on as true and 0/false/no/n/f/off/None as false"
1820 s_l = s.lower()
1821 if s_l in {'1', 'true', 'yes', 'y', 't', 'on'}:
1822 return True
1823 if s_l in {'0', 'false', 'no', 'n', 'f', 'off'}:
1824 return False
1825 raise ValueError('f"Invalid boolean literal: {s!r}')
1826
1827 # arguments for argparse.ArgumentParser.add_argument()
1828 name: Union[str, tuple[str, str]]
1829 dest: Optional[str] = None
1830 metavar: Optional[str] = None
1831 type: Optional[Callable[[str], Any]] = None
1832 nargs: Optional[str] = None
1833 action: Optional[Union[str, Callable[[str], Any], builtins.type[argparse.Action]]] = None
1834 default: Any = None
1835 version: Optional[str] = None
1836 choices: Optional[tuple[str, ...]] = None
1837 const: Optional[Any] = None
1838 help: Optional[str] = None
1839
1840 # metadata for config file parsing
1841 config_key: Optional[str] = None
1842 config_push: Callable[[argparse.Namespace, Optional[str], str, Any], None] = config_set_if_unset
1843
1844 def _names(self) -> tuple[str, ...]:
1845 return self.name if isinstance(self.name, tuple) else (self.name,)
1846
1847 def argparse_dest(self) -> str:
1848 # It'd be nice if argparse exported this, but I don't see that in the API
1849 if self.dest:
1850 return self.dest
1851 return self._names()[0].lstrip('-').replace('-', '_')
1852
1853 def add_to(self, parser: argparse.ArgumentParser) -> None:
1854 kwargs = {
1855 key: val
1856 for key in dataclasses.asdict(self)
1857 if (key not in ('name', 'config_key', 'config_push') and (val := getattr(self, key)) is not None)
1858 }
1859 args = self._names()
1860 parser.add_argument(*args, **kwargs)
1861
1862 def parse_value(self, s: str) -> Any:
1863 if self.action == argparse.BooleanOptionalAction:
1864 # We need to handle this case separately: the options are called
1865 # --foo and --no-foo, and no argument is parsed. But in the config
1866 # file, we have Foo=yes or Foo=no.
1867 return self.parse_boolean(s)
1868 elif self.type:
1869 return self.type(s)
1870
1871 return s
1872
1873 def apply_config(
1874 self,
1875 namespace: argparse.Namespace,
1876 section: str,
1877 group: Optional[str],
1878 key: str,
1879 value: Any,
1880 ) -> None:
1881 assert f'{section}/{key}' == self.config_key
1882 dest = self.argparse_dest()
1883
1884 # This is a bit ugly, but --initrd and --devicetree-auto are the only options
1885 # with multiple args on the command line and a space-separated list in the
1886 # config file.
1887 if self.name in ['--initrd', '--devicetree-auto']:
1888 value = [self.parse_value(v) for v in value.split()]
1889 else:
1890 value = self.parse_value(value)
1891
1892 self.config_push(namespace, group, dest, value)
1893
1894 def config_example(self) -> tuple[Optional[str], Optional[str], Optional[str]]:
1895 if not self.config_key:
1896 return None, None, None
1897 section_name, key = self.config_key.split('/', 1)
1898 if section_name.endswith(':'):
1899 section_name += 'NAME'
1900 if self.choices:
1901 value = '|'.join(self.choices)
1902 else:
1903 value = self.metavar or self.argparse_dest().upper()
1904 return (section_name, key, value)
1905
1906
1907 VERBS = ('build', 'genkey', 'inspect')
1908
1909 CONFIG_ITEMS = [
1910 ConfigItem(
1911 'positional',
1912 metavar='VERB',
1913 nargs='*',
1914 help=argparse.SUPPRESS,
1915 ),
1916 ConfigItem(
1917 '--version',
1918 action='version',
1919 version=f'ukify {__version__}',
1920 ),
1921 ConfigItem(
1922 '--summary',
1923 help='print parsed config and exit',
1924 action='store_true',
1925 ),
1926 ConfigItem(
1927 ('--config', '-c'),
1928 metavar='PATH',
1929 type=Path,
1930 help='configuration file',
1931 ),
1932 ConfigItem(
1933 '--linux',
1934 type=Path,
1935 help='vmlinuz file [.linux section]',
1936 config_key='UKI/Linux',
1937 ),
1938 ConfigItem(
1939 '--os-release',
1940 metavar='TEXT|@PATH',
1941 help='path to os-release file [.osrel section]',
1942 config_key='UKI/OSRelease',
1943 ),
1944 ConfigItem(
1945 '--cmdline',
1946 metavar='TEXT|@PATH',
1947 help='kernel command line [.cmdline section]',
1948 config_key='UKI/Cmdline',
1949 ),
1950 ConfigItem(
1951 '--initrd',
1952 metavar='INITRD',
1953 type=Path,
1954 action='append',
1955 help='initrd file [part of .initrd section]',
1956 config_key='UKI/Initrd',
1957 config_push=ConfigItem.config_list_prepend,
1958 ),
1959 ConfigItem(
1960 '--efifw',
1961 metavar='DIR',
1962 type=Path,
1963 action='append',
1964 default=[],
1965 help='Directory with efi firmware binary file [.efifw section]',
1966 config_key='UKI/Firmware',
1967 config_push=ConfigItem.config_list_prepend,
1968 ),
1969 ConfigItem(
1970 '--microcode',
1971 metavar='UCODE',
1972 type=Path,
1973 help='microcode file [.ucode section]',
1974 config_key='UKI/Microcode',
1975 ),
1976 ConfigItem(
1977 '--splash',
1978 metavar='BMP',
1979 type=Path,
1980 help='splash image bitmap file [.splash section]',
1981 config_key='UKI/Splash',
1982 ),
1983 ConfigItem(
1984 '--devicetree',
1985 metavar='PATH',
1986 type=Path,
1987 help='Device Tree file [.dtb section]',
1988 config_key='UKI/DeviceTree',
1989 ),
1990 ConfigItem(
1991 '--devicetree-auto',
1992 metavar='PATH',
1993 type=Path,
1994 action='append',
1995 help='DeviceTree file for automatic selection [.dtbauto section]',
1996 default=[],
1997 config_key='UKI/DeviceTreeAuto',
1998 config_push=ConfigItem.config_list_prepend,
1999 ),
2000 ConfigItem(
2001 '--hwids',
2002 metavar='DIR',
2003 help='Directory with HWID text files [.hwids section]',
2004 config_key='UKI/HWIDs',
2005 ),
2006 ConfigItem(
2007 '--uname',
2008 metavar='VERSION',
2009 help='"uname -r" information [.uname section]',
2010 config_key='UKI/Uname',
2011 ),
2012 ConfigItem(
2013 '--sbat',
2014 metavar='TEXT|@PATH',
2015 help='SBAT policy [.sbat section]',
2016 default=[],
2017 action='append',
2018 config_key='UKI/SBAT',
2019 ),
2020 ConfigItem(
2021 '--pcrpkey',
2022 metavar='KEY',
2023 type=Path,
2024 help='embedded public key to seal secrets to [.pcrpkey section]',
2025 config_key='UKI/PCRPKey',
2026 ),
2027 ConfigItem(
2028 '--section',
2029 dest='sections',
2030 metavar='NAME:TEXT|@PATH',
2031 action='append',
2032 default=[],
2033 help='section as name and contents [NAME section] or section to print',
2034 ),
2035 ConfigItem(
2036 '--profile',
2037 metavar='TEST|@PATH',
2038 help='Profile information [.profile section]',
2039 config_key='UKI/Profile',
2040 ),
2041 ConfigItem(
2042 '--join-profile',
2043 dest='join_profiles',
2044 metavar='PATH',
2045 action='append',
2046 default=[],
2047 help='A PE binary containing an additional profile to add to the UKI',
2048 ),
2049 ConfigItem(
2050 '--sign-profile',
2051 dest='sign_profiles',
2052 metavar='ID',
2053 action='append',
2054 default=[],
2055 help='Which profiles to sign expected PCR measurements for',
2056 ),
2057 ConfigItem(
2058 '--pcrsig',
2059 metavar='TEST|@PATH',
2060 help='Signed PCR policy JSON [.pcrsig section] to append to an existing UKI',
2061 config_key='UKI/PCRSig',
2062 ),
2063 ConfigItem(
2064 '--join-pcrsig',
2065 metavar='PATH',
2066 help='A PE binary containing a UKI without a .pcrsig to join with --pcrsig',
2067 ),
2068 ConfigItem(
2069 '--efi-arch',
2070 metavar='ARCH',
2071 choices=('ia32', 'x64', 'arm', 'aa64', 'riscv32', 'riscv64', 'loongarch32', 'loongarch64'),
2072 help='target EFI architecture',
2073 config_key='UKI/EFIArch',
2074 ),
2075 ConfigItem(
2076 '--stub',
2077 type=Path,
2078 help='path to the sd-stub file [.text,.data,… sections]',
2079 config_key='UKI/Stub',
2080 ),
2081 ConfigItem(
2082 '--pcr-banks',
2083 metavar='BANK…',
2084 type=parse_banks,
2085 config_key='UKI/PCRBanks',
2086 ),
2087 ConfigItem(
2088 '--signing-engine',
2089 metavar='ENGINE',
2090 help='OpenSSL engine to use for signing',
2091 config_key='UKI/SigningEngine',
2092 ),
2093 ConfigItem(
2094 '--signing-provider',
2095 metavar='PROVIDER',
2096 help='OpenSSL provider to use for signing',
2097 config_key='UKI/SigningProvider',
2098 ),
2099 ConfigItem(
2100 '--certificate-provider',
2101 metavar='PROVIDER',
2102 help='OpenSSL provider to load certificate from',
2103 config_key='UKI/CertificateProvider',
2104 ),
2105 ConfigItem(
2106 '--signtool',
2107 choices=('sbsign', 'pesign', 'systemd-sbsign'),
2108 dest='signtool',
2109 help=(
2110 'whether to use sbsign or pesign. It will also be inferred by the other '
2111 'parameters given: when using --secureboot-{private-key/certificate}, sbsign '
2112 'will be used, otherwise pesign will be used'
2113 ),
2114 config_key='UKI/SecureBootSigningTool',
2115 ),
2116 ConfigItem(
2117 '--secureboot-private-key',
2118 dest='sb_key',
2119 help='required by --signtool=sbsign|systemd-sbsign. Path to key file or engine/provider designation for SB signing',
2120 config_key='UKI/SecureBootPrivateKey',
2121 ),
2122 ConfigItem(
2123 '--secureboot-certificate',
2124 dest='sb_cert',
2125 help=(
2126 'required by --signtool=sbsign. sbsign needs a path to certificate file or engine-specific designation for SB signing'
2127 ),
2128 config_key='UKI/SecureBootCertificate',
2129 ),
2130 ConfigItem(
2131 '--secureboot-certificate-dir',
2132 dest='sb_certdir',
2133 default='/etc/pki/pesign',
2134 help=(
2135 'required by --signtool=pesign. Path to nss certificate database directory for PE signing. Default is /etc/pki/pesign'
2136 ),
2137 config_key='UKI/SecureBootCertificateDir',
2138 config_push=ConfigItem.config_set,
2139 ),
2140 ConfigItem(
2141 '--secureboot-certificate-name',
2142 dest='sb_cert_name',
2143 help=(
2144 'required by --signtool=pesign. pesign needs a certificate nickname of nss certificate database entry to use for PE signing'
2145 ),
2146 config_key='UKI/SecureBootCertificateName',
2147 ),
2148 ConfigItem(
2149 '--secureboot-certificate-validity',
2150 metavar='DAYS',
2151 type=int,
2152 dest='sb_cert_validity',
2153 default=365 * 10,
2154 help="period of validity (in days) for a certificate created by 'genkey'",
2155 config_key='UKI/SecureBootCertificateValidity',
2156 config_push=ConfigItem.config_set,
2157 ),
2158 ConfigItem(
2159 '--sign-kernel',
2160 action=argparse.BooleanOptionalAction,
2161 help='Sign the embedded kernel',
2162 config_key='UKI/SignKernel',
2163 ),
2164 ConfigItem(
2165 '--pcr-private-key',
2166 dest='pcr_private_keys',
2167 action='append',
2168 help='private part of the keypair or engine/provider designation for signing PCR signatures',
2169 config_key='PCRSignature:/PCRPrivateKey',
2170 config_push=ConfigItem.config_set_group,
2171 ),
2172 ConfigItem(
2173 '--pcr-public-key',
2174 dest='pcr_public_keys',
2175 metavar='PATH',
2176 action='append',
2177 help='public part of the keypair or engine/provider designation for signing PCR signatures',
2178 config_key='PCRSignature:/PCRPublicKey',
2179 config_push=ConfigItem.config_set_group,
2180 ),
2181 ConfigItem(
2182 '--pcr-certificate',
2183 dest='pcr_certificates',
2184 metavar='PATH',
2185 action='append',
2186 help='certificate part of the keypair or engine/provider designation for signing PCR signatures',
2187 config_key='PCRSignature:/PCRCertificate',
2188 config_push=ConfigItem.config_set_group,
2189 ),
2190 ConfigItem(
2191 '--phases',
2192 dest='phase_path_groups',
2193 metavar='PHASE-PATH…',
2194 type=parse_phase_paths,
2195 action='append',
2196 help='phase-paths to create signatures for',
2197 config_key='PCRSignature:/Phases',
2198 config_push=ConfigItem.config_set_group,
2199 ),
2200 ConfigItem(
2201 '--tools',
2202 type=Path,
2203 action='append',
2204 help='Directories to search for tools (systemd-measure, …)',
2205 ),
2206 ConfigItem(
2207 ('--output', '-o'),
2208 type=Path,
2209 help='output file path',
2210 ),
2211 ConfigItem(
2212 '--measure',
2213 action=argparse.BooleanOptionalAction,
2214 help='print systemd-measure output for the UKI',
2215 ),
2216 ConfigItem(
2217 '--policy-digest',
2218 action=argparse.BooleanOptionalAction,
2219 help='print systemd-measure policy digests for the UKI',
2220 ),
2221 ConfigItem(
2222 '--json',
2223 choices=('pretty', 'short', 'off'),
2224 default='off',
2225 help='generate JSON output',
2226 ),
2227 ConfigItem(
2228 '-j',
2229 dest='json',
2230 action='store_const',
2231 const='pretty',
2232 help='equivalent to --json=pretty',
2233 ),
2234 ConfigItem(
2235 '--all',
2236 help='print all sections',
2237 action='store_true',
2238 ),
2239 ]
2240
2241 CONFIGFILE_ITEMS = {item.config_key: item for item in CONFIG_ITEMS if item.config_key}
2242
2243
2244 def apply_config(namespace: argparse.Namespace, filename: Union[str, Path, None] = None) -> None:
2245 if filename is None:
2246 if namespace.config:
2247 # Config set by the user, use that.
2248 filename = namespace.config
2249 print(f'Using config file: {filename}', file=sys.stderr)
2250 else:
2251 # Try to look for a config file then use the first one found.
2252 for config_dir in DEFAULT_CONFIG_DIRS:
2253 filename = Path(config_dir) / DEFAULT_CONFIG_FILE
2254 if filename.is_file():
2255 # Found a config file, use it.
2256 print(f'Using found config file: {filename}', file=sys.stderr)
2257 break
2258 else:
2259 # No config file specified or found, nothing to do.
2260 return
2261
2262 # Fill in ._groups based on --pcr-public-key=, --pcr-private-key=, and --phases=.
2263 assert '_groups' not in namespace
2264 n_pcr_priv = len(namespace.pcr_private_keys or ())
2265 namespace._groups = list(range(n_pcr_priv)) # pylint: disable=protected-access
2266
2267 cp = configparser.ConfigParser(
2268 comment_prefixes='#',
2269 inline_comment_prefixes='#',
2270 delimiters='=',
2271 empty_lines_in_values=False,
2272 interpolation=None,
2273 strict=False,
2274 )
2275 # Do not make keys lowercase
2276 cp.optionxform = lambda option: option # type: ignore
2277
2278 # The API is not great.
2279 read = cp.read(filename)
2280 if not read:
2281 raise OSError(f'Failed to read {filename}')
2282
2283 for section_name, section in cp.items():
2284 idx = section_name.find(':')
2285 if idx >= 0:
2286 section_name, group = section_name[: idx + 1], section_name[idx + 1 :]
2287 if not section_name or not group:
2288 raise ValueError('Section name components cannot be empty')
2289 if ':' in group:
2290 raise ValueError('Section name cannot contain more than one ":"')
2291 else:
2292 group = None
2293 for key, value in section.items():
2294 if item := CONFIGFILE_ITEMS.get(f'{section_name}/{key}'):
2295 item.apply_config(namespace, section_name, group, key, value)
2296 else:
2297 print(f'Unknown config setting [{section_name}] {key}=', file=sys.stderr)
2298
2299
2300 def config_example() -> Iterator[str]:
2301 prev_section: Optional[str] = None
2302 for item in CONFIG_ITEMS:
2303 section, key, value = item.config_example()
2304 if section:
2305 if prev_section != section:
2306 if prev_section:
2307 yield ''
2308 yield f'[{section}]'
2309 prev_section = section
2310 yield f'{key} = {value}'
2311
2312
2313 class PagerHelpAction(argparse._HelpAction): # pylint: disable=protected-access
2314 def __call__(
2315 self,
2316 parser: argparse.ArgumentParser,
2317 namespace: argparse.Namespace,
2318 values: Union[str, Sequence[Any], None] = None,
2319 option_string: Optional[str] = None,
2320 ) -> None:
2321 page(parser.format_help(), True)
2322 parser.exit()
2323
2324
2325 def create_parser() -> argparse.ArgumentParser:
2326 p = argparse.ArgumentParser(
2327 description='Build and sign Unified Kernel Images',
2328 usage='\n '
2329 + textwrap.dedent('''\
2330 ukify {b}build{e} [--linux=LINUX] [--initrd=INITRD] [options…]
2331 ukify {b}genkey{e} [options…]
2332 ukify {b}inspect{e} FILE… [options…]
2333 ''').format(b=Style.bold, e=Style.reset),
2334 allow_abbrev=False,
2335 add_help=False,
2336 epilog='\n '.join(('config file:', *config_example())),
2337 formatter_class=argparse.RawDescriptionHelpFormatter,
2338 )
2339
2340 for item in CONFIG_ITEMS:
2341 item.add_to(p)
2342
2343 # Suppress printing of usage synopsis on errors
2344 p.error = lambda message: p.exit(2, f'{p.prog}: error: {message}\n') # type: ignore
2345
2346 # Make --help paged
2347 p.add_argument(
2348 '-h', '--help',
2349 action=PagerHelpAction,
2350 help='show this help message and exit',
2351 ) # fmt: skip
2352
2353 return p
2354
2355
2356 def resolve_at_path(value: Optional[str]) -> Union[Path, str, None]:
2357 if value and value.startswith('@'):
2358 return Path(value[1:])
2359
2360 return value
2361
2362
2363 def finalize_options(opts: argparse.Namespace) -> None:
2364 # Figure out which syntax is being used, one of:
2365 # ukify verb --arg --arg --arg
2366 # ukify linux initrd…
2367 if len(opts.positional) >= 1 and opts.positional[0] == 'inspect':
2368 opts.verb = opts.positional[0]
2369 opts.files = opts.positional[1:]
2370 if not opts.files:
2371 raise ValueError('file(s) to inspect must be specified')
2372 if len(opts.files) > 1 and opts.json != 'off':
2373 # We could allow this in the future, but we need to figure out the right structure
2374 raise ValueError('JSON output is not allowed with multiple files')
2375 elif len(opts.positional) == 1 and opts.positional[0] in VERBS:
2376 opts.verb = opts.positional[0]
2377 elif opts.linux or opts.initrd:
2378 raise ValueError('--linux=/--initrd= options cannot be used with positional arguments')
2379 else:
2380 print("Assuming obsolete command line syntax with no verb. Please use 'build'.", file=sys.stderr)
2381 if opts.positional:
2382 opts.linux = Path(opts.positional[0])
2383 # If we have initrds from parsing config files, append our positional args at the end
2384 opts.initrd = (opts.initrd or []) + [Path(arg) for arg in opts.positional[1:]]
2385 opts.verb = 'build'
2386
2387 # Check that --pcr-public-key=, --pcr-private-key=, and --phases=
2388 # have either the same number of arguments or are not specified at all.
2389 # Also check that --pcr-public-key= and --pcr-certificate= are not set at the same time.
2390 # But allow a single public key, for offline PCR signing, to pre-populate the JSON object
2391 # with the certificate's fingerprint.
2392 n_pcr_cert = None if opts.pcr_certificates is None else len(opts.pcr_certificates)
2393 n_pcr_pub = None if opts.pcr_public_keys is None else len(opts.pcr_public_keys)
2394 n_pcr_priv = None if opts.pcr_private_keys is None else len(opts.pcr_private_keys)
2395 n_phase_path_groups = None if opts.phase_path_groups is None else len(opts.phase_path_groups)
2396 if opts.policy_digest and n_pcr_priv is not None:
2397 raise ValueError('--pcr-private-key= cannot be specified with --policy-digest')
2398 if (
2399 opts.policy_digest
2400 and (n_pcr_pub is None or n_pcr_pub != 1)
2401 and (n_pcr_cert is None or n_pcr_cert != 1)
2402 ):
2403 raise ValueError('--policy-digest requires exactly one --pcr-public-key= or --pcr-certificate=')
2404 if n_pcr_pub is not None and n_pcr_priv is not None and n_pcr_pub != n_pcr_priv:
2405 raise ValueError('--pcr-public-key= specifications must match --pcr-private-key=')
2406 if n_pcr_cert is not None and n_pcr_priv is not None and n_pcr_cert != n_pcr_priv:
2407 raise ValueError('--pcr-certificate= specifications must match --pcr-private-key=')
2408 if n_pcr_pub is not None and n_pcr_cert is not None:
2409 raise ValueError('--pcr-public-key= and --pcr-certificate= cannot be used at the same time')
2410 if n_phase_path_groups is not None and n_phase_path_groups != n_pcr_priv:
2411 raise ValueError('--phases= specifications must match --pcr-private-key=')
2412
2413 opts.cmdline = resolve_at_path(opts.cmdline)
2414
2415 if isinstance(opts.cmdline, str):
2416 # Drop whitespace from the command line. If we're reading from a file,
2417 # we copy the contents verbatim. But configuration specified on the command line
2418 # or in the config file may contain additional whitespace that has no meaning.
2419 opts.cmdline = ' '.join(opts.cmdline.split())
2420
2421 opts.os_release = resolve_at_path(opts.os_release)
2422
2423 if opts.os_release == '':
2424 # If --os-release= with an empty string was passed, treat that as
2425 # explicitly disabling the .osrel section, and do not fallback to the
2426 # system's os-release files.
2427 pass
2428 elif opts.os_release is None and opts.linux:
2429 p = Path('/etc/os-release')
2430 if not p.exists():
2431 p = Path('/usr/lib/os-release')
2432 opts.os_release = p
2433
2434 if opts.efi_arch is None:
2435 opts.efi_arch = guess_efi_arch()
2436
2437 if opts.stub is None and not opts.join_pcrsig:
2438 if opts.linux is not None:
2439 opts.stub = Path(f'/usr/lib/systemd/boot/efi/linux{opts.efi_arch}.efi.stub')
2440 else:
2441 opts.stub = Path(f'/usr/lib/systemd/boot/efi/addon{opts.efi_arch}.efi.stub')
2442
2443 if opts.signing_engine and opts.signing_provider:
2444 raise ValueError('Only one of --signing-engine= and --signing-provider= may be specified')
2445
2446 if opts.signing_engine is None and opts.signing_provider is None and opts.sb_key:
2447 opts.sb_key = Path(opts.sb_key)
2448
2449 if opts.certificate_provider is None and opts.sb_cert:
2450 opts.sb_cert = Path(opts.sb_cert)
2451
2452 if bool(opts.sb_key) ^ bool(opts.sb_cert):
2453 # one param only given, sbsign needs both
2454 raise ValueError(
2455 '--secureboot-private-key= and --secureboot-certificate= must be specified together'
2456 )
2457 elif bool(opts.sb_key) and bool(opts.sb_cert):
2458 # both param given, infer sbsign and in case it was given, ensure signtool=sbsign
2459 if opts.signtool and opts.signtool not in ('sbsign', 'systemd-sbsign'):
2460 raise ValueError(
2461 f'Cannot provide --signtool={opts.signtool} with --secureboot-private-key= and --secureboot-certificate='
2462 )
2463 if not opts.signtool:
2464 opts.signtool = 'sbsign'
2465 elif bool(opts.sb_cert_name):
2466 # sb_cert_name given, infer pesign and in case it was given, ensure signtool=pesign
2467 if opts.signtool and opts.signtool != 'pesign':
2468 raise ValueError(
2469 f'Cannot provide --signtool={opts.signtool} with --secureboot-certificate-name='
2470 )
2471 opts.signtool = 'pesign'
2472
2473 if opts.signing_provider and opts.signtool != 'systemd-sbsign':
2474 raise ValueError('--signing-provider= can only be used with --signtool=systemd-sbsign')
2475
2476 if opts.certificate_provider and opts.signtool != 'systemd-sbsign':
2477 raise ValueError('--certificate-provider= can only be used with --signtool=systemd-sbsign')
2478
2479 if opts.sign_kernel and not opts.sb_key and not opts.sb_cert_name:
2480 raise ValueError(
2481 '--sign-kernel requires either --secureboot-private-key= and --secureboot-certificate= (for sbsign) or --secureboot-certificate-name= (for pesign) to be specified'
2482 )
2483
2484 opts.profile = resolve_at_path(opts.profile)
2485 if opts.profile and isinstance(opts.profile, Path):
2486 opts.profile = opts.profile.read_text()
2487
2488 if opts.join_profiles and not opts.profile:
2489 # If any additional profiles are added, we need a base profile as well so add one if
2490 # one wasn't explicitly provided
2491 opts.profile = 'ID=main'
2492
2493 if opts.pcrsig and not opts.join_pcrsig:
2494 raise ValueError('--pcrsig requires --join-pcrsig')
2495 if opts.join_pcrsig and not opts.pcrsig:
2496 raise ValueError('--join-pcrsig requires --pcrsig')
2497 if opts.pcrsig and (
2498 opts.linux
2499 or opts.initrd
2500 or opts.profile
2501 or opts.join_profiles
2502 or opts.microcode
2503 or opts.sbat
2504 or opts.uname
2505 or opts.os_release
2506 or opts.cmdline
2507 or opts.hwids
2508 or opts.splash
2509 or opts.devicetree
2510 or opts.devicetree_auto
2511 or opts.pcr_private_keys
2512 or opts.pcr_public_keys
2513 or opts.pcr_certificates
2514 ):
2515 raise ValueError('--pcrsig and --join-pcrsig cannot be used with other sections')
2516 if opts.pcrsig:
2517 opts.pcrsig = resolve_at_path(opts.pcrsig)
2518 if isinstance(opts.pcrsig, Path):
2519 opts.pcrsig = opts.pcrsig.read_text()
2520
2521 if opts.verb == 'build' and opts.output is None:
2522 if opts.linux is None:
2523 raise ValueError('--output= must be specified when building a PE addon')
2524 suffix = '.efi' if opts.sb_key or opts.sb_cert_name else '.unsigned.efi'
2525 opts.output = opts.linux.name + suffix
2526
2527 # Now that we know if we're inputting or outputting, really parse section config
2528 f = Section.parse_output if opts.verb == 'inspect' else Section.parse_input
2529 opts.sections = [f(s) for s in opts.sections]
2530 # A convenience dictionary to make it easy to look up sections
2531 opts.sections_by_name = {s.name: s for s in opts.sections}
2532
2533
2534 def parse_args(args: Optional[list[str]] = None) -> argparse.Namespace:
2535 opts = create_parser().parse_args(args)
2536
2537 # argparse puts some unknown options in opts.positional. Make sure we don't
2538 # try to interpret something that is an option as a positional argument.
2539 if any((bad_opt := o).startswith('-') for o in opts.positional):
2540 raise ValueError(f'Unknown option: {bad_opt.partition("=")[0]}')
2541
2542 apply_config(opts)
2543 finalize_options(opts)
2544 return opts
2545
2546
2547 def main() -> None:
2548 opts = UkifyConfig.from_namespace(parse_args())
2549 if opts.summary:
2550 # TODO: replace pprint() with some fancy formatting.
2551 pprint.pprint(vars(opts))
2552 elif opts.verb == 'build':
2553 check_inputs(opts)
2554 make_uki(opts)
2555 elif opts.verb == 'genkey':
2556 check_cert_and_keys_nonexistent(opts)
2557 generate_keys(opts)
2558 elif opts.verb == 'inspect':
2559 inspect_sections(opts)
2560 else:
2561 assert False
2562
2563
2564 if __name__ == '__main__':
2565 main()