blob: cfa4127d0518f8636d08e388294c0e0830b7b3ab [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Dmitry Kasatkine0751252013-02-07 00:12:08 +02002/*
3 * Copyright (C) 2013 Intel Corporation
4 *
5 * Author:
6 * Dmitry Kasatkin <dmitry.kasatkin@intel.com>
Dmitry Kasatkine0751252013-02-07 00:12:08 +02007 */
8
Dmitry Kasatkine0751252013-02-07 00:12:08 +02009#include <linux/err.h>
Dmitry Kasatkind9a2e5d2014-07-02 15:12:26 +030010#include <linux/ratelimit.h>
Dmitry Kasatkine0751252013-02-07 00:12:08 +020011#include <linux/key-type.h>
12#include <crypto/public_key.h>
David Howells4e8ae722016-03-03 21:49:27 +000013#include <crypto/hash_info.h>
Dmitry Kasatkine0751252013-02-07 00:12:08 +020014#include <keys/asymmetric-type.h>
Petko Manolov41c89b62015-12-02 17:47:55 +020015#include <keys/system_keyring.h>
Dmitry Kasatkine0751252013-02-07 00:12:08 +020016
17#include "integrity.h"
18
19/*
Dmitry Kasatkine0751252013-02-07 00:12:08 +020020 * Request an asymmetric key.
21 */
22static struct key *request_asymmetric_key(struct key *keyring, uint32_t keyid)
23{
24 struct key *key;
25 char name[12];
26
Dmitry Kasatkin594081e2014-10-06 17:31:58 +010027 sprintf(name, "id:%08x", keyid);
Dmitry Kasatkine0751252013-02-07 00:12:08 +020028
29 pr_debug("key search: \"%s\"\n", name);
30
Petko Manolov41c89b62015-12-02 17:47:55 +020031 key = get_ima_blacklist_keyring();
32 if (key) {
33 key_ref_t kref;
34
35 kref = keyring_search(make_key_ref(key, 1),
David Howellsdcf49db2019-06-26 21:02:32 +010036 &key_type_asymmetric, name, true);
Petko Manolov41c89b62015-12-02 17:47:55 +020037 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 Kasatkine0751252013-02-07 00:12:08 +020043 if (keyring) {
44 /* search in specific keyring */
45 key_ref_t kref;
Petko Manolov41c89b62015-12-02 17:47:55 +020046
Dmitry Kasatkine0751252013-02-07 00:12:08 +020047 kref = keyring_search(make_key_ref(keyring, 1),
David Howellsdcf49db2019-06-26 21:02:32 +010048 &key_type_asymmetric, name, true);
Dmitry Kasatkine0751252013-02-07 00:12:08 +020049 if (IS_ERR(kref))
50 key = ERR_CAST(kref);
51 else
52 key = key_ref_to_ptr(kref);
53 } else {
Linus Torvalds028db3e2019-07-10 18:43:43 -070054 key = request_key(&key_type_asymmetric, name, NULL);
Dmitry Kasatkine0751252013-02-07 00:12:08 +020055 }
56
57 if (IS_ERR(key)) {
Dmitry Kasatkind9a2e5d2014-07-02 15:12:26 +030058 pr_err_ratelimited("Request for unknown key '%s' err %ld\n",
59 name, PTR_ERR(key));
Dmitry Kasatkine0751252013-02-07 00:12:08 +020060 switch (PTR_ERR(key)) {
61 /* Hide some search errors */
62 case -EACCES:
63 case -ENOTDIR:
64 case -EAGAIN:
65 return ERR_PTR(-ENOKEY);
66 default:
67 return key;
68 }
69 }
70
71 pr_debug("%s() = 0 [%x]\n", __func__, key_serial(key));
72
73 return key;
74}
75
76int asymmetric_verify(struct key *keyring, const char *sig,
77 int siglen, const char *data, int datalen)
78{
79 struct public_key_signature pks;
80 struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;
81 struct key *key;
Colin Ian King3db0d0c2020-07-01 14:46:34 +010082 int ret;
Dmitry Kasatkine0751252013-02-07 00:12:08 +020083
84 if (siglen <= sizeof(*hdr))
85 return -EBADMSG;
86
87 siglen -= sizeof(*hdr);
88
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -030089 if (siglen != be16_to_cpu(hdr->sig_size))
Dmitry Kasatkine0751252013-02-07 00:12:08 +020090 return -EBADMSG;
91
David Howells4e8ae722016-03-03 21:49:27 +000092 if (hdr->hash_algo >= HASH_ALGO__LAST)
Dmitry Kasatkine0751252013-02-07 00:12:08 +020093 return -ENOPKG;
94
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -030095 key = request_asymmetric_key(keyring, be32_to_cpu(hdr->keyid));
Dmitry Kasatkine0751252013-02-07 00:12:08 +020096 if (IS_ERR(key))
97 return PTR_ERR(key);
98
99 memset(&pks, 0, sizeof(pks));
100
David Howells4e8ae722016-03-03 21:49:27 +0000101 pks.hash_algo = hash_algo_name[hdr->hash_algo];
Vitaly Chikunovbe08f0c2019-04-11 18:51:22 +0300102 if (hdr->hash_algo == HASH_ALGO_STREEBOG_256 ||
103 hdr->hash_algo == HASH_ALGO_STREEBOG_512) {
104 /* EC-RDSA and Streebog should go together. */
105 pks.pkey_algo = "ecrdsa";
106 pks.encoding = "raw";
107 } else {
108 pks.pkey_algo = "rsa";
109 pks.encoding = "pkcs1";
110 }
Dmitry Kasatkine0751252013-02-07 00:12:08 +0200111 pks.digest = (u8 *)data;
112 pks.digest_size = datalen;
Tadeusz Strukeb5798f2016-02-02 10:08:58 -0800113 pks.s = hdr->sig;
114 pks.s_size = siglen;
115 ret = verify_signature(key, &pks);
Dmitry Kasatkine0751252013-02-07 00:12:08 +0200116 key_put(key);
117 pr_debug("%s() = %d\n", __func__, ret);
118 return ret;
119}
Mikhail Kurinnoi6eb864c2018-06-27 16:33:42 +0300120
121/**
122 * integrity_kernel_module_request - prevent crypto-pkcs1pad(rsa,*) requests
123 * @kmod_name: kernel module name
124 *
125 * We have situation, when public_key_verify_signature() in case of RSA
126 * algorithm use alg_name to store internal information in order to
127 * construct an algorithm on the fly, but crypto_larval_lookup() will try
128 * to use alg_name in order to load kernel module with same name.
129 * Since we don't have any real "crypto-pkcs1pad(rsa,*)" kernel modules,
130 * we are safe to fail such module request from crypto_larval_lookup().
131 *
132 * In this way we prevent modprobe execution during digsig verification
133 * and avoid possible deadlock if modprobe and/or it's dependencies
134 * also signed with digsig.
135 */
136int integrity_kernel_module_request(char *kmod_name)
137{
138 if (strncmp(kmod_name, "crypto-pkcs1pad(rsa,", 20) == 0)
139 return -EINVAL;
140
141 return 0;
142}