Debian Codegraph @

src/test/test-pe-binary.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* Regression tests for the algorithmic-complexity DoS fix in src/shared/pe-binary.c
4 * (issue #42344 — PE file with attacker-controlled VirtualSize wedges uki_hash
5 * in an unbounded SHA-256 zero-padding loop).
6 *
7 * Coverage:
8 * - pe_load_sections() rejects sections whose PointerToRawData + SizeOfRawData
9 * exceeds the actual file size (PLAN §3.1).
10 * - uki_hash() refuses to hash a section whose VirtualSize exceeds
11 * SizeOfRawData by more than UKI_HASH_VIRTUAL_SIZE_PADDING_MAX (=64 MiB)
12 * and instead returns -EBADMSG (PLAN §3.2).
13 *
14 * Tests build small PE32+ images in a memfd. The header layout mirrors the
15 * canonical 382-byte reproducer (DOS at 0x00, e_lfanew=0x40, PE32+ optional
16 * header of 0xf0 bytes, section table starting at file offset 0x148) so
17 * pe_load_headers() accepts every test fixture; only the section table and
18 * file size are varied per test. */
19
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "alloc-util.h"
26 #include "fd-util.h"
27 #include "memfd-util.h"
28 #include "pe-binary.h"
29 #include "tests.h"
30 #include "uki.h"
31 #include "unaligned.h"
32
33 #if HAVE_OPENSSL
34 # include "crypto-util.h"
35 #endif
36
37 /* Match the cap defined in src/shared/pe-binary.c (PLAN §3.2). */
38 #define UKI_HASH_PADDING_CAP_BYTES (64U * 1024U * 1024U)
39
40 /* File offsets of the PE32+ header skeleton this test builds. */
41 #define SECTION_TABLE_OFFSET 0x148
42 #define IMAGE_FILE_HEADER_NOS_OFFSET 0x46 /* NumberOfSections (le16) */
43 #define IMAGE_SECTION_HEADER_BYTES 40
44
45 /* Header skeleton, byte-for-byte compatible with the canonical reproducer
46 * so that pe_load_headers() succeeds without further mutation. We only
47 * patch NumberOfSections at runtime (offset 0x46) and append section
48 * headers + raw data after the optional header. */
49 static const uint8_t HEADER_SKELETON[SECTION_TABLE_OFFSET] = {
50 /* DOS header: "MZ" at 0x00, e_lfanew = 0x40 at 0x3c */
51 [0x00] = 'M', 'Z',
52 [0x3c] = 0x40, 0x00, 0x00, 0x00,
53
54 /* PE signature at 0x40 */
55 [0x40] = 'P', 'E', 0x00, 0x00,
56
57 /* IMAGE_FILE_HEADER at 0x44 */
58 [0x44] = 0x64, 0x86, /* Machine = 0x8664 (x86_64) */
59 /* [0x46]: NumberOfSections — patched per test */
60 [0x54] = 0xf0, 0x00, /* SizeOfOptionalHeader = 0xf0 */
61 [0x56] = 0x22, 0x00, /* Characteristics = executable image */
62
63 /* IMAGE_OPTIONAL_HEADER (PE32+) at 0x58 */
64 [0x58] = 0x0b, 0x02, /* Magic = 0x020b (PE32+) */
65 [0x9c] = 0x0a, 0x00, /* Subsystem = EFI Application */
66 [0xc4] = 0x10, 0x00, 0x00, 0x00, /* NumberOfRvaAndSizes = 16 (matches SizeOfOptionalHeader=0xf0) */
67 };
68
69 typedef struct SectionSpec {
70 const char *name; /* PE section name; truncated/padded to 8 bytes on write */
71 uint32_t virtual_size;
72 uint32_t virtual_address;
73 uint32_t size_of_raw_data;
74 uint32_t pointer_to_raw_data;
75 } SectionSpec;
76
77 /* Build a PE32+ image in a fresh memfd containing the given sections.
78 *
79 * If override_file_size > 0, the memfd is written with exactly that many
80 * bytes (zero-padded if larger than what the sections need). This lets a
81 * test claim section data past the actual EOF, or reserve headroom past
82 * the last section. If 0, the file is sized to exactly contain the
83 * headers + section table + every section's raw data. */
84 static int build_pe_file(
85 const SectionSpec *specs,
86 size_t n_sections,
87 size_t override_file_size) {
88
89 /* Minimum size is "headers + section table"; sections with SizeOfRawData>0
90 * must additionally fit inside the file. */
91 size_t base = SECTION_TABLE_OFFSET + n_sections * IMAGE_SECTION_HEADER_BYTES;
92 size_t needed = base;
93 FOREACH_ARRAY(spec, specs, n_sections)
94 if (spec->size_of_raw_data > 0) {
95 uint64_t end = (uint64_t) spec->pointer_to_raw_data + spec->size_of_raw_data;
96 if (end > needed)
97 needed = end;
98 }
99
100 size_t file_size = override_file_size > 0 ? override_file_size : needed;
101 ASSERT_GE(file_size, base);
102 _cleanup_free_ uint8_t *buf = ASSERT_NOT_NULL(new0(uint8_t, file_size));
103
104 memcpy(buf, HEADER_SKELETON, sizeof(HEADER_SKELETON));
105 unaligned_write_le16(buf + IMAGE_FILE_HEADER_NOS_OFFSET, (uint16_t) n_sections);
106
107 /* Section table at 0x148. */
108 FOREACH_ARRAY(spec, specs, n_sections) {
109 uint8_t *sh = buf + SECTION_TABLE_OFFSET + (spec - specs) * IMAGE_SECTION_HEADER_BYTES;
110 size_t nlen = strnlen(spec->name, 8);
111 memcpy(sh, spec->name, nlen);
112 unaligned_write_le32(sh + 8, spec->virtual_size);
113 unaligned_write_le32(sh + 12, spec->virtual_address);
114 unaligned_write_le32(sh + 16, spec->size_of_raw_data);
115 unaligned_write_le32(sh + 20, spec->pointer_to_raw_data);
116 /* Characteristics, relocs, etc., left zero. */
117 }
118
119 int fd = ASSERT_OK(memfd_new("test-pe-binary"));
120
121 /* Write only file_size bytes — that lets the caller construct a
122 * PE whose section table claims data past EOF. */
123 ASSERT_OK_EQ_ERRNO(write(fd, buf, file_size), (ssize_t) file_size);
124 ASSERT_OK_ERRNO(lseek(fd, 0, SEEK_SET));
125 return fd;
126 }
127
128 static int load_headers_and_sections(
129 int fd,
130 IMAGE_DOS_HEADER **ret_dos,
131 PeHeader **ret_pe,
132 IMAGE_SECTION_HEADER **ret_sections) {
133
134 int r = pe_load_headers(fd, ret_dos, ret_pe);
135 if (r < 0)
136 return r;
137 return pe_load_sections(fd, *ret_dos, *ret_pe, ret_sections);
138 }
139
140 /* ======================================================================
141 * pe_load_headers — SizeOfOptionalHeader / NumberOfRvaAndSizes bound
142 * ====================================================================== */
143
144 /* If SizeOfOptionalHeader is so small that pread() does not actually
145 * populate NumberOfRvaAndSizes, the optional-header size check at the end
146 * of pe_load_headers would read uninitialised heap memory (caught by MSAN
147 * under CIFuzz). pe_load_headers must reject such files with -EBADMSG
148 * before touching that field. */
149 TEST(pe_load_headers_optional_header_too_small) {
150 /* Minimum allowed by the existing magic check is 2 bytes (only
151 * IMAGE_OPTIONAL_HEADER.Magic). That is well below the
152 * NumberOfRvaAndSizes offset for both PE32 and PE32+. */
153 uint8_t buf[64 + 4 + 20 + 2] = {}; /* DOS + PE sig + file hdr + 2-byte optional */
154 buf[0] = 'M'; buf[1] = 'Z';
155 unaligned_write_le32(buf + 0x3c, 64);
156 memcpy(buf + 64, "PE\0\0", 4);
157 /* IMAGE_FILE_HEADER: leave most fields zero, set
158 * SizeOfOptionalHeader = 2 (offset 0x10 within IMAGE_FILE_HEADER). */
159 unaligned_write_le16(buf + 64 + 4 + 16, 2);
160 /* Optional header Magic = 0x020B (PE32+). */
161 unaligned_write_le16(buf + 64 + 4 + 20, 0x020B);
162
163 _cleanup_close_ int fd = ASSERT_OK(memfd_new("test-pe-binary"));
164 ASSERT_OK_EQ_ERRNO(write(fd, buf, sizeof(buf)), (ssize_t) sizeof(buf));
165 ASSERT_OK_ERRNO(lseek(fd, 0, SEEK_SET));
166
167 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
168 _cleanup_free_ PeHeader *pe = NULL;
169 ASSERT_ERROR(pe_load_headers(fd, &dos, &pe), EBADMSG);
170 }
171
172 /* ======================================================================
173 * pe_load_sections — bounds check from PLAN §3.1
174 * ====================================================================== */
175
176 /* Happy path: a section whose raw data fits comfortably inside the file
177 * (i.e. file has plenty of trailing headroom beyond the section). */
178 TEST(pe_load_sections_valid_in_bounds) {
179 SectionSpec s = {
180 .name = ".initrd",
181 .virtual_size = 16,
182 .size_of_raw_data = 16,
183 .pointer_to_raw_data = SECTION_TABLE_OFFSET + IMAGE_SECTION_HEADER_BYTES,
184 };
185 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, /* override_file_size= */ 4096);
186 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
187 _cleanup_free_ PeHeader *pe = NULL;
188 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
189
190 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
191 }
192
193 /* Boundary: PointerToRawData + SizeOfRawData == file_size is OK. */
194 TEST(pe_load_sections_section_exactly_fills_file) {
195 SectionSpec s = {
196 .name = ".initrd",
197 .virtual_size = 16,
198 .size_of_raw_data = 16,
199 .pointer_to_raw_data = SECTION_TABLE_OFFSET + IMAGE_SECTION_HEADER_BYTES,
200 };
201 size_t exact_size = (size_t) s.pointer_to_raw_data + s.size_of_raw_data;
202 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, exact_size);
203 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
204 _cleanup_free_ PeHeader *pe = NULL;
205 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
206
207 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
208 }
209
210 /* Section's raw data claims to extend one byte past EOF — must be rejected. */
211 TEST(pe_load_sections_section_past_eof_by_one) {
212 SectionSpec s = {
213 .name = ".initrd",
214 .virtual_size = 16,
215 .size_of_raw_data = 16,
216 .pointer_to_raw_data = SECTION_TABLE_OFFSET + IMAGE_SECTION_HEADER_BYTES,
217 };
218 size_t short_size = (size_t) s.pointer_to_raw_data + s.size_of_raw_data - 1;
219 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, short_size);
220 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
221 _cleanup_free_ PeHeader *pe = NULL;
222 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
223
224 ASSERT_ERROR(load_headers_and_sections(fd, &dos, &pe, &sections), EBADMSG);
225 }
226
227 /* PointerToRawData itself already past EOF, with non-zero SizeOfRawData. */
228 TEST(pe_load_sections_pointer_way_past_eof) {
229 SectionSpec s = {
230 .name = ".initrd",
231 .virtual_size = 16,
232 .size_of_raw_data = 16,
233 .pointer_to_raw_data = UINT32_C(0x10000000),
234 };
235 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, /* override_file_size= */ 4096);
236 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
237 _cleanup_free_ PeHeader *pe = NULL;
238 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
239
240 ASSERT_ERROR(load_headers_and_sections(fd, &dos, &pe, &sections), EBADMSG);
241 }
242
243 /* SizeOfRawData == 0 means "BSS-equivalent / uninitialised"; PointerToRawData
244 * is a don't-care and must NOT be bounds-checked, even when nonsense. */
245 TEST(pe_load_sections_size_zero_with_huge_pointer) {
246 SectionSpec s = {
247 .name = ".initrd",
248 .virtual_size = 0x100,
249 .size_of_raw_data = 0,
250 .pointer_to_raw_data = UINT32_MAX,
251 };
252 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, /* override_file_size= */ 0);
253 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
254 _cleanup_free_ PeHeader *pe = NULL;
255 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
256
257 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
258 }
259
260 /* SizeOfRawData == 0 with PointerToRawData == 0 — most natural BSS case. */
261 TEST(pe_load_sections_size_zero_pointer_zero) {
262 SectionSpec s = {
263 .name = ".initrd",
264 .virtual_size = 0x40,
265 .size_of_raw_data = 0,
266 .pointer_to_raw_data = 0,
267 };
268 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, /* override_file_size= */ 0);
269 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
270 _cleanup_free_ PeHeader *pe = NULL;
271 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
272
273 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
274 }
275
276 /* PointerToRawData + SizeOfRawData overflows uint32 (both near UINT32_MAX).
277 * The fix uses uint64 arithmetic + __builtin_add_overflow, so this must be
278 * rejected, not silently wrap. */
279 TEST(pe_load_sections_uint32_overflow_sum) {
280 SectionSpec s = {
281 .name = ".initrd",
282 .virtual_size = UINT32_C(0xffffffff),
283 .size_of_raw_data = UINT32_C(0x80000000),
284 .pointer_to_raw_data = UINT32_C(0x80000000),
285 };
286 /* uint32 sum wraps to 0, but uint64 sum = 0x100000000 — must still
287 * be rejected against a tiny file. */
288 _cleanup_close_ int fd = build_pe_file(&s, /* n_sections= */ 1, /* override_file_size= */ 4096);
289 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
290 _cleanup_free_ PeHeader *pe = NULL;
291 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
292
293 ASSERT_ERROR(load_headers_and_sections(fd, &dos, &pe, &sections), EBADMSG);
294 }
295
296 /* Zero sections: bounds check loop runs zero times, fstat still succeeds. */
297 TEST(pe_load_sections_zero_sections) {
298 _cleanup_close_ int fd = build_pe_file(NULL, /* n_sections= */ 0, /* override_file_size= */ 0);
299 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
300 _cleanup_free_ PeHeader *pe = NULL;
301 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
302
303 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
304 }
305
306 /* Multiple sections, all valid — happy path with N>1. */
307 TEST(pe_load_sections_multi_all_valid) {
308 size_t base = SECTION_TABLE_OFFSET + 3 * IMAGE_SECTION_HEADER_BYTES;
309 SectionSpec specs[3] = {
310 { .name = ".linux", .virtual_size = 16, .size_of_raw_data = 16,
311 .pointer_to_raw_data = (uint32_t) base },
312 { .name = ".initrd", .virtual_size = 8, .size_of_raw_data = 8,
313 .pointer_to_raw_data = (uint32_t) (base + 16) },
314 { .name = ".cmdline", .virtual_size = 0, .size_of_raw_data = 0,
315 .pointer_to_raw_data = 0 },
316 };
317 _cleanup_close_ int fd = build_pe_file(specs, /* n_sections= */ 3, /* override_file_size= */ 0);
318 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
319 _cleanup_free_ PeHeader *pe = NULL;
320 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
321
322 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
323 }
324
325 /* Multiple sections; one of them claims data past EOF — the whole file
326 * must be rejected, not just that section silently skipped. */
327 TEST(pe_load_sections_multi_one_past_eof) {
328 size_t base = SECTION_TABLE_OFFSET + 3 * IMAGE_SECTION_HEADER_BYTES;
329 SectionSpec specs[3] = {
330 { .name = ".linux", .virtual_size = 16, .size_of_raw_data = 16,
331 .pointer_to_raw_data = (uint32_t) base },
332 { .name = ".initrd", .virtual_size = 8, .size_of_raw_data = 8,
333 .pointer_to_raw_data = (uint32_t) (base + 16) },
334 { .name = ".cmdline", .virtual_size = 32, .size_of_raw_data = 32,
335 .pointer_to_raw_data = UINT32_C(0x40000000) }, /* nonsense */
336 };
337 _cleanup_close_ int fd = build_pe_file(specs, /* n_sections= */ 3, /* override_file_size= */ 4096);
338 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
339 _cleanup_free_ PeHeader *pe = NULL;
340 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
341
342 ASSERT_ERROR(load_headers_and_sections(fd, &dos, &pe, &sections), EBADMSG);
343 }
344
345 /* ======================================================================
346 * uki_hash — zero-padding cap from PLAN §3.2
347 * ====================================================================== */
348
349 #if HAVE_OPENSSL
350
351 /* Helper: call uki_hash on a freshly built single-section PE. Returns the
352 * uki_hash() return value; any allocated hashes are freed. */
353 static int call_uki_hash_with(SectionSpec spec) {
354 _cleanup_close_ int fd = build_pe_file(&spec, /* n_sections= */ 1, /* override_file_size= */ 0);
355 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
356 _cleanup_free_ PeHeader *pe = NULL;
357 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
358
359 int r = load_headers_and_sections(fd, &dos, &pe, &sections);
360 if (r < 0)
361 return r;
362
363 void *hashes[_UNIFIED_SECTION_MAX] = {};
364 size_t hash_size = 0;
365 r = uki_hash(fd, sym_EVP_sha256(), hashes, &hash_size);
366 free_many(hashes, _UNIFIED_SECTION_MAX);
367 return r;
368 }
369
370 /* No padding needed (VirtualSize == SizeOfRawData) — the cap branch is
371 * skipped entirely; hash succeeds. */
372 TEST_RET(uki_hash_no_padding) {
373 if (dlopen_libcrypto(LOG_DEBUG) < 0)
374 return log_tests_skipped("openssl not available");
375
376 SectionSpec s = {
377 .name = ".initrd",
378 .virtual_size = 16,
379 .size_of_raw_data = 16,
380 .pointer_to_raw_data = SECTION_TABLE_OFFSET + IMAGE_SECTION_HEADER_BYTES,
381 };
382 ASSERT_OK(call_uki_hash_with(s));
383 return EXIT_SUCCESS;
384 }
385
386 /* One-byte padding — well below the cap. Must succeed. */
387 TEST_RET(uki_hash_one_byte_padding) {
388 if (dlopen_libcrypto(LOG_DEBUG) < 0)
389 return log_tests_skipped("openssl not available");
390
391 SectionSpec s = {
392 .name = ".initrd",
393 .virtual_size = 17,
394 .size_of_raw_data = 16,
395 .pointer_to_raw_data = SECTION_TABLE_OFFSET + IMAGE_SECTION_HEADER_BYTES,
396 };
397 ASSERT_OK(call_uki_hash_with(s));
398 return EXIT_SUCCESS;
399 }
400
401 /* Boundary: VirtualSize - SizeOfRawData == cap (64 MiB) — must succeed
402 * (PLAN §4 says the comparison is strict ">"). SHA-256 of 64 MiB of zeros
403 * is ~100–200 ms on a modern CPU but can be much slower on emulated arches,
404 * so gate behind the slow-tests opt-in. */
405 TEST_RET(uki_hash_at_cap_boundary) {
406 if (!slow_tests_enabled())
407 return log_tests_skipped("slow tests disabled");
408 if (dlopen_libcrypto(LOG_DEBUG) < 0)
409 return log_tests_skipped("openssl not available");
410
411 SectionSpec s = {
412 .name = ".initrd",
413 .virtual_size = UKI_HASH_PADDING_CAP_BYTES,
414 .size_of_raw_data = 0,
415 .pointer_to_raw_data = 0,
416 };
417 ASSERT_OK(call_uki_hash_with(s));
418 return EXIT_SUCCESS;
419 }
420
421 /* One byte over the cap — must be rejected with -EBADMSG. */
422 TEST_RET(uki_hash_one_over_cap_rejected) {
423 if (dlopen_libcrypto(LOG_DEBUG) < 0)
424 return log_tests_skipped("openssl not available");
425
426 SectionSpec s = {
427 .name = ".initrd",
428 .virtual_size = UKI_HASH_PADDING_CAP_BYTES + 1,
429 .size_of_raw_data = 0,
430 .pointer_to_raw_data = 0,
431 };
432 ASSERT_ERROR(call_uki_hash_with(s), EBADMSG);
433 return EXIT_SUCCESS;
434 }
435
436 /* VirtualSize == UINT32_MAX, SizeOfRawData == 0. This is the worst-case
437 * shape an attacker can produce: ~4 GiB of zero-padding. Without the cap
438 * this is the >10 s wedge from #42344; with the cap it must return
439 * -EBADMSG essentially instantly. */
440 TEST_RET(uki_hash_max_virtual_size_rejected) {
441 if (dlopen_libcrypto(LOG_DEBUG) < 0)
442 return log_tests_skipped("openssl not available");
443
444 SectionSpec s = {
445 .name = ".initrd",
446 .virtual_size = UINT32_MAX,
447 .size_of_raw_data = 0,
448 .pointer_to_raw_data = 0,
449 };
450 ASSERT_ERROR(call_uki_hash_with(s), EBADMSG);
451 return EXIT_SUCCESS;
452 }
453
454 /* Reproduces the exact section shape of the canonical 382-byte slow-unit
455 * (.initrd VS=0xff000000 RSD=0). Pre-fix this drove ~8.5 s of SHA-256; the
456 * cap must reject it instantly. This is the in-code analogue of the
457 * regression input pinned in test/fuzz/fuzz-pe-binary/. */
458 TEST_RET(uki_hash_canonical_42344_reproducer_pattern) {
459 if (dlopen_libcrypto(LOG_DEBUG) < 0)
460 return log_tests_skipped("openssl not available");
461
462 SectionSpec s = {
463 .name = ".initrd",
464 .virtual_size = UINT32_C(0xff000000),
465 .size_of_raw_data = 0,
466 .pointer_to_raw_data = 0,
467 };
468 ASSERT_ERROR(call_uki_hash_with(s), EBADMSG);
469 return EXIT_SUCCESS;
470 }
471
472 /* Multiple sections, one of which trips the cap — uki_hash() must abort
473 * the entire call (return -EBADMSG) rather than silently skipping. */
474 TEST_RET(uki_hash_one_bad_among_many_rejected) {
475 if (dlopen_libcrypto(LOG_DEBUG) < 0)
476 return log_tests_skipped("openssl not available");
477
478 size_t base = SECTION_TABLE_OFFSET + 2 * IMAGE_SECTION_HEADER_BYTES;
479 SectionSpec specs[2] = {
480 { .name = ".linux", .virtual_size = 16, .size_of_raw_data = 16,
481 .pointer_to_raw_data = (uint32_t) base },
482 { .name = ".initrd", .virtual_size = UKI_HASH_PADDING_CAP_BYTES + 1,
483 .size_of_raw_data = 0, .pointer_to_raw_data = 0 },
484 };
485 _cleanup_close_ int fd = build_pe_file(specs, /* n_sections= */ 2, /* override_file_size= */ 0);
486 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
487 _cleanup_free_ PeHeader *pe = NULL;
488 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
489
490 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
491
492 void *hashes[_UNIFIED_SECTION_MAX] = {};
493 size_t hash_size = 0;
494 ASSERT_ERROR(uki_hash(fd, sym_EVP_sha256(), hashes, &hash_size), EBADMSG);
495 free_many(hashes, _UNIFIED_SECTION_MAX);
496 return EXIT_SUCCESS;
497 }
498
499 /* Sanity check: uki_hash on a "no UKI sections present" PE doesn't hit
500 * the cap path at all (no section with VS>SRD that matches the unified
501 * sections table). Just confirms the cap doesn't reject the empty case. */
502 TEST_RET(uki_hash_empty_pe_does_not_reject) {
503 if (dlopen_libcrypto(LOG_DEBUG) < 0)
504 return log_tests_skipped("openssl not available");
505
506 _cleanup_close_ int fd = build_pe_file(NULL, /* n_sections= */ 0, /* override_file_size= */ 0);
507 _cleanup_free_ IMAGE_DOS_HEADER *dos = NULL;
508 _cleanup_free_ PeHeader *pe = NULL;
509 _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
510
511 ASSERT_OK(load_headers_and_sections(fd, &dos, &pe, &sections));
512
513 void *hashes[_UNIFIED_SECTION_MAX] = {};
514 size_t hash_size = 0;
515 /* No UKI sections to hash — succeeds without hitting the cap path. */
516 ASSERT_OK(uki_hash(fd, sym_EVP_sha256(), hashes, &hash_size));
517 free_many(hashes, _UNIFIED_SECTION_MAX);
518 return EXIT_SUCCESS;
519 }
520
521 #endif /* HAVE_OPENSSL */
522
523 DEFINE_TEST_MAIN(LOG_DEBUG);