verify_pefile.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Parse a signed PE binary
  3. *
  4. * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #define pr_fmt(fmt) "PEFILE: "fmt
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/pe.h>
  13. #include <linux/asn1.h>
  14. #include <linux/verification.h>
  15. #include <crypto/hash.h>
  16. #include "verify_pefile.h"
  17. /*
  18. * Parse a PE binary.
  19. */
  20. static int pefile_parse_binary(const void *pebuf, unsigned int pelen,
  21. struct pefile_context *ctx)
  22. {
  23. const struct mz_hdr *mz = pebuf;
  24. const struct pe_hdr *pe;
  25. const struct pe32_opt_hdr *pe32;
  26. const struct pe32plus_opt_hdr *pe64;
  27. const struct data_directory *ddir;
  28. const struct data_dirent *dde;
  29. const struct section_header *secs, *sec;
  30. size_t cursor, datalen = pelen;
  31. kenter("");
  32. #define chkaddr(base, x, s) \
  33. do { \
  34. if ((x) < base || (s) >= datalen || (x) > datalen - (s)) \
  35. return -ELIBBAD; \
  36. } while (0)
  37. chkaddr(0, 0, sizeof(*mz));
  38. if (mz->magic != MZ_MAGIC)
  39. return -ELIBBAD;
  40. cursor = sizeof(*mz);
  41. chkaddr(cursor, mz->peaddr, sizeof(*pe));
  42. pe = pebuf + mz->peaddr;
  43. if (pe->magic != PE_MAGIC)
  44. return -ELIBBAD;
  45. cursor = mz->peaddr + sizeof(*pe);
  46. chkaddr(0, cursor, sizeof(pe32->magic));
  47. pe32 = pebuf + cursor;
  48. pe64 = pebuf + cursor;
  49. switch (pe32->magic) {
  50. case PE_OPT_MAGIC_PE32:
  51. chkaddr(0, cursor, sizeof(*pe32));
  52. ctx->image_checksum_offset =
  53. (unsigned long)&pe32->csum - (unsigned long)pebuf;
  54. ctx->header_size = pe32->header_size;
  55. cursor += sizeof(*pe32);
  56. ctx->n_data_dirents = pe32->data_dirs;
  57. break;
  58. case PE_OPT_MAGIC_PE32PLUS:
  59. chkaddr(0, cursor, sizeof(*pe64));
  60. ctx->image_checksum_offset =
  61. (unsigned long)&pe64->csum - (unsigned long)pebuf;
  62. ctx->header_size = pe64->header_size;
  63. cursor += sizeof(*pe64);
  64. ctx->n_data_dirents = pe64->data_dirs;
  65. break;
  66. default:
  67. pr_warn("Unknown PEOPT magic = %04hx\n", pe32->magic);
  68. return -ELIBBAD;
  69. }
  70. pr_debug("checksum @ %x\n", ctx->image_checksum_offset);
  71. pr_debug("header size = %x\n", ctx->header_size);
  72. if (cursor >= ctx->header_size || ctx->header_size >= datalen)
  73. return -ELIBBAD;
  74. if (ctx->n_data_dirents > (ctx->header_size - cursor) / sizeof(*dde))
  75. return -ELIBBAD;
  76. ddir = pebuf + cursor;
  77. cursor += sizeof(*dde) * ctx->n_data_dirents;
  78. ctx->cert_dirent_offset =
  79. (unsigned long)&ddir->certs - (unsigned long)pebuf;
  80. ctx->certs_size = ddir->certs.size;
  81. if (!ddir->certs.virtual_address || !ddir->certs.size) {
  82. pr_warn("Unsigned PE binary\n");
  83. return -ENODATA;
  84. }
  85. chkaddr(ctx->header_size, ddir->certs.virtual_address,
  86. ddir->certs.size);
  87. ctx->sig_offset = ddir->certs.virtual_address;
  88. ctx->sig_len = ddir->certs.size;
  89. pr_debug("cert = %x @%x [%*ph]\n",
  90. ctx->sig_len, ctx->sig_offset,
  91. ctx->sig_len, pebuf + ctx->sig_offset);
  92. ctx->n_sections = pe->sections;
  93. if (ctx->n_sections > (ctx->header_size - cursor) / sizeof(*sec))
  94. return -ELIBBAD;
  95. ctx->secs = secs = pebuf + cursor;
  96. return 0;
  97. }
  98. /*
  99. * Check and strip the PE wrapper from around the signature and check that the
  100. * remnant looks something like PKCS#7.
  101. */
  102. static int pefile_strip_sig_wrapper(const void *pebuf,
  103. struct pefile_context *ctx)
  104. {
  105. struct win_certificate wrapper;
  106. const u8 *pkcs7;
  107. unsigned len;
  108. if (ctx->sig_len < sizeof(wrapper)) {
  109. pr_warn("Signature wrapper too short\n");
  110. return -ELIBBAD;
  111. }
  112. memcpy(&wrapper, pebuf + ctx->sig_offset, sizeof(wrapper));
  113. pr_debug("sig wrapper = { %x, %x, %x }\n",
  114. wrapper.length, wrapper.revision, wrapper.cert_type);
  115. /* sbsign rounds up the length of certificate table (in optional
  116. * header data directories) to 8 byte alignment. However, the PE
  117. * specification states that while entries are 8-byte aligned, this is
  118. * not included in their length, and as a result, pesign has not
  119. * rounded up since 0.110.
  120. */
  121. if (wrapper.length > ctx->sig_len) {
  122. pr_warn("Signature wrapper bigger than sig len (%x > %x)\n",
  123. ctx->sig_len, wrapper.length);
  124. return -ELIBBAD;
  125. }
  126. if (wrapper.revision != WIN_CERT_REVISION_2_0) {
  127. pr_warn("Signature is not revision 2.0\n");
  128. return -ENOTSUPP;
  129. }
  130. if (wrapper.cert_type != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
  131. pr_warn("Signature certificate type is not PKCS\n");
  132. return -ENOTSUPP;
  133. }
  134. /* It looks like the pkcs signature length in wrapper->length and the
  135. * size obtained from the data dir entries, which lists the total size
  136. * of certificate table, are both aligned to an octaword boundary, so
  137. * we may have to deal with some padding.
  138. */
  139. ctx->sig_len = wrapper.length;
  140. ctx->sig_offset += sizeof(wrapper);
  141. ctx->sig_len -= sizeof(wrapper);
  142. if (ctx->sig_len < 4) {
  143. pr_warn("Signature data missing\n");
  144. return -EKEYREJECTED;
  145. }
  146. /* What's left should be a PKCS#7 cert */
  147. pkcs7 = pebuf + ctx->sig_offset;
  148. if (pkcs7[0] != (ASN1_CONS_BIT | ASN1_SEQ))
  149. goto not_pkcs7;
  150. switch (pkcs7[1]) {
  151. case 0 ... 0x7f:
  152. len = pkcs7[1] + 2;
  153. goto check_len;
  154. case ASN1_INDEFINITE_LENGTH:
  155. return 0;
  156. case 0x81:
  157. len = pkcs7[2] + 3;
  158. goto check_len;
  159. case 0x82:
  160. len = ((pkcs7[2] << 8) | pkcs7[3]) + 4;
  161. goto check_len;
  162. case 0x83 ... 0xff:
  163. return -EMSGSIZE;
  164. default:
  165. goto not_pkcs7;
  166. }
  167. check_len:
  168. if (len <= ctx->sig_len) {
  169. /* There may be padding */
  170. ctx->sig_len = len;
  171. return 0;
  172. }
  173. not_pkcs7:
  174. pr_warn("Signature data not PKCS#7\n");
  175. return -ELIBBAD;
  176. }
  177. /*
  178. * Compare two sections for canonicalisation.
  179. */
  180. static int pefile_compare_shdrs(const void *a, const void *b)
  181. {
  182. const struct section_header *shdra = a;
  183. const struct section_header *shdrb = b;
  184. int rc;
  185. if (shdra->data_addr > shdrb->data_addr)
  186. return 1;
  187. if (shdrb->data_addr > shdra->data_addr)
  188. return -1;
  189. if (shdra->virtual_address > shdrb->virtual_address)
  190. return 1;
  191. if (shdrb->virtual_address > shdra->virtual_address)
  192. return -1;
  193. rc = strcmp(shdra->name, shdrb->name);
  194. if (rc != 0)
  195. return rc;
  196. if (shdra->virtual_size > shdrb->virtual_size)
  197. return 1;
  198. if (shdrb->virtual_size > shdra->virtual_size)
  199. return -1;
  200. if (shdra->raw_data_size > shdrb->raw_data_size)
  201. return 1;
  202. if (shdrb->raw_data_size > shdra->raw_data_size)
  203. return -1;
  204. return 0;
  205. }
  206. /*
  207. * Load the contents of the PE binary into the digest, leaving out the image
  208. * checksum and the certificate data block.
  209. */
  210. static int pefile_digest_pe_contents(const void *pebuf, unsigned int pelen,
  211. struct pefile_context *ctx,
  212. struct shash_desc *desc)
  213. {
  214. unsigned *canon, tmp, loop, i, hashed_bytes;
  215. int ret;
  216. /* Digest the header and data directory, but leave out the image
  217. * checksum and the data dirent for the signature.
  218. */
  219. ret = crypto_shash_update(desc, pebuf, ctx->image_checksum_offset);
  220. if (ret < 0)
  221. return ret;
  222. tmp = ctx->image_checksum_offset + sizeof(uint32_t);
  223. ret = crypto_shash_update(desc, pebuf + tmp,
  224. ctx->cert_dirent_offset - tmp);
  225. if (ret < 0)
  226. return ret;
  227. tmp = ctx->cert_dirent_offset + sizeof(struct data_dirent);
  228. ret = crypto_shash_update(desc, pebuf + tmp, ctx->header_size - tmp);
  229. if (ret < 0)
  230. return ret;
  231. canon = kcalloc(ctx->n_sections, sizeof(unsigned), GFP_KERNEL);
  232. if (!canon)
  233. return -ENOMEM;
  234. /* We have to canonicalise the section table, so we perform an
  235. * insertion sort.
  236. */
  237. canon[0] = 0;
  238. for (loop = 1; loop < ctx->n_sections; loop++) {
  239. for (i = 0; i < loop; i++) {
  240. if (pefile_compare_shdrs(&ctx->secs[canon[i]],
  241. &ctx->secs[loop]) > 0) {
  242. memmove(&canon[i + 1], &canon[i],
  243. (loop - i) * sizeof(canon[0]));
  244. break;
  245. }
  246. }
  247. canon[i] = loop;
  248. }
  249. hashed_bytes = ctx->header_size;
  250. for (loop = 0; loop < ctx->n_sections; loop++) {
  251. i = canon[loop];
  252. if (ctx->secs[i].raw_data_size == 0)
  253. continue;
  254. ret = crypto_shash_update(desc,
  255. pebuf + ctx->secs[i].data_addr,
  256. ctx->secs[i].raw_data_size);
  257. if (ret < 0) {
  258. kfree(canon);
  259. return ret;
  260. }
  261. hashed_bytes += ctx->secs[i].raw_data_size;
  262. }
  263. kfree(canon);
  264. if (pelen > hashed_bytes) {
  265. tmp = hashed_bytes + ctx->certs_size;
  266. ret = crypto_shash_update(desc,
  267. pebuf + hashed_bytes,
  268. pelen - tmp);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. return 0;
  273. }
  274. /*
  275. * Digest the contents of the PE binary, leaving out the image checksum and the
  276. * certificate data block.
  277. */
  278. static int pefile_digest_pe(const void *pebuf, unsigned int pelen,
  279. struct pefile_context *ctx)
  280. {
  281. struct crypto_shash *tfm;
  282. struct shash_desc *desc;
  283. size_t digest_size, desc_size;
  284. void *digest;
  285. int ret;
  286. kenter(",%s", ctx->digest_algo);
  287. /* Allocate the hashing algorithm we're going to need and find out how
  288. * big the hash operational data will be.
  289. */
  290. tfm = crypto_alloc_shash(ctx->digest_algo, 0, 0);
  291. if (IS_ERR(tfm))
  292. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  293. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  294. digest_size = crypto_shash_digestsize(tfm);
  295. if (digest_size != ctx->digest_len) {
  296. pr_warn("Digest size mismatch (%zx != %x)\n",
  297. digest_size, ctx->digest_len);
  298. ret = -EBADMSG;
  299. goto error_no_desc;
  300. }
  301. pr_debug("Digest: desc=%zu size=%zu\n", desc_size, digest_size);
  302. ret = -ENOMEM;
  303. desc = kzalloc(desc_size + digest_size, GFP_KERNEL);
  304. if (!desc)
  305. goto error_no_desc;
  306. desc->tfm = tfm;
  307. ret = crypto_shash_init(desc);
  308. if (ret < 0)
  309. goto error;
  310. ret = pefile_digest_pe_contents(pebuf, pelen, ctx, desc);
  311. if (ret < 0)
  312. goto error;
  313. digest = (void *)desc + desc_size;
  314. ret = crypto_shash_final(desc, digest);
  315. if (ret < 0)
  316. goto error;
  317. pr_debug("Digest calc = [%*ph]\n", ctx->digest_len, digest);
  318. /* Check that the PE file digest matches that in the MSCODE part of the
  319. * PKCS#7 certificate.
  320. */
  321. if (memcmp(digest, ctx->digest, ctx->digest_len) != 0) {
  322. pr_warn("Digest mismatch\n");
  323. ret = -EKEYREJECTED;
  324. } else {
  325. pr_debug("The digests match!\n");
  326. }
  327. error:
  328. kfree_sensitive(desc);
  329. error_no_desc:
  330. crypto_free_shash(tfm);
  331. kleave(" = %d", ret);
  332. return ret;
  333. }
  334. /**
  335. * verify_pefile_signature - Verify the signature on a PE binary image
  336. * @pebuf: Buffer containing the PE binary image
  337. * @pelen: Length of the binary image
  338. * @trust_keys: Signing certificate(s) to use as starting points
  339. * @usage: The use to which the key is being put.
  340. *
  341. * Validate that the certificate chain inside the PKCS#7 message inside the PE
  342. * binary image intersects keys we already know and trust.
  343. *
  344. * Returns, in order of descending priority:
  345. *
  346. * (*) -ELIBBAD if the image cannot be parsed, or:
  347. *
  348. * (*) -EKEYREJECTED if a signature failed to match for which we have a valid
  349. * key, or:
  350. *
  351. * (*) 0 if at least one signature chain intersects with the keys in the trust
  352. * keyring, or:
  353. *
  354. * (*) -ENODATA if there is no signature present.
  355. *
  356. * (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
  357. * chain.
  358. *
  359. * (*) -ENOKEY if we couldn't find a match for any of the signature chains in
  360. * the message.
  361. *
  362. * May also return -ENOMEM.
  363. */
  364. int verify_pefile_signature(const void *pebuf, unsigned pelen,
  365. struct key *trusted_keys,
  366. enum key_being_used_for usage)
  367. {
  368. struct pefile_context ctx;
  369. int ret;
  370. kenter("");
  371. memset(&ctx, 0, sizeof(ctx));
  372. ret = pefile_parse_binary(pebuf, pelen, &ctx);
  373. if (ret < 0)
  374. return ret;
  375. ret = pefile_strip_sig_wrapper(pebuf, &ctx);
  376. if (ret < 0)
  377. return ret;
  378. ret = verify_pkcs7_signature(NULL, 0,
  379. pebuf + ctx.sig_offset, ctx.sig_len,
  380. trusted_keys, usage,
  381. mscode_parse, &ctx);
  382. if (ret < 0)
  383. goto error;
  384. pr_debug("Digest: %u [%*ph]\n",
  385. ctx.digest_len, ctx.digest_len, ctx.digest);
  386. /* Generate the digest and check against the PKCS7 certificate
  387. * contents.
  388. */
  389. ret = pefile_digest_pe(pebuf, pelen, &ctx);
  390. error:
  391. kfree_sensitive(ctx.digest);
  392. return ret;
  393. }