Debian Codegraph @

src/tpm2-setup/tpm2-generator.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "dropin.h"
6 #include "efivars.h"
7 #include "generator.h"
8 #include "initrd-util.h"
9 #include "log.h"
10 #include "parse-util.h"
11 #include "path-util.h"
12 #include "proc-cmdline.h"
13 #include "special.h"
14 #include "tpm2-util.h"
15
16 /* A small generator that enqueues tpm2.target as synchronization point if the TPM2 device hasn't shown up
17 * yet, but the firmware reports it to exist. This is supposed to deal with systems where the TPM2 driver
18 * support is built as kmod and must be loaded before it's ready to be used. The tpm2.target is only enqueued
19 * if firmware says there is a TPM2 device, our userspace support for TPM2 is fully available but the TPM2
20 * device hasn't shown up in /dev/ yet. */
21
22 static const char *arg_dest = NULL;
23 static int arg_tpm2_wait = -1; /* tri-state: negative → don't know */
24 static bool arg_tpm2_software_fallback = false;
25
26 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
27 int r;
28
29 assert(key);
30
31 if (proc_cmdline_key_streq(key, "systemd.tpm2_wait")) {
32 r = value ? parse_boolean(value) : 1;
33 if (r < 0)
34 log_warning_errno(r, "Failed to parse 'systemd.tpm2_wait=' kernel command line argument, ignoring: %s", value);
35 else
36 arg_tpm2_wait = r;
37
38 } else if (proc_cmdline_key_streq(key, "systemd.tpm2_software_fallback")) {
39 r = value ? parse_boolean(value) : 1;
40 if (r < 0)
41 log_warning_errno(r, "Failed to parse 'systemd.tpm2_software_fallback=' kernel command line argument, ignoring: %s", value);
42 else
43 arg_tpm2_software_fallback = r;
44 }
45
46 return 0;
47 }
48
49 static int generate_tpm_target_symlink(Tpm2Support support, bool software_fallback_enabled) {
50 int r;
51
52 if (arg_tpm2_wait == 0) {
53 log_debug("Not generating tpm2.target synchronization point, as this was explicitly turned off via kernel command line.");
54 return 0;
55 }
56
57 if (arg_tpm2_wait < 0 && !software_fallback_enabled) {
58 if (FLAGS_SET(support, TPM2_SUPPORT_DRIVER)) {
59 log_debug("Not generating tpm2.target synchronization point, as TPM2 device is already present.");
60 return 0;
61 }
62
63 if (!FLAGS_SET(support, TPM2_SUPPORT_FIRMWARE)) {
64 log_debug("Not generating tpm2.target synchronization point, as firmware reports no TPM2 present.");
65 return 0;
66 }
67 }
68
69 r = generator_add_symlink(arg_dest, SPECIAL_SYSINIT_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/" SPECIAL_TPM2_TARGET);
70 if (r < 0)
71 return log_error_errno(r, "Failed to hook in tpm2.target: %m");
72
73 return 0;
74 }
75
76 static int generate_swtpm_symlink(Tpm2Support support) {
77 int r;
78
79 if (!arg_tpm2_software_fallback)
80 return 0;
81
82 if (FLAGS_SET(support, TPM2_SUPPORT_DRIVER) || FLAGS_SET(support, TPM2_SUPPORT_FIRMWARE)) {
83 log_debug("Not generating software TPM units, as a TPM2 device is otherwise available.");
84 return 0;
85 }
86
87 if (!is_efi_boot()) { /* We need the ESP to store the TPM state. */
88 log_warning("TPM software fallback requested but not booted in EFI mode, not pulling in software TPM unit.");
89 return 0;
90 }
91
92 r = find_executable("swtpm", /* ret_filename= */ NULL);
93 if (r == -ENOENT) {
94 log_warning("TPM software fallback requested but swtpm not available, not pulling in software TPM unit.");
95 return 0;
96 }
97 if (r < 0)
98 return log_error_errno(r, "Failed to determine if 'swtpm' is available: %m");
99
100 r = generator_add_symlink(arg_dest, SPECIAL_SYSINIT_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/systemd-tpm2-swtpm.service");
101 if (r < 0)
102 return log_error_errno(r, "Failed to hook in systemd-tpm2-swtpm.service: %m");
103
104 if (in_initrd())
105 /* Order + pull in the early ESP mount so that swtpm has a place to store its data. */
106 r = write_drop_in(
107 arg_dest,
108 "systemd-tpm2-swtpm.service",
109 50, "esp",
110 "# Automatically generated by systemd-tpm2-generator\n\n"
111 "[Unit]\n"
112 "Wants=sysefi.mount\n"
113 "After=sysefi.mount\n");
114 else
115 /* Order (but not pull in) the regular ESP automount so that swtpm has a place to store its
116 * data. Note that it might be mounted to two different places depending on the existence of
117 * XBOOTLDR, hence order after both. */
118 r = write_drop_in(
119 arg_dest,
120 "systemd-tpm2-swtpm.service",
121 50, "esp",
122 "# Automatically generated by systemd-tpm2-generator\n\n"
123 "[Unit]\n"
124 "After=boot.automount efi.automount\n");
125 if (r < 0)
126 return log_error_errno(r, "Failed to hook ESP mount before systemd-tpm2-swtpm.service: %m");
127
128 return 1; /* Tell caller we now created swtpm units */
129 }
130
131 static int generate_now(void) {
132 int r;
133
134 /* Let's shortcut things before we check for TPM2 support if no one cares anyway */
135 if (arg_tpm2_wait == 0 && !arg_tpm2_software_fallback) {
136 log_debug("Not generating tpm2.target synchronization point or activating software TPM, as turned off via kernel command line.");
137 return 0;
138 }
139
140 /* We are supposed to sync on TPM or do a software fallback, let's first and unconditionally validate this makes sense at
141 * all, i.e. if we have a suitable kernel+userspace. */
142 Tpm2Support support = tpm2_support();
143 if (!FLAGS_SET(support, TPM2_SUPPORT_SYSTEM|TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_LIBRARIES)) {
144
145 /* Raise log level if things were explicitly configured */
146 log_full((arg_tpm2_wait > 0 ||
147 arg_tpm2_software_fallback) ? LOG_NOTICE : LOG_DEBUG,
148 "Not generating tpm2.target synchronization point or activating software TPM, as userspace support for TPM2 is not complete.");
149 return 0;
150 }
151
152 r = generate_swtpm_symlink(support);
153 if (r < 0)
154 return r;
155
156 r = generate_tpm_target_symlink(support, /* software_fallback_enabled= */ r > 0);
157 if (r < 0)
158 return r;
159
160 return 0;
161 }
162
163 static int run(const char *dest, const char *dest_early, const char *dest_late) {
164 int r;
165
166 assert_se(arg_dest = dest);
167
168 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
169 if (r < 0)
170 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
171
172 return generate_now();
173 }
174
175 DEFINE_MAIN_GENERATOR_FUNCTION(run);