Debian Codegraph @

src/boot/cpio.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "cpio.h"
4 #include "efi-log.h"
5 #include "iovec-util.h"
6 #include "measure.h"
7 #include "string-util.h"
8 #include "tpm2-pcr.h"
9 #include "util.h"
10
11 static char *write_cpio_word(char *p, uint32_t v) {
12 const char *hex = LOWERCASE_HEXDIGITS;
13
14 assert(p);
15
16 /* Writes a CPIO header 8 character hex value */
17
18 for (size_t i = 0; i < 8; i++)
19 p[7-i] = hex[(v >> (4 * i)) & 0xF];
20
21 return p + 8;
22 }
23
24 static char *mangle_filename(char *p, const char16_t *f) {
25 char* w;
26
27 assert(p);
28 assert(f);
29
30 /* Basically converts UTF-16 to plain ASCII (note that we filtered non-ASCII filenames beforehand, so
31 * this operation is always safe) */
32
33 for (w = p; *f != 0; f++) {
34 assert(*f <= 0x7fu);
35
36 *(w++) = *f;
37 }
38
39 *(w++) = 0;
40 return w;
41 }
42
43 static char *pad4(char *p, const char *start) {
44 assert(p);
45 assert(start);
46 assert(p >= start);
47
48 /* Appends NUL bytes to 'p', until the address is divisible by 4, when taken relative to 'start' */
49
50 while ((p - start) % 4 != 0)
51 *(p++) = 0;
52
53 return p;
54 }
55
56 EFI_STATUS pack_cpio_one(
57 const char16_t *fname,
58 const void *contents,
59 size_t contents_size,
60 const CpioTarget *target,
61 uint32_t *inode_counter,
62 void **cpio_buffer,
63 size_t *cpio_buffer_size) {
64
65 size_t l, target_dir_prefix_size, fname_size, q;
66 char *a;
67
68 assert(fname);
69 assert(contents || contents_size == 0);
70 assert(target);
71 assert(inode_counter);
72 assert(cpio_buffer);
73 assert(cpio_buffer_size);
74
75 /* Serializes one file in the cpio format understood by the kernel initrd logic.
76 *
77 * See: https://docs.kernel.org/driver-api/early-userspace/buffer-format.html */
78
79 if (contents_size > UINT32_MAX) /* cpio cannot deal with > 32-bit file sizes */
80 return EFI_LOAD_ERROR;
81
82 if (*inode_counter == UINT32_MAX) /* more than 2^32-1 inodes? yikes. cpio doesn't support that either */
83 return EFI_OUT_OF_RESOURCES;
84
85 l = 6 + 13*8 + 1 + 1; /* Fixed CPIO header size, slash separator, and NUL byte after the file name */
86
87 target_dir_prefix_size = strlen8(target->directory);
88 if (l > SIZE_MAX - target_dir_prefix_size)
89 return EFI_OUT_OF_RESOURCES;
90 l += target_dir_prefix_size;
91
92 fname_size = strlen16(fname);
93 if (l > SIZE_MAX - fname_size)
94 return EFI_OUT_OF_RESOURCES;
95 l += fname_size; /* append space for file name */
96
97 /* CPIO can't deal with fnames longer than 2^32-1 */
98 if (target_dir_prefix_size + fname_size >= UINT32_MAX)
99 return EFI_OUT_OF_RESOURCES;
100
101 /* Align the whole header to 4 byte size */
102 l = ALIGN4(l);
103 if (l == SIZE_MAX) /* overflow check */
104 return EFI_OUT_OF_RESOURCES;
105
106 /* Align the contents to 4 byte size */
107 q = ALIGN4(contents_size);
108 if (q == SIZE_MAX) /* overflow check */
109 return EFI_OUT_OF_RESOURCES;
110
111 if (l > SIZE_MAX - q) /* overflow check */
112 return EFI_OUT_OF_RESOURCES;
113 l += q; /* Add contents to header */
114
115 if (*cpio_buffer_size > SIZE_MAX - l) /* overflow check */
116 return EFI_OUT_OF_RESOURCES;
117 a = xrealloc(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
118
119 *cpio_buffer = a;
120 a = (char *) *cpio_buffer + *cpio_buffer_size;
121
122 a = mempcpy(a, "070701", 6); /* magic ID */
123
124 a = write_cpio_word(a, (*inode_counter)++); /* inode */
125 a = write_cpio_word(a, target->access_mode | 0100000 /* = S_IFREG */); /* mode */
126 a = write_cpio_word(a, 0); /* uid */
127 a = write_cpio_word(a, 0); /* gid */
128 a = write_cpio_word(a, 1); /* nlink */
129
130 /* Note: we don't make any attempt to propagate the mtime here, for two reasons: it's a mess given
131 * that FAT usually is assumed to operate with timezoned timestamps, while UNIX does not. More
132 * importantly though: the modifications times would hamper our goals of providing stable
133 * measurements for the same boots. After all we extend the initrds we generate here into TPM2
134 * PCRs. */
135 a = write_cpio_word(a, 0); /* mtime */
136 a = write_cpio_word(a, contents_size); /* size */
137 a = write_cpio_word(a, 0); /* major(dev) */
138 a = write_cpio_word(a, 0); /* minor(dev) */
139 a = write_cpio_word(a, 0); /* major(rdev) */
140 a = write_cpio_word(a, 0); /* minor(rdev) */
141 a = write_cpio_word(a, target_dir_prefix_size + fname_size + 2); /* fname size */
142 a = write_cpio_word(a, 0); /* "crc" */
143
144 a = mempcpy(a, target->directory, target_dir_prefix_size);
145 *(a++) = '/';
146 a = mangle_filename(a, fname);
147
148 /* Pad to next multiple of 4 */
149 a = pad4(a, *cpio_buffer);
150
151 a = mempcpy(a, contents, contents_size);
152
153 /* Pad to next multiple of 4 */
154 a = pad4(a, *cpio_buffer);
155
156 assert(a == (char *) *cpio_buffer + *cpio_buffer_size + l);
157 *cpio_buffer_size += l;
158
159 return EFI_SUCCESS;
160 }
161
162 static EFI_STATUS pack_cpio_dir(
163 const char *path,
164 uint32_t access_mode,
165 uint32_t *inode_counter,
166 void **cpio_buffer,
167 size_t *cpio_buffer_size) {
168
169 size_t l, path_size;
170 char *a;
171
172 assert(path);
173 assert(inode_counter);
174 assert(cpio_buffer);
175 assert(cpio_buffer_size);
176
177 /* Serializes one directory inode in cpio format. Note that cpio archives must first create the dirs
178 * they want to place files in. */
179
180 if (*inode_counter == UINT32_MAX)
181 return EFI_OUT_OF_RESOURCES;
182
183 l = 6 + 13*8 + 1; /* Fixed CPIO header size, and NUL byte after the file name */
184
185 path_size = strlen8(path);
186 if (l > SIZE_MAX - path_size)
187 return EFI_OUT_OF_RESOURCES;
188 l += path_size;
189
190 /* Align the whole header to 4 byte size */
191 l = ALIGN4(l);
192 if (l == SIZE_MAX) /* overflow check */
193 return EFI_OUT_OF_RESOURCES;
194
195 if (*cpio_buffer_size > SIZE_MAX - l) /* overflow check */
196 return EFI_OUT_OF_RESOURCES;
197
198 *cpio_buffer = a = xrealloc(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
199 a = (char *) *cpio_buffer + *cpio_buffer_size;
200
201 a = mempcpy(a, "070701", 6); /* magic ID */
202
203 a = write_cpio_word(a, (*inode_counter)++); /* inode */
204 a = write_cpio_word(a, access_mode | 0040000 /* = S_IFDIR */); /* mode */
205 a = write_cpio_word(a, 0); /* uid */
206 a = write_cpio_word(a, 0); /* gid */
207 a = write_cpio_word(a, 1); /* nlink */
208 a = write_cpio_word(a, 0); /* mtime */
209 a = write_cpio_word(a, 0); /* size */
210 a = write_cpio_word(a, 0); /* major(dev) */
211 a = write_cpio_word(a, 0); /* minor(dev) */
212 a = write_cpio_word(a, 0); /* major(rdev) */
213 a = write_cpio_word(a, 0); /* minor(rdev) */
214 a = write_cpio_word(a, path_size + 1); /* fname size */
215 a = write_cpio_word(a, 0); /* "crc" */
216
217 a = mempcpy(a, path, path_size + 1);
218
219 /* Pad to next multiple of 4 */
220 a = pad4(a, *cpio_buffer);
221
222 assert(a == (char *) *cpio_buffer + *cpio_buffer_size + l);
223
224 *cpio_buffer_size += l;
225 return EFI_SUCCESS;
226 }
227
228 EFI_STATUS pack_cpio_prefix(
229 const CpioTarget *target,
230 uint32_t *inode_counter,
231 void **cpio_buffer,
232 size_t *cpio_buffer_size) {
233
234 EFI_STATUS err;
235
236 assert(target);
237 assert(inode_counter);
238 assert(cpio_buffer);
239 assert(cpio_buffer_size);
240
241 /* Serializes directory inodes of all prefix paths of the specified path in cpio format. Note that
242 * (similar to mkdir -p behaviour) all leading paths are created with 0555 access mode, only the
243 * final dir is created with the specified directory access mode. */
244
245 for (const char *p = target->directory;;) {
246 const char *e;
247
248 e = strchr8(p, '/');
249 if (!e)
250 break;
251
252 if (e > p) {
253 _cleanup_free_ char *t = NULL;
254
255 t = xstrndup8(target->directory, e - target->directory);
256 if (!t)
257 return EFI_OUT_OF_RESOURCES;
258
259 err = pack_cpio_dir(t, 0555, inode_counter, cpio_buffer, cpio_buffer_size);
260 if (err != EFI_SUCCESS)
261 return err;
262 }
263
264 p = e + 1;
265 }
266
267 return pack_cpio_dir(target->directory, target->dir_mode, inode_counter, cpio_buffer, cpio_buffer_size);
268 }
269
270 EFI_STATUS pack_cpio_trailer(
271 void **cpio_buffer,
272 size_t *cpio_buffer_size) {
273
274 static const char trailer[] =
275 "070701"
276 "00000000"
277 "00000000"
278 "00000000"
279 "00000000"
280 "00000001"
281 "00000000"
282 "00000000"
283 "00000000"
284 "00000000"
285 "00000000"
286 "00000000"
287 "0000000B"
288 "00000000"
289 "TRAILER!!!\0\0\0"; /* There's a fourth NUL byte appended here, because this is a string */
290
291 /* Generates the cpio trailer record that indicates the end of our initrd cpio archive */
292
293 assert(cpio_buffer);
294 assert(cpio_buffer_size);
295 assert_cc(sizeof(trailer) % 4 == 0);
296
297 *cpio_buffer = xrealloc(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + sizeof(trailer));
298 memcpy((uint8_t*) *cpio_buffer + *cpio_buffer_size, trailer, sizeof(trailer));
299 *cpio_buffer_size += sizeof(trailer);
300
301 return EFI_SUCCESS;
302 }
303
304 EFI_STATUS pack_cpio(
305 EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
306 const char16_t *dropin_dir,
307 const char16_t *match_suffix,
308 const char16_t *exclude_suffix,
309 const CpioTarget *target,
310 const char16_t *tpm_description,
311 struct iovec *ret_buffer,
312 bool *ret_measured) {
313
314 _cleanup_file_close_ EFI_FILE *root = NULL, *extra_dir = NULL;
315 size_t dirent_size = 0, buffer_size = 0, n_items = 0, n_allocated = 0;
316 _cleanup_free_ char16_t *rel_dropin_dir = NULL;
317 _cleanup_free_ EFI_FILE_INFO *dirent = NULL;
318 _cleanup_strv_free_ char16_t **items = NULL;
319 _cleanup_free_ void *buffer = NULL;
320 uint32_t inode = 1; /* inode counter, so that each item gets a new inode */
321 EFI_STATUS err;
322
323 assert(loaded_image);
324 assert(target);
325 assert(ret_buffer);
326
327 if (!loaded_image->DeviceHandle)
328 goto nothing;
329
330 err = open_volume(loaded_image->DeviceHandle, &root);
331 if (err == EFI_UNSUPPORTED)
332 /* Error will be unsupported if the bootloader doesn't implement the file system protocol on
333 * its file handles. */
334 goto nothing;
335 if (err != EFI_SUCCESS)
336 return log_error_status(err, "Unable to open root directory: %m");
337
338 if (!dropin_dir) {
339 dropin_dir = rel_dropin_dir = get_extra_dir(loaded_image->FilePath);
340 if (!dropin_dir)
341 goto nothing;
342 }
343
344 err = open_directory(root, dropin_dir, &extra_dir);
345 if (err == EFI_NOT_FOUND)
346 /* No extra subdir, that's totally OK */
347 goto nothing;
348 if (err != EFI_SUCCESS)
349 return log_error_status(err, "Failed to open extra directory of loaded image: %m");
350
351 for (;;) {
352 _cleanup_free_ char16_t *d = NULL;
353
354 err = readdir(extra_dir, &dirent, &dirent_size);
355 if (err != EFI_SUCCESS)
356 return log_error_status(err, "Failed to read extra directory of loaded image: %m");
357 if (!dirent) /* End of directory */
358 break;
359
360 if (dirent->FileName[0] == '.')
361 continue;
362 if (FLAGS_SET(dirent->Attribute, EFI_FILE_DIRECTORY))
363 continue;
364 if (match_suffix && !endswith_no_case(dirent->FileName, match_suffix))
365 continue;
366 if (exclude_suffix && endswith_no_case(dirent->FileName, exclude_suffix))
367 continue;
368 if (!is_ascii(dirent->FileName))
369 continue;
370 if (strlen16(dirent->FileName) > 255) /* Max filename size on Linux */
371 continue;
372
373 d = xstrdup16(dirent->FileName);
374
375 if (n_items+2 > n_allocated) {
376 /* We allocate 16 entries at a time, as a matter of optimization */
377 if (n_items > (SIZE_MAX / sizeof(uint16_t)) - 16) /* Overflow check, just in case */
378 return log_oom();
379
380 size_t m = n_items + 16;
381 items = xrealloc(items, n_allocated * sizeof(uint16_t *), m * sizeof(uint16_t *));
382 n_allocated = m;
383 }
384
385 items[n_items++] = TAKE_PTR(d);
386 items[n_items] = NULL; /* Let's always NUL terminate, to make freeing via strv_free() easy */
387 }
388
389 if (n_items == 0)
390 /* Empty directory */
391 goto nothing;
392
393 /* Now, sort the files we found, to make this uniform and stable (and to ensure the TPM measurements
394 * are not dependent on read order) */
395 sort_pointer_array((void**) items, n_items, (compare_pointer_func_t) strcmp16);
396
397 /* Generate the leading directory inodes right before adding the first files, to the
398 * archive. Otherwise the cpio archive cannot be unpacked, since the leading dirs won't exist. */
399 err = pack_cpio_prefix(target, &inode, &buffer, &buffer_size);
400 if (err != EFI_SUCCESS)
401 return log_error_status(err, "Failed to pack cpio prefix: %m");
402
403 for (size_t i = 0; i < n_items; i++) {
404 _cleanup_free_ char *content = NULL;
405 size_t contentsize = 0; /* avoid false maybe-uninitialized warning */
406
407 err = file_read(extra_dir, items[i], 0, 0, &content, &contentsize);
408 if (err != EFI_SUCCESS) {
409 log_warning_status(err, "Failed to read %ls, ignoring: %m", items[i]);
410 continue;
411 }
412
413 err = pack_cpio_one(
414 items[i],
415 content, contentsize,
416 target,
417 &inode,
418 &buffer, &buffer_size);
419 if (err != EFI_SUCCESS)
420 return log_error_status(err, "Failed to pack cpio file %ls: %m", dirent->FileName);
421 }
422
423 err = pack_cpio_trailer(&buffer, &buffer_size);
424 if (err != EFI_SUCCESS)
425 return log_error_status(err, "Failed to pack cpio trailer: %m");
426
427 err = tpm_log_ipl_event(
428 target->tpm_pcr,
429 POINTER_TO_PHYSICAL_ADDRESS(buffer),
430 buffer_size,
431 tpm_description,
432 ret_measured);
433 if (err != EFI_SUCCESS)
434 return log_error_status(
435 err,
436 "Unable to add cpio TPM measurement for PCR %u (%ls): %m",
437 target->tpm_pcr,
438 tpm_description);
439
440 *ret_buffer = IOVEC_MAKE(TAKE_PTR(buffer), buffer_size);
441 return EFI_SUCCESS;
442
443 nothing:
444 *ret_buffer = (struct iovec) {};
445
446 if (ret_measured)
447 *ret_measured = false;
448
449 return EFI_SUCCESS;
450 }
451
452 EFI_STATUS pack_cpio_literal(
453 const void *data,
454 size_t data_size,
455 const CpioTarget *target,
456 const char16_t *target_filename,
457 const char16_t *tpm_description,
458 struct iovec *ret_buffer,
459 bool *ret_measured) {
460
461 uint32_t inode = 1; /* inode counter, so that each item gets a new inode */
462 _cleanup_free_ void *buffer = NULL;
463 size_t buffer_size = 0;
464 EFI_STATUS err;
465
466 assert(data || data_size == 0);
467 assert(target);
468 assert(target_filename);
469 assert(ret_buffer);
470
471 /* Generate the leading directory inodes right before adding the first files, to the
472 * archive. Otherwise the cpio archive cannot be unpacked, since the leading dirs won't exist. */
473
474 err = pack_cpio_prefix(target, &inode, &buffer, &buffer_size);
475 if (err != EFI_SUCCESS)
476 return log_error_status(err, "Failed to pack cpio prefix: %m");
477
478 err = pack_cpio_one(
479 target_filename,
480 data, data_size,
481 target,
482 &inode,
483 &buffer, &buffer_size);
484 if (err != EFI_SUCCESS)
485 return log_error_status(err, "Failed to pack cpio file %ls: %m", target_filename);
486
487 err = pack_cpio_trailer(&buffer, &buffer_size);
488 if (err != EFI_SUCCESS)
489 return log_error_status(err, "Failed to pack cpio trailer: %m");
490
491 err = tpm_log_ipl_event(
492 target->tpm_pcr,
493 POINTER_TO_PHYSICAL_ADDRESS(buffer),
494 buffer_size,
495 tpm_description,
496 ret_measured);
497 if (err != EFI_SUCCESS)
498 return log_error_status(
499 err,
500 "Unable to add cpio TPM measurement for PCR %u (%ls): %m",
501 target->tpm_pcr,
502 tpm_description);
503
504 *ret_buffer = IOVEC_MAKE(TAKE_PTR(buffer), buffer_size);
505 return EFI_SUCCESS;
506 }
507
508 /* The following are canonical definitions of the various cpio target directories we place resources in. We
509 * define them here in a single canonical list of targets because we need to reuse them at various places
510 * (well, some of them at least), and we don't want the access modes to deviate slightly on each use. */
511
512 const CpioTarget cpio_target_credentials = {
513 .directory = ".extra/credentials",
514 .dir_mode = 0500,
515 .access_mode = 0400,
516 .tpm_pcr = TPM2_PCR_KERNEL_CONFIG,
517 };
518
519 const CpioTarget cpio_target_global_credentials = {
520 .directory = ".extra/global_credentials",
521 .dir_mode = 0500,
522 .access_mode = 0400,
523 .tpm_pcr = TPM2_PCR_KERNEL_CONFIG,
524 };
525
526 const CpioTarget cpio_target_sysext = {
527 .directory = ".extra/sysext",
528 .dir_mode = 0555,
529 .access_mode = 0444,
530 .tpm_pcr = TPM2_PCR_SYSEXTS,
531 };
532
533 const CpioTarget cpio_target_global_sysext = {
534 .directory = ".extra/global_sysext",
535 .dir_mode = 0555,
536 .access_mode = 0444,
537 .tpm_pcr = TPM2_PCR_SYSEXTS,
538 };
539
540 const CpioTarget cpio_target_confext = {
541 .directory = ".extra/confext",
542 .dir_mode = 0555,
543 .access_mode = 0444,
544 .tpm_pcr = TPM2_PCR_KERNEL_CONFIG,
545 };
546
547 const CpioTarget cpio_target_global_confext = {
548 .directory = ".extra/global_confext",
549 .dir_mode = 0555,
550 .access_mode = 0444,
551 .tpm_pcr = TPM2_PCR_KERNEL_CONFIG,
552 };
553
554 const CpioTarget cpio_target_meta = {
555 .directory = ".extra",
556 .dir_mode = 0555,
557 .access_mode = 0444,
558 .tpm_pcr = UINT32_MAX,
559 };
560
561 const CpioTarget cpio_target_meta_secret = {
562 .directory = ".extra",
563 .dir_mode = 0555,
564 .access_mode = 0400,
565 .tpm_pcr = UINT32_MAX,
566 };