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