Debian Codegraph @

src/shared/pe-binary.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "crypto-util.h"
8 #include "hexdecoct.h"
9 #include "log.h"
10 #include "pe-binary.h"
11 #include "sort-util.h"
12 #include "stat-util.h"
13 #include "string-table.h"
14 #include "string-util.h"
15
16 /* Cap on the (VirtualSize - SizeOfRawData) zero-padding the UKI hasher
17 * will produce for a single section. Any value beyond this is treated as
18 * a malformed PE — bounds the hash work an attacker can drive (#42344). */
19 #define UKI_HASH_VIRTUAL_SIZE_PADDING_MAX (64U * 1024U * 1024U)
20
21 /* Note: none of these function change the file position of the provided fd, as they use pread() */
22
23 bool pe_header_is_64bit(const PeHeader *h) {
24 assert(h);
25
26 if (le16toh(h->optional.Magic) == UINT16_C(0x010B)) /* PE32 */
27 return false;
28
29 if (le16toh(h->optional.Magic) == UINT16_C(0x020B)) /* PE32+ */
30 return true;
31
32 assert_not_reached();
33 }
34
35 static size_t pe_header_size(const PeHeader *pe_header) {
36 assert(pe_header);
37
38 return offsetof(PeHeader, optional) + le16toh(pe_header->pe.SizeOfOptionalHeader);
39 }
40
41 const IMAGE_DATA_DIRECTORY* pe_header_get_data_directory(
42 const PeHeader *h,
43 size_t i) {
44
45 assert(h);
46
47 if (i >= le32toh(PE_HEADER_OPTIONAL_FIELD(h, NumberOfRvaAndSizes)))
48 return NULL;
49
50 return PE_HEADER_OPTIONAL_FIELD(h, DataDirectory) + i;
51 }
52
53 const IMAGE_SECTION_HEADER* pe_section_table_find(
54 const IMAGE_SECTION_HEADER *sections,
55 size_t n_sections,
56 const char *name) {
57
58 size_t n;
59
60 assert(name);
61 assert(sections || n_sections == 0);
62
63 n = strlen(name);
64 if (n > sizeof(sections[0].Name)) /* Too long? */
65 return NULL;
66
67 FOREACH_ARRAY(section, sections, n_sections)
68 if (memcmp(section->Name, name, n) == 0 &&
69 (n == sizeof(sections[0].Name) || memeqzero(section->Name + n, sizeof(section->Name) - n)))
70 return section;
71
72 return NULL;
73 }
74
75 const IMAGE_SECTION_HEADER* pe_header_find_section(
76 const PeHeader *pe_header,
77 const IMAGE_SECTION_HEADER *sections,
78 const char *name) {
79
80 assert(pe_header);
81
82 return pe_section_table_find(sections, le16toh(pe_header->pe.NumberOfSections), name);
83 }
84
85 int pe_load_headers(
86 int fd,
87 IMAGE_DOS_HEADER **ret_dos_header,
88 PeHeader **ret_pe_header) {
89
90 _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
91 _cleanup_free_ PeHeader *pe_header = NULL;
92 ssize_t n;
93
94 assert(fd >= 0);
95
96 dos_header = new(IMAGE_DOS_HEADER, 1);
97 if (!dos_header)
98 return log_oom_debug();
99
100 n = pread(fd,
101 dos_header,
102 sizeof(IMAGE_DOS_HEADER),
103 0);
104 if (n < 0)
105 return log_debug_errno(errno, "Failed to read DOS header: %m");
106 if ((size_t) n != sizeof(IMAGE_DOS_HEADER))
107 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Short read while reading MZ executable header.");
108
109 if (le16toh(dos_header->e_magic) != UINT16_C(0x5A4D))
110 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "File lacks MZ executable header.");
111
112 pe_header = new(PeHeader, 1);
113 if (!pe_header)
114 return log_oom_debug();
115
116 n = pread(fd,
117 pe_header,
118 offsetof(PeHeader, optional),
119 le32toh(dos_header->e_lfanew));
120 if (n < 0)
121 return log_debug_errno(errno, "Failed to read PE executable header: %m");
122 if ((size_t) n != offsetof(PeHeader, optional))
123 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Short read while reading PE executable header.");
124
125 if (le32toh(pe_header->signature) != UINT32_C(0x00004550))
126 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "File lacks PE executable header.");
127
128 if (le16toh(pe_header->pe.SizeOfOptionalHeader) < sizeof_field(PeHeader, optional.Magic))
129 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Optional header size too short for magic.");
130
131 PeHeader *pe_header_tmp = realloc(pe_header, MAX(sizeof(PeHeader), pe_header_size(pe_header)));
132 if (!pe_header_tmp)
133 return log_oom_debug();
134 pe_header = pe_header_tmp;
135
136 n = pread(fd,
137 &pe_header->optional,
138 le16toh(pe_header->pe.SizeOfOptionalHeader),
139 le32toh(dos_header->e_lfanew) + offsetof(PeHeader, optional));
140 if (n < 0)
141 return log_debug_errno(errno, "Failed to read PE executable optional header: %m");
142 if ((size_t) n != le16toh(pe_header->pe.SizeOfOptionalHeader))
143 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Short read while reading PE executable optional header.");
144
145 if (!IN_SET(le16toh(pe_header->optional.Magic), UINT16_C(0x010B), UINT16_C(0x020B)))
146 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Optional header magic invalid.");
147
148 /* The optional header must be at least large enough to cover everything
149 * up to and including NumberOfRvaAndSizes — otherwise the equality
150 * check below would read uninitialised memory for that field. */
151 if (pe_header_size(pe_header) < PE_HEADER_OPTIONAL_FIELD_OFFSET(pe_header, DataDirectory))
152 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Optional header too small.");
153
154 if (pe_header_size(pe_header) !=
155 PE_HEADER_OPTIONAL_FIELD_OFFSET(pe_header, DataDirectory) +
156 sizeof(IMAGE_DATA_DIRECTORY) * (uint64_t) le32toh(PE_HEADER_OPTIONAL_FIELD(pe_header, NumberOfRvaAndSizes)))
157 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Optional header size mismatch.");
158
159 if (ret_dos_header)
160 *ret_dos_header = TAKE_PTR(dos_header);
161 if (ret_pe_header)
162 *ret_pe_header = TAKE_PTR(pe_header);
163
164 return 0;
165 }
166
167 int pe_load_sections(
168 int fd,
169 const IMAGE_DOS_HEADER *dos_header,
170 const PeHeader *pe_header,
171 IMAGE_SECTION_HEADER **ret_sections) {
172
173 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
174 struct stat st;
175 size_t nos;
176 ssize_t n;
177
178 assert(fd >= 0);
179 assert(dos_header);
180 assert(pe_header);
181
182 nos = le16toh(pe_header->pe.NumberOfSections);
183
184 sections = new(IMAGE_SECTION_HEADER, nos);
185 if (!sections)
186 return log_oom_debug();
187
188 n = pread(fd,
189 sections,
190 sizeof(IMAGE_SECTION_HEADER) * nos,
191 le32toh(dos_header->e_lfanew) + pe_header_size(pe_header));
192 if (n < 0)
193 return log_debug_errno(errno, "Failed to read section table: %m");
194 if ((size_t) n != sizeof(IMAGE_SECTION_HEADER) * nos)
195 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Short read while reading section table.");
196
197 /* The section's raw bytes must fit inside the file. This is the
198 * fundamental invariant the parser relies on later (pe_hash, uki_hash,
199 * pe_read_section_data, ...); reject obvious malformations early so
200 * downstream loops don't get driven by attacker-controlled sizes. */
201 if (fstat(fd, &st) < 0)
202 return log_debug_errno(errno, "Failed to stat PE file: %m");
203
204 FOREACH_ARRAY(section, sections, nos) {
205 uint64_t prd = le32toh(section->PointerToRawData), srd = le32toh(section->SizeOfRawData), end;
206
207 /* SizeOfRawData == 0 is legitimate (BSS-like, uninitialised) —
208 * PointerToRawData is then meaningless and not used as an offset. */
209 if (srd == 0)
210 continue;
211
212 if (!ADD_SAFE(&end, prd, srd) || end > (uint64_t) st.st_size)
213 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
214 "PE section raw data exceeds file size.");
215 }
216
217 if (ret_sections)
218 *ret_sections = TAKE_PTR(sections);
219
220 return 0;
221 }
222
223 int pe_read_section_data(
224 int fd,
225 const IMAGE_SECTION_HEADER *section,
226 size_t max_size,
227 void **ret,
228 size_t *ret_size) {
229
230 assert(fd >= 0);
231 assert(section);
232
233 size_t n = le32toh(section->VirtualSize);
234 if (n > MIN(max_size, (size_t) SSIZE_MAX))
235 return -EBADMSG;
236
237 _cleanup_free_ void *data = malloc(n+1);
238 if (!data)
239 return -ENOMEM;
240
241 ssize_t ss = pread(fd, data, n, le32toh(section->PointerToRawData));
242 if (ss < 0)
243 return -errno;
244 if ((size_t) ss != n)
245 return -EBADMSG;
246
247 if (ret_size)
248 *ret_size = n;
249 else {
250 /* Check that there are no embedded NUL bytes if the caller doesn't want to know the size
251 * (i.e. treats the blob as a string) */
252 const char *nul;
253
254 nul = memchr(data, 0, n);
255 if (nul && !memeqzero(nul, n - (nul - (const char*) data))) /* If there's a NUL it must only be NULs from there on */
256 return -EBADMSG;
257 }
258 if (ret) {
259 ((uint8_t*) data)[n] = 0; /* NUL terminate, no matter what */
260 *ret = TAKE_PTR(data);
261 }
262
263 return 0;
264 }
265
266 int pe_read_section_data_by_name(
267 int fd,
268 const PeHeader *pe_header,
269 const IMAGE_SECTION_HEADER *sections,
270 const char *name,
271 size_t max_size,
272 void **ret,
273 size_t *ret_size) {
274
275 const IMAGE_SECTION_HEADER *section;
276
277 assert(fd >= 0);
278 assert(pe_header);
279 assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0);
280 assert(name);
281
282 section = pe_header_find_section(pe_header, sections, name);
283 if (!section)
284 return -ENXIO;
285
286 return pe_read_section_data(fd, section, max_size, ret, ret_size);
287 }
288
289 bool pe_is_uki(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections) {
290 assert(pe_header);
291 assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0);
292
293 if (le16toh(pe_header->optional.Subsystem) != IMAGE_SUBSYSTEM_EFI_APPLICATION)
294 return false;
295
296 /* Note that the UKI spec only requires .linux, but we are stricter here, and require .osrel too,
297 * since for sd-boot it just doesn't make sense to not have that. */
298 return
299 pe_header_find_section(pe_header, sections, ".osrel") &&
300 pe_header_find_section(pe_header, sections, ".linux");
301 }
302
303 bool pe_is_addon(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections) {
304 assert(pe_header);
305 assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0);
306
307 if (le16toh(pe_header->optional.Subsystem) != IMAGE_SUBSYSTEM_EFI_APPLICATION)
308 return false;
309
310 /* Add-ons do not have a Linux kernel, but do have one of .cmdline, .dtb, .initrd or .ucode (currently) */
311 return !pe_header_find_section(pe_header, sections, ".linux") &&
312 (pe_header_find_section(pe_header, sections, ".cmdline") ||
313 pe_header_find_section(pe_header, sections, ".dtb") ||
314 pe_header_find_section(pe_header, sections, ".initrd") ||
315 pe_header_find_section(pe_header, sections, ".ucode"));
316 }
317
318 bool pe_is_native(const PeHeader *pe_header) {
319 assert(pe_header);
320
321 #ifdef _IMAGE_FILE_MACHINE_NATIVE
322 return le16toh(pe_header->pe.Machine) == _IMAGE_FILE_MACHINE_NATIVE;
323 #else
324 return false;
325 #endif
326 }
327
328 int pe_is_native_fd(int fd) {
329 _cleanup_free_ PeHeader *pe_header = NULL;
330 int r;
331
332 r = pe_load_headers(fd, /* ret_dos_header= */ NULL, &pe_header);
333 if (r < 0)
334 return r;
335
336 return pe_is_native(pe_header);
337 }
338
339 /* Implements:
340 *
341 * https://download.microsoft.com/download/9/c/5/9c5b2167-8017-4bae-9fde-d599bac8184a/authenticode_pe.docx
342 * → Section "Calculating the PE Image Hash"
343 */
344
345 #if HAVE_OPENSSL
346 static int hash_file(int fd, EVP_MD_CTX *md_ctx, uint64_t offset, uint64_t size) {
347 uint8_t buffer[64*1024];
348
349 log_debug("Hashing %" PRIu64 " @ %" PRIu64 " → %" PRIu64, size, offset, offset + size);
350
351 while (size > 0) {
352 size_t m = MIN(size, sizeof(buffer));
353 ssize_t n;
354
355 n = pread(fd, buffer, m, offset);
356 if (n < 0)
357 return log_debug_errno(errno, "Failed to read file for hashing: %m");
358 if ((size_t) n != m)
359 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Short read while hashing.");
360
361 if (sym_EVP_DigestUpdate(md_ctx, buffer, m) != 1)
362 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Unable to hash data.");
363
364 offset += m;
365 size -= m;
366 }
367
368 return 0;
369 }
370
371 static int section_offset_cmp(const IMAGE_SECTION_HEADER *a, const IMAGE_SECTION_HEADER *b) {
372 return CMP(le32toh(ASSERT_PTR(a)->PointerToRawData), le32toh(ASSERT_PTR(b)->PointerToRawData));
373 }
374
375 int pe_hash(int fd,
376 const EVP_MD *md,
377 void **ret_hash,
378 size_t *ret_hash_size) {
379
380 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *mdctx = NULL;
381 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
382 _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
383 _cleanup_free_ PeHeader *pe_header = NULL;
384 const IMAGE_DATA_DIRECTORY *certificate_table;
385 struct stat st;
386 uint64_t p, q;
387 int r;
388
389 assert(fd >= 0);
390 assert(md);
391 assert(ret_hash_size);
392 assert(ret_hash);
393
394 r = dlopen_libcrypto(LOG_DEBUG);
395 if (r < 0)
396 return r;
397
398 if (fstat(fd, &st) < 0)
399 return log_debug_errno(errno, "Failed to stat file: %m");
400 r = stat_verify_regular(&st);
401 if (r < 0)
402 return log_debug_errno(r, "Not a regular file: %m");
403
404 r = pe_load_headers(fd, &dos_header, &pe_header);
405 if (r < 0)
406 return r;
407
408 r = pe_load_sections(fd, dos_header, pe_header, &sections);
409 if (r < 0)
410 return r;
411
412 certificate_table = pe_header_get_data_directory(pe_header, IMAGE_DATA_DIRECTORY_INDEX_CERTIFICATION_TABLE);
413 if (!certificate_table)
414 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "File lacks certificate table.");
415
416 mdctx = sym_EVP_MD_CTX_new();
417 if (!mdctx)
418 return log_oom_debug();
419
420 if (sym_EVP_DigestInit_ex(mdctx, md, NULL) != 1)
421 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to allocate message digest.");
422
423 /* Everything from beginning of file to CheckSum field in PE header */
424 p = (uint64_t) le32toh(dos_header->e_lfanew) +
425 offsetof(PeHeader, optional.CheckSum);
426 r = hash_file(fd, mdctx, 0, p);
427 if (r < 0)
428 return r;
429 p += sizeof(le32_t);
430
431 /* Everything between the CheckSum field and the Image Data Directory Entry for the Certification Table */
432 q = (uint64_t) le32toh(dos_header->e_lfanew) +
433 PE_HEADER_OPTIONAL_FIELD_OFFSET(pe_header, DataDirectory[IMAGE_DATA_DIRECTORY_INDEX_CERTIFICATION_TABLE]);
434 r = hash_file(fd, mdctx, p, q - p);
435 if (r < 0)
436 return r;
437 q += sizeof(IMAGE_DATA_DIRECTORY);
438
439 /* The rest of the header + the section table */
440 p = le32toh(pe_header->optional.SizeOfHeaders);
441 if (p < q)
442 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "SizeOfHeaders too short.");
443 r = hash_file(fd, mdctx, q, p - q);
444 if (r < 0)
445 return r;
446
447 /* Sort by location in file */
448 typesafe_qsort(sections, le16toh(pe_header->pe.NumberOfSections), section_offset_cmp);
449
450 FOREACH_ARRAY(section, sections, le16toh(pe_header->pe.NumberOfSections)) {
451 r = hash_file(fd, mdctx, le32toh(section->PointerToRawData), le32toh(section->SizeOfRawData));
452 if (r < 0)
453 return r;
454
455 p += le32toh(section->SizeOfRawData);
456 }
457
458 if ((uint64_t) st.st_size > p) {
459
460 if ((uint64_t) st.st_size - p < le32toh(certificate_table->Size))
461 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "No space for certificate table, refusing.");
462
463 r = hash_file(fd, mdctx, p, (uint64_t) st.st_size - p - le32toh(certificate_table->Size));
464 if (r < 0)
465 return r;
466
467 /* If the file size is not a multiple of 8 bytes, pad the hash with zero bytes. */
468 if (st.st_size % 8 != 0 && sym_EVP_DigestUpdate(mdctx, (const uint8_t[8]) {}, 8 - (st.st_size % 8)) != 1)
469 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Unable to hash data.");
470 }
471
472 int hsz = sym_EVP_MD_CTX_get_size(mdctx);
473 if (hsz < 0)
474 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to get hash size.");
475
476 unsigned hash_size = (unsigned) hsz;
477 _cleanup_free_ void *hash = malloc(hsz);
478 if (!hash)
479 return log_oom_debug();
480
481 if (sym_EVP_DigestFinal_ex(mdctx, hash, &hash_size) != 1)
482 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to finalize hash function.");
483
484 assert(hash_size == (unsigned) hsz);
485
486 *ret_hash = TAKE_PTR(hash);
487 *ret_hash_size = hash_size;
488
489 return 0;
490 }
491
492 int pe_checksum(int fd, uint32_t *ret) {
493 _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
494 _cleanup_free_ PeHeader *pe_header = NULL;
495 struct stat st;
496 int r;
497
498 assert(fd >= 0);
499 assert(ret);
500
501 if (fstat(fd, &st) < 0)
502 return log_debug_errno(errno, "Failed to stat file: %m");
503
504 r = pe_load_headers(fd, &dos_header, &pe_header);
505 if (r < 0)
506 return r;
507
508 uint32_t checksum = 0, checksum_offset = le32toh(dos_header->e_lfanew) + offsetof(PeHeader, optional.CheckSum);
509 size_t off = 0;
510 for (;;) {
511 uint16_t buf[32*1024];
512
513 ssize_t n = pread(fd, buf, sizeof(buf), off);
514 if (n == 0)
515 break;
516 if (n < 0)
517 return log_debug_errno(errno, "Failed to read from PE file: %m");
518 if (n % sizeof(uint16_t) != 0)
519 return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Short read from PE file");
520
521 for (size_t i = 0; i < (size_t) n / 2; i++) {
522 size_t pos = off + i * sizeof(uint16_t);
523 if (pos >= checksum_offset && pos < checksum_offset + sizeof(pe_header->optional.CheckSum))
524 continue;
525
526 uint16_t val = le16toh(buf[i]);
527
528 checksum += val;
529 checksum = (checksum >> 16) + (checksum & 0xffff);
530 }
531
532 off += n;
533 }
534
535 checksum = (checksum >> 16) + (checksum & 0xffff);
536 checksum += off;
537
538 *ret = checksum;
539 return 0;
540 }
541
542 typedef void* SectionHashArray[_UNIFIED_SECTION_MAX];
543
544 static void section_hash_array_done(SectionHashArray *array) {
545 assert(array);
546
547 for (size_t i = 0; i < _UNIFIED_SECTION_MAX; i++)
548 free((*array)[i]);
549 }
550
551 int uki_hash(int fd,
552 const EVP_MD *md,
553 void* ret_hashes[static _UNIFIED_SECTION_MAX],
554 size_t *ret_hash_size) {
555
556 _cleanup_(section_hash_array_done) SectionHashArray hashes = {};
557 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
558 _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
559 _cleanup_free_ PeHeader *pe_header = NULL;
560 int r;
561
562 assert(fd >= 0);
563 assert(ret_hashes);
564 assert(ret_hash_size);
565
566 r = dlopen_libcrypto(LOG_DEBUG);
567 if (r < 0)
568 return r;
569
570 r = pe_load_headers(fd, &dos_header, &pe_header);
571 if (r < 0)
572 return r;
573
574 r = pe_load_sections(fd, dos_header, pe_header, &sections);
575 if (r < 0)
576 return r;
577
578 int hsz = sym_EVP_MD_get_size(md);
579 if (hsz < 0)
580 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to get hash size.");
581
582 FOREACH_ARRAY(section, sections, le16toh(pe_header->pe.NumberOfSections)) {
583 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *mdctx = NULL;
584 _cleanup_free_ char *n = NULL;
585 ssize_t i;
586
587 n = memdup_suffix0(section->Name, sizeof(section->Name));
588 if (!n)
589 return log_oom_debug();
590
591 i = string_table_lookup_from_string(unified_sections, _UNIFIED_SECTION_MAX, n);
592 if (i < 0)
593 continue;
594
595 if (hashes[i])
596 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Duplicate section");
597
598 mdctx = sym_EVP_MD_CTX_new();
599 if (!mdctx)
600 return log_oom_debug();
601
602 if (sym_EVP_DigestInit_ex(mdctx, md, NULL) != 1)
603 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to allocate message digest.");
604
605 r = hash_file(fd, mdctx, le32toh(section->PointerToRawData), MIN(le32toh(section->VirtualSize), le32toh(section->SizeOfRawData)));
606 if (r < 0)
607 return r;
608
609 if (le32toh(section->SizeOfRawData) < le32toh(section->VirtualSize)) {
610 uint8_t zeroes[1024] = {};
611 size_t remaining = le32toh(section->VirtualSize) - le32toh(section->SizeOfRawData);
612
613 /* Bound the zero-padding hash work. An attacker can otherwise
614 * set VirtualSize close to UINT32_MAX with SizeOfRawData = 0,
615 * driving ~4 GiB of SHA-256 work per section on a tiny file
616 * (issue #42344 — wedges the parser for >10 s on 382 B). */
617 if (remaining > UKI_HASH_VIRTUAL_SIZE_PADDING_MAX)
618 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
619 "Section VirtualSize exceeds SizeOfRawData by %zu bytes (cap %zu); refusing to hash.",
620 remaining,
621 (size_t) UKI_HASH_VIRTUAL_SIZE_PADDING_MAX);
622
623 while (remaining > 0) {
624 size_t sz = MIN(sizeof(zeroes), remaining);
625
626 if (sym_EVP_DigestUpdate(mdctx, zeroes, sz) != 1)
627 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Unable to hash data.");
628
629 remaining -= sz;
630 }
631 }
632
633 hashes[i] = malloc(hsz);
634 if (!hashes[i])
635 return log_oom_debug();
636
637 unsigned hash_size = (unsigned) hsz;
638 if (sym_EVP_DigestFinal_ex(mdctx, hashes[i], &hash_size) != 1)
639 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to finalize hash function.");
640
641 assert(hash_size == (unsigned) hsz);
642
643 if (DEBUG_LOGGING) {
644 _cleanup_free_ char *hs = NULL;
645
646 hs = hexmem(hashes[i], hsz);
647 log_debug("Section %s with %s is %s.", n, sym_EVP_MD_get0_name(md), strna(hs));
648 }
649 }
650
651 memcpy(ret_hashes, hashes, sizeof(hashes));
652 zero(hashes);
653 *ret_hash_size = (unsigned) hsz;
654
655 return 0;
656 }
657 #endif