Debian Codegraph @

src/shared/swtpm-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-json.h"
6
7 #include "alloc-util.h"
8 #include "escape.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "json-util.h"
12 #include "log.h"
13 #include "memfd-util.h"
14 #include "path-util.h"
15 #include "pidref.h"
16 #include "process-util.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "swtpm-util.h"
20
21 static int swtpm_find_best_profile(const char *swtpm_setup, char **ret) {
22 int r;
23
24 assert(swtpm_setup);
25 assert(ret);
26
27 _cleanup_strv_free_ char **args = strv_new(
28 swtpm_setup,
29 "--tpm2",
30 "--print-profiles");
31 if (!args)
32 return log_oom();
33
34 _cleanup_close_ int mfd = memfd_new("swtpm-profiles");
35 if (mfd < 0)
36 return log_error_errno(mfd, "Failed to allocate memfd: %m");
37
38 if (DEBUG_LOGGING) {
39 _cleanup_free_ char *cmdline = quote_command_line(args, SHELL_ESCAPE_EMPTY);
40 log_debug("About to spawn: %s", strnull(cmdline));
41 }
42
43 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
44 r = pidref_safe_fork_full(
45 "(swtpm-lprof)",
46 (int[]) { -EBADF, mfd, STDERR_FILENO },
47 /* except_fds= */ NULL,
48 /* n_except_fds= */ 0,
49 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_REARRANGE_STDIO|FORK_LOG,
50 &pidref);
51 if (r < 0)
52 return log_error_errno(r, "Failed to run swtpm_setup: %m");
53 if (r == 0) {
54 /* Child */
55 execvp(args[0], args);
56 log_error_errno(errno, "Failed to execute '%s': %m", args[0]);
57 _exit(EXIT_FAILURE);
58 }
59
60 r = pidref_wait_for_terminate_and_check("(swtpm-lprof)", &pidref, WAIT_LOG_ABNORMAL);
61 if (r < 0)
62 return r;
63
64 /* NB: we ignore the exit status of --print-profiles, it's broken. Instead we check if we have
65 * received a valid JSON object via STDOUT. */
66 (void) r;
67
68 _cleanup_free_ char *text = NULL;
69 r = read_full_file_full(
70 mfd,
71 /* filename= */ NULL,
72 /* offset= */ 0,
73 /* size= */ SIZE_MAX,
74 /* flags= */ 0,
75 /* bind_name= */ NULL,
76 &text,
77 /* ret_size= */ NULL);
78 if (r < 0)
79 return log_error_errno(r, "Failed to read memory fd: %m");
80
81 _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
82 r = sd_json_parse(text, SD_JSON_PARSE_MUST_BE_OBJECT, &j, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
83 if (r < 0) {
84 log_notice("Failed to parse swtpm's --print-profiles output as JSON, assuming the implementation is too old to know the concept of profiles.");
85 *ret = NULL;
86 return 0;
87 }
88
89 sd_json_variant *v = sd_json_variant_by_key(j, "builtin");
90 if (!v) {
91 *ret = NULL;
92 return 0;
93 }
94
95 if (!sd_json_variant_is_array(v))
96 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'builtin' field is not an array.");
97
98 const char *best_profile = NULL;
99 sd_json_variant *i;
100 JSON_VARIANT_ARRAY_FOREACH(i, v) {
101 if (!sd_json_variant_is_object(i))
102 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Profile object is not a JSON object.");
103
104 sd_json_variant *n = sd_json_variant_by_key(i, "Name");
105 if (!n) {
106 log_debug("Object in profiles array does not have a 'Name', skipping.");
107 continue;
108 }
109
110 if (!sd_json_variant_is_string(n))
111 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Profile's 'Name' field is not a string.");
112
113 const char *s = sd_json_variant_string(n);
114
115 /* Pick the best of the default-v1, default-v2, … profiles */
116 if (!startswith(s, "default-v"))
117 continue;
118 if (!best_profile || strverscmp_improved(s, best_profile) > 0)
119 best_profile = s;
120 }
121
122 _cleanup_free_ char *copy = NULL;
123 if (best_profile) {
124 copy = strdup(best_profile);
125 if (!copy)
126 return log_oom();
127 }
128
129 *ret = TAKE_PTR(copy);
130 return 0;
131 }
132
133 int manufacture_swtpm(const char *state_dir, const char *secret) {
134 int r;
135
136 assert(state_dir);
137
138 _cleanup_free_ char *swtpm_setup = NULL;
139 r = find_executable("swtpm_setup", &swtpm_setup);
140 if (r < 0)
141 return log_error_errno(r, "Failed to find 'swtpm_setup' binary: %m");
142
143 _cleanup_free_ char *best_profile = NULL;
144 r = swtpm_find_best_profile(swtpm_setup, &best_profile);
145 if (r < 0)
146 return r;
147
148 /* Create custom swtpm config files so that swtpm_localca uses our state directory instead of
149 * the system-wide /var/lib/swtpm-localca/ which may not be writable. */
150 _cleanup_free_ char *localca_conf = path_join(state_dir, "swtpm-localca.conf");
151 if (!localca_conf)
152 return log_oom();
153
154 r = write_string_filef(
155 localca_conf,
156 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755,
157 "statedir = %1$s\n"
158 "signingkey = %1$s/signing-private-key.pem\n"
159 "issuercert = %1$s/issuer-certificate.pem\n"
160 "certserial = %1$s/certserial\n",
161 state_dir);
162 if (r < 0)
163 return log_error_errno(r, "Failed to write swtpm-localca.conf: %m");
164
165 _cleanup_free_ char *localca_options = path_join(state_dir, "swtpm-localca.options");
166 if (!localca_options)
167 return log_oom();
168
169 r = write_string_file(
170 localca_options,
171 "--platform-manufacturer systemd\n"
172 "--platform-version 2.1\n"
173 "--platform-model swtpm\n",
174 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755);
175 if (r < 0)
176 return log_error_errno(r, "Failed to write swtpm-localca.options: %m");
177
178 _cleanup_free_ char *swtpm_localca = NULL;
179 r = find_executable("swtpm_localca", &swtpm_localca);
180 if (r < 0)
181 return log_error_errno(r, "Failed to find 'swtpm_localca' binary: %m");
182
183 _cleanup_free_ char *setup_conf = path_join(state_dir, "swtpm_setup.conf");
184 if (!setup_conf)
185 return log_oom();
186
187 r = write_string_filef(
188 setup_conf,
189 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755,
190 "create_certs_tool = %1$s\n"
191 "create_certs_tool_config = %2$s\n"
192 "create_certs_tool_options = %3$s\n",
193 swtpm_localca,
194 localca_conf,
195 localca_options);
196 if (r < 0)
197 return log_error_errno(r, "Failed to write swtpm_setup.conf: %m");
198
199 _cleanup_strv_free_ char **args = strv_new(
200 swtpm_setup,
201 "--tpm-state", state_dir,
202 "--tpm2",
203 "--pcr-banks", "sha256",
204 "--ecc",
205 "--createek",
206 "--create-ek-cert",
207 "--create-platform-cert",
208 "--not-overwrite",
209 "--config", setup_conf);
210 if (!args)
211 return log_oom();
212
213 if (secret && strv_extendf(&args, "--keyfile=%s", secret) < 0)
214 return log_oom();
215
216 if (best_profile && strv_extendf(&args, "--profile-name=%s", best_profile) < 0)
217 return log_oom();
218
219 if (!DEBUG_LOGGING && strv_extend_many(&args, "--logfile", "/dev/null") < 0)
220 return log_oom();
221
222 if (DEBUG_LOGGING) {
223 _cleanup_free_ char *cmdline = quote_command_line(args, SHELL_ESCAPE_EMPTY);
224 log_debug("About to spawn: %s", strnull(cmdline));
225 }
226
227 r = pidref_safe_fork("(swtpm-setup)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_LOG|FORK_WAIT, /* ret= */ NULL);
228 if (r < 0)
229 return log_error_errno(r, "Failed to run swtpm_setup: %m");
230 if (r == 0) {
231 /* Child */
232 execvp(args[0], args);
233 log_error_errno(errno, "Failed to execute '%s': %m", args[0]);
234 _exit(EXIT_FAILURE);
235 }
236
237 return 0;
238 }