Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Intel Corporation |
| 4 | * |
| 5 | * Author: |
| 6 | * Dmitry Kasatkin <dmitry.kasatkin@intel.com> |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/err.h> |
Dmitry Kasatkin | d9a2e5d | 2014-07-02 15:12:26 +0300 | [diff] [blame] | 12 | #include <linux/ratelimit.h> |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 13 | #include <linux/key-type.h> |
| 14 | #include <crypto/public_key.h> |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 15 | #include <crypto/hash_info.h> |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 16 | #include <keys/asymmetric-type.h> |
Petko Manolov | 41c89b6 | 2015-12-02 17:47:55 +0200 | [diff] [blame] | 17 | #include <keys/system_keyring.h> |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 18 | |
| 19 | #include "integrity.h" |
| 20 | |
| 21 | /* |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 22 | * Request an asymmetric key. |
| 23 | */ |
| 24 | static struct key *request_asymmetric_key(struct key *keyring, uint32_t keyid) |
| 25 | { |
| 26 | struct key *key; |
| 27 | char name[12]; |
| 28 | |
Dmitry Kasatkin | 594081e | 2014-10-06 17:31:58 +0100 | [diff] [blame] | 29 | sprintf(name, "id:%08x", keyid); |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 30 | |
| 31 | pr_debug("key search: \"%s\"\n", name); |
| 32 | |
Petko Manolov | 41c89b6 | 2015-12-02 17:47:55 +0200 | [diff] [blame] | 33 | key = get_ima_blacklist_keyring(); |
| 34 | if (key) { |
| 35 | key_ref_t kref; |
| 36 | |
| 37 | kref = keyring_search(make_key_ref(key, 1), |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 38 | &key_type_asymmetric, name, true); |
Petko Manolov | 41c89b6 | 2015-12-02 17:47:55 +0200 | [diff] [blame] | 39 | if (!IS_ERR(kref)) { |
| 40 | pr_err("Key '%s' is in ima_blacklist_keyring\n", name); |
| 41 | return ERR_PTR(-EKEYREJECTED); |
| 42 | } |
| 43 | } |
| 44 | |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 45 | if (keyring) { |
| 46 | /* search in specific keyring */ |
| 47 | key_ref_t kref; |
Petko Manolov | 41c89b6 | 2015-12-02 17:47:55 +0200 | [diff] [blame] | 48 | |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 49 | kref = keyring_search(make_key_ref(keyring, 1), |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 50 | &key_type_asymmetric, name, true); |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 51 | if (IS_ERR(kref)) |
| 52 | key = ERR_CAST(kref); |
| 53 | else |
| 54 | key = key_ref_to_ptr(kref); |
| 55 | } else { |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 56 | key = request_key(&key_type_asymmetric, name, NULL); |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (IS_ERR(key)) { |
Dmitry Kasatkin | d9a2e5d | 2014-07-02 15:12:26 +0300 | [diff] [blame] | 60 | pr_err_ratelimited("Request for unknown key '%s' err %ld\n", |
| 61 | name, PTR_ERR(key)); |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 62 | switch (PTR_ERR(key)) { |
| 63 | /* Hide some search errors */ |
| 64 | case -EACCES: |
| 65 | case -ENOTDIR: |
| 66 | case -EAGAIN: |
| 67 | return ERR_PTR(-ENOKEY); |
| 68 | default: |
| 69 | return key; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pr_debug("%s() = 0 [%x]\n", __func__, key_serial(key)); |
| 74 | |
| 75 | return key; |
| 76 | } |
| 77 | |
| 78 | int asymmetric_verify(struct key *keyring, const char *sig, |
| 79 | int siglen, const char *data, int datalen) |
| 80 | { |
| 81 | struct public_key_signature pks; |
| 82 | struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig; |
| 83 | struct key *key; |
| 84 | int ret = -ENOMEM; |
| 85 | |
| 86 | if (siglen <= sizeof(*hdr)) |
| 87 | return -EBADMSG; |
| 88 | |
| 89 | siglen -= sizeof(*hdr); |
| 90 | |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 91 | if (siglen != be16_to_cpu(hdr->sig_size)) |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 92 | return -EBADMSG; |
| 93 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 94 | if (hdr->hash_algo >= HASH_ALGO__LAST) |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 95 | return -ENOPKG; |
| 96 | |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 97 | key = request_asymmetric_key(keyring, be32_to_cpu(hdr->keyid)); |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 98 | if (IS_ERR(key)) |
| 99 | return PTR_ERR(key); |
| 100 | |
| 101 | memset(&pks, 0, sizeof(pks)); |
| 102 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 103 | pks.hash_algo = hash_algo_name[hdr->hash_algo]; |
Vitaly Chikunov | be08f0c | 2019-04-11 18:51:22 +0300 | [diff] [blame] | 104 | if (hdr->hash_algo == HASH_ALGO_STREEBOG_256 || |
| 105 | hdr->hash_algo == HASH_ALGO_STREEBOG_512) { |
| 106 | /* EC-RDSA and Streebog should go together. */ |
| 107 | pks.pkey_algo = "ecrdsa"; |
| 108 | pks.encoding = "raw"; |
| 109 | } else { |
| 110 | pks.pkey_algo = "rsa"; |
| 111 | pks.encoding = "pkcs1"; |
| 112 | } |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 113 | pks.digest = (u8 *)data; |
| 114 | pks.digest_size = datalen; |
Tadeusz Struk | eb5798f | 2016-02-02 10:08:58 -0800 | [diff] [blame] | 115 | pks.s = hdr->sig; |
| 116 | pks.s_size = siglen; |
| 117 | ret = verify_signature(key, &pks); |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 118 | key_put(key); |
| 119 | pr_debug("%s() = %d\n", __func__, ret); |
| 120 | return ret; |
| 121 | } |
Mikhail Kurinnoi | 6eb864c | 2018-06-27 16:33:42 +0300 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * integrity_kernel_module_request - prevent crypto-pkcs1pad(rsa,*) requests |
| 125 | * @kmod_name: kernel module name |
| 126 | * |
| 127 | * We have situation, when public_key_verify_signature() in case of RSA |
| 128 | * algorithm use alg_name to store internal information in order to |
| 129 | * construct an algorithm on the fly, but crypto_larval_lookup() will try |
| 130 | * to use alg_name in order to load kernel module with same name. |
| 131 | * Since we don't have any real "crypto-pkcs1pad(rsa,*)" kernel modules, |
| 132 | * we are safe to fail such module request from crypto_larval_lookup(). |
| 133 | * |
| 134 | * In this way we prevent modprobe execution during digsig verification |
| 135 | * and avoid possible deadlock if modprobe and/or it's dependencies |
| 136 | * also signed with digsig. |
| 137 | */ |
| 138 | int integrity_kernel_module_request(char *kmod_name) |
| 139 | { |
| 140 | if (strncmp(kmod_name, "crypto-pkcs1pad(rsa,", 20) == 0) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | return 0; |
| 144 | } |