1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * algif_hash: User-space interface for hash algorithms
4 *
5 * This file provides the user-space API for hash algorithms.
6 *
7 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 */
9
10 #include <crypto/hash.h>
11 #include <crypto/if_alg.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <net/sock.h>
18
19 struct hash_ctx {
20 struct af_alg_sgl sgl;
21
22 u8 *result;
23
24 struct crypto_wait wait;
25
26 unsigned int len;
27 bool more;
28
29 struct ahash_request req;
30 };
31
hash_alloc_result(struct sock * sk,struct hash_ctx * ctx)32 static int hash_alloc_result(struct sock *sk, struct hash_ctx *ctx)
33 {
34 unsigned ds;
35
36 if (ctx->result)
37 return 0;
38
39 ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
40
41 ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
42 if (!ctx->result)
43 return -ENOMEM;
44
45 memset(ctx->result, 0, ds);
46
47 return 0;
48 }
49
hash_free_result(struct sock * sk,struct hash_ctx * ctx)50 static void hash_free_result(struct sock *sk, struct hash_ctx *ctx)
51 {
52 unsigned ds;
53
54 if (!ctx->result)
55 return;
56
57 ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
58
59 sock_kzfree_s(sk, ctx->result, ds);
60 ctx->result = NULL;
61 }
62
hash_sendmsg(struct socket * sock,struct msghdr * msg,size_t ignored)63 static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
64 size_t ignored)
65 {
66 int limit = ALG_MAX_PAGES * PAGE_SIZE;
67 struct sock *sk = sock->sk;
68 struct alg_sock *ask = alg_sk(sk);
69 struct hash_ctx *ctx = ask->private;
70 long copied = 0;
71 int err;
72
73 if (limit > sk->sk_sndbuf)
74 limit = sk->sk_sndbuf;
75
76 lock_sock(sk);
77 if (!ctx->more) {
78 if ((msg->msg_flags & MSG_MORE))
79 hash_free_result(sk, ctx);
80
81 err = crypto_wait_req(crypto_ahash_init(&ctx->req), &ctx->wait);
82 if (err)
83 goto unlock;
84 }
85
86 ctx->more = false;
87
88 while (msg_data_left(msg)) {
89 int len = msg_data_left(msg);
90
91 if (len > limit)
92 len = limit;
93
94 len = af_alg_make_sg(&ctx->sgl, &msg->msg_iter, len);
95 if (len < 0) {
96 err = copied ? 0 : len;
97 goto unlock;
98 }
99
100 ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL, len);
101
102 err = crypto_wait_req(crypto_ahash_update(&ctx->req),
103 &ctx->wait);
104 af_alg_free_sg(&ctx->sgl);
105 if (err) {
106 iov_iter_revert(&msg->msg_iter, len);
107 goto unlock;
108 }
109
110 copied += len;
111 }
112
113 err = 0;
114
115 ctx->more = msg->msg_flags & MSG_MORE;
116 if (!ctx->more) {
117 err = hash_alloc_result(sk, ctx);
118 if (err)
119 goto unlock;
120
121 ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
122 err = crypto_wait_req(crypto_ahash_final(&ctx->req),
123 &ctx->wait);
124 }
125
126 unlock:
127 release_sock(sk);
128
129 return err ?: copied;
130 }
131
hash_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)132 static ssize_t hash_sendpage(struct socket *sock, struct page *page,
133 int offset, size_t size, int flags)
134 {
135 struct sock *sk = sock->sk;
136 struct alg_sock *ask = alg_sk(sk);
137 struct hash_ctx *ctx = ask->private;
138 int err;
139
140 if (flags & MSG_SENDPAGE_NOTLAST)
141 flags |= MSG_MORE;
142
143 lock_sock(sk);
144 sg_init_table(ctx->sgl.sg, 1);
145 sg_set_page(ctx->sgl.sg, page, size, offset);
146
147 if (!(flags & MSG_MORE)) {
148 err = hash_alloc_result(sk, ctx);
149 if (err)
150 goto unlock;
151 } else if (!ctx->more)
152 hash_free_result(sk, ctx);
153
154 ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
155
156 if (!(flags & MSG_MORE)) {
157 if (ctx->more)
158 err = crypto_ahash_finup(&ctx->req);
159 else
160 err = crypto_ahash_digest(&ctx->req);
161 } else {
162 if (!ctx->more) {
163 err = crypto_ahash_init(&ctx->req);
164 err = crypto_wait_req(err, &ctx->wait);
165 if (err)
166 goto unlock;
167 }
168
169 err = crypto_ahash_update(&ctx->req);
170 }
171
172 err = crypto_wait_req(err, &ctx->wait);
173 if (err)
174 goto unlock;
175
176 ctx->more = flags & MSG_MORE;
177
178 unlock:
179 release_sock(sk);
180
181 return err ?: size;
182 }
183
hash_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)184 static int hash_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
185 int flags)
186 {
187 struct sock *sk = sock->sk;
188 struct alg_sock *ask = alg_sk(sk);
189 struct hash_ctx *ctx = ask->private;
190 unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
191 bool result;
192 int err;
193
194 if (len > ds)
195 len = ds;
196 else if (len < ds)
197 msg->msg_flags |= MSG_TRUNC;
198
199 lock_sock(sk);
200 result = ctx->result;
201 err = hash_alloc_result(sk, ctx);
202 if (err)
203 goto unlock;
204
205 ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
206
207 if (!result && !ctx->more) {
208 err = crypto_wait_req(crypto_ahash_init(&ctx->req),
209 &ctx->wait);
210 if (err)
211 goto unlock;
212 }
213
214 if (!result || ctx->more) {
215 ctx->more = false;
216 err = crypto_wait_req(crypto_ahash_final(&ctx->req),
217 &ctx->wait);
218 if (err)
219 goto unlock;
220 }
221
222 err = memcpy_to_msg(msg, ctx->result, len);
223
224 unlock:
225 hash_free_result(sk, ctx);
226 release_sock(sk);
227
228 return err ?: len;
229 }
230
hash_accept(struct socket * sock,struct socket * newsock,int flags,bool kern)231 static int hash_accept(struct socket *sock, struct socket *newsock, int flags,
232 bool kern)
233 {
234 struct sock *sk = sock->sk;
235 struct alg_sock *ask = alg_sk(sk);
236 struct hash_ctx *ctx = ask->private;
237 struct ahash_request *req = &ctx->req;
238 char state[HASH_MAX_STATESIZE];
239 struct sock *sk2;
240 struct alg_sock *ask2;
241 struct hash_ctx *ctx2;
242 bool more;
243 int err;
244
245 lock_sock(sk);
246 more = ctx->more;
247 err = more ? crypto_ahash_export(req, state) : 0;
248 release_sock(sk);
249
250 if (err)
251 return err;
252
253 err = af_alg_accept(ask->parent, newsock, kern);
254 if (err)
255 return err;
256
257 sk2 = newsock->sk;
258 ask2 = alg_sk(sk2);
259 ctx2 = ask2->private;
260 ctx2->more = more;
261
262 if (!more)
263 return err;
264
265 err = crypto_ahash_import(&ctx2->req, state);
266 if (err) {
267 sock_orphan(sk2);
268 sock_put(sk2);
269 }
270
271 return err;
272 }
273
274 static struct proto_ops algif_hash_ops = {
275 .family = PF_ALG,
276
277 .connect = sock_no_connect,
278 .socketpair = sock_no_socketpair,
279 .getname = sock_no_getname,
280 .ioctl = sock_no_ioctl,
281 .listen = sock_no_listen,
282 .shutdown = sock_no_shutdown,
283 .mmap = sock_no_mmap,
284 .bind = sock_no_bind,
285
286 .release = af_alg_release,
287 .sendmsg = hash_sendmsg,
288 .sendpage = hash_sendpage,
289 .recvmsg = hash_recvmsg,
290 .accept = hash_accept,
291 };
292
hash_check_key(struct socket * sock)293 static int hash_check_key(struct socket *sock)
294 {
295 int err = 0;
296 struct sock *psk;
297 struct alg_sock *pask;
298 struct crypto_ahash *tfm;
299 struct sock *sk = sock->sk;
300 struct alg_sock *ask = alg_sk(sk);
301
302 lock_sock(sk);
303 if (!atomic_read(&ask->nokey_refcnt))
304 goto unlock_child;
305
306 psk = ask->parent;
307 pask = alg_sk(ask->parent);
308 tfm = pask->private;
309
310 err = -ENOKEY;
311 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
312 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
313 goto unlock;
314
315 atomic_dec(&pask->nokey_refcnt);
316 atomic_set(&ask->nokey_refcnt, 0);
317
318 err = 0;
319
320 unlock:
321 release_sock(psk);
322 unlock_child:
323 release_sock(sk);
324
325 return err;
326 }
327
hash_sendmsg_nokey(struct socket * sock,struct msghdr * msg,size_t size)328 static int hash_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
329 size_t size)
330 {
331 int err;
332
333 err = hash_check_key(sock);
334 if (err)
335 return err;
336
337 return hash_sendmsg(sock, msg, size);
338 }
339
hash_sendpage_nokey(struct socket * sock,struct page * page,int offset,size_t size,int flags)340 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
341 int offset, size_t size, int flags)
342 {
343 int err;
344
345 err = hash_check_key(sock);
346 if (err)
347 return err;
348
349 return hash_sendpage(sock, page, offset, size, flags);
350 }
351
hash_recvmsg_nokey(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)352 static int hash_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
353 size_t ignored, int flags)
354 {
355 int err;
356
357 err = hash_check_key(sock);
358 if (err)
359 return err;
360
361 return hash_recvmsg(sock, msg, ignored, flags);
362 }
363
hash_accept_nokey(struct socket * sock,struct socket * newsock,int flags,bool kern)364 static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
365 int flags, bool kern)
366 {
367 int err;
368
369 err = hash_check_key(sock);
370 if (err)
371 return err;
372
373 return hash_accept(sock, newsock, flags, kern);
374 }
375
376 static struct proto_ops algif_hash_ops_nokey = {
377 .family = PF_ALG,
378
379 .connect = sock_no_connect,
380 .socketpair = sock_no_socketpair,
381 .getname = sock_no_getname,
382 .ioctl = sock_no_ioctl,
383 .listen = sock_no_listen,
384 .shutdown = sock_no_shutdown,
385 .mmap = sock_no_mmap,
386 .bind = sock_no_bind,
387
388 .release = af_alg_release,
389 .sendmsg = hash_sendmsg_nokey,
390 .sendpage = hash_sendpage_nokey,
391 .recvmsg = hash_recvmsg_nokey,
392 .accept = hash_accept_nokey,
393 };
394
hash_bind(const char * name,u32 type,u32 mask)395 static void *hash_bind(const char *name, u32 type, u32 mask)
396 {
397 return crypto_alloc_ahash(name, type, mask);
398 }
399
hash_release(void * private)400 static void hash_release(void *private)
401 {
402 crypto_free_ahash(private);
403 }
404
hash_setkey(void * private,const u8 * key,unsigned int keylen)405 static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
406 {
407 return crypto_ahash_setkey(private, key, keylen);
408 }
409
hash_sock_destruct(struct sock * sk)410 static void hash_sock_destruct(struct sock *sk)
411 {
412 struct alg_sock *ask = alg_sk(sk);
413 struct hash_ctx *ctx = ask->private;
414
415 hash_free_result(sk, ctx);
416 sock_kfree_s(sk, ctx, ctx->len);
417 af_alg_release_parent(sk);
418 }
419
hash_accept_parent_nokey(void * private,struct sock * sk)420 static int hash_accept_parent_nokey(void *private, struct sock *sk)
421 {
422 struct crypto_ahash *tfm = private;
423 struct alg_sock *ask = alg_sk(sk);
424 struct hash_ctx *ctx;
425 unsigned int len = sizeof(*ctx) + crypto_ahash_reqsize(tfm);
426
427 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
428 if (!ctx)
429 return -ENOMEM;
430
431 ctx->result = NULL;
432 ctx->len = len;
433 ctx->more = false;
434 crypto_init_wait(&ctx->wait);
435
436 ask->private = ctx;
437
438 ahash_request_set_tfm(&ctx->req, tfm);
439 ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
440 crypto_req_done, &ctx->wait);
441
442 sk->sk_destruct = hash_sock_destruct;
443
444 return 0;
445 }
446
hash_accept_parent(void * private,struct sock * sk)447 static int hash_accept_parent(void *private, struct sock *sk)
448 {
449 struct crypto_ahash *tfm = private;
450
451 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
452 return -ENOKEY;
453
454 return hash_accept_parent_nokey(private, sk);
455 }
456
457 static const struct af_alg_type algif_type_hash = {
458 .bind = hash_bind,
459 .release = hash_release,
460 .setkey = hash_setkey,
461 .accept = hash_accept_parent,
462 .accept_nokey = hash_accept_parent_nokey,
463 .ops = &algif_hash_ops,
464 .ops_nokey = &algif_hash_ops_nokey,
465 .name = "hash",
466 .owner = THIS_MODULE
467 };
468
algif_hash_init(void)469 static int __init algif_hash_init(void)
470 {
471 return af_alg_register_type(&algif_type_hash);
472 }
473
algif_hash_exit(void)474 static void __exit algif_hash_exit(void)
475 {
476 int err = af_alg_unregister_type(&algif_type_hash);
477 BUG_ON(err);
478 }
479
480 module_init(algif_hash_init);
481 module_exit(algif_hash_exit);
482 MODULE_LICENSE("GPL");
483