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