]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - crypto/crypto_user_stat.c
Merge tag 'block-6.9-20240329' of git://git.kernel.dk/linux
[thirdparty/kernel/linux.git] / crypto / crypto_user_stat.c
CommitLineData
cac5818c
CL
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Crypto user configuration API.
4 *
5 * Copyright (C) 2017-2018 Corentin Labbe <clabbe@baylibre.com>
6 *
7 */
8
9807e49b
HX
9#include <crypto/algapi.h>
10#include <crypto/internal/cryptouser.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/string.h>
cac5818c 15#include <net/netlink.h>
91b05a7e 16#include <net/sock.h>
cac5818c
CL
17
18#define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
19
cac5818c
CL
20struct crypto_dump_info {
21 struct sk_buff *in_skb;
22 struct sk_buff *out_skb;
23 u32 nlmsg_seq;
24 u16 nlmsg_flags;
25};
26
cac5818c
CL
27static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
28{
7f0a9d5c 29 struct crypto_stat_cipher rcipher;
cac5818c 30
9f4debe3
CL
31 memset(&rcipher, 0, sizeof(rcipher));
32
37db69e0 33 strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
cac5818c 34
37db69e0 35 return nla_put(skb, CRYPTOCFGA_STAT_CIPHER, sizeof(rcipher), &rcipher);
cac5818c
CL
36}
37
38static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
39{
7f0a9d5c 40 struct crypto_stat_compress rcomp;
cac5818c 41
9f4debe3
CL
42 memset(&rcomp, 0, sizeof(rcomp));
43
37db69e0 44 strscpy(rcomp.type, "compression", sizeof(rcomp.type));
cac5818c 45
37db69e0 46 return nla_put(skb, CRYPTOCFGA_STAT_COMPRESS, sizeof(rcomp), &rcomp);
cac5818c
CL
47}
48
cac5818c
CL
49static int crypto_reportstat_one(struct crypto_alg *alg,
50 struct crypto_user_alg *ualg,
51 struct sk_buff *skb)
52{
9f4debe3
CL
53 memset(ualg, 0, sizeof(*ualg));
54
37db69e0
EB
55 strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
56 strscpy(ualg->cru_driver_name, alg->cra_driver_name,
cac5818c 57 sizeof(ualg->cru_driver_name));
37db69e0 58 strscpy(ualg->cru_module_name, module_name(alg->cra_module),
cac5818c
CL
59 sizeof(ualg->cru_module_name));
60
61 ualg->cru_type = 0;
62 ualg->cru_mask = 0;
63 ualg->cru_flags = alg->cra_flags;
64 ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
65
66 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
67 goto nla_put_failure;
68 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
7f0a9d5c 69 struct crypto_stat_larval rl;
cac5818c 70
9f4debe3 71 memset(&rl, 0, sizeof(rl));
37db69e0
EB
72 strscpy(rl.type, "larval", sizeof(rl.type));
73 if (nla_put(skb, CRYPTOCFGA_STAT_LARVAL, sizeof(rl), &rl))
cac5818c
CL
74 goto nla_put_failure;
75 goto out;
76 }
77
ed0733ea
HX
78 if (alg->cra_type && alg->cra_type->report_stat) {
79 if (alg->cra_type->report_stat(skb, alg))
80 goto nla_put_failure;
81 goto out;
82 }
83
cac5818c 84 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
cac5818c
CL
85 case CRYPTO_ALG_TYPE_CIPHER:
86 if (crypto_report_cipher(skb, alg))
87 goto nla_put_failure;
88 break;
89 case CRYPTO_ALG_TYPE_COMPRESS:
90 if (crypto_report_comp(skb, alg))
91 goto nla_put_failure;
92 break;
cac5818c
CL
93 default:
94 pr_err("ERROR: Unhandled alg %d in %s\n",
95 alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL),
96 __func__);
97 }
98
99out:
100 return 0;
101
102nla_put_failure:
103 return -EMSGSIZE;
104}
105
106static int crypto_reportstat_alg(struct crypto_alg *alg,
107 struct crypto_dump_info *info)
108{
109 struct sk_buff *in_skb = info->in_skb;
110 struct sk_buff *skb = info->out_skb;
111 struct nlmsghdr *nlh;
112 struct crypto_user_alg *ualg;
113 int err = 0;
114
115 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
116 CRYPTO_MSG_GETSTAT, sizeof(*ualg), info->nlmsg_flags);
117 if (!nlh) {
118 err = -EMSGSIZE;
119 goto out;
120 }
121
122 ualg = nlmsg_data(nlh);
123
124 err = crypto_reportstat_one(alg, ualg, skb);
125 if (err) {
126 nlmsg_cancel(skb, nlh);
127 goto out;
128 }
129
130 nlmsg_end(skb, nlh);
131
132out:
133 return err;
134}
135
136int crypto_reportstat(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
137 struct nlattr **attrs)
138{
91b05a7e 139 struct net *net = sock_net(in_skb->sk);
cac5818c
CL
140 struct crypto_user_alg *p = nlmsg_data(in_nlh);
141 struct crypto_alg *alg;
142 struct sk_buff *skb;
143 struct crypto_dump_info info;
144 int err;
145
146 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
147 return -EINVAL;
148
149 alg = crypto_alg_match(p, 0);
150 if (!alg)
151 return -ENOENT;
152
153 err = -ENOMEM;
154 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
155 if (!skb)
156 goto drop_alg;
157
158 info.in_skb = in_skb;
159 info.out_skb = skb;
160 info.nlmsg_seq = in_nlh->nlmsg_seq;
161 info.nlmsg_flags = 0;
162
163 err = crypto_reportstat_alg(alg, &info);
164
165drop_alg:
166 crypto_mod_put(alg);
167
c03b04dc
NE
168 if (err) {
169 kfree_skb(skb);
cac5818c 170 return err;
c03b04dc 171 }
cac5818c 172
91b05a7e 173 return nlmsg_unicast(net->crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
cac5818c
CL
174}
175
cac5818c 176MODULE_LICENSE("GPL");