Skip to content

Commit 8508b1c

Browse files
committed
[RFC 0165] Bootspec v2
This introduces the second revision of RFC-0125, Bootspec, addressing the feedback we received in #125 and building on our experience of Bootspec v1.
1 parent c8569f6 commit 8508b1c

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed

rfcs/0165-bootspecv2.md

+372
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
---
2+
feature: bootspec_v2
3+
start-date: 2022-11-01
4+
author: Ryan Lahfa
5+
co-authors:
6+
shepherd-team:
7+
shepherd-leader: @JulienMalka
8+
---
9+
10+
# Summary
11+
[summary]: #summary
12+
13+
Bootspec v2 is the second revision of the Bootspec document, introduced in [RFC-0125](https://github.com/NixOS/rfcs/blob/master/rfcs/0125-bootspec.md).
14+
15+
These facts are used as the primary input for bootloader backends like systemd-boot and grub, for creating files in `/boot/loader/entries/` and `grub.cfg`.
16+
17+
In this proposal, we aim to tackle known weaknesses of Bootspec v1, namely:
18+
19+
- Multiple initrds support
20+
- Rework of initrd secrets mechanism
21+
- Lack of devicetree
22+
- Further iterations of Bootspec
23+
24+
This document describes **Bootspec v2**.
25+
26+
# Motivation
27+
[motivation]: #motivation
28+
29+
The motivation of Bootspec v1 remains but we address the v1 weaknesses and
30+
includes our experience of running with it for a while.
31+
32+
## Multiple initrds
33+
34+
The Boot Loader Specification, developed by the Userspace API group, allows the
35+
specification of multiple initrd entries [1], with the purpose of merging them into
36+
CPIO archives. This functionality is valuable in the systemd-stub ecosystem,
37+
where various types of initrds are combined, including specific credential
38+
initrds, global credential initrds, system extension initrds, PCR signature
39+
initrds, and PCR public key initrds. Some initrds, such as credential initrds,
40+
are dynamically generated from an EFI System Partition (ESP) location. [2]
41+
42+
Moreover, an additional initrd is frequently used to store CPU microcode. To
43+
ensure compatibility and flexibility, it is essential to rework the initrd
44+
support in the Boot Loader Specification. The proposed changes aim to treat
45+
initrds as a set, allowing bootloaders to handle a list of initrds or a single
46+
initrd in cases where multiple initrds are not supported.
47+
48+
[1]: https://uapi-group.org/specifications/specs/boot_loader_specification/#type-1-boot-loader-entry-keys
49+
[2]: https://github.com/systemd/systemd/blob/main/src/boot/efi/stub.c#L778-L793
50+
51+
## Initrd secrets
52+
53+
Initrd secrets play a crucial role in the NixOS ecosystem, but they have raised
54+
concerns, including security vulnerabilities (e.g., [GHSA-3rvf-24q2-24ww](https://github.com/NixOS/calamares-nixos-extensions/security/advisories/GHSA-3rvf-24q2-24ww
55+
)) and
56+
issues with booting in the Heads platform (e.g., [Issue #1348](https://github.com/linuxboot/heads/issues/1348.
57+
)).
58+
59+
Initrd secrets serve to protect boot-time secrets from exposure within the Nix
60+
store. This is achieved by using runtime scripts that append CPIO archives to
61+
the generation's initrd during bootloader installation. However, the term
62+
"initrd secrets" can be misleading, as the secrets are plaintext and can be
63+
accessed from the ESP partition, offering limited confidentiality and no
64+
integrity.
65+
66+
In practice, initrd secrets are often employed to establish stable fingerprints
67+
for SSH servers within the initrd, or aiding in remote disk decryption on servers.
68+
To address these issues, this RFC proposes moving away from the "appender
69+
script" model in Bootspec v1 and instead adopting a hash map format to
70+
represent secrets and their corresponding values.
71+
72+
This change allows for greater flexibility and enhanced security. For users in
73+
the NixOS ecosystem relying on systemd and its semantics, the proposal suggests
74+
offering a systemd-credentials approach for handling initrd secrets,
75+
potentially enabling encryption using TPM2 if available, or a straightforward
76+
key if TPM2 is not present. Additionally, this approach provides the means to
77+
build using the `LoadEncryptedCredential` abstraction.
78+
79+
For those not using systemd, the hash map format offers full flexibility to
80+
bootloader implementations to determine how to add the secrets, such as
81+
appending to the initrd or other methods.
82+
83+
**Key Takeaway**: This RFC recommends removing the `initrdSecrets` field in
84+
favor of letting consumers defining extensions replacing it. We define such an
85+
extension `org.nixos.initrd-secrets.v1` and propose it to be a hashmap with
86+
string keys and values. The key represents a name used to organize secrets, and
87+
the value denotes an accessible path during bootloader installation.
88+
89+
### Expectations about initrd environment
90+
91+
In general, the semantics of initrd secrets lends themselves to expect the
92+
existence of a files in the stage 1 environment of the initrd to be available
93+
at certain paths.
94+
95+
With this proposed change, we specify that end users should expect that the
96+
initrd environment will populate the referenced files of the initrd secrets
97+
fields in the stage 1 environment at the very start.
98+
99+
More precisely, the predecessor of stage 1 environment is responsible for
100+
filling the filesystem with the expected files, this can be the bootloader or
101+
any prior stage to the stage 1 runtime environment.
102+
103+
For example, systemd credentials are populated by systemd at the very start in
104+
an cooperation with `systemd-boot` and companion files present on the ESP.
105+
106+
Switching from a bootloader backend to another bootloader backend should have
107+
no visible effect on that matter as long as the "files are present where they
108+
are specified in the initrd secret field at the very start of the stage 1
109+
environment" invariant is respected.
110+
111+
There is no specification about how to cleanup prior bootloader backend data
112+
from a boot partition as this is out of scope for this RFC and multi-bootloader
113+
cooperation is an open problem, especially for dual boots or more setups.
114+
115+
### Example
116+
117+
```
118+
"org.nixos.initrd-secrets.v1": {
119+
"my-private-key": "/etc/nixos/secrets/wireguard-key"
120+
}
121+
```
122+
123+
## Device Tree and Device Tree Overlays
124+
125+
Non-x86 systems often rely on device trees to inform firmware or bootloader about the available devices and hardware support.
126+
127+
There are two distinct requirements for device trees:
128+
129+
- generic hardware support, where firmware or bootloader selects the appropriate device tree
130+
- device-specific support, where a hardcoded device tree is required.
131+
132+
The latter indicates a potentially problematic, non-upstreamed, or in-development platform.
133+
134+
The current Bootspec v1 does not formally encode information about hardcoded device trees or the folder containing available device trees. As a result, unformalized extensions are needed to address these fundamental use cases.
135+
136+
Regarding overlays in NixOS, any expressed overlay is incorporated into the final device tree. This eliminates the necessity to formalize an additional overlays field list, as overlays can be transformed into device trees as needed.
137+
138+
## Further iterations of Bootspec
139+
140+
It has been brought that the RFC process may not be adequate to discuss further
141+
iterations of Bootspec.
142+
143+
While the author disagree with this vision and believe this would be a loss
144+
for increased participation in the elaboration of further iterations and would lead to a
145+
decrease in discoverability of Bootspec matters.
146+
147+
We propose to move further iterations of Bootspec away from the RFC process.
148+
149+
For this, we propose to follow a lightweight process inside a GitHub repository
150+
containing the specification and other relevant metadata about Bootspec.
151+
152+
In instances of controversy or difficulties to reach consensus among the Bootspec ecosystem
153+
developers and maintainers, we should reach out to the standard RFC process.
154+
155+
# Goals
156+
[goals]: #goals
157+
158+
- Improve non-x86 support in Bootspec, emphasizing the importance of
159+
devicetrees.
160+
- Address and reduce the risks associated with initrd secrets, providing a
161+
default "secure" implementation within the systemd ecosystem and offering
162+
flexibility for other ecosystems.
163+
- Enhance initrd flexibility, allowing developers to optimize their systems by
164+
supporting multiple initrds, including one for microcode and other specific
165+
purposes.
166+
- Make further iterations of Bootspec easier to build.
167+
168+
### Non-Goals
169+
[non-goals]: #non-goals
170+
171+
- Store TPM2-related information (hashes)
172+
We believe that Bootspec is still too immature for this and pcrlock
173+
(https://github.com/systemd/systemd/pull/28891) offers a more reliable and
174+
robust solution for generating signed PCR policies.
175+
- Supporting SecureBoot.
176+
Secure Boot has one maintained implementation that is being upstreamed: https://github.com/nix-community/lanzaboote
177+
which was enabled by Bootspec v1.
178+
- Specifying how to discover generations. This is desirable, but should not be tied to bootspec directly since bootspec may be useful with diverse discovery mechanisms.
179+
- Address how bootloader backends should handle foreign bootloader data in their stead and manipulate it or how transition from a bootloader to another should happen.
180+
181+
# Proposed Solution
182+
[proposed-solution]: #proposed-solution
183+
184+
- `initrd` will be removed from the v2
185+
- `initrds` will be introduced as a list of initrd (compressed or uncompressed
186+
CPIO archives), this list can be empty, but the field is **required**
187+
nonetheless.
188+
- `org.nixos.initrd-secrets.v1` will now be an **official extension** of
189+
Bootspec which should be an hashmap of strings (key) and strings (value)
190+
where the key is the "name" of a secret and the value is the **filesystem path** towards a
191+
secret.
192+
- `fdtdir` will be introduced as an opaque string to a directory in a shape as
193+
the kernel outputs give them, e.g. U-Boot's extlinux directive `FDTDIR` is an
194+
example of this behavior — it is **optional**
195+
- `devicetree` will be introduced as a path to a single devicetree that will be
196+
hardcoded — it is **optional**
197+
198+
All the Bootspec ecosystem will be updated as part of this specification. Regarding the transition period:
199+
200+
- All the Bootspec ecosystem is advised to **emit** the latest document.
201+
- The synthesis feature should be upgraded to **emit** the latest version of the document based on the previous one, thus, by induction, based on no Bootspec document at all, if needed.
202+
203+
This way, we guarantee forward and backward compatibility.
204+
205+
Finally, Bootspec will follow a lightweight process by default for its further iterations and rely on standard RFC process only for conflict resolution.
206+
207+
### Bootspec Format v2
208+
[format-v2]: #format-v2
209+
210+
211+
Using the following JSON:
212+
213+
```json5
214+
{
215+
// Toplevel key describing the version of the specification used in the document
216+
"org.nixos.bootspec.v2": {
217+
// (Required) System type the bootspec is intended for (e.g. `x86_64-linux`, `aarch64-linux`)
218+
"system": "x86_64-linux",
219+
220+
// (Required) Path to the stage-2 init, executed by the initrd (if present)
221+
"init": "/nix/store/xxx-nixos-system-xxx/init",
222+
223+
// (Required) List of paths to the initrd, can be empty
224+
"initrds": [ "/nix/store/xxx-initrd-linux/initrd" ],
225+
226+
// (Required) Path to the kernel image
227+
"kernel": "/nix/store/xxx-linux/bzImage",
228+
229+
// (Required) Kernel commandline options
230+
"kernelParams": [
231+
"amd_iommu=on",
232+
"amd_iommu=pt",
233+
"iommu=pt",
234+
"kvm.ignore_msrs=1",
235+
"kvm.report_ignored_msrs=0",
236+
"udev.log_priority=3",
237+
"systemd.unified_cgroup_hierarchy=1",
238+
"loglevel=4"
239+
],
240+
241+
// (Required) The label of the system. It should contain the operating system, kernel version,
242+
// and other user-relevant information to identify the system. This corresponds
243+
// loosely to `config.system.nixos.label`.
244+
"label": "NixOS 21.11.20210810.dirty (Linux 5.15.30)",
245+
246+
// (Required) Top level path of the closure, in case some spelunking is required
247+
"toplevel": "/nix/store/xxx-nixos-system-xxx",
248+
249+
// (Optional) FDTDIR is assumed to be a path to a directory in the shape
250+
// of what `FDTDIR` in U-Boot extlinux would expect.
251+
// At the time of writing, it is assumed to follow the kernel output shape.
252+
"fdtdir": "/nix/store/xxx-uboot-fdtdir",
253+
254+
// (Optional) devicetree is assumed to be path to a single devicetree file
255+
// which will be hardcoded for that generation.
256+
"devicetree": "/nix/store/xxx-arm64-machine/my-device.dtb"
257+
},
258+
// The top-level object may contain arbitrary further keys ("extensions"), whose semantics may be defined by third parties.
259+
// The use of reverse-domain-name namespacing is recommended in order to avoid name collisions.
260+
261+
// (Optional) Specialisations are an extension to the specification which allows bundling multiple variants of a NixOS configuration with a single parent.
262+
// These are shaped like the top level; to be precise:
263+
// - Each entry in the toplevel "org.nixos.specialisation.v2" object represents a specialisation.
264+
// - In order for the top-level document to be a valid v2 bootspec, each specialisation must have a valid "org.nixos.bootspec.v2" key whose value conforms to the same schema as the toplevel "org.nixos.bootspec.v2" object.
265+
// - The behaviour of nested specialisations (i.e. entries in "org.nixos.specialisation.v2" which themselves contain the "org.nixos.specialisation.v2" key) is not defined.
266+
// - In particular, there is no expectation that such nested specialisations will be handled by consumers of bootspec documents.
267+
// - Each specialisation document may contain arbitrary further keys (extensions), like the top-level document.
268+
// - The semantics of these should be the same as when these keys are used at the top level, but only apply for the given specialisation.
269+
"org.nixos.specialisation.v2": {
270+
// Each key in this object corresponds to a specialisation as defined by the `specialisation.<name>` NixOS option.
271+
"<name>": {
272+
"org.nixos.bootspec.v2": {
273+
// See above
274+
}
275+
}
276+
},
277+
278+
// (Optional) Hash map of desired secrets for that generation inside of the initrd.
279+
// Implementors of a bootloader installation procedure should examine their options
280+
// to securely make available the secret inside the initrd phase.
281+
// This may involve leveraging TPM2 via systemd-credentials or any measure you deem
282+
// to be reasonable in the context.
283+
// The legacy behavior is to prepare a CPIO archive for each file and
284+
// extend the `initrds` fields with those CPIO archives.
285+
// Make sure the location where the secrets are dropped in the initrd are visible
286+
// for the user.
287+
"org.nixos.initrd-secrets.v1": {
288+
"my-private-key": "/etc/nixos/secrets/wireguard-private-key",
289+
}
290+
}
291+
```
292+
293+
An *optional* field means: a field that is either missing or present, but **never `null`**.
294+
295+
### Risks
296+
[risks]: #risks
297+
298+
- Some of the bootloader backends are quite complicated, and in many cases have
299+
inadequate tests. We could accidentally break corner cases.
300+
- The bootloader backends are inherently a weak point for NixOS, as it is our
301+
last option for rolling back. We cannot roll back a broken bootloader. This
302+
and the previous point are risks, but also help demonstrate the value of
303+
reducing the amount of code and complexity in the generator.
304+
305+
### Milestones
306+
[milestones]: #milestones
307+
308+
- Update Bootspec with the version 2 of that specification
309+
- The [Bootspec Rust library](https://github.com/DeterminateSystems/bootspec)
310+
- The [Bootspec interface in Nixpkgs](https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/activation/bootspec.nix)
311+
- Implement changes inside at least one bootloader backend.
312+
313+
# FAQ
314+
[faq]: #faq
315+
316+
Familiarize yourself with [Bootspec v1](https://github.com/NixOS/rfcs/blob/master/rfcs/0125-bootspec.md) which may already contain answers.
317+
318+
## Why doing a RFC at all if it's not controversial?
319+
320+
Mentioned in https://github.com/NixOS/rfcs/pull/165#discussion_r1379020798 originally, we made it explicit
321+
to the consumers of Bootspec that further iterations required a RFC. While we could have expected
322+
that further iterations would not be controversial, we still wanted to go through to RFC process
323+
to give reasonable chances to all the ecosystem.
324+
325+
While we disagree that there is no controversial change, as we can see on the DTB discussions here:
326+
https://github.com/NixOS/rfcs/pull/165#discussion_r1379666004.
327+
328+
Further iterations as mentioned in the body of that RFC will be moved to a repository
329+
and we will invite all the people interested into Bootspec to subscribe to that repository
330+
for further developments.
331+
332+
## How will I be able to express complicated logic for initrd secrets, e.g. dynamic secrets?
333+
334+
Some users may have used the appender script to provide dynamic logic that
335+
provides a secret at activation time rather than storing it on the long run.
336+
337+
While this usecase is interesting, it is very advanced and the footgun that
338+
initrd secrets represent cannot be made up by our only goal of supporting that
339+
usecase.
340+
341+
Authors may propose to think about how a filesystem could implement the dynamic
342+
fetching at activation time, e.g. a FUSE secretfs that will dynamically query
343+
the secret engine for a secret and make it available for short time.
344+
345+
If any logic requires user interaction, it is preferable to invest in a custom
346+
bootloader installer logic and use the static fields to refer to secrets that
347+
will be requested.
348+
349+
Finally, as the new initrd secrets are implemented as an **extension** of
350+
Bootspec, nothing prevent an end user to define an non-official extension of
351+
Bootspec to address their own special needs.
352+
353+
# Open Questions
354+
[open-questions]: #open-questions
355+
356+
- Should the initrd secrets work itself with systemd-credentials to load
357+
further credential and have a closed loop of credentials, this would require
358+
the activation to run inside systemd:
359+
https://github.com/NixOS/nixpkgs/pull/258571 !
360+
- What are lessons we can learn from
361+
https://github.com/aarch64-laptops/edk2/tree/dtbloader-app#dtbloader for
362+
devicetrees manipulation?
363+
364+
# Future Work
365+
[future]: #future-work
366+
367+
- Continue the migration from filesystem-spelunking into using the bootspec
368+
data.
369+
- Implement a systemd-credentials based of `org.nixos.initrd-secrets.v1`
370+
- Implement an [Verified Boot for
371+
Embedded](https://u-boot.readthedocs.io/en/latest/develop/vbe.html)
372+
bootloader installation script using bootspec data

0 commit comments

Comments
 (0)