| 1 | |
| 2 | *
|
| 3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
|
| 4 | * Written by David Howells ([email protected])
|
| 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>
|
| 13 | #include <linux/errno.h>
|
| 14 | #include <keys/system_keyring.h>
|
| 15 | #include <crypto/public_key.h>
|
| 16 | #include "module-internal.h"
|
| 17 |
|
| 18 | |
| 19 | * Module signature information block.
|
| 20 | *
|
| 21 | * The constituents of the signature section are, in order:
|
| 22 | *
|
| 23 | * - Signer's name
|
| 24 | * - Key identifier
|
| 25 | * - Signature data
|
| 26 | * - Information block
|
| 27 |
|
| 28 | struct module_signature {
|
| 29 | u8 algo;
|
| 30 | u8 hash;
|
| 31 | u8 id_type;
|
| 32 | u8 signer_len;
|
| 33 | u8 key_id_len;
|
| 34 | u8 __pad[3];
|
| 35 | __be32 sig_len;
|
| 36 | };
|
| 37 |
|
| 38 | |
| 39 | * Verify the signature on a module.
|
| 40 |
|
| 41 | int mod_verify_sig(const void *mod, unsigned long *_modlen)
|
| 42 | {
|
| 43 | struct module_signature ms;
|
| 44 | size_t modlen = *_modlen, sig_len;
|
| 45 |
|
| 46 | pr_devel("==>%s(,%zu)\n", __func__, modlen);
|
| 47 |
|
| 48 | if (modlen <= sizeof(ms))
|
| 49 | return -EBADMSG;
|
| 50 |
|
| 51 | memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
|
| 52 | modlen -= sizeof(ms);
|
| 53 |
|
| 54 | sig_len = be32_to_cpu(ms.sig_len);
|
| 55 | if (sig_len >= modlen)
|
| 56 | return -EBADMSG;
|
| 57 | modlen -= sig_len;
|
| 58 | *_modlen = modlen;
|
| 59 |
|
| 60 | if (ms.id_type != PKEY_ID_PKCS7) {
|
| 61 | pr_err("Module is not signed with expected PKCS#7 message\n");
|
| 62 | return -ENOPKG;
|
| 63 | }
|
| 64 |
|
| 65 | if (ms.algo != 0 ||
|
| 66 | ms.hash != 0 ||
|
| 67 | ms.signer_len != 0 ||
|
| 68 | ms.key_id_len != 0 ||
|
| 69 | ms.__pad[0] != 0 ||
|
| 70 | ms.__pad[1] != 0 ||
|
| 71 | ms.__pad[2] != 0) {
|
| 72 | pr_err("PKCS#7 signature info has unexpected non-zero params\n");
|
| 73 | return -EBADMSG;
|
| 74 | }
|
| 75 |
|
| 76 | return system_verify_data(mod, modlen, mod + modlen, sig_len,
|
| 77 | VERIFYING_MODULE_SIGNATURE);
|
| 78 | }
|