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