2012-09-26 17:09:40 +08:00
|
|
|
/* Module signature checker
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public Licence
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the Licence, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2013-08-30 23:07:30 +08:00
|
|
|
#include <keys/system_keyring.h>
|
2015-07-21 04:16:27 +08:00
|
|
|
#include <crypto/public_key.h>
|
2012-09-26 17:09:40 +08:00
|
|
|
#include "module-internal.h"
|
|
|
|
|
2012-09-26 17:11:03 +08:00
|
|
|
/*
|
|
|
|
* Module signature information block.
|
|
|
|
*
|
|
|
|
* The constituents of the signature section are, in order:
|
|
|
|
*
|
|
|
|
* - Signer's name
|
|
|
|
* - Key identifier
|
|
|
|
* - Signature data
|
|
|
|
* - Information block
|
|
|
|
*/
|
|
|
|
struct module_signature {
|
2015-07-21 04:16:27 +08:00
|
|
|
u8 algo; /* Public-key crypto algorithm [0] */
|
|
|
|
u8 hash; /* Digest algorithm [0] */
|
|
|
|
u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */
|
|
|
|
u8 signer_len; /* Length of signer's name [0] */
|
|
|
|
u8 key_id_len; /* Length of key identifier [0] */
|
2012-10-22 22:05:48 +08:00
|
|
|
u8 __pad[3];
|
|
|
|
__be32 sig_len; /* Length of signature data */
|
2012-09-26 17:11:03 +08:00
|
|
|
};
|
|
|
|
|
2012-09-26 17:09:40 +08:00
|
|
|
/*
|
|
|
|
* Verify the signature on a module.
|
|
|
|
*/
|
2012-10-20 08:19:29 +08:00
|
|
|
int mod_verify_sig(const void *mod, unsigned long *_modlen)
|
2012-09-26 17:09:40 +08:00
|
|
|
{
|
2012-09-26 17:11:03 +08:00
|
|
|
struct module_signature ms;
|
2012-10-20 08:19:29 +08:00
|
|
|
size_t modlen = *_modlen, sig_len;
|
2012-09-26 17:11:03 +08:00
|
|
|
|
2012-10-21 09:59:31 +08:00
|
|
|
pr_devel("==>%s(,%zu)\n", __func__, modlen);
|
2012-09-26 17:11:03 +08:00
|
|
|
|
2012-10-20 08:19:29 +08:00
|
|
|
if (modlen <= sizeof(ms))
|
2012-09-26 17:11:03 +08:00
|
|
|
return -EBADMSG;
|
|
|
|
|
2012-10-20 08:19:29 +08:00
|
|
|
memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
|
|
|
|
modlen -= sizeof(ms);
|
2012-09-26 17:11:03 +08:00
|
|
|
|
|
|
|
sig_len = be32_to_cpu(ms.sig_len);
|
2012-10-20 08:19:29 +08:00
|
|
|
if (sig_len >= modlen)
|
2012-09-26 17:11:03 +08:00
|
|
|
return -EBADMSG;
|
2012-10-20 08:19:29 +08:00
|
|
|
modlen -= sig_len;
|
|
|
|
*_modlen = modlen;
|
2012-09-26 17:11:03 +08:00
|
|
|
|
2015-07-21 04:16:27 +08:00
|
|
|
if (ms.id_type != PKEY_ID_PKCS7) {
|
|
|
|
pr_err("Module is not signed with expected PKCS#7 message\n");
|
2012-09-26 17:11:03 +08:00
|
|
|
return -ENOPKG;
|
|
|
|
}
|
|
|
|
|
2015-07-21 04:16:27 +08:00
|
|
|
if (ms.algo != 0 ||
|
|
|
|
ms.hash != 0 ||
|
|
|
|
ms.signer_len != 0 ||
|
|
|
|
ms.key_id_len != 0 ||
|
|
|
|
ms.__pad[0] != 0 ||
|
|
|
|
ms.__pad[1] != 0 ||
|
|
|
|
ms.__pad[2] != 0) {
|
|
|
|
pr_err("PKCS#7 signature info has unexpected non-zero params\n");
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
2012-09-26 17:11:03 +08:00
|
|
|
|
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>
2015-08-05 22:22:27 +08:00
|
|
|
return system_verify_data(mod, modlen, mod + modlen, sig_len,
|
|
|
|
VERIFYING_MODULE_SIGNATURE);
|
2012-09-26 17:09:40 +08:00
|
|
|
}
|