]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/veritysetup/veritysetup-generator.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / veritysetup / veritysetup-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <stdbool.h>
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "fstab-util.h"
16 #include "generator.h"
17 #include "hexdecoct.h"
18 #include "id128-util.h"
19 #include "mkdir.h"
20 #include "parse-util.h"
21 #include "proc-cmdline.h"
22 #include "specifier.h"
23 #include "string-util.h"
24 #include "unit-name.h"
25
26 #define SYSTEMD_VERITYSETUP_SERVICE "systemd-veritysetup@root.service"
27
28 static char *arg_dest = NULL;
29 static bool arg_enabled = true;
30 static char *arg_root_hash = NULL;
31 static char *arg_data_what = NULL;
32 static char *arg_hash_what = NULL;
33
34 static int create_device(void) {
35 _cleanup_free_ char *u = NULL, *v = NULL, *d = NULL, *e = NULL, *u_escaped = NULL, *v_escaped = NULL, *root_hash_escaped = NULL;
36 _cleanup_fclose_ FILE *f = NULL;
37 const char *to;
38 int r;
39
40 /* If all three pieces of information are missing, then verity is turned off */
41 if (!arg_root_hash && !arg_data_what && !arg_hash_what)
42 return 0;
43
44 /* if one of them is missing however, the data is simply incomplete and this is an error */
45 if (!arg_root_hash)
46 log_error("Verity information incomplete, root hash unspecified.");
47 if (!arg_data_what)
48 log_error("Verity information incomplete, root data device unspecified.");
49 if (!arg_hash_what)
50 log_error("Verity information incomplete, root hash device unspecified.");
51
52 if (!arg_root_hash || !arg_data_what || !arg_hash_what)
53 return -EINVAL;
54
55 log_debug("Using root verity data device %s,\n"
56 " hash device %s,\n"
57 " and root hash %s.", arg_data_what, arg_hash_what, arg_root_hash);
58
59 u = fstab_node_to_udev_node(arg_data_what);
60 if (!u)
61 return log_oom();
62 v = fstab_node_to_udev_node(arg_hash_what);
63 if (!v)
64 return log_oom();
65
66 u_escaped = specifier_escape(u);
67 if (!u_escaped)
68 return log_oom();
69 v_escaped = specifier_escape(v);
70 if (!v_escaped)
71 return log_oom();
72
73 r = unit_name_from_path(u, ".device", &d);
74 if (r < 0)
75 return log_error_errno(r, "Failed to generate unit name: %m");
76 r = unit_name_from_path(v, ".device", &e);
77 if (r < 0)
78 return log_error_errno(r, "Failed to generate unit name: %m");
79
80 root_hash_escaped = specifier_escape(arg_root_hash);
81 if (!root_hash_escaped)
82 return log_oom();
83
84 r = generator_open_unit_file(arg_dest, NULL, SYSTEMD_VERITYSETUP_SERVICE, &f);
85 if (r < 0)
86 return r;
87
88 fprintf(f,
89 "[Unit]\n"
90 "Description=Integrity Protection Setup for %%I\n"
91 "Documentation=man:systemd-veritysetup-generator(8) man:systemd-veritysetup@.service(8)\n"
92 "SourcePath=/proc/cmdline\n"
93 "DefaultDependencies=no\n"
94 "Conflicts=umount.target\n"
95 "BindsTo=%s %s\n"
96 "IgnoreOnIsolate=true\n"
97 "After=cryptsetup-pre.target %s %s\n"
98 "Before=cryptsetup.target umount.target\n"
99 "\n[Service]\n"
100 "Type=oneshot\n"
101 "RemainAfterExit=yes\n"
102 "ExecStart=" ROOTLIBEXECDIR "/systemd-veritysetup attach root '%s' '%s' '%s'\n"
103 "ExecStop=" ROOTLIBEXECDIR "/systemd-veritysetup detach root\n",
104 d, e,
105 d, e,
106 u_escaped, v_escaped, root_hash_escaped);
107
108 r = fflush_and_check(f);
109 if (r < 0)
110 return log_error_errno(r, "Failed to write file unit "SYSTEMD_VERITYSETUP_SERVICE": %m");
111
112 to = strjoina(arg_dest, "/cryptsetup.target.requires/" SYSTEMD_VERITYSETUP_SERVICE);
113
114 (void) mkdir_parents(to, 0755);
115 if (symlink("../" SYSTEMD_VERITYSETUP_SERVICE, to) < 0)
116 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
117
118 return 0;
119 }
120
121 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
122 int r;
123
124 if (streq(key, "systemd.verity")) {
125
126 r = value ? parse_boolean(value) : 1;
127 if (r < 0)
128 log_warning("Failed to parse verity= kernel command line switch %s. Ignoring.", value);
129 else
130 arg_enabled = r;
131
132 } else if (streq(key, "roothash")) {
133
134 if (proc_cmdline_value_missing(key, value))
135 return 0;
136
137 r = free_and_strdup(&arg_root_hash, value);
138 if (r < 0)
139 return log_oom();
140
141 } else if (streq(key, "systemd.verity_root_data")) {
142
143 if (proc_cmdline_value_missing(key, value))
144 return 0;
145
146 r = free_and_strdup(&arg_data_what, value);
147 if (r < 0)
148 return log_oom();
149
150 } else if (streq(key, "systemd.verity_root_hash")) {
151
152 if (proc_cmdline_value_missing(key, value))
153 return 0;
154
155 r = free_and_strdup(&arg_hash_what, value);
156 if (r < 0)
157 return log_oom();
158 }
159
160 return 0;
161 }
162
163 static int determine_devices(void) {
164 _cleanup_free_ void *m = NULL;
165 sd_id128_t root_uuid, verity_uuid;
166 char ids[37];
167 size_t l;
168 int r;
169
170 /* Try to automatically derive the root data and hash device paths from the root hash */
171
172 if (!arg_root_hash)
173 return 0;
174
175 if (arg_data_what && arg_hash_what)
176 return 0;
177
178 r = unhexmem(arg_root_hash, strlen(arg_root_hash), &m, &l);
179 if (r < 0)
180 return log_error_errno(r, "Failed to parse root hash: %s", arg_root_hash);
181 if (l < sizeof(sd_id128_t)) {
182 log_debug("Root hash is shorter than 128 bits (32 characters), ignoring for discovering verity partition.");
183 return 0;
184 }
185
186 if (!arg_data_what) {
187 memcpy(&root_uuid, m, sizeof(root_uuid));
188
189 arg_data_what = strjoin("/dev/disk/by-partuuid/", id128_to_uuid_string(root_uuid, ids));
190 if (!arg_data_what)
191 return log_oom();
192 }
193
194 if (!arg_hash_what) {
195 memcpy(&verity_uuid, (uint8_t*) m + l - sizeof(verity_uuid), sizeof(verity_uuid));
196
197 arg_hash_what = strjoin("/dev/disk/by-partuuid/", id128_to_uuid_string(verity_uuid, ids));
198 if (!arg_hash_what)
199 return log_oom();
200 }
201
202 return 1;
203 }
204
205 int main(int argc, char *argv[]) {
206 int r;
207
208 if (argc > 1 && argc != 4) {
209 log_error("This program takes three or no arguments.");
210 return EXIT_FAILURE;
211 }
212
213 if (argc > 1)
214 arg_dest = argv[1];
215
216 log_set_prohibit_ipc(true);
217 log_set_target(LOG_TARGET_AUTO);
218 log_parse_environment();
219 log_open();
220
221 umask(0022);
222
223 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
224 if (r < 0) {
225 log_warning_errno(r, "Failed to parse kernel command line: %m");
226 goto finish;
227 }
228
229 /* For now we only support the root device on verity. Later on we might want to add support for /etc/veritytab
230 * or similar to define additional mappings */
231
232 if (!arg_enabled) {
233 r = 0;
234 goto finish;
235 }
236
237 r = determine_devices();
238 if (r < 0)
239 goto finish;
240
241 r = create_device();
242 if (r < 0)
243 goto finish;
244
245 r = 0;
246
247 finish:
248 free(arg_root_hash);
249 free(arg_data_what);
250 free(arg_hash_what);
251
252 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
253 }