hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/crypto/asymmetric_keys/verify_pefile.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /* Parse a signed PE binary
23 *
34 * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
45 * Written by David Howells (dhowells@redhat.com)
5
- *
6
- * This program is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU General Public Licence
8
- * as published by the Free Software Foundation; either version
9
- * 2 of the Licence, or (at your option) any later version.
106 */
117
128 #define pr_fmt(fmt) "PEFILE: "fmt
....@@ -78,7 +74,7 @@
7874 break;
7975
8076 default:
81
- pr_debug("Unknown PEOPT magic = %04hx\n", pe32->magic);
77
+ pr_warn("Unknown PEOPT magic = %04hx\n", pe32->magic);
8278 return -ELIBBAD;
8379 }
8480
....@@ -99,8 +95,8 @@
9995 ctx->certs_size = ddir->certs.size;
10096
10197 if (!ddir->certs.virtual_address || !ddir->certs.size) {
102
- pr_debug("Unsigned PE binary\n");
103
- return -EKEYREJECTED;
98
+ pr_warn("Unsigned PE binary\n");
99
+ return -ENODATA;
104100 }
105101
106102 chkaddr(ctx->header_size, ddir->certs.virtual_address,
....@@ -131,7 +127,7 @@
131127 unsigned len;
132128
133129 if (ctx->sig_len < sizeof(wrapper)) {
134
- pr_debug("Signature wrapper too short\n");
130
+ pr_warn("Signature wrapper too short\n");
135131 return -ELIBBAD;
136132 }
137133
....@@ -139,19 +135,23 @@
139135 pr_debug("sig wrapper = { %x, %x, %x }\n",
140136 wrapper.length, wrapper.revision, wrapper.cert_type);
141137
142
- /* Both pesign and sbsign round up the length of certificate table
143
- * (in optional header data directories) to 8 byte alignment.
138
+ /* sbsign rounds up the length of certificate table (in optional
139
+ * header data directories) to 8 byte alignment. However, the PE
140
+ * specification states that while entries are 8-byte aligned, this is
141
+ * not included in their length, and as a result, pesign has not
142
+ * rounded up since 0.110.
144143 */
145
- if (round_up(wrapper.length, 8) != ctx->sig_len) {
146
- pr_debug("Signature wrapper len wrong\n");
144
+ if (wrapper.length > ctx->sig_len) {
145
+ pr_warn("Signature wrapper bigger than sig len (%x > %x)\n",
146
+ ctx->sig_len, wrapper.length);
147147 return -ELIBBAD;
148148 }
149149 if (wrapper.revision != WIN_CERT_REVISION_2_0) {
150
- pr_debug("Signature is not revision 2.0\n");
150
+ pr_warn("Signature is not revision 2.0\n");
151151 return -ENOTSUPP;
152152 }
153153 if (wrapper.cert_type != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
154
- pr_debug("Signature certificate type is not PKCS\n");
154
+ pr_warn("Signature certificate type is not PKCS\n");
155155 return -ENOTSUPP;
156156 }
157157
....@@ -164,7 +164,7 @@
164164 ctx->sig_offset += sizeof(wrapper);
165165 ctx->sig_len -= sizeof(wrapper);
166166 if (ctx->sig_len < 4) {
167
- pr_debug("Signature data missing\n");
167
+ pr_warn("Signature data missing\n");
168168 return -EKEYREJECTED;
169169 }
170170
....@@ -198,7 +198,7 @@
198198 return 0;
199199 }
200200 not_pkcs7:
201
- pr_debug("Signature data not PKCS#7\n");
201
+ pr_warn("Signature data not PKCS#7\n");
202202 return -ELIBBAD;
203203 }
204204
....@@ -341,8 +341,8 @@
341341 digest_size = crypto_shash_digestsize(tfm);
342342
343343 if (digest_size != ctx->digest_len) {
344
- pr_debug("Digest size mismatch (%zx != %x)\n",
345
- digest_size, ctx->digest_len);
344
+ pr_warn("Digest size mismatch (%zx != %x)\n",
345
+ digest_size, ctx->digest_len);
346346 ret = -EBADMSG;
347347 goto error_no_desc;
348348 }
....@@ -354,7 +354,6 @@
354354 goto error_no_desc;
355355
356356 desc->tfm = tfm;
357
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
358357 ret = crypto_shash_init(desc);
359358 if (ret < 0)
360359 goto error;
....@@ -374,14 +373,14 @@
374373 * PKCS#7 certificate.
375374 */
376375 if (memcmp(digest, ctx->digest, ctx->digest_len) != 0) {
377
- pr_debug("Digest mismatch\n");
376
+ pr_warn("Digest mismatch\n");
378377 ret = -EKEYREJECTED;
379378 } else {
380379 pr_debug("The digests match!\n");
381380 }
382381
383382 error:
384
- kzfree(desc);
383
+ kfree_sensitive(desc);
385384 error_no_desc:
386385 crypto_free_shash(tfm);
387386 kleave(" = %d", ret);
....@@ -407,6 +406,8 @@
407406 *
408407 * (*) 0 if at least one signature chain intersects with the keys in the trust
409408 * keyring, or:
409
+ *
410
+ * (*) -ENODATA if there is no signature present.
410411 *
411412 * (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
412413 * chain.
....@@ -450,6 +451,6 @@
450451 ret = pefile_digest_pe(pebuf, pelen, &ctx);
451452
452453 error:
453
- kzfree(ctx.digest);
454
+ kfree_sensitive(ctx.digest);
454455 return ret;
455456 }