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 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 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 63 static int hash_sendmsg(struct socket *sock, struct msghdr *msg, 64 size_t ignored) 65 { 66 struct sock *sk = sock->sk; 67 struct alg_sock *ask = alg_sk(sk); 68 struct hash_ctx *ctx = ask->private; 69 ssize_t copied = 0; 70 size_t len, max_pages, npages; 71 bool continuing = ctx->more, need_init = false; 72 int err; 73 74 max_pages = min_t(size_t, ALG_MAX_PAGES, 75 DIV_ROUND_UP(sk->sk_sndbuf, PAGE_SIZE)); 76 77 lock_sock(sk); 78 if (!continuing) { 79 if ((msg->msg_flags & MSG_MORE)) 80 hash_free_result(sk, ctx); 81 need_init = true; 82 } 83 84 ctx->more = false; 85 86 while (msg_data_left(msg)) { 87 ctx->sgl.sgt.sgl = ctx->sgl.sgl; 88 ctx->sgl.sgt.nents = 0; 89 ctx->sgl.sgt.orig_nents = 0; 90 91 err = -EIO; 92 npages = iov_iter_npages(&msg->msg_iter, max_pages); 93 if (npages == 0) 94 goto unlock_free; 95 96 if (npages > ARRAY_SIZE(ctx->sgl.sgl)) { 97 err = -ENOMEM; 98 ctx->sgl.sgt.sgl = 99 kvmalloc(array_size(npages, 100 sizeof(*ctx->sgl.sgt.sgl)), 101 GFP_KERNEL); 102 if (!ctx->sgl.sgt.sgl) 103 goto unlock_free; 104 } 105 sg_init_table(ctx->sgl.sgl, npages); 106 107 ctx->sgl.need_unpin = iov_iter_extract_will_pin(&msg->msg_iter); 108 109 err = extract_iter_to_sg(&msg->msg_iter, LONG_MAX, 110 &ctx->sgl.sgt, npages, 0); 111 if (err < 0) 112 goto unlock_free; 113 len = err; 114 sg_mark_end(ctx->sgl.sgt.sgl + ctx->sgl.sgt.nents - 1); 115 116 if (!msg_data_left(msg)) { 117 err = hash_alloc_result(sk, ctx); 118 if (err) 119 goto unlock_free; 120 } 121 122 ahash_request_set_crypt(&ctx->req, ctx->sgl.sgt.sgl, 123 ctx->result, len); 124 125 if (!msg_data_left(msg) && !continuing && 126 !(msg->msg_flags & MSG_MORE)) { 127 err = crypto_ahash_digest(&ctx->req); 128 } else { 129 if (need_init) { 130 err = crypto_wait_req( 131 crypto_ahash_init(&ctx->req), 132 &ctx->wait); 133 if (err) 134 goto unlock_free; 135 need_init = false; 136 } 137 138 if (msg_data_left(msg) || (msg->msg_flags & MSG_MORE)) 139 err = crypto_ahash_update(&ctx->req); 140 else 141 err = crypto_ahash_finup(&ctx->req); 142 continuing = true; 143 } 144 145 err = crypto_wait_req(err, &ctx->wait); 146 if (err) 147 goto unlock_free; 148 149 copied += len; 150 af_alg_free_sg(&ctx->sgl); 151 } 152 153 ctx->more = msg->msg_flags & MSG_MORE; 154 err = 0; 155 unlock: 156 release_sock(sk); 157 return copied ?: err; 158 159 unlock_free: 160 af_alg_free_sg(&ctx->sgl); 161 goto unlock; 162 } 163 164 static ssize_t hash_sendpage(struct socket *sock, struct page *page, 165 int offset, size_t size, int flags) 166 { 167 struct sock *sk = sock->sk; 168 struct alg_sock *ask = alg_sk(sk); 169 struct hash_ctx *ctx = ask->private; 170 int err; 171 172 if (flags & MSG_SENDPAGE_NOTLAST) 173 flags |= MSG_MORE; 174 175 lock_sock(sk); 176 sg_init_table(ctx->sgl.sgl, 1); 177 sg_set_page(ctx->sgl.sgl, page, size, offset); 178 179 if (!(flags & MSG_MORE)) { 180 err = hash_alloc_result(sk, ctx); 181 if (err) 182 goto unlock; 183 } else if (!ctx->more) 184 hash_free_result(sk, ctx); 185 186 ahash_request_set_crypt(&ctx->req, ctx->sgl.sgl, ctx->result, size); 187 188 if (!(flags & MSG_MORE)) { 189 if (ctx->more) 190 err = crypto_ahash_finup(&ctx->req); 191 else 192 err = crypto_ahash_digest(&ctx->req); 193 } else { 194 if (!ctx->more) { 195 err = crypto_ahash_init(&ctx->req); 196 err = crypto_wait_req(err, &ctx->wait); 197 if (err) 198 goto unlock; 199 } 200 201 err = crypto_ahash_update(&ctx->req); 202 } 203 204 err = crypto_wait_req(err, &ctx->wait); 205 if (err) 206 goto unlock; 207 208 ctx->more = flags & MSG_MORE; 209 210 unlock: 211 release_sock(sk); 212 213 return err ?: size; 214 } 215 216 static int hash_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 217 int flags) 218 { 219 struct sock *sk = sock->sk; 220 struct alg_sock *ask = alg_sk(sk); 221 struct hash_ctx *ctx = ask->private; 222 unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req)); 223 bool result; 224 int err; 225 226 if (len > ds) 227 len = ds; 228 else if (len < ds) 229 msg->msg_flags |= MSG_TRUNC; 230 231 lock_sock(sk); 232 result = ctx->result; 233 err = hash_alloc_result(sk, ctx); 234 if (err) 235 goto unlock; 236 237 ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0); 238 239 if (!result && !ctx->more) { 240 err = crypto_wait_req(crypto_ahash_init(&ctx->req), 241 &ctx->wait); 242 if (err) 243 goto unlock; 244 } 245 246 if (!result || ctx->more) { 247 ctx->more = false; 248 err = crypto_wait_req(crypto_ahash_final(&ctx->req), 249 &ctx->wait); 250 if (err) 251 goto unlock; 252 } 253 254 err = memcpy_to_msg(msg, ctx->result, len); 255 256 unlock: 257 hash_free_result(sk, ctx); 258 release_sock(sk); 259 260 return err ?: len; 261 } 262 263 static int hash_accept(struct socket *sock, struct socket *newsock, int flags, 264 bool kern) 265 { 266 struct sock *sk = sock->sk; 267 struct alg_sock *ask = alg_sk(sk); 268 struct hash_ctx *ctx = ask->private; 269 struct ahash_request *req = &ctx->req; 270 struct crypto_ahash *tfm; 271 struct sock *sk2; 272 struct alg_sock *ask2; 273 struct hash_ctx *ctx2; 274 char *state; 275 bool more; 276 int err; 277 278 tfm = crypto_ahash_reqtfm(req); 279 state = kmalloc(crypto_ahash_statesize(tfm), GFP_KERNEL); 280 err = -ENOMEM; 281 if (!state) 282 goto out; 283 284 lock_sock(sk); 285 more = ctx->more; 286 err = more ? crypto_ahash_export(req, state) : 0; 287 release_sock(sk); 288 289 if (err) 290 goto out_free_state; 291 292 err = af_alg_accept(ask->parent, newsock, kern); 293 if (err) 294 goto out_free_state; 295 296 sk2 = newsock->sk; 297 ask2 = alg_sk(sk2); 298 ctx2 = ask2->private; 299 ctx2->more = more; 300 301 if (!more) 302 goto out_free_state; 303 304 err = crypto_ahash_import(&ctx2->req, state); 305 if (err) { 306 sock_orphan(sk2); 307 sock_put(sk2); 308 } 309 310 out_free_state: 311 kfree_sensitive(state); 312 313 out: 314 return err; 315 } 316 317 static struct proto_ops algif_hash_ops = { 318 .family = PF_ALG, 319 320 .connect = sock_no_connect, 321 .socketpair = sock_no_socketpair, 322 .getname = sock_no_getname, 323 .ioctl = sock_no_ioctl, 324 .listen = sock_no_listen, 325 .shutdown = sock_no_shutdown, 326 .mmap = sock_no_mmap, 327 .bind = sock_no_bind, 328 329 .release = af_alg_release, 330 .sendmsg = hash_sendmsg, 331 .sendpage = hash_sendpage, 332 .recvmsg = hash_recvmsg, 333 .accept = hash_accept, 334 }; 335 336 static int hash_check_key(struct socket *sock) 337 { 338 int err = 0; 339 struct sock *psk; 340 struct alg_sock *pask; 341 struct crypto_ahash *tfm; 342 struct sock *sk = sock->sk; 343 struct alg_sock *ask = alg_sk(sk); 344 345 lock_sock(sk); 346 if (!atomic_read(&ask->nokey_refcnt)) 347 goto unlock_child; 348 349 psk = ask->parent; 350 pask = alg_sk(ask->parent); 351 tfm = pask->private; 352 353 err = -ENOKEY; 354 lock_sock_nested(psk, SINGLE_DEPTH_NESTING); 355 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) 356 goto unlock; 357 358 atomic_dec(&pask->nokey_refcnt); 359 atomic_set(&ask->nokey_refcnt, 0); 360 361 err = 0; 362 363 unlock: 364 release_sock(psk); 365 unlock_child: 366 release_sock(sk); 367 368 return err; 369 } 370 371 static int hash_sendmsg_nokey(struct socket *sock, struct msghdr *msg, 372 size_t size) 373 { 374 int err; 375 376 err = hash_check_key(sock); 377 if (err) 378 return err; 379 380 return hash_sendmsg(sock, msg, size); 381 } 382 383 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page, 384 int offset, size_t size, int flags) 385 { 386 int err; 387 388 err = hash_check_key(sock); 389 if (err) 390 return err; 391 392 return hash_sendpage(sock, page, offset, size, flags); 393 } 394 395 static int hash_recvmsg_nokey(struct socket *sock, struct msghdr *msg, 396 size_t ignored, int flags) 397 { 398 int err; 399 400 err = hash_check_key(sock); 401 if (err) 402 return err; 403 404 return hash_recvmsg(sock, msg, ignored, flags); 405 } 406 407 static int hash_accept_nokey(struct socket *sock, struct socket *newsock, 408 int flags, bool kern) 409 { 410 int err; 411 412 err = hash_check_key(sock); 413 if (err) 414 return err; 415 416 return hash_accept(sock, newsock, flags, kern); 417 } 418 419 static struct proto_ops algif_hash_ops_nokey = { 420 .family = PF_ALG, 421 422 .connect = sock_no_connect, 423 .socketpair = sock_no_socketpair, 424 .getname = sock_no_getname, 425 .ioctl = sock_no_ioctl, 426 .listen = sock_no_listen, 427 .shutdown = sock_no_shutdown, 428 .mmap = sock_no_mmap, 429 .bind = sock_no_bind, 430 431 .release = af_alg_release, 432 .sendmsg = hash_sendmsg_nokey, 433 .sendpage = hash_sendpage_nokey, 434 .recvmsg = hash_recvmsg_nokey, 435 .accept = hash_accept_nokey, 436 }; 437 438 static void *hash_bind(const char *name, u32 type, u32 mask) 439 { 440 return crypto_alloc_ahash(name, type, mask); 441 } 442 443 static void hash_release(void *private) 444 { 445 crypto_free_ahash(private); 446 } 447 448 static int hash_setkey(void *private, const u8 *key, unsigned int keylen) 449 { 450 return crypto_ahash_setkey(private, key, keylen); 451 } 452 453 static void hash_sock_destruct(struct sock *sk) 454 { 455 struct alg_sock *ask = alg_sk(sk); 456 struct hash_ctx *ctx = ask->private; 457 458 hash_free_result(sk, ctx); 459 sock_kfree_s(sk, ctx, ctx->len); 460 af_alg_release_parent(sk); 461 } 462 463 static int hash_accept_parent_nokey(void *private, struct sock *sk) 464 { 465 struct crypto_ahash *tfm = private; 466 struct alg_sock *ask = alg_sk(sk); 467 struct hash_ctx *ctx; 468 unsigned int len = sizeof(*ctx) + crypto_ahash_reqsize(tfm); 469 470 ctx = sock_kmalloc(sk, len, GFP_KERNEL); 471 if (!ctx) 472 return -ENOMEM; 473 474 ctx->result = NULL; 475 ctx->len = len; 476 ctx->more = false; 477 crypto_init_wait(&ctx->wait); 478 479 ask->private = ctx; 480 481 ahash_request_set_tfm(&ctx->req, tfm); 482 ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG, 483 crypto_req_done, &ctx->wait); 484 485 sk->sk_destruct = hash_sock_destruct; 486 487 return 0; 488 } 489 490 static int hash_accept_parent(void *private, struct sock *sk) 491 { 492 struct crypto_ahash *tfm = private; 493 494 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) 495 return -ENOKEY; 496 497 return hash_accept_parent_nokey(private, sk); 498 } 499 500 static const struct af_alg_type algif_type_hash = { 501 .bind = hash_bind, 502 .release = hash_release, 503 .setkey = hash_setkey, 504 .accept = hash_accept_parent, 505 .accept_nokey = hash_accept_parent_nokey, 506 .ops = &algif_hash_ops, 507 .ops_nokey = &algif_hash_ops_nokey, 508 .name = "hash", 509 .owner = THIS_MODULE 510 }; 511 512 static int __init algif_hash_init(void) 513 { 514 return af_alg_register_type(&algif_type_hash); 515 } 516 517 static void __exit algif_hash_exit(void) 518 { 519 int err = af_alg_unregister_type(&algif_type_hash); 520 BUG_ON(err); 521 } 522 523 module_init(algif_hash_init); 524 module_exit(algif_hash_exit); 525 MODULE_LICENSE("GPL"); 526