Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Crypto user configuration API. |
| 4 | * |
| 5 | * Copyright (C) 2011 secunet Security Networks AG |
| 6 | * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com> |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/crypto.h> |
| 11 | #include <linux/cryptouser.h> |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 12 | #include <linux/sched.h> |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 13 | #include <linux/security.h> |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 14 | #include <net/netlink.h> |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 15 | #include <net/net_namespace.h> |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 16 | #include <net/sock.h> |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 17 | #include <crypto/internal/skcipher.h> |
Herbert Xu | 9aa867e | 2015-06-21 19:11:45 +0800 | [diff] [blame] | 18 | #include <crypto/internal/rng.h> |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 19 | #include <crypto/akcipher.h> |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 20 | #include <crypto/kpp.h> |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 21 | #include <crypto/internal/cryptouser.h> |
Steffen Klassert | 1e12299 | 2012-03-29 09:03:47 +0200 | [diff] [blame] | 22 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | |
Mathias Krause | 8fd61d3 | 2013-02-05 18:19:15 +0100 | [diff] [blame] | 25 | #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x)) |
| 26 | |
Jussi Kivilinna | 66ce0b0 | 2012-08-28 16:46:54 +0300 | [diff] [blame] | 27 | static DEFINE_MUTEX(crypto_cfg_mutex); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 28 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 29 | struct crypto_dump_info { |
| 30 | struct sk_buff *in_skb; |
| 31 | struct sk_buff *out_skb; |
| 32 | u32 nlmsg_seq; |
| 33 | u16 nlmsg_flags; |
| 34 | }; |
| 35 | |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 36 | struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact) |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 37 | { |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 38 | struct crypto_alg *q, *alg = NULL; |
| 39 | |
| 40 | down_read(&crypto_alg_sem); |
| 41 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 42 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | e6ea64e | 2011-10-21 14:37:10 +0200 | [diff] [blame] | 43 | int match = 0; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 44 | |
Eric Biggers | 21d4120 | 2019-07-02 14:17:00 -0700 | [diff] [blame] | 45 | if (crypto_is_larval(q)) |
| 46 | continue; |
| 47 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 48 | if ((q->cra_flags ^ p->cru_type) & p->cru_mask) |
| 49 | continue; |
| 50 | |
| 51 | if (strlen(p->cru_driver_name)) |
| 52 | match = !strcmp(q->cra_driver_name, |
| 53 | p->cru_driver_name); |
| 54 | else if (!exact) |
| 55 | match = !strcmp(q->cra_name, p->cru_name); |
| 56 | |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 57 | if (!match) |
| 58 | continue; |
| 59 | |
| 60 | if (unlikely(!crypto_mod_get(q))) |
| 61 | continue; |
| 62 | |
| 63 | alg = q; |
| 64 | break; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | up_read(&crypto_alg_sem); |
| 68 | |
| 69 | return alg; |
| 70 | } |
| 71 | |
Steffen Klassert | 07a5fa4 | 2011-09-27 07:48:01 +0200 | [diff] [blame] | 72 | static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg) |
| 73 | { |
| 74 | struct crypto_report_cipher rcipher; |
| 75 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 76 | memset(&rcipher, 0, sizeof(rcipher)); |
| 77 | |
| 78 | strscpy(rcipher.type, "cipher", sizeof(rcipher.type)); |
Steffen Klassert | 07a5fa4 | 2011-09-27 07:48:01 +0200 | [diff] [blame] | 79 | |
| 80 | rcipher.blocksize = alg->cra_blocksize; |
| 81 | rcipher.min_keysize = alg->cra_cipher.cia_min_keysize; |
| 82 | rcipher.max_keysize = alg->cra_cipher.cia_max_keysize; |
| 83 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 84 | return nla_put(skb, CRYPTOCFGA_REPORT_CIPHER, |
| 85 | sizeof(rcipher), &rcipher); |
Steffen Klassert | 07a5fa4 | 2011-09-27 07:48:01 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Steffen Klassert | 540b97c | 2011-09-27 07:48:48 +0200 | [diff] [blame] | 88 | static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg) |
| 89 | { |
| 90 | struct crypto_report_comp rcomp; |
| 91 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 92 | memset(&rcomp, 0, sizeof(rcomp)); |
Steffen Klassert | 540b97c | 2011-09-27 07:48:48 +0200 | [diff] [blame] | 93 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 94 | strscpy(rcomp.type, "compression", sizeof(rcomp.type)); |
| 95 | |
| 96 | return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, sizeof(rcomp), &rcomp); |
Steffen Klassert | 540b97c | 2011-09-27 07:48:48 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 99 | static int crypto_report_one(struct crypto_alg *alg, |
| 100 | struct crypto_user_alg *ualg, struct sk_buff *skb) |
| 101 | { |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 102 | memset(ualg, 0, sizeof(*ualg)); |
| 103 | |
| 104 | strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name)); |
| 105 | strscpy(ualg->cru_driver_name, alg->cra_driver_name, |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 106 | sizeof(ualg->cru_driver_name)); |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 107 | strscpy(ualg->cru_module_name, module_name(alg->cra_module), |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 108 | sizeof(ualg->cru_module_name)); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 109 | |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 110 | ualg->cru_type = 0; |
| 111 | ualg->cru_mask = 0; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 112 | ualg->cru_flags = alg->cra_flags; |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 113 | ualg->cru_refcnt = refcount_read(&alg->cra_refcnt); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 114 | |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 115 | if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority)) |
| 116 | goto nla_put_failure; |
Steffen Klassert | 6c5a86f5 | 2011-09-27 07:25:05 +0200 | [diff] [blame] | 117 | if (alg->cra_flags & CRYPTO_ALG_LARVAL) { |
| 118 | struct crypto_report_larval rl; |
| 119 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 120 | memset(&rl, 0, sizeof(rl)); |
| 121 | strscpy(rl.type, "larval", sizeof(rl.type)); |
| 122 | if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL, sizeof(rl), &rl)) |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 123 | goto nla_put_failure; |
Steffen Klassert | 6c5a86f5 | 2011-09-27 07:25:05 +0200 | [diff] [blame] | 124 | goto out; |
| 125 | } |
| 126 | |
Steffen Klassert | b6aa63c | 2011-09-27 07:24:29 +0200 | [diff] [blame] | 127 | if (alg->cra_type && alg->cra_type->report) { |
| 128 | if (alg->cra_type->report(skb, alg)) |
| 129 | goto nla_put_failure; |
Steffen Klassert | 07a5fa4 | 2011-09-27 07:48:01 +0200 | [diff] [blame] | 130 | |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) { |
| 135 | case CRYPTO_ALG_TYPE_CIPHER: |
| 136 | if (crypto_report_cipher(skb, alg)) |
| 137 | goto nla_put_failure; |
| 138 | |
| 139 | break; |
Steffen Klassert | 540b97c | 2011-09-27 07:48:48 +0200 | [diff] [blame] | 140 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 141 | if (crypto_report_comp(skb, alg)) |
| 142 | goto nla_put_failure; |
| 143 | |
| 144 | break; |
Steffen Klassert | b6aa63c | 2011-09-27 07:24:29 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Steffen Klassert | 6c5a86f5 | 2011-09-27 07:25:05 +0200 | [diff] [blame] | 147 | out: |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 148 | return 0; |
| 149 | |
| 150 | nla_put_failure: |
| 151 | return -EMSGSIZE; |
| 152 | } |
| 153 | |
| 154 | static int crypto_report_alg(struct crypto_alg *alg, |
| 155 | struct crypto_dump_info *info) |
| 156 | { |
| 157 | struct sk_buff *in_skb = info->in_skb; |
| 158 | struct sk_buff *skb = info->out_skb; |
| 159 | struct nlmsghdr *nlh; |
| 160 | struct crypto_user_alg *ualg; |
| 161 | int err = 0; |
| 162 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 163 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq, |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 164 | CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags); |
| 165 | if (!nlh) { |
| 166 | err = -EMSGSIZE; |
| 167 | goto out; |
| 168 | } |
| 169 | |
| 170 | ualg = nlmsg_data(nlh); |
| 171 | |
| 172 | err = crypto_report_one(alg, ualg, skb); |
| 173 | if (err) { |
| 174 | nlmsg_cancel(skb, nlh); |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | nlmsg_end(skb, nlh); |
| 179 | |
| 180 | out: |
| 181 | return err; |
| 182 | } |
| 183 | |
| 184 | static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh, |
| 185 | struct nlattr **attrs) |
| 186 | { |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 187 | struct net *net = sock_net(in_skb->sk); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 188 | struct crypto_user_alg *p = nlmsg_data(in_nlh); |
| 189 | struct crypto_alg *alg; |
| 190 | struct sk_buff *skb; |
| 191 | struct crypto_dump_info info; |
| 192 | int err; |
| 193 | |
Mathias Krause | 8fd61d3 | 2013-02-05 18:19:15 +0100 | [diff] [blame] | 194 | if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name)) |
| 195 | return -EINVAL; |
| 196 | |
Herbert Xu | 5d4a5e7 | 2014-11-20 12:44:32 +0800 | [diff] [blame] | 197 | alg = crypto_alg_match(p, 0); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 198 | if (!alg) |
| 199 | return -ENOENT; |
| 200 | |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 201 | err = -ENOMEM; |
Jia-Ju Bai | 9a69b7a | 2018-01-25 18:06:02 +0800 | [diff] [blame] | 202 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 203 | if (!skb) |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 204 | goto drop_alg; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 205 | |
| 206 | info.in_skb = in_skb; |
| 207 | info.out_skb = skb; |
| 208 | info.nlmsg_seq = in_nlh->nlmsg_seq; |
| 209 | info.nlmsg_flags = 0; |
| 210 | |
| 211 | err = crypto_report_alg(alg, &info); |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 212 | |
| 213 | drop_alg: |
| 214 | crypto_mod_put(alg); |
| 215 | |
Navid Emamdoost | ffdde59 | 2019-10-04 14:29:16 -0500 | [diff] [blame] | 216 | if (err) { |
| 217 | kfree_skb(skb); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 218 | return err; |
Navid Emamdoost | ffdde59 | 2019-10-04 14:29:16 -0500 | [diff] [blame] | 219 | } |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 220 | |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 221 | return nlmsg_unicast(net->crypto_nlsk, skb, NETLINK_CB(in_skb).portid); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb) |
| 225 | { |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 226 | const size_t start_pos = cb->args[0]; |
| 227 | size_t pos = 0; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 228 | struct crypto_dump_info info; |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 229 | struct crypto_alg *alg; |
| 230 | int res; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 231 | |
| 232 | info.in_skb = cb->skb; |
| 233 | info.out_skb = skb; |
| 234 | info.nlmsg_seq = cb->nlh->nlmsg_seq; |
| 235 | info.nlmsg_flags = NLM_F_MULTI; |
| 236 | |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 237 | down_read(&crypto_alg_sem); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 238 | list_for_each_entry(alg, &crypto_alg_list, cra_list) { |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 239 | if (pos >= start_pos) { |
| 240 | res = crypto_report_alg(alg, &info); |
| 241 | if (res == -EMSGSIZE) |
| 242 | break; |
| 243 | if (res) |
| 244 | goto out; |
| 245 | } |
| 246 | pos++; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 247 | } |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 248 | cb->args[0] = pos; |
| 249 | res = skb->len; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 250 | out: |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 251 | up_read(&crypto_alg_sem); |
| 252 | return res; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static int crypto_dump_report_done(struct netlink_callback *cb) |
| 256 | { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 261 | struct nlattr **attrs) |
| 262 | { |
| 263 | struct crypto_alg *alg; |
| 264 | struct crypto_user_alg *p = nlmsg_data(nlh); |
| 265 | struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL]; |
| 266 | LIST_HEAD(list); |
| 267 | |
Linus Torvalds | 639b4ac | 2014-06-07 19:44:40 -0700 | [diff] [blame] | 268 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Matthias-Christian Ott | c568398 | 2014-05-08 21:58:12 +0800 | [diff] [blame] | 269 | return -EPERM; |
| 270 | |
Mathias Krause | 8fd61d3 | 2013-02-05 18:19:15 +0100 | [diff] [blame] | 271 | if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name)) |
| 272 | return -EINVAL; |
| 273 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 274 | if (priority && !strlen(p->cru_driver_name)) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | alg = crypto_alg_match(p, 1); |
| 278 | if (!alg) |
| 279 | return -ENOENT; |
| 280 | |
| 281 | down_write(&crypto_alg_sem); |
| 282 | |
| 283 | crypto_remove_spawns(alg, &list, NULL); |
| 284 | |
| 285 | if (priority) |
| 286 | alg->cra_priority = nla_get_u32(priority); |
| 287 | |
| 288 | up_write(&crypto_alg_sem); |
| 289 | |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 290 | crypto_mod_put(alg); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 291 | crypto_remove_final(&list); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 297 | struct nlattr **attrs) |
| 298 | { |
| 299 | struct crypto_alg *alg; |
| 300 | struct crypto_user_alg *p = nlmsg_data(nlh); |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 301 | int err; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 302 | |
Linus Torvalds | 639b4ac | 2014-06-07 19:44:40 -0700 | [diff] [blame] | 303 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Matthias-Christian Ott | c568398 | 2014-05-08 21:58:12 +0800 | [diff] [blame] | 304 | return -EPERM; |
| 305 | |
Mathias Krause | 8fd61d3 | 2013-02-05 18:19:15 +0100 | [diff] [blame] | 306 | if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name)) |
| 307 | return -EINVAL; |
| 308 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 309 | alg = crypto_alg_match(p, 1); |
| 310 | if (!alg) |
| 311 | return -ENOENT; |
| 312 | |
| 313 | /* We can not unregister core algorithms such as aes-generic. |
| 314 | * We would loose the reference in the crypto_alg_list to this algorithm |
| 315 | * if we try to unregister. Unregistering such an algorithm without |
| 316 | * removing the module is not possible, so we restrict to crypto |
| 317 | * instances that are build from templates. */ |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 318 | err = -EINVAL; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 319 | if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE)) |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 320 | goto drop_alg; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 321 | |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 322 | err = -EBUSY; |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 323 | if (refcount_read(&alg->cra_refcnt) > 2) |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 324 | goto drop_alg; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 325 | |
Eric Biggers | c6d633a | 2019-12-15 15:51:19 -0800 | [diff] [blame] | 326 | crypto_unregister_instance((struct crypto_instance *)alg); |
| 327 | err = 0; |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 328 | |
| 329 | drop_alg: |
| 330 | crypto_mod_put(alg); |
| 331 | return err; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 335 | struct nlattr **attrs) |
| 336 | { |
Jesper Juhl | 0cfdec7 | 2012-01-29 23:39:22 +0100 | [diff] [blame] | 337 | int exact = 0; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 338 | const char *name; |
| 339 | struct crypto_alg *alg; |
| 340 | struct crypto_user_alg *p = nlmsg_data(nlh); |
| 341 | struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL]; |
| 342 | |
Linus Torvalds | 639b4ac | 2014-06-07 19:44:40 -0700 | [diff] [blame] | 343 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Matthias-Christian Ott | c568398 | 2014-05-08 21:58:12 +0800 | [diff] [blame] | 344 | return -EPERM; |
| 345 | |
Mathias Krause | 8fd61d3 | 2013-02-05 18:19:15 +0100 | [diff] [blame] | 346 | if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name)) |
| 347 | return -EINVAL; |
| 348 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 349 | if (strlen(p->cru_driver_name)) |
| 350 | exact = 1; |
| 351 | |
| 352 | if (priority && !exact) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | alg = crypto_alg_match(p, exact); |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 356 | if (alg) { |
| 357 | crypto_mod_put(alg); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 358 | return -EEXIST; |
Herbert Xu | 016baaa | 2015-04-07 21:27:01 +0800 | [diff] [blame] | 359 | } |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 360 | |
| 361 | if (strlen(p->cru_driver_name)) |
| 362 | name = p->cru_driver_name; |
| 363 | else |
| 364 | name = p->cru_name; |
| 365 | |
Herbert Xu | 6cf80a2 | 2016-07-12 13:17:49 +0800 | [diff] [blame] | 366 | alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 367 | if (IS_ERR(alg)) |
| 368 | return PTR_ERR(alg); |
| 369 | |
| 370 | down_write(&crypto_alg_sem); |
| 371 | |
| 372 | if (priority) |
| 373 | alg->cra_priority = nla_get_u32(priority); |
| 374 | |
| 375 | up_write(&crypto_alg_sem); |
| 376 | |
| 377 | crypto_mod_put(alg); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Herbert Xu | 9aa867e | 2015-06-21 19:11:45 +0800 | [diff] [blame] | 382 | static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 383 | struct nlattr **attrs) |
| 384 | { |
| 385 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
| 386 | return -EPERM; |
| 387 | return crypto_del_default_rng(); |
| 388 | } |
| 389 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 390 | #define MSGSIZE(type) sizeof(struct type) |
| 391 | |
| 392 | static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = { |
| 393 | [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg), |
| 394 | [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg), |
| 395 | [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg), |
Mathias Krause | 055ddaa | 2016-06-22 20:29:37 +0200 | [diff] [blame] | 396 | [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg), |
Herbert Xu | 9aa867e | 2015-06-21 19:11:45 +0800 | [diff] [blame] | 397 | [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0, |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 398 | [CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg), |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = { |
| 402 | [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32}, |
| 403 | }; |
| 404 | |
| 405 | #undef MSGSIZE |
| 406 | |
Mathias Krause | a84fb79 | 2013-02-24 14:09:12 +0100 | [diff] [blame] | 407 | static const struct crypto_link { |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 408 | int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); |
| 409 | int (*dump)(struct sk_buff *, struct netlink_callback *); |
| 410 | int (*done)(struct netlink_callback *); |
| 411 | } crypto_dispatch[CRYPTO_NR_MSGTYPES] = { |
| 412 | [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg}, |
| 413 | [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg}, |
| 414 | [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg}, |
| 415 | [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report, |
| 416 | .dump = crypto_dump_report, |
| 417 | .done = crypto_dump_report_done}, |
Herbert Xu | 9aa867e | 2015-06-21 19:11:45 +0800 | [diff] [blame] | 418 | [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng }, |
Corentin Labbe | 0c99c2a | 2018-12-13 08:36:37 +0000 | [diff] [blame] | 419 | [CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = { .doit = crypto_reportstat}, |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 420 | }; |
| 421 | |
Johannes Berg | 2d4bc93 | 2017-04-12 14:34:04 +0200 | [diff] [blame] | 422 | static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 423 | struct netlink_ext_ack *extack) |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 424 | { |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 425 | struct net *net = sock_net(skb->sk); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 426 | struct nlattr *attrs[CRYPTOCFGA_MAX+1]; |
Mathias Krause | a84fb79 | 2013-02-24 14:09:12 +0100 | [diff] [blame] | 427 | const struct crypto_link *link; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 428 | int type, err; |
| 429 | |
| 430 | type = nlh->nlmsg_type; |
| 431 | if (type > CRYPTO_MSG_MAX) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | type -= CRYPTO_MSG_BASE; |
| 435 | link = &crypto_dispatch[type]; |
| 436 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 437 | if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) && |
| 438 | (nlh->nlmsg_flags & NLM_F_DUMP))) { |
Steffen Klassert | 5219a53 | 2012-03-29 09:04:46 +0200 | [diff] [blame] | 439 | struct crypto_alg *alg; |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 440 | unsigned long dump_alloc = 0; |
Steffen Klassert | 5219a53 | 2012-03-29 09:04:46 +0200 | [diff] [blame] | 441 | |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 442 | if (link->dump == NULL) |
| 443 | return -EINVAL; |
Steffen Klassert | 5219a53 | 2012-03-29 09:04:46 +0200 | [diff] [blame] | 444 | |
Mathias Krause | 63e41eb | 2016-02-01 14:27:30 +0100 | [diff] [blame] | 445 | down_read(&crypto_alg_sem); |
Steffen Klassert | 5219a53 | 2012-03-29 09:04:46 +0200 | [diff] [blame] | 446 | list_for_each_entry(alg, &crypto_alg_list, cra_list) |
| 447 | dump_alloc += CRYPTO_REPORT_MAXSIZE; |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 448 | up_read(&crypto_alg_sem); |
Steffen Klassert | 5219a53 | 2012-03-29 09:04:46 +0200 | [diff] [blame] | 449 | |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 450 | { |
| 451 | struct netlink_dump_control c = { |
| 452 | .dump = link->dump, |
| 453 | .done = link->done, |
Eric Biggers | 0ac6b8f | 2018-12-06 15:55:41 -0800 | [diff] [blame] | 454 | .min_dump_alloc = min(dump_alloc, 65535UL), |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 455 | }; |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 456 | err = netlink_dump_start(net->crypto_nlsk, skb, nlh, &c); |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 457 | } |
Mathias Krause | 63e41eb | 2016-02-01 14:27:30 +0100 | [diff] [blame] | 458 | |
| 459 | return err; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 462 | err = nlmsg_parse_deprecated(nlh, crypto_msg_min[type], attrs, |
| 463 | CRYPTOCFGA_MAX, crypto_policy, extack); |
Herbert Xu | fd2efd9 | 2016-06-23 18:06:02 +0800 | [diff] [blame] | 464 | if (err < 0) |
| 465 | return err; |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 466 | |
| 467 | if (link->doit == NULL) |
| 468 | return -EINVAL; |
| 469 | |
| 470 | return link->doit(skb, nlh, attrs); |
| 471 | } |
| 472 | |
| 473 | static void crypto_netlink_rcv(struct sk_buff *skb) |
| 474 | { |
| 475 | mutex_lock(&crypto_cfg_mutex); |
| 476 | netlink_rcv_skb(skb, &crypto_user_rcv_msg); |
| 477 | mutex_unlock(&crypto_cfg_mutex); |
| 478 | } |
| 479 | |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 480 | static int __net_init crypto_netlink_init(struct net *net) |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 481 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 482 | struct netlink_kernel_cfg cfg = { |
| 483 | .input = crypto_netlink_rcv, |
| 484 | }; |
| 485 | |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 486 | net->crypto_nlsk = netlink_kernel_create(net, NETLINK_CRYPTO, &cfg); |
| 487 | return net->crypto_nlsk == NULL ? -ENOMEM : 0; |
| 488 | } |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 489 | |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 490 | static void __net_exit crypto_netlink_exit(struct net *net) |
| 491 | { |
| 492 | netlink_kernel_release(net->crypto_nlsk); |
| 493 | net->crypto_nlsk = NULL; |
| 494 | } |
| 495 | |
| 496 | static struct pernet_operations crypto_netlink_net_ops = { |
| 497 | .init = crypto_netlink_init, |
| 498 | .exit = crypto_netlink_exit, |
| 499 | }; |
| 500 | |
| 501 | static int __init crypto_user_init(void) |
| 502 | { |
| 503 | return register_pernet_subsys(&crypto_netlink_net_ops); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static void __exit crypto_user_exit(void) |
| 507 | { |
Ondrej Mosnacek | 91b05a7 | 2019-07-09 13:11:24 +0200 | [diff] [blame] | 508 | unregister_pernet_subsys(&crypto_netlink_net_ops); |
Steffen Klassert | a38f790 | 2011-09-27 07:23:50 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | module_init(crypto_user_init); |
| 512 | module_exit(crypto_user_exit); |
| 513 | MODULE_LICENSE("GPL"); |
| 514 | MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); |
| 515 | MODULE_DESCRIPTION("Crypto userspace configuration API"); |
Stephan Mueller | 476c7fe | 2014-11-24 17:12:45 +0100 | [diff] [blame] | 516 | MODULE_ALIAS("net-pf-16-proto-21"); |