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