Rusty Russell | 106a4ee | 2012-09-26 10:09:40 +0100 | [diff] [blame] | 1 | /* Module signature checker |
| 2 | * |
| 3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 4 | * 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. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 13 | #include <linux/errno.h> |
David Howells | 89053aa | 2016-03-01 10:36:07 +0000 | [diff] [blame] | 14 | #include <linux/string.h> |
David Howells | a511e1a | 2016-04-06 16:14:26 +0100 | [diff] [blame] | 15 | #include <linux/verification.h> |
David Howells | 3f1e1be | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 16 | #include <crypto/public_key.h> |
Rusty Russell | 106a4ee | 2012-09-26 10:09:40 +0100 | [diff] [blame] | 17 | #include "module-internal.h" |
| 18 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 19 | enum pkey_id_type { |
| 20 | PKEY_ID_PGP, /* OpenPGP generated key ID */ |
| 21 | PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */ |
| 22 | PKEY_ID_PKCS7, /* Signature in PKCS#7 message */ |
| 23 | }; |
| 24 | |
Rusty Russell | 106a4ee | 2012-09-26 10:09:40 +0100 | [diff] [blame] | 25 | /* |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 26 | * Module signature information block. |
| 27 | * |
| 28 | * The constituents of the signature section are, in order: |
| 29 | * |
| 30 | * - Signer's name |
| 31 | * - Key identifier |
| 32 | * - Signature data |
| 33 | * - Information block |
| 34 | */ |
| 35 | struct module_signature { |
David Howells | 3f1e1be | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 36 | u8 algo; /* Public-key crypto algorithm [0] */ |
| 37 | u8 hash; /* Digest algorithm [0] */ |
| 38 | u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ |
| 39 | u8 signer_len; /* Length of signer's name [0] */ |
| 40 | u8 key_id_len; /* Length of key identifier [0] */ |
David Howells | 12e130b | 2012-10-22 15:05:48 +0100 | [diff] [blame] | 41 | u8 __pad[3]; |
| 42 | __be32 sig_len; /* Length of signature data */ |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
Rusty Russell | 106a4ee | 2012-09-26 10:09:40 +0100 | [diff] [blame] | 46 | * Verify the signature on a module. |
| 47 | */ |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 48 | int mod_verify_sig(const void *mod, unsigned long *_modlen) |
Rusty Russell | 106a4ee | 2012-09-26 10:09:40 +0100 | [diff] [blame] | 49 | { |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 50 | struct module_signature ms; |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 51 | size_t modlen = *_modlen, sig_len; |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 52 | |
Randy Dunlap | 0390c88 | 2012-10-20 18:59:31 -0700 | [diff] [blame] | 53 | pr_devel("==>%s(,%zu)\n", __func__, modlen); |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 54 | |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 55 | if (modlen <= sizeof(ms)) |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 56 | return -EBADMSG; |
| 57 | |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 58 | memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms)); |
| 59 | modlen -= sizeof(ms); |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 60 | |
| 61 | sig_len = be32_to_cpu(ms.sig_len); |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 62 | if (sig_len >= modlen) |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 63 | return -EBADMSG; |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 64 | modlen -= sig_len; |
David Howells | caabe24 | 2012-10-20 01:19:29 +0100 | [diff] [blame] | 65 | *_modlen = modlen; |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 66 | |
David Howells | 3f1e1be | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 67 | if (ms.id_type != PKEY_ID_PKCS7) { |
| 68 | pr_err("Module is not signed with expected PKCS#7 message\n"); |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 69 | return -ENOPKG; |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 70 | } |
| 71 | |
David Howells | 3f1e1be | 2015-07-20 21:16:27 +0100 | [diff] [blame] | 72 | if (ms.algo != 0 || |
| 73 | ms.hash != 0 || |
| 74 | ms.signer_len != 0 || |
| 75 | ms.key_id_len != 0 || |
| 76 | ms.__pad[0] != 0 || |
| 77 | ms.__pad[1] != 0 || |
| 78 | ms.__pad[2] != 0) { |
| 79 | pr_err("PKCS#7 signature info has unexpected non-zero params\n"); |
| 80 | return -EBADMSG; |
| 81 | } |
David Howells | 48ba246 | 2012-09-26 10:11:03 +0100 | [diff] [blame] | 82 | |
David Howells | e68503b | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 83 | return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len, |
David Howells | bda850c | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 84 | NULL, VERIFYING_MODULE_SIGNATURE, |
David Howells | e68503b | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 85 | NULL, NULL); |
Rusty Russell | 106a4ee | 2012-09-26 10:09:40 +0100 | [diff] [blame] | 86 | } |