Skip to content

Commit 99db443

Browse files
committed
PKCS#7: Appropriately restrict authenticated attributes and content type
A PKCS#7 or CMS message can have per-signature authenticated attributes that are digested as a lump and signed by the authorising key for that signature. If such attributes exist, the content digest isn't itself signed, but rather it is included in a special authattr which then contributes to the signature. Further, we already require the master message content type to be pkcs7_signedData - but there's also a separate content type for the data itself within the SignedData object and this must be repeated inside the authattrs for each signer [RFC2315 9.2, RFC5652 11.1]. We should really validate the authattrs if they exist or forbid them entirely as appropriate. To this end: (1) Alter the PKCS#7 parser to reject any message that has more than one signature where at least one signature has authattrs and at least one that does not. (2) Validate authattrs if they are present and strongly restrict them. Only the following authattrs are permitted and all others are rejected: (a) contentType. This is checked to be an OID that matches the content type in the SignedData object. (b) messageDigest. This must match the crypto digest of the data. (c) signingTime. If present, we check that this is a valid, parseable UTCTime or GeneralTime and that the date it encodes fits within the validity window of the matching X.509 cert. (d) S/MIME capabilities. We don't check the contents. (e) Authenticode SP Opus Info. We don't check the contents. (f) Authenticode Statement Type. We don't check the contents. The message is rejected if (a) or (b) are missing. If the message is an Authenticode type, the message is rejected if (e) is missing; if not Authenticode, the message is rejected if (d) - (f) are present. The S/MIME capabilities authattr (d) unfortunately has to be allowed to support kernels already signed by the pesign program. This only affects kexec. sign-file suppresses them (CMS_NOSMIMECAP). The message is also rejected if an authattr is given more than once or if it contains more than one element in its set of values. (3) Add a parameter to pkcs7_verify() to select one of the following restrictions and pass in the appropriate option from the callers: (*) VERIFYING_MODULE_SIGNATURE This requires that the SignedData content type be pkcs7-data and forbids authattrs. sign-file sets CMS_NOATTR. We could be more flexible and permit authattrs optionally, but only permit minimal content. (*) VERIFYING_FIRMWARE_SIGNATURE This requires that the SignedData content type be pkcs7-data and requires authattrs. In future, this will require an attribute holding the target firmware name in addition to the minimal set. (*) VERIFYING_UNSPECIFIED_SIGNATURE This requires that the SignedData content type be pkcs7-data but allows either no authattrs or only permits the minimal set. (*) VERIFYING_KEXEC_PE_SIGNATURE This only supports the Authenticode SPC_INDIRECT_DATA content type and requires at least an SpcSpOpusInfo authattr in addition to the minimal set. It also permits an SPC_STATEMENT_TYPE authattr (and an S/MIME capabilities authattr because the pesign program doesn't remove these). (*) VERIFYING_KEY_SIGNATURE (*) VERIFYING_KEY_SELF_SIGNATURE These are invalid in this context but are included for later use when limiting the use of X.509 certs. (4) The pkcs7_test key type is given a module parameter to select between the above options for testing purposes. For example: echo 1 >/sys/module/pkcs7_test_key/parameters/usage keyctl padd pkcs7_test foo @s </tmp/stuff.pkcs7 will attempt to check the signature on stuff.pkcs7 as if it contains a firmware blob (1 being VERIFYING_FIRMWARE_SIGNATURE). Suggested-by: Andy Lutomirski <luto@kernel.org> Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Marcel Holtmann <marcel@holtmann.org> Reviewed-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent f29299b commit 99db443

16 files changed

Lines changed: 285 additions & 27 deletions

File tree

‎arch/x86/kernel/kexec-bzimage64.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
536536
int ret;
537537

538538
ret = verify_pefile_signature(kernel, kernel_len,
539-
system_trusted_keyring, &trusted);
539+
system_trusted_keyring,
540+
VERIFYING_KEXEC_PE_SIGNATURE,
541+
&trusted);
540542
if (ret < 0)
541543
return ret;
542544
if (!trusted)

‎crypto/asymmetric_keys/asymmetric_type.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
#include <keys/asymmetric-subtype.h>
1414
#include <keys/asymmetric-parser.h>
15+
#include <crypto/public_key.h>
1516
#include <linux/seq_file.h>
1617
#include <linux/module.h>
1718
#include <linux/slab.h>
@@ -20,6 +21,16 @@
2021

2122
MODULE_LICENSE("GPL");
2223

24+
const char *const key_being_used_for[NR__KEY_BEING_USED_FOR] = {
25+
[VERIFYING_MODULE_SIGNATURE] = "mod sig",
26+
[VERIFYING_FIRMWARE_SIGNATURE] = "firmware sig",
27+
[VERIFYING_KEXEC_PE_SIGNATURE] = "kexec PE sig",
28+
[VERIFYING_KEY_SIGNATURE] = "key sig",
29+
[VERIFYING_KEY_SELF_SIGNATURE] = "key self sig",
30+
[VERIFYING_UNSPECIFIED_SIGNATURE] = "unspec sig",
31+
};
32+
EXPORT_SYMBOL_GPL(key_being_used_for);
33+
2334
static LIST_HEAD(asymmetric_key_parsers);
2435
static DECLARE_RWSEM(asymmetric_key_parsers_sem);
2536

‎crypto/asymmetric_keys/pkcs7.asn1‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ContentType ::= OBJECT IDENTIFIER ({ pkcs7_note_OID })
88
SignedData ::= SEQUENCE {
99
version INTEGER ({ pkcs7_note_signeddata_version }),
1010
digestAlgorithms DigestAlgorithmIdentifiers,
11-
contentInfo ContentInfo,
11+
contentInfo ContentInfo ({ pkcs7_note_content }),
1212
certificates CHOICE {
1313
certSet [0] IMPLICIT ExtendedCertificatesAndCertificates,
1414
certSequence [2] IMPLICIT Certificates
@@ -21,7 +21,7 @@ SignedData ::= SEQUENCE {
2121
}
2222

2323
ContentInfo ::= SEQUENCE {
24-
contentType ContentType,
24+
contentType ContentType ({ pkcs7_note_OID }),
2525
content [0] EXPLICIT Data OPTIONAL
2626
}
2727

@@ -111,7 +111,7 @@ AuthenticatedAttribute ::= SEQUENCE {
111111
}
112112

113113
UnauthenticatedAttribute ::= SEQUENCE {
114-
type OBJECT IDENTIFIER ({ pkcs7_note_OID }),
114+
type OBJECT IDENTIFIER,
115115
values SET OF ANY
116116
}
117117

‎crypto/asymmetric_keys/pkcs7_key_type.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@
1414
#include <linux/err.h>
1515
#include <linux/module.h>
1616
#include <linux/key-type.h>
17+
#include <keys/asymmetric-type.h>
1718
#include <crypto/pkcs7.h>
1819
#include <keys/user-type.h>
1920
#include <keys/system_keyring.h>
2021
#include "pkcs7_parser.h"
2122

23+
static unsigned pkcs7_usage;
24+
module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
25+
MODULE_PARM_DESC(pkcs7_usage,
26+
"Usage to specify when verifying the PKCS#7 message");
27+
2228
/*
2329
* Preparse a PKCS#7 wrapped and validated data blob.
2430
*/
2531
static int pkcs7_preparse(struct key_preparsed_payload *prep)
2632
{
33+
enum key_being_used_for usage = pkcs7_usage;
2734
struct pkcs7_message *pkcs7;
2835
const void *data, *saved_prep_data;
2936
size_t datalen, saved_prep_datalen;
@@ -32,6 +39,11 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep)
3239

3340
kenter("");
3441

42+
if (usage >= NR__KEY_BEING_USED_FOR) {
43+
pr_err("Invalid usage type %d\n", usage);
44+
return -EINVAL;
45+
}
46+
3547
saved_prep_data = prep->data;
3648
saved_prep_datalen = prep->datalen;
3749
pkcs7 = pkcs7_parse_message(saved_prep_data, saved_prep_datalen);
@@ -40,7 +52,7 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep)
4052
goto error;
4153
}
4254

43-
ret = pkcs7_verify(pkcs7);
55+
ret = pkcs7_verify(pkcs7, usage);
4456
if (ret < 0)
4557
goto error_free;
4658

‎crypto/asymmetric_keys/pkcs7_parser.c‎

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ void pkcs7_free_message(struct pkcs7_message *pkcs7)
8181
}
8282
EXPORT_SYMBOL_GPL(pkcs7_free_message);
8383

84+
/*
85+
* Check authenticatedAttributes are provided or not provided consistently.
86+
*/
87+
static int pkcs7_check_authattrs(struct pkcs7_message *msg)
88+
{
89+
struct pkcs7_signed_info *sinfo;
90+
bool want;
91+
92+
sinfo = msg->signed_infos;
93+
if (sinfo->authattrs) {
94+
want = true;
95+
msg->have_authattrs = true;
96+
}
97+
98+
for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
99+
if (!!sinfo->authattrs != want)
100+
goto inconsistent;
101+
return 0;
102+
103+
inconsistent:
104+
pr_warn("Inconsistently supplied authAttrs\n");
105+
return -EINVAL;
106+
}
107+
84108
/**
85109
* pkcs7_parse_message - Parse a PKCS#7 message
86110
* @data: The raw binary ASN.1 encoded message to be parsed
@@ -113,6 +137,10 @@ struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
113137
goto out;
114138
}
115139

140+
ret = pkcs7_check_authattrs(ctx->msg);
141+
if (ret < 0)
142+
goto out;
143+
116144
msg = ctx->msg;
117145
ctx->msg = NULL;
118146

@@ -380,6 +408,25 @@ int pkcs7_note_certificate_list(void *context, size_t hdrlen,
380408
return 0;
381409
}
382410

411+
/*
412+
* Note the content type.
413+
*/
414+
int pkcs7_note_content(void *context, size_t hdrlen,
415+
unsigned char tag,
416+
const void *value, size_t vlen)
417+
{
418+
struct pkcs7_parse_context *ctx = context;
419+
420+
if (ctx->last_oid != OID_data &&
421+
ctx->last_oid != OID_msIndirectData) {
422+
pr_warn("Unsupported data type %d\n", ctx->last_oid);
423+
return -EINVAL;
424+
}
425+
426+
ctx->msg->data_type = ctx->last_oid;
427+
return 0;
428+
}
429+
383430
/*
384431
* Extract the data from the message and store that and its content type OID in
385432
* the context.
@@ -395,31 +442,90 @@ int pkcs7_note_data(void *context, size_t hdrlen,
395442
ctx->msg->data = value;
396443
ctx->msg->data_len = vlen;
397444
ctx->msg->data_hdrlen = hdrlen;
398-
ctx->msg->data_type = ctx->last_oid;
399445
return 0;
400446
}
401447

402448
/*
403-
* Parse authenticated attributes
449+
* Parse authenticated attributes.
404450
*/
405451
int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
406452
unsigned char tag,
407453
const void *value, size_t vlen)
408454
{
409455
struct pkcs7_parse_context *ctx = context;
456+
struct pkcs7_signed_info *sinfo = ctx->sinfo;
457+
enum OID content_type;
410458

411459
pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
412460

413461
switch (ctx->last_oid) {
462+
case OID_contentType:
463+
if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
464+
goto repeated;
465+
content_type = look_up_OID(value, vlen);
466+
if (content_type != ctx->msg->data_type) {
467+
pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
468+
ctx->msg->data_type, sinfo->index,
469+
content_type);
470+
return -EBADMSG;
471+
}
472+
return 0;
473+
474+
case OID_signingTime:
475+
if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
476+
goto repeated;
477+
/* Should we check that the signing time is consistent
478+
* with the signer's X.509 cert?
479+
*/
480+
return x509_decode_time(&sinfo->signing_time,
481+
hdrlen, tag, value, vlen);
482+
414483
case OID_messageDigest:
484+
if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
485+
goto repeated;
415486
if (tag != ASN1_OTS)
416487
return -EBADMSG;
417-
ctx->sinfo->msgdigest = value;
418-
ctx->sinfo->msgdigest_len = vlen;
488+
sinfo->msgdigest = value;
489+
sinfo->msgdigest_len = vlen;
490+
return 0;
491+
492+
case OID_smimeCapabilites:
493+
if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
494+
goto repeated;
495+
if (ctx->msg->data_type != OID_msIndirectData) {
496+
pr_warn("S/MIME Caps only allowed with Authenticode\n");
497+
return -EKEYREJECTED;
498+
}
499+
return 0;
500+
501+
/* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
502+
* char URLs and cont[1] 8-bit char URLs.
503+
*
504+
* Microsoft StatementType seems to contain a list of OIDs that
505+
* are also used as extendedKeyUsage types in X.509 certs.
506+
*/
507+
case OID_msSpOpusInfo:
508+
if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
509+
goto repeated;
510+
goto authenticode_check;
511+
case OID_msStatementType:
512+
if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
513+
goto repeated;
514+
authenticode_check:
515+
if (ctx->msg->data_type != OID_msIndirectData) {
516+
pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
517+
return -EKEYREJECTED;
518+
}
519+
/* I'm not sure how to validate these */
419520
return 0;
420521
default:
421522
return 0;
422523
}
524+
525+
repeated:
526+
/* We permit max one item per AuthenticatedAttribute and no repeats */
527+
pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
528+
return -EKEYREJECTED;
423529
}
424530

425531
/*
@@ -430,10 +536,25 @@ int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
430536
const void *value, size_t vlen)
431537
{
432538
struct pkcs7_parse_context *ctx = context;
539+
struct pkcs7_signed_info *sinfo = ctx->sinfo;
540+
541+
if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
542+
!test_bit(sinfo_has_message_digest, &sinfo->aa_set) ||
543+
(ctx->msg->data_type == OID_msIndirectData &&
544+
!test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))) {
545+
pr_warn("Missing required AuthAttr\n");
546+
return -EBADMSG;
547+
}
548+
549+
if (ctx->msg->data_type != OID_msIndirectData &&
550+
test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
551+
pr_warn("Unexpected Authenticode AuthAttr\n");
552+
return -EBADMSG;
553+
}
433554

434555
/* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
435-
ctx->sinfo->authattrs = value - (hdrlen - 1);
436-
ctx->sinfo->authattrs_len = vlen + (hdrlen - 1);
556+
sinfo->authattrs = value - (hdrlen - 1);
557+
sinfo->authattrs_len = vlen + (hdrlen - 1);
437558
return 0;
438559
}
439560

@@ -511,6 +632,11 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen,
511632
struct pkcs7_signed_info *sinfo = ctx->sinfo;
512633
struct asymmetric_key_id *kid;
513634

635+
if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
636+
pr_warn("Authenticode requires AuthAttrs\n");
637+
return -EBADMSG;
638+
}
639+
514640
/* Generate cert issuer + serial number key ID */
515641
if (!ctx->expect_skid) {
516642
kid = asymmetric_key_generate_id(ctx->raw_serial,

‎crypto/asymmetric_keys/pkcs7_parser.h‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
struct pkcs7_signed_info {
2222
struct pkcs7_signed_info *next;
2323
struct x509_certificate *signer; /* Signing certificate (in msg->certs) */
24-
unsigned index;
25-
bool trusted;
26-
bool unsupported_crypto; /* T if not usable due to missing crypto */
24+
unsigned index;
25+
bool trusted;
26+
bool unsupported_crypto; /* T if not usable due to missing crypto */
2727

2828
/* Message digest - the digest of the Content Data (or NULL) */
2929
const void *msgdigest;
@@ -32,6 +32,14 @@ struct pkcs7_signed_info {
3232
/* Authenticated Attribute data (or NULL) */
3333
unsigned authattrs_len;
3434
const void *authattrs;
35+
unsigned long aa_set;
36+
#define sinfo_has_content_type 0
37+
#define sinfo_has_signing_time 1
38+
#define sinfo_has_message_digest 2
39+
#define sinfo_has_smime_caps 3
40+
#define sinfo_has_ms_opus_info 4
41+
#define sinfo_has_ms_statement_type 5
42+
time64_t signing_time;
3543

3644
/* Issuing cert serial number and issuer's name [PKCS#7 or CMS ver 1]
3745
* or issuing cert's SKID [CMS ver 3].
@@ -53,6 +61,7 @@ struct pkcs7_message {
5361
struct x509_certificate *crl; /* Revocation list */
5462
struct pkcs7_signed_info *signed_infos;
5563
u8 version; /* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */
64+
bool have_authattrs; /* T if have authattrs */
5665

5766
/* Content Data (or NULL) */
5867
enum OID data_type; /* Type of Data */

0 commit comments

Comments
 (0)