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