]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd-stub.xml
man: "the initial RAM disk" → "the initrd"
[thirdparty/systemd.git] / man / systemd-stub.xml
1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6 <refentry id="systemd-stub" conditional='HAVE_GNU_EFI'
7 xmlns:xi="http://www.w3.org/2001/XInclude">
8 <refentryinfo>
9 <title>systemd-stub</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>systemd-stub</refentrytitle>
15 <manvolnum>7</manvolnum>
16 </refmeta>
17
18 <refnamediv>
19 <refname>systemd-stub</refname>
20 <refname>sd-stub</refname>
21 <refname>linuxx64.efi.stub</refname>
22 <refname>linuxia32.efi.stub</refname>
23 <refname>linuxaa64.efi.stub</refname>
24 <refpurpose>A simple UEFI kernel boot stub</refpurpose>
25 </refnamediv>
26
27 <refsynopsisdiv>
28 <para><filename>/usr/lib/systemd/boot/efi/linuxx64.efi.stub</filename></para>
29 <para><filename>/usr/lib/systemd/boot/efi/linuxia32.efi.stub</filename></para>
30 <para><filename>/usr/lib/systemd/boot/efi/linuxaa64.efi.stub</filename></para>
31 <para><filename><replaceable>ESP</replaceable>/.../<replaceable>foo</replaceable>.efi.extra.d/*.cred</filename></para>
32 <para><filename><replaceable>ESP</replaceable>/.../<replaceable>foo</replaceable>.efi.extra.d/*.raw</filename></para>
33 <para><filename><replaceable>ESP</replaceable>/loader/credentials/*.cred</filename></para>
34 </refsynopsisdiv>
35
36 <refsect1>
37 <title>Description</title>
38
39 <para><command>systemd-stub</command> (stored in per-architecture files
40 <filename>linuxx64.efi.stub</filename>, <filename>linuxia32.efi.stub</filename>,
41 <filename>linuxaa64.efi.stub</filename> on disk) is a simple UEFI boot stub. An UEFI boot stub is
42 attached to a Linux kernel binary image, and is a piece of code that runs in the UEFI firmware
43 environment before transitioning into the Linux kernel environment. The UEFI boot stub ensures a Linux
44 kernel is executable as regular UEFI binary, and is able to do various preparations before switching the
45 system into the Linux world.</para>
46
47 <para>The UEFI boot stub looks for various resources for the kernel invocation inside the UEFI PE binary
48 itself. This allows combining various resources inside a single PE binary image (usually called "Unified
49 Kernel Image", or "UKI" for short), which may then be signed via UEFI SecureBoot as a whole, covering all
50 individual resources at once. Specifically it may include:</para>
51
52 <itemizedlist>
53 <listitem><para>The ELF Linux kernel images will be looked for in the <literal>.linux</literal> PE
54 section of the executed image.</para></listitem>
55
56 <listitem><para>OS release information, i.e. the
57 <citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry> file of
58 the OS the kernel belongs to, in the <literal>.osrel</literal> PE section.</para></listitem>
59
60 <listitem><para>The initrd will be loaded from the <literal>.initrd</literal> PE section.
61 </para></listitem>
62
63 <listitem><para>A compiled binary DeviceTree will be looked for in the <literal>.dtb</literal> PE
64 section.</para></listitem>
65
66 <listitem><para>The kernel command line to pass to the invoked kernel will be looked for in the
67 <literal>.cmdline</literal> PE section.</para></listitem>
68
69 <listitem><para>A boot splash (in Windows <filename>.BMP</filename> format) to show on screen before
70 invoking the kernel will be looked for in the <literal>.splash</literal> PE section.</para></listitem>
71
72 <listitem><para>A set of cryptographic signatures for expected TPM2 PCR values when this kernel is
73 booted, in JSON format, in the <literal>.pcrsig</literal> section. This is useful for implementing TPM2
74 policies that bind disk encryption and similar to kernels that are signed by a specific
75 key.</para></listitem>
76
77 <listitem><para>A public key in PEM format matching this TPM2 PCR signature data in the
78 <literal>.pcrpkey</literal> section.</para></listitem>
79 </itemizedlist>
80
81 <para>If UEFI SecureBoot is enabled and the <literal>.cmdline</literal> section is present in the executed
82 image, any attempts to override the kernel command line by passing one as invocation parameters to the
83 EFI binary are ignored. Thus, in order to allow overriding the kernel command line, either disable UEFI
84 SecureBoot, or don't include a kernel command line PE section in the kernel image file. If a command line
85 is accepted via EFI invocation parameters to the EFI binary it is measured into TPM PCR 12 (if a TPM is
86 present).</para>
87
88 <para>If a DeviceTree is embedded in the <literal>.dtb</literal> section, it replaces an existing
89 DeviceTree in the corresponding EFI configuration table. systemd-stub will ask the firmware via the
90 <literal>EFI_DT_FIXUP_PROTOCOL</literal> for hardware specific fixups to the DeviceTree.</para>
91
92 <para>The contents of seven of these eight PE sections are measured into TPM PCR 11, that is otherwise
93 not used. Thus, it can be pre-calculated without too much effort. The <literal>.pcrsig</literal> section
94 is not included in this PCR measurement, since it's supposed to contain signatures for the expected
95 results for these measurements, i.e. of the outputs of the measurement operation, and thus cannot also be
96 input to it.</para>
97
98 <para>When <literal>.pcrsig</literal> and/or <literal>.pcrpkey</literal> are present in a unified kernel
99 image their contents are passed to the booted kernel in an synthetic initrd cpio archive that places them in the
100 <filename>/.extra/tpm2-pcr-signature.json</filename> and
101 <filename>/.extra/tpm2-pcr-public-key.pem</filename> files. Typically, a
102 <citerefentry><refentrytitle>tmpfiles.d</refentrytitle><manvolnum>5</manvolnum></citerefentry> line then
103 ensures they are copied into <filename>/run/systemd/tpm2-pcr-signature.json</filename> and
104 <filename>/run/systemd/tpm2-pcr-public-key.pem</filename> where they remain accessible even after the
105 system transitions out of the initrd environment into the host file system. Tools such
106 <citerefentry><refentrytitle>systemd-cryptsetup@.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
107 <citerefentry><refentrytitle>systemd-cryptenroll</refentrytitle><manvolnum>1</manvolnum></citerefentry>
108 and <citerefentry><refentrytitle>systemd-creds</refentrytitle><manvolnum>1</manvolnum></citerefentry>
109 will automatically use files present under these paths to unlock protected resources (encrypted storage
110 or credentials) or bind encryption to booted kernels.</para>
111 </refsect1>
112
113 <refsect1>
114 <title>Companion Files</title>
115
116 <para>The <command>systemd-stub</command> UEFI boot stub automatically collects two types of auxiliary
117 companion files optionally placed in drop-in directories on the same partition as the EFI binary,
118 dynamically generates <command>cpio</command> initrd archives from them, and passes them to the kernel.
119 Specifically:</para>
120
121 <itemizedlist>
122 <listitem><para>For a kernel binary called <filename><replaceable>foo</replaceable>.efi</filename>, it
123 will look for files with the <filename>.cred</filename> suffix in a directory named
124 <filename><replaceable>foo</replaceable>.efi.extra.d/</filename> next to it. A <command>cpio</command>
125 archive is generated from all files found that way, placing them in the
126 <filename>/.extra/credentials/</filename> directory of the initrd file hierarchy. The main initrd may
127 then access them in this directory. This is supposed to be used to store auxiliary, encrypted,
128 authenticated credentials for use with <varname>LoadCredentialEncrypted=</varname> in the UEFI System
129 Partition. See
130 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
131 and
132 <citerefentry><refentrytitle>systemd-creds</refentrytitle><manvolnum>1</manvolnum></citerefentry>
133 for
134 details on encrypted credentials. The generated <command>cpio</command> archive is measured into TPM
135 PCR 12 (if a TPM is present).</para></listitem>
136
137 <listitem><para>Similarly, files <filename><replaceable>foo</replaceable>.efi.extra.d/*.raw</filename>
138 are packed up in a <command>cpio</command> archive and placed in the <filename>/.extra/sysext/</filename>
139 directory in the initrd file hierarchy. This is supposed to be used to pass additional system extension
140 images to the initrd. See
141 <citerefentry><refentrytitle>systemd-sysext</refentrytitle><manvolnum>8</manvolnum></citerefentry> for
142 details on system extension images. The generated <command>cpio</command> archive containing these
143 system extension images is measured into TPM PCR 13 (if a TPM is present).</para></listitem>
144
145 <listitem><para>Files <filename>/loader/credentials/*.cred</filename> are packed up in a
146 <command>cpio</command> archive and placed in the <filename>/.extra/global_credentials/</filename>
147 directory of the initrd file hierarchy. This is supposed to be used to pass additional credentials to
148 the initrd, regardless of the kernel being booted. The generated <command>cpio</command> archive is
149 measured into TPM PCR 12 (if a TPM is present)</para></listitem>
150 </itemizedlist>
151
152 <para>These mechanisms may be used to parameterize and extend trusted (i.e. signed), immutable initrd
153 images in a reasonably safe way: all data they contain is measured into TPM PCRs. On access they should be
154 further validated: in case of the credentials case by encrypting/authenticating them via TPM, as exposed
155 by <command>systemd-creds encrypt -T</command> (see
156 <citerefentry><refentrytitle>systemd-creds</refentrytitle><manvolnum>1</manvolnum></citerefentry> for
157 details); in case of the system extension images by using signed Verity images.</para>
158 </refsect1>
159
160 <refsect1>
161 <title>TPM PCR Notes</title>
162
163 <para>Note that when a unified kernel using <command>systemd-stub</command> is invoked the firmware will
164 measure it as a whole to TPM PCR 4, covering all embedded resources, such as the stub code itself, the
165 core kernel, the embedded initrd and kernel command line (see above for a full list).</para>
166
167 <para>Also note that the Linux kernel will measure all initrds it receives into TPM PCR 9. This means
168 every type of initrd will be measured two or three times: the initrd embedded in the kernel image will be
169 measured to PCR 4, PCR 9 and PCR 11; the initrd synthesized from credentials will be measured to both PCR
170 9 and PCR 12; the initrd synthesized from system extensions will be measured to both PCR 4 and PCR
171 9. Let's summarize the OS resources and the PCRs they are measured to:</para>
172
173 <table>
174 <title>OS Resource PCR Summary</title>
175
176 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
177 <colspec colname="pcr" />
178 <colspec colname="definition" />
179
180 <thead>
181 <row>
182 <entry>OS Resource</entry>
183 <entry>Measurement PCR</entry>
184 </row>
185 </thead>
186
187 <tbody>
188 <row>
189 <entry><command>systemd-stub</command> code (the entry point of the unified PE binary)</entry>
190 <entry>4</entry>
191 </row>
192
193 <row>
194 <entry>Core kernel code (embedded in unified PE binary)</entry>
195 <entry>4 + 11</entry>
196 </row>
197
198 <row>
199 <entry>OS release information (embedded in the unified PE binary)</entry>
200 <entry>4 + 11</entry>
201 </row>
202
203 <row>
204 <entry>Main initrd (embedded in unified PE binary)</entry>
205 <entry>4 + 9 + 11</entry>
206 </row>
207
208 <row>
209 <entry>Default kernel command line (embedded in unified PE binary)</entry>
210 <entry>4 + 11</entry>
211 </row>
212
213 <row>
214 <entry>Overridden kernel command line</entry>
215 <entry>12</entry>
216 </row>
217
218 <row>
219 <entry>Boot splash (embedded in the unified PE binary)</entry>
220 <entry>4 + 11</entry>
221 </row>
222
223 <row>
224 <entry>TPM2 PCR signature JSON (embedded in unified PE binary, synthesized into initrd)</entry>
225 <entry>4 + 9</entry>
226 </row>
227
228 <row>
229 <entry>TPM2 PCR PEM public key (embedded in unified PE binary, synthesized into initrd)</entry>
230 <entry>4 + 9 + 11</entry>
231 </row>
232
233 <row>
234 <entry>Credentials (synthesized initrd from companion files)</entry>
235 <entry>9 + 12</entry>
236 </row>
237
238 <row>
239 <entry>System Extensions (synthesized initrd from companion files)</entry>
240 <entry>9 + 13</entry>
241 </row>
242 </tbody>
243 </tgroup>
244 </table>
245 </refsect1>
246
247 <refsect1>
248 <title>EFI Variables</title>
249
250 <para>The following EFI variables are defined, set and read by <command>systemd-stub</command>, under the
251 vendor UUID <literal>4a67b082-0a4c-41cf-b6c7-440b29bb8c4f</literal>, for communication between the boot
252 stub and the OS:</para>
253
254 <variablelist class='efi-variables'>
255 <varlistentry>
256 <term><varname>LoaderDevicePartUUID</varname></term>
257
258 <listitem><para>Contains the partition UUID of the EFI System Partition the EFI image was run
259 from. <citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
260 uses this information to automatically find the disk booted from, in order to discover various other
261 partitions on the same disk automatically.</para></listitem>
262 </varlistentry>
263
264 <varlistentry>
265 <term><varname>LoaderFirmwareInfo</varname></term>
266 <term><varname>LoaderFirmwareType</varname></term>
267
268 <listitem><para>Brief firmware information. Use
269 <citerefentry><refentrytitle>bootctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> to view this
270 data.</para></listitem>
271 </varlistentry>
272
273 <varlistentry>
274 <term><varname>LoaderImageIdentifier</varname></term>
275
276 <listitem><para>The path of EFI executable, relative to the EFI System Partition's root
277 directory. Use
278 <citerefentry><refentrytitle>bootctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> to view
279 this data.</para></listitem>
280 </varlistentry>
281
282 <varlistentry>
283 <term><varname>StubInfo</varname></term>
284
285 <listitem><para>Brief stub information. Use
286 <citerefentry><refentrytitle>bootctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> to view
287 this data.</para></listitem>
288 </varlistentry>
289
290 <varlistentry>
291 <term><varname>StubPcrKernelImage</varname></term>
292
293 <listitem><para>The PCR register index the kernel image, initrd image, boot splash, devicetree
294 database, and the embedded command line are measured into, formatted as decimal ASCII string (e.g.
295 <literal>11</literal>). This variable is set if a measurement was successfully completed, and remains
296 unset otherwise.</para></listitem>
297 </varlistentry>
298
299 <varlistentry>
300 <term><varname>StubPcrKernelParameters</varname></term>
301
302 <listitem><para>The PCR register index the kernel command line and credentials are measured into,
303 formatted as decimal ASCII string (e.g. <literal>12</literal>). This variable is set if a measurement
304 was successfully completed, and remains unset otherwise.</para></listitem>
305 </varlistentry>
306
307 <varlistentry>
308 <term><varname>StubPcrInitRDSysExts</varname></term>
309
310 <listitem><para>The PCR register index the systemd extensions for the initrd, which are picked up
311 from the file system the kernel image is located on. Formatted as decimal ASCII string (e.g.
312 <literal>13</literal>). This variable is set if a measurement was successfully completed, and remains
313 unset otherwise.</para></listitem>
314 </varlistentry>
315 </variablelist>
316
317 <para>Note that some of the variables above may also be set by the boot loader. The stub will only set
318 them if they aren't set already. Some of these variables are defined by the <ulink
319 url="https://systemd.io/BOOT_LOADER_INTERFACE">Boot Loader Interface</ulink>.</para>
320 </refsect1>
321
322 <refsect1>
323 <title>initrd Resources</title>
324
325 <para>The following resources are passed as initrd cpio archives to the booted kernel, and thus make up
326 the initial file system hierarchy in the initrd execution environment:</para>
327
328 <variablelist>
329 <varlistentry>
330 <term><filename>/</filename></term>
331
332 <listitem><para>The main initrd from the <literal>.initrd</literal> PE section of the unified kernel image.</para></listitem>
333 </varlistentry>
334
335 <varlistentry>
336 <term><filename>/.extra/credentials/*.cred</filename></term>
337 <listitem><para>Credential files (suffix <literal>.cred</literal>) that are placed next to the
338 unified kernel image (as described above) are copied into the
339 <filename>/.extra/credentials/</filename> directory in the initrd execution
340 environment.</para></listitem>
341 </varlistentry>
342
343 <varlistentry>
344 <term><filename>/.extra/global_credentials/*.cred</filename></term>
345 <listitem><para>Similar, credential files in the <filename>/loader/credentials/</filename> directory
346 in the file system the unified kernel image is placed in are copied into the
347 <filename>/.extra/global_credentials/</filename> directory in the initrd execution
348 environment.</para></listitem>
349 </varlistentry>
350
351 <varlistentry>
352 <term><filename>/.extra/sysext/*.raw</filename></term>
353 <listitem><para>System extension image files (suffix <literal>.raw</literal>) that are placed next to
354 the unified kernel image (as described above) are copied into the
355 <filename>/.extra/sysext/</filename> directory in the initrd execution environment.</para></listitem>
356 </varlistentry>
357
358 <varlistentry>
359 <term><filename>/.extra/tpm2-pcr-signature.json</filename></term>
360 <listitem><para>The TPM2 PCR signature JSON object included in the <literal>.pcrsig</literal> PE
361 section of the unified kernel image is copied into the
362 <filename>/.extra/tpm2-pcr-signature.json</filename> file in the initrd execution
363 environment.</para></listitem>
364 </varlistentry>
365
366 <varlistentry>
367 <term><filename>/.extra/tpm2-pcr-pkey.pem</filename></term>
368 <listitem><para>The PEM public key included in the <literal>.pcrpkey</literal> PE section of the
369 unified kernel image is copied into the <filename>/.extra/tpm2-pcr-public-key.pem</filename> file in
370 the initrd execution environment.</para></listitem>
371 </varlistentry>
372 </variablelist>
373
374 <para>Note that all these files are located in the <literal>tmpfs</literal> file system the kernel sets
375 up for the initrd file hierarchy and are thus lost when the system transitions from the initrd execution
376 environment into the host file system. If these resources shall be kept around over this transition they
377 need to be copied to a place that survives the transition first, for example via a suitable
378 <citerefentry><refentrytitle>tmpfiles.d</refentrytitle><manvolnum>5</manvolnum></citerefentry> line. By
379 default, this is done for the TPM2 PCR signature and public key files.</para>
380 </refsect1>
381
382 <refsect1>
383 <title>Assembling Kernel Images</title>
384
385 <para>In order to assemble an UEFI PE kernel image from various components as described above, use an
386 <citerefentry project='man-pages'><refentrytitle>objcopy</refentrytitle><manvolnum>1</manvolnum></citerefentry> command line
387 like this:</para>
388
389 <programlisting>objcopy \
390 --add-section .osrel=os-release --change-section-vma .osrel=0x20000 \
391 --add-section .cmdline=cmdline.txt --change-section-vma .cmdline=0x30000 \
392 --add-section .dtb=devicetree.dtb --change-section-vma .dtb=0x40000 \
393 --add-section .splash=splash.bmp --change-section-vma .splash=0x100000 \
394 --add-section .linux=vmlinux --change-section-vma .linux=0x2000000 \
395 --add-section .initrd=initrd.cpio --change-section-vma .initrd=0x3000000 \
396 /usr/lib/systemd/boot/efi/linuxx64.efi.stub \
397 foo-unsigned.efi</programlisting>
398
399 <para>This generates one PE executable file <filename>foo-unsigned.efi</filename> from the six individual
400 files for OS release information, kernel command line, boot splash image, kernel image, main initrd and
401 UEFI boot stub.</para>
402
403 <para>To then sign the resulting image for UEFI SecureBoot use an
404 <citerefentry project='archlinux'><refentrytitle>sbsign</refentrytitle><manvolnum>1</manvolnum></citerefentry> command like
405 the following:</para>
406
407 <programlisting>sbsign \
408 --key mykey.pem \
409 --cert mykey.crt \
410 --output foo.efi \
411 foo-unsigned.efi</programlisting>
412
413 <para>This expects a pair of X.509 private key and certificate as parameters and then signs the UEFI PE
414 executable we generated above for UEFI SecureBoot and generates a signed UEFI PE executable as
415 result.</para>
416
417 <para>See
418 <citerefentry><refentrytitle>systemd-measure</refentrytitle><manvolnum>1</manvolnum></citerefentry> for
419 an example involving the <literal>.pcrsig</literal> and <literal>.pcrpkey</literal> sections.</para>
420 </refsect1>
421
422 <refsect1>
423 <title>See Also</title>
424 <para>
425 <citerefentry><refentrytitle>systemd-boot</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
426 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
427 <citerefentry><refentrytitle>systemd-creds</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
428 <citerefentry><refentrytitle>systemd-sysext</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
429 <ulink url="https://systemd.io/BOOT_LOADER_SPECIFICATION">Boot Loader Specification</ulink>,
430 <ulink url="https://systemd.io/BOOT_LOADER_INTERFACE">Boot Loader Interface</ulink>,
431 <citerefentry project='man-pages'><refentrytitle>objcopy</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
432 <citerefentry project='archlinux'><refentrytitle>sbsign</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
433 <citerefentry><refentrytitle>systemd-measure</refentrytitle><manvolnum>1</manvolnum></citerefentry>
434 </para>
435 </refsect1>
436 </refentry>