1 /*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 /* 32 * PE format reference: 33 * http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <assert.h> 40 #include <err.h> 41 #include <errno.h> 42 #include <stddef.h> 43 #include <stdio.h> 44 #include <stdint.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #include "uefisign.h" 50 51 #ifndef CTASSERT 52 #define CTASSERT(x) _CTASSERT(x, __LINE__) 53 #define _CTASSERT(x, y) __CTASSERT(x, y) 54 #define __CTASSERT(x, y) typedef char __assert_ ## y [(x) ? 1 : -1] 55 #endif 56 57 struct mz_header { 58 uint8_t mz_signature[2]; 59 uint8_t mz_dont_care[58]; 60 uint16_t mz_lfanew; 61 } __attribute__((packed)); 62 63 struct coff_header { 64 uint8_t coff_dont_care[2]; 65 uint16_t coff_number_of_sections; 66 uint8_t coff_dont_care_either[16]; 67 } __attribute__((packed)); 68 69 #define PE_SIGNATURE 0x00004550 70 71 struct pe_header { 72 uint32_t pe_signature; 73 struct coff_header pe_coff; 74 } __attribute__((packed)); 75 76 #define PE_OPTIONAL_MAGIC_32 0x010B 77 #define PE_OPTIONAL_MAGIC_32_PLUS 0x020B 78 79 #define PE_OPTIONAL_SUBSYSTEM_EFI_APPLICATION 10 80 #define PE_OPTIONAL_SUBSYSTEM_EFI_BOOT 11 81 #define PE_OPTIONAL_SUBSYSTEM_EFI_RUNTIME 12 82 83 struct pe_optional_header_32 { 84 uint16_t po_magic; 85 uint8_t po_dont_care[58]; 86 uint32_t po_size_of_headers; 87 uint32_t po_checksum; 88 uint16_t po_subsystem; 89 uint8_t po_dont_care_either[22]; 90 uint32_t po_number_of_rva_and_sizes; 91 } __attribute__((packed)); 92 93 CTASSERT(offsetof(struct pe_optional_header_32, po_size_of_headers) == 60); 94 CTASSERT(offsetof(struct pe_optional_header_32, po_checksum) == 64); 95 CTASSERT(offsetof(struct pe_optional_header_32, po_subsystem) == 68); 96 CTASSERT(offsetof(struct pe_optional_header_32, po_number_of_rva_and_sizes) == 92); 97 98 struct pe_optional_header_32_plus { 99 uint16_t po_magic; 100 uint8_t po_dont_care[58]; 101 uint32_t po_size_of_headers; 102 uint32_t po_checksum; 103 uint16_t po_subsystem; 104 uint8_t po_dont_care_either[38]; 105 uint32_t po_number_of_rva_and_sizes; 106 } __attribute__((packed)); 107 108 CTASSERT(offsetof(struct pe_optional_header_32_plus, po_size_of_headers) == 60); 109 CTASSERT(offsetof(struct pe_optional_header_32_plus, po_checksum) == 64); 110 CTASSERT(offsetof(struct pe_optional_header_32_plus, po_subsystem) == 68); 111 CTASSERT(offsetof(struct pe_optional_header_32_plus, po_number_of_rva_and_sizes) == 108); 112 113 #define PE_DIRECTORY_ENTRY_CERTIFICATE 4 114 115 struct pe_directory_entry { 116 uint32_t pde_rva; 117 uint32_t pde_size; 118 } __attribute__((packed)); 119 120 struct pe_section_header { 121 uint8_t psh_dont_care[16]; 122 uint32_t psh_size_of_raw_data; 123 uint32_t psh_pointer_to_raw_data; 124 uint8_t psh_dont_care_either[16]; 125 } __attribute__((packed)); 126 127 CTASSERT(offsetof(struct pe_section_header, psh_size_of_raw_data) == 16); 128 CTASSERT(offsetof(struct pe_section_header, psh_pointer_to_raw_data) == 20); 129 130 #define PE_CERTIFICATE_REVISION 0x0200 131 #define PE_CERTIFICATE_TYPE 0x0002 132 133 struct pe_certificate { 134 uint32_t pc_len; 135 uint16_t pc_revision; 136 uint16_t pc_type; 137 char pc_signature[0]; 138 } __attribute__((packed)); 139 140 void 141 range_check(const struct executable *x, off_t off, size_t len, 142 const char *name) 143 { 144 145 if (off < 0) { 146 errx(1, "%s starts at negative offset %jd", 147 name, (intmax_t)off); 148 } 149 if (off >= (off_t)x->x_len) { 150 errx(1, "%s starts at %jd, past the end of executable at %zd", 151 name, (intmax_t)off, x->x_len); 152 } 153 if (len >= x->x_len) { 154 errx(1, "%s size %zd is larger than the executable size %zd", 155 name, len, x->x_len); 156 } 157 if (off + len > x->x_len) { 158 errx(1, "%s extends to %jd, past the end of executable at %zd", 159 name, (intmax_t)(off + len), x->x_len); 160 } 161 } 162 163 size_t 164 signature_size(const struct executable *x) 165 { 166 const struct pe_directory_entry *pde; 167 168 range_check(x, x->x_certificate_entry_off, 169 x->x_certificate_entry_len, "Certificate Directory"); 170 171 pde = (struct pe_directory_entry *) 172 (x->x_buf + x->x_certificate_entry_off); 173 174 if (pde->pde_rva != 0 && pde->pde_size == 0) 175 warnx("signature size is 0, but its RVA is %d", pde->pde_rva); 176 if (pde->pde_rva == 0 && pde->pde_size != 0) 177 warnx("signature RVA is 0, but its size is %d", pde->pde_size); 178 179 return (pde->pde_size); 180 } 181 182 void 183 show_certificate(const struct executable *x) 184 { 185 struct pe_certificate *pc; 186 const struct pe_directory_entry *pde; 187 188 range_check(x, x->x_certificate_entry_off, 189 x->x_certificate_entry_len, "Certificate Directory"); 190 191 pde = (struct pe_directory_entry *) 192 (x->x_buf + x->x_certificate_entry_off); 193 194 if (signature_size(x) == 0) { 195 printf("file not signed\n"); 196 return; 197 } 198 199 #if 0 200 printf("certificate chunk at offset %zd, size %zd\n", 201 pde->pde_rva, pde->pde_size); 202 #endif 203 204 range_check(x, pde->pde_rva, pde->pde_size, "Certificate chunk"); 205 206 pc = (struct pe_certificate *)(x->x_buf + pde->pde_rva); 207 if (pc->pc_revision != PE_CERTIFICATE_REVISION) { 208 errx(1, "wrong certificate chunk revision, is %d, should be %d", 209 pc->pc_revision, PE_CERTIFICATE_REVISION); 210 } 211 if (pc->pc_type != PE_CERTIFICATE_TYPE) { 212 errx(1, "wrong certificate chunk type, is %d, should be %d", 213 pc->pc_type, PE_CERTIFICATE_TYPE); 214 } 215 printf("to dump PKCS7:\n " 216 "dd if='%s' bs=1 skip=%zd | openssl pkcs7 -inform DER -print\n", 217 x->x_path, pde->pde_rva + offsetof(struct pe_certificate, pc_signature)); 218 printf("to dump raw ASN.1:\n " 219 "openssl asn1parse -i -inform DER -offset %zd -in '%s'\n", 220 pde->pde_rva + offsetof(struct pe_certificate, pc_signature), x->x_path); 221 } 222 223 static void 224 parse_section_table(struct executable *x, off_t off, int number_of_sections) 225 { 226 const struct pe_section_header *psh; 227 int i; 228 229 range_check(x, off, sizeof(*psh) * number_of_sections, 230 "section table"); 231 232 if (x->x_headers_len <= off + sizeof(*psh) * number_of_sections) 233 errx(1, "section table outside of headers"); 234 235 psh = (const struct pe_section_header *)(x->x_buf + off); 236 237 if (number_of_sections >= MAX_SECTIONS) { 238 errx(1, "too many sections: got %d, should be %d", 239 number_of_sections, MAX_SECTIONS); 240 } 241 x->x_nsections = number_of_sections; 242 243 for (i = 0; i < number_of_sections; i++) { 244 if (psh->psh_pointer_to_raw_data < x->x_headers_len) 245 errx(1, "section points inside the headers"); 246 247 range_check(x, psh->psh_pointer_to_raw_data, 248 psh->psh_size_of_raw_data, "section"); 249 #if 0 250 printf("section %d: start %d, size %d\n", 251 i, psh->psh_pointer_to_raw_data, psh->psh_size_of_raw_data); 252 #endif 253 x->x_section_off[i] = psh->psh_pointer_to_raw_data; 254 x->x_section_len[i] = psh->psh_size_of_raw_data; 255 psh++; 256 } 257 } 258 259 static void 260 parse_directory(struct executable *x, off_t off, 261 int number_of_rva_and_sizes, int number_of_sections) 262 { 263 //int i; 264 const struct pe_directory_entry *pde; 265 266 //printf("Data Directory at offset %zd\n", off); 267 268 if (number_of_rva_and_sizes <= PE_DIRECTORY_ENTRY_CERTIFICATE) { 269 errx(1, "wrong NumberOfRvaAndSizes %d; should be at least %d", 270 number_of_rva_and_sizes, PE_DIRECTORY_ENTRY_CERTIFICATE); 271 } 272 273 range_check(x, off, sizeof(*pde) * number_of_rva_and_sizes, 274 "PE Data Directory"); 275 if (x->x_headers_len <= off + sizeof(*pde) * number_of_rva_and_sizes) 276 errx(1, "PE Data Directory outside of headers"); 277 278 x->x_certificate_entry_off = 279 off + sizeof(*pde) * PE_DIRECTORY_ENTRY_CERTIFICATE; 280 x->x_certificate_entry_len = sizeof(*pde); 281 #if 0 282 printf("certificate directory entry at offset %zd, len %zd\n", 283 x->x_certificate_entry_off, x->x_certificate_entry_len); 284 285 pde = (struct pe_directory_entry *)(x->x_buf + off); 286 for (i = 0; i < number_of_rva_and_sizes; i++) { 287 printf("rva %zd, size %zd\n", pde->pde_rva, pde->pde_size); 288 pde++; 289 } 290 #endif 291 292 return (parse_section_table(x, 293 off + sizeof(*pde) * number_of_rva_and_sizes, number_of_sections)); 294 } 295 296 /* 297 * The PE checksum algorithm is undocumented; this code is mostly based on 298 * http://forum.sysinternals.com/optional-header-checksum-calculation_topic24214.html 299 * 300 * "Sum the entire image file, excluding the CheckSum field in the optional 301 * header, as an array of USHORTs, allowing any carry above 16 bits to be added 302 * back onto the low 16 bits. Then add the file size to get a 32-bit value." 303 * 304 * Note that most software does not care about the checksum at all; perhaps 305 * we could just set it to 0 instead. 306 * 307 * XXX: Endianess? 308 */ 309 static uint32_t 310 compute_checksum(const struct executable *x) 311 { 312 uint32_t cksum = 0; 313 uint16_t tmp; 314 int i; 315 316 range_check(x, x->x_checksum_off, x->x_checksum_len, "PE checksum"); 317 318 assert(x->x_checksum_off % 2 == 0); 319 320 for (i = 0; i + sizeof(tmp) < x->x_len; i += 2) { 321 /* 322 * Don't checksum the checksum. The +2 is because the checksum 323 * is 4 bytes, and here we're iterating over 2 byte chunks. 324 */ 325 if (i == x->x_checksum_off || i == x->x_checksum_off + 2) { 326 tmp = 0; 327 } else { 328 assert(i + sizeof(tmp) <= x->x_len); 329 memcpy(&tmp, x->x_buf + i, sizeof(tmp)); 330 } 331 332 cksum += tmp; 333 cksum += cksum >> 16; 334 cksum &= 0xffff; 335 } 336 337 cksum += cksum >> 16; 338 cksum &= 0xffff; 339 340 cksum += x->x_len; 341 342 return (cksum); 343 } 344 345 static void 346 parse_optional_32_plus(struct executable *x, off_t off, 347 int number_of_sections) 348 { 349 uint32_t computed_checksum; 350 const struct pe_optional_header_32_plus *po; 351 352 range_check(x, off, sizeof(*po), "PE Optional Header"); 353 354 po = (struct pe_optional_header_32_plus *)(x->x_buf + off); 355 switch (po->po_subsystem) { 356 case PE_OPTIONAL_SUBSYSTEM_EFI_APPLICATION: 357 case PE_OPTIONAL_SUBSYSTEM_EFI_BOOT: 358 case PE_OPTIONAL_SUBSYSTEM_EFI_RUNTIME: 359 break; 360 default: 361 errx(1, "wrong PE Optional Header subsystem 0x%x", 362 po->po_subsystem); 363 } 364 365 #if 0 366 printf("subsystem %d, checksum 0x%x, %d data directories\n", 367 po->po_subsystem, po->po_checksum, po->po_number_of_rva_and_sizes); 368 #endif 369 370 x->x_checksum_off = off + 371 offsetof(struct pe_optional_header_32_plus, po_checksum); 372 x->x_checksum_len = sizeof(po->po_checksum); 373 #if 0 374 printf("checksum 0x%x at offset %zd, len %zd\n", 375 po->po_checksum, x->x_checksum_off, x->x_checksum_len); 376 #endif 377 378 computed_checksum = compute_checksum(x); 379 if (computed_checksum != po->po_checksum) { 380 warnx("invalid PE+ checksum; is 0x%x, should be 0x%x", 381 po->po_checksum, computed_checksum); 382 } 383 384 if (x->x_len < x->x_headers_len) 385 errx(1, "invalid SizeOfHeaders %d", po->po_size_of_headers); 386 x->x_headers_len = po->po_size_of_headers; 387 //printf("Size of Headers: %d\n", po->po_size_of_headers); 388 389 return (parse_directory(x, off + sizeof(*po), 390 po->po_number_of_rva_and_sizes, number_of_sections)); 391 } 392 393 static void 394 parse_optional_32(struct executable *x, off_t off, int number_of_sections) 395 { 396 uint32_t computed_checksum; 397 const struct pe_optional_header_32 *po; 398 399 range_check(x, off, sizeof(*po), "PE Optional Header"); 400 401 po = (struct pe_optional_header_32 *)(x->x_buf + off); 402 switch (po->po_subsystem) { 403 case PE_OPTIONAL_SUBSYSTEM_EFI_APPLICATION: 404 case PE_OPTIONAL_SUBSYSTEM_EFI_BOOT: 405 case PE_OPTIONAL_SUBSYSTEM_EFI_RUNTIME: 406 break; 407 default: 408 errx(1, "wrong PE Optional Header subsystem 0x%x", 409 po->po_subsystem); 410 } 411 412 #if 0 413 printf("subsystem %d, checksum 0x%x, %d data directories\n", 414 po->po_subsystem, po->po_checksum, po->po_number_of_rva_and_sizes); 415 #endif 416 417 x->x_checksum_off = off + 418 offsetof(struct pe_optional_header_32, po_checksum); 419 x->x_checksum_len = sizeof(po->po_checksum); 420 #if 0 421 printf("checksum at offset %zd, len %zd\n", 422 x->x_checksum_off, x->x_checksum_len); 423 #endif 424 425 computed_checksum = compute_checksum(x); 426 if (computed_checksum != po->po_checksum) { 427 warnx("invalid PE checksum; is 0x%x, should be 0x%x", 428 po->po_checksum, computed_checksum); 429 } 430 431 if (x->x_len < x->x_headers_len) 432 errx(1, "invalid SizeOfHeaders %d", po->po_size_of_headers); 433 x->x_headers_len = po->po_size_of_headers; 434 //printf("Size of Headers: %d\n", po->po_size_of_headers); 435 436 return (parse_directory(x, off + sizeof(*po), 437 po->po_number_of_rva_and_sizes, number_of_sections)); 438 } 439 440 static void 441 parse_optional(struct executable *x, off_t off, int number_of_sections) 442 { 443 const struct pe_optional_header_32 *po; 444 445 //printf("Optional header offset %zd\n", off); 446 447 range_check(x, off, sizeof(*po), "PE Optional Header"); 448 449 po = (struct pe_optional_header_32 *)(x->x_buf + off); 450 451 switch (po->po_magic) { 452 case PE_OPTIONAL_MAGIC_32: 453 return (parse_optional_32(x, off, number_of_sections)); 454 case PE_OPTIONAL_MAGIC_32_PLUS: 455 return (parse_optional_32_plus(x, off, number_of_sections)); 456 default: 457 errx(1, "wrong PE Optional Header magic 0x%x", po->po_magic); 458 } 459 } 460 461 static void 462 parse_pe(struct executable *x, off_t off) 463 { 464 const struct pe_header *pe; 465 466 //printf("PE offset %zd, PE size %zd\n", off, sizeof(*pe)); 467 468 range_check(x, off, sizeof(*pe), "PE header"); 469 470 pe = (struct pe_header *)(x->x_buf + off); 471 if (pe->pe_signature != PE_SIGNATURE) 472 errx(1, "wrong PE signature 0x%x", pe->pe_signature); 473 474 //printf("Number of sections: %d\n", pe->pe_coff.coff_number_of_sections); 475 476 parse_optional(x, off + sizeof(*pe), 477 pe->pe_coff.coff_number_of_sections); 478 } 479 480 void 481 parse(struct executable *x) 482 { 483 const struct mz_header *mz; 484 485 range_check(x, 0, sizeof(*mz), "MZ header"); 486 487 mz = (struct mz_header *)x->x_buf; 488 if (mz->mz_signature[0] != 'M' || mz->mz_signature[1] != 'Z') 489 errx(1, "MZ header not found"); 490 491 return (parse_pe(x, mz->mz_lfanew)); 492 } 493 494 static off_t 495 append(struct executable *x, void *ptr, size_t len) 496 { 497 off_t off; 498 499 /* 500 * XXX: Alignment. 501 */ 502 off = x->x_len; 503 x->x_buf = realloc(x->x_buf, x->x_len + len); 504 if (x->x_buf == NULL) 505 err(1, "realloc"); 506 memcpy(x->x_buf + x->x_len, ptr, len); 507 x->x_len += len; 508 509 return (off); 510 } 511 512 void 513 update(struct executable *x) 514 { 515 uint32_t checksum; 516 struct pe_certificate *pc; 517 struct pe_directory_entry pde; 518 size_t pc_len; 519 off_t pc_off; 520 521 pc_len = sizeof(*pc) + x->x_signature_len; 522 pc = calloc(1, pc_len); 523 if (pc == NULL) 524 err(1, "calloc"); 525 526 #if 0 527 /* 528 * Note that pc_len is the length of pc_certificate, 529 * not the whole structure. 530 * 531 * XXX: That's what the spec says - but it breaks at least 532 * sbverify and "pesign -S", so the spec is probably wrong. 533 */ 534 pc->pc_len = x->x_signature_len; 535 #else 536 pc->pc_len = pc_len; 537 #endif 538 pc->pc_revision = PE_CERTIFICATE_REVISION; 539 pc->pc_type = PE_CERTIFICATE_TYPE; 540 memcpy(&pc->pc_signature, x->x_signature, x->x_signature_len); 541 542 pc_off = append(x, pc, pc_len); 543 #if 0 544 printf("added signature chunk at offset %zd, len %zd\n", 545 pc_off, pc_len); 546 #endif 547 548 free(pc); 549 550 pde.pde_rva = pc_off; 551 pde.pde_size = pc_len; 552 memcpy(x->x_buf + x->x_certificate_entry_off, &pde, sizeof(pde)); 553 554 checksum = compute_checksum(x); 555 assert(sizeof(checksum) == x->x_checksum_len); 556 memcpy(x->x_buf + x->x_checksum_off, &checksum, sizeof(checksum)); 557 #if 0 558 printf("new checksum 0x%x\n", checksum); 559 #endif 560 } 561