Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 1 | /* |
| 2 | * af_alg: User-space algorithm interface |
| 3 | * |
| 4 | * This file provides the user-space API for algorithms. |
| 5 | * |
| 6 | * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 15 | #include <linux/atomic.h> |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 16 | #include <crypto/if_alg.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/net.h> |
| 23 | #include <linux/rwsem.h> |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 24 | #include <linux/sched/signal.h> |
Milan Broz | 4c63f83 | 2014-07-29 18:41:09 +0000 | [diff] [blame] | 25 | #include <linux/security.h> |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 26 | |
| 27 | struct alg_type_list { |
| 28 | const struct af_alg_type *type; |
| 29 | struct list_head list; |
| 30 | }; |
| 31 | |
Randy Dunlap | 0686952 | 2010-12-21 22:22:40 +1100 | [diff] [blame] | 32 | static atomic_long_t alg_memory_allocated; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 33 | |
| 34 | static struct proto alg_proto = { |
| 35 | .name = "ALG", |
| 36 | .owner = THIS_MODULE, |
| 37 | .memory_allocated = &alg_memory_allocated, |
| 38 | .obj_size = sizeof(struct alg_sock), |
| 39 | }; |
| 40 | |
| 41 | static LIST_HEAD(alg_types); |
| 42 | static DECLARE_RWSEM(alg_types_sem); |
| 43 | |
| 44 | static const struct af_alg_type *alg_get_type(const char *name) |
| 45 | { |
| 46 | const struct af_alg_type *type = ERR_PTR(-ENOENT); |
| 47 | struct alg_type_list *node; |
| 48 | |
| 49 | down_read(&alg_types_sem); |
| 50 | list_for_each_entry(node, &alg_types, list) { |
| 51 | if (strcmp(node->type->name, name)) |
| 52 | continue; |
| 53 | |
| 54 | if (try_module_get(node->type->owner)) |
| 55 | type = node->type; |
| 56 | break; |
| 57 | } |
| 58 | up_read(&alg_types_sem); |
| 59 | |
| 60 | return type; |
| 61 | } |
| 62 | |
| 63 | int af_alg_register_type(const struct af_alg_type *type) |
| 64 | { |
| 65 | struct alg_type_list *node; |
| 66 | int err = -EEXIST; |
| 67 | |
| 68 | down_write(&alg_types_sem); |
| 69 | list_for_each_entry(node, &alg_types, list) { |
| 70 | if (!strcmp(node->type->name, type->name)) |
| 71 | goto unlock; |
| 72 | } |
| 73 | |
| 74 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 75 | err = -ENOMEM; |
| 76 | if (!node) |
| 77 | goto unlock; |
| 78 | |
| 79 | type->ops->owner = THIS_MODULE; |
Herbert Xu | 3776658 | 2016-01-04 13:35:18 +0900 | [diff] [blame] | 80 | if (type->ops_nokey) |
| 81 | type->ops_nokey->owner = THIS_MODULE; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 82 | node->type = type; |
| 83 | list_add(&node->list, &alg_types); |
| 84 | err = 0; |
| 85 | |
| 86 | unlock: |
| 87 | up_write(&alg_types_sem); |
| 88 | |
| 89 | return err; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(af_alg_register_type); |
| 92 | |
| 93 | int af_alg_unregister_type(const struct af_alg_type *type) |
| 94 | { |
| 95 | struct alg_type_list *node; |
| 96 | int err = -ENOENT; |
| 97 | |
| 98 | down_write(&alg_types_sem); |
| 99 | list_for_each_entry(node, &alg_types, list) { |
| 100 | if (strcmp(node->type->name, type->name)) |
| 101 | continue; |
| 102 | |
| 103 | list_del(&node->list); |
| 104 | kfree(node); |
| 105 | err = 0; |
| 106 | break; |
| 107 | } |
| 108 | up_write(&alg_types_sem); |
| 109 | |
| 110 | return err; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(af_alg_unregister_type); |
| 113 | |
| 114 | static void alg_do_release(const struct af_alg_type *type, void *private) |
| 115 | { |
| 116 | if (!type) |
| 117 | return; |
| 118 | |
| 119 | type->release(private); |
| 120 | module_put(type->owner); |
| 121 | } |
| 122 | |
| 123 | int af_alg_release(struct socket *sock) |
| 124 | { |
| 125 | if (sock->sk) |
| 126 | sock_put(sock->sk); |
| 127 | return 0; |
| 128 | } |
| 129 | EXPORT_SYMBOL_GPL(af_alg_release); |
| 130 | |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 131 | void af_alg_release_parent(struct sock *sk) |
| 132 | { |
| 133 | struct alg_sock *ask = alg_sk(sk); |
Herbert Xu | a6a48c5 | 2016-01-13 15:03:32 +0800 | [diff] [blame] | 134 | unsigned int nokey = ask->nokey_refcnt; |
| 135 | bool last = nokey && !ask->refcnt; |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 136 | |
| 137 | sk = ask->parent; |
| 138 | ask = alg_sk(sk); |
| 139 | |
| 140 | lock_sock(sk); |
Herbert Xu | a6a48c5 | 2016-01-13 15:03:32 +0800 | [diff] [blame] | 141 | ask->nokey_refcnt -= nokey; |
| 142 | if (!last) |
| 143 | last = !--ask->refcnt; |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 144 | release_sock(sk); |
| 145 | |
| 146 | if (last) |
| 147 | sock_put(sk); |
| 148 | } |
| 149 | EXPORT_SYMBOL_GPL(af_alg_release_parent); |
| 150 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 151 | static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 152 | { |
Stephan Mueller | bb30b88 | 2018-01-02 08:55:25 +0100 | [diff] [blame] | 153 | const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 154 | struct sock *sk = sock->sk; |
| 155 | struct alg_sock *ask = alg_sk(sk); |
| 156 | struct sockaddr_alg *sa = (void *)uaddr; |
| 157 | const struct af_alg_type *type; |
| 158 | void *private; |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 159 | int err; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 160 | |
| 161 | if (sock->state == SS_CONNECTED) |
| 162 | return -EINVAL; |
| 163 | |
Herbert Xu | 3f69cc6 | 2017-04-06 16:16:09 +0800 | [diff] [blame] | 164 | if (addr_len < sizeof(*sa)) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | |
Eric Dumazet | a466856 | 2018-04-07 13:42:36 -0700 | [diff] [blame] | 167 | /* If caller uses non-allowed flag, return error. */ |
| 168 | if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed)) |
| 169 | return -EINVAL; |
| 170 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 171 | sa->salg_type[sizeof(sa->salg_type) - 1] = 0; |
Herbert Xu | 3f69cc6 | 2017-04-06 16:16:09 +0800 | [diff] [blame] | 172 | sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 173 | |
| 174 | type = alg_get_type(sa->salg_type); |
| 175 | if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) { |
| 176 | request_module("algif-%s", sa->salg_type); |
| 177 | type = alg_get_type(sa->salg_type); |
| 178 | } |
| 179 | |
| 180 | if (IS_ERR(type)) |
| 181 | return PTR_ERR(type); |
| 182 | |
Stephan Mueller | bb30b88 | 2018-01-02 08:55:25 +0100 | [diff] [blame] | 183 | private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 184 | if (IS_ERR(private)) { |
| 185 | module_put(type->owner); |
| 186 | return PTR_ERR(private); |
| 187 | } |
| 188 | |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 189 | err = -EBUSY; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 190 | lock_sock(sk); |
Herbert Xu | a6a48c5 | 2016-01-13 15:03:32 +0800 | [diff] [blame] | 191 | if (ask->refcnt | ask->nokey_refcnt) |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 192 | goto unlock; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 193 | |
| 194 | swap(ask->type, type); |
| 195 | swap(ask->private, private); |
| 196 | |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 197 | err = 0; |
| 198 | |
| 199 | unlock: |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 200 | release_sock(sk); |
| 201 | |
| 202 | alg_do_release(type, private); |
| 203 | |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 204 | return err; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int alg_setkey(struct sock *sk, char __user *ukey, |
| 208 | unsigned int keylen) |
| 209 | { |
| 210 | struct alg_sock *ask = alg_sk(sk); |
| 211 | const struct af_alg_type *type = ask->type; |
| 212 | u8 *key; |
| 213 | int err; |
| 214 | |
| 215 | key = sock_kmalloc(sk, keylen, GFP_KERNEL); |
| 216 | if (!key) |
| 217 | return -ENOMEM; |
| 218 | |
| 219 | err = -EFAULT; |
| 220 | if (copy_from_user(key, ukey, keylen)) |
| 221 | goto out; |
| 222 | |
| 223 | err = type->setkey(ask->private, key, keylen); |
| 224 | |
| 225 | out: |
Stephan Mueller | ad202c8 | 2014-12-23 09:34:03 +0100 | [diff] [blame] | 226 | sock_kzfree_s(sk, key, keylen); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 227 | |
| 228 | return err; |
| 229 | } |
| 230 | |
| 231 | static int alg_setsockopt(struct socket *sock, int level, int optname, |
| 232 | char __user *optval, unsigned int optlen) |
| 233 | { |
| 234 | struct sock *sk = sock->sk; |
| 235 | struct alg_sock *ask = alg_sk(sk); |
| 236 | const struct af_alg_type *type; |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 237 | int err = -EBUSY; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 238 | |
| 239 | lock_sock(sk); |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 240 | if (ask->refcnt) |
| 241 | goto unlock; |
| 242 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 243 | type = ask->type; |
| 244 | |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 245 | err = -ENOPROTOOPT; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 246 | if (level != SOL_ALG || !type) |
| 247 | goto unlock; |
| 248 | |
| 249 | switch (optname) { |
| 250 | case ALG_SET_KEY: |
| 251 | if (sock->state == SS_CONNECTED) |
| 252 | goto unlock; |
| 253 | if (!type->setkey) |
| 254 | goto unlock; |
| 255 | |
| 256 | err = alg_setkey(sk, optval, optlen); |
Stephan Mueller | 25fb863 | 2014-12-07 23:21:42 +0100 | [diff] [blame] | 257 | break; |
| 258 | case ALG_SET_AEAD_AUTHSIZE: |
| 259 | if (sock->state == SS_CONNECTED) |
| 260 | goto unlock; |
| 261 | if (!type->setauthsize) |
| 262 | goto unlock; |
| 263 | err = type->setauthsize(ask->private, optlen); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | unlock: |
| 267 | release_sock(sk); |
| 268 | |
| 269 | return err; |
| 270 | } |
| 271 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 272 | int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 273 | { |
| 274 | struct alg_sock *ask = alg_sk(sk); |
| 275 | const struct af_alg_type *type; |
| 276 | struct sock *sk2; |
Herbert Xu | 6a93517 | 2016-01-13 14:59:03 +0800 | [diff] [blame] | 277 | unsigned int nokey; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 278 | int err; |
| 279 | |
| 280 | lock_sock(sk); |
| 281 | type = ask->type; |
| 282 | |
| 283 | err = -EINVAL; |
| 284 | if (!type) |
| 285 | goto unlock; |
| 286 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 287 | sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 288 | err = -ENOMEM; |
| 289 | if (!sk2) |
| 290 | goto unlock; |
| 291 | |
| 292 | sock_init_data(newsock, sk2); |
Herbert Xu | 2acce6a | 2017-07-10 22:00:48 +0800 | [diff] [blame] | 293 | security_sock_graft(sk2, newsock); |
Milan Broz | 4c63f83 | 2014-07-29 18:41:09 +0000 | [diff] [blame] | 294 | security_sk_clone(sk, sk2); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 295 | |
| 296 | err = type->accept(ask->private, sk2); |
Herbert Xu | 3776658 | 2016-01-04 13:35:18 +0900 | [diff] [blame] | 297 | |
| 298 | nokey = err == -ENOKEY; |
| 299 | if (nokey && type->accept_nokey) |
| 300 | err = type->accept_nokey(ask->private, sk2); |
| 301 | |
Herbert Xu | a383292 | 2015-12-30 20:24:17 +0800 | [diff] [blame] | 302 | if (err) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 303 | goto unlock; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 304 | |
| 305 | sk2->sk_family = PF_ALG; |
| 306 | |
Herbert Xu | 3776658 | 2016-01-04 13:35:18 +0900 | [diff] [blame] | 307 | if (nokey || !ask->refcnt++) |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 308 | sock_hold(sk); |
Herbert Xu | a6a48c5 | 2016-01-13 15:03:32 +0800 | [diff] [blame] | 309 | ask->nokey_refcnt += nokey; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 310 | alg_sk(sk2)->parent = sk; |
| 311 | alg_sk(sk2)->type = type; |
Herbert Xu | 6a93517 | 2016-01-13 14:59:03 +0800 | [diff] [blame] | 312 | alg_sk(sk2)->nokey_refcnt = nokey; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 313 | |
| 314 | newsock->ops = type->ops; |
| 315 | newsock->state = SS_CONNECTED; |
| 316 | |
Herbert Xu | 3776658 | 2016-01-04 13:35:18 +0900 | [diff] [blame] | 317 | if (nokey) |
| 318 | newsock->ops = type->ops_nokey; |
| 319 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 320 | err = 0; |
| 321 | |
| 322 | unlock: |
| 323 | release_sock(sk); |
| 324 | |
| 325 | return err; |
| 326 | } |
| 327 | EXPORT_SYMBOL_GPL(af_alg_accept); |
| 328 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 329 | static int alg_accept(struct socket *sock, struct socket *newsock, int flags, |
| 330 | bool kern) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 331 | { |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 332 | return af_alg_accept(sock->sk, newsock, kern); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static const struct proto_ops alg_proto_ops = { |
| 336 | .family = PF_ALG, |
| 337 | .owner = THIS_MODULE, |
| 338 | |
| 339 | .connect = sock_no_connect, |
| 340 | .socketpair = sock_no_socketpair, |
| 341 | .getname = sock_no_getname, |
| 342 | .ioctl = sock_no_ioctl, |
| 343 | .listen = sock_no_listen, |
| 344 | .shutdown = sock_no_shutdown, |
| 345 | .getsockopt = sock_no_getsockopt, |
| 346 | .mmap = sock_no_mmap, |
| 347 | .sendpage = sock_no_sendpage, |
| 348 | .sendmsg = sock_no_sendmsg, |
| 349 | .recvmsg = sock_no_recvmsg, |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 350 | |
| 351 | .bind = alg_bind, |
| 352 | .release = af_alg_release, |
| 353 | .setsockopt = alg_setsockopt, |
| 354 | .accept = alg_accept, |
| 355 | }; |
| 356 | |
| 357 | static void alg_sock_destruct(struct sock *sk) |
| 358 | { |
| 359 | struct alg_sock *ask = alg_sk(sk); |
| 360 | |
| 361 | alg_do_release(ask->type, ask->private); |
| 362 | } |
| 363 | |
| 364 | static int alg_create(struct net *net, struct socket *sock, int protocol, |
| 365 | int kern) |
| 366 | { |
| 367 | struct sock *sk; |
| 368 | int err; |
| 369 | |
| 370 | if (sock->type != SOCK_SEQPACKET) |
| 371 | return -ESOCKTNOSUPPORT; |
| 372 | if (protocol != 0) |
| 373 | return -EPROTONOSUPPORT; |
| 374 | |
| 375 | err = -ENOMEM; |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 376 | sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 377 | if (!sk) |
| 378 | goto out; |
| 379 | |
| 380 | sock->ops = &alg_proto_ops; |
| 381 | sock_init_data(sock, sk); |
| 382 | |
| 383 | sk->sk_family = PF_ALG; |
| 384 | sk->sk_destruct = alg_sock_destruct; |
| 385 | |
| 386 | return 0; |
| 387 | out: |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | static const struct net_proto_family alg_family = { |
| 392 | .family = PF_ALG, |
| 393 | .create = alg_create, |
| 394 | .owner = THIS_MODULE, |
| 395 | }; |
| 396 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 397 | int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 398 | { |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 399 | size_t off; |
| 400 | ssize_t n; |
| 401 | int npages, i; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 402 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 403 | n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off); |
| 404 | if (n < 0) |
| 405 | return n; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 406 | |
Linus Torvalds | 9399f0c | 2015-02-10 19:55:45 -0800 | [diff] [blame] | 407 | npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 408 | if (WARN_ON(npages == 0)) |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 409 | return -EINVAL; |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 410 | /* Add one extra for linking */ |
| 411 | sg_init_table(sgl->sg, npages + 1); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 412 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 413 | for (i = 0, len = n; i < npages; i++) { |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 414 | int plen = min_t(int, len, PAGE_SIZE - off); |
| 415 | |
| 416 | sg_set_page(sgl->sg + i, sgl->pages[i], plen, off); |
| 417 | |
| 418 | off = 0; |
| 419 | len -= plen; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 420 | } |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 421 | sg_mark_end(sgl->sg + npages - 1); |
| 422 | sgl->npages = npages; |
| 423 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 424 | return n; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(af_alg_make_sg); |
| 427 | |
Eric Biggers | 466e075 | 2019-01-10 12:18:00 -0800 | [diff] [blame^] | 428 | static void af_alg_link_sg(struct af_alg_sgl *sgl_prev, |
| 429 | struct af_alg_sgl *sgl_new) |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 430 | { |
| 431 | sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1); |
| 432 | sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg); |
| 433 | } |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 434 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 435 | void af_alg_free_sg(struct af_alg_sgl *sgl) |
| 436 | { |
| 437 | int i; |
| 438 | |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 439 | for (i = 0; i < sgl->npages; i++) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 440 | put_page(sgl->pages[i]); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 441 | } |
| 442 | EXPORT_SYMBOL_GPL(af_alg_free_sg); |
| 443 | |
Eric Biggers | 466e075 | 2019-01-10 12:18:00 -0800 | [diff] [blame^] | 444 | static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con) |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 445 | { |
| 446 | struct cmsghdr *cmsg; |
| 447 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 448 | for_each_cmsghdr(cmsg, msg) { |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 449 | if (!CMSG_OK(msg, cmsg)) |
| 450 | return -EINVAL; |
| 451 | if (cmsg->cmsg_level != SOL_ALG) |
| 452 | continue; |
| 453 | |
Joshua I. James | 267c422 | 2014-12-05 14:38:40 +0900 | [diff] [blame] | 454 | switch (cmsg->cmsg_type) { |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 455 | case ALG_SET_IV: |
| 456 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv))) |
| 457 | return -EINVAL; |
| 458 | con->iv = (void *)CMSG_DATA(cmsg); |
| 459 | if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen + |
| 460 | sizeof(*con->iv))) |
| 461 | return -EINVAL; |
| 462 | break; |
| 463 | |
| 464 | case ALG_SET_OP: |
| 465 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32))) |
| 466 | return -EINVAL; |
| 467 | con->op = *(u32 *)CMSG_DATA(cmsg); |
| 468 | break; |
| 469 | |
Stephan Mueller | af8e807 | 2014-12-03 20:55:42 +0100 | [diff] [blame] | 470 | case ALG_SET_AEAD_ASSOCLEN: |
| 471 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32))) |
| 472 | return -EINVAL; |
| 473 | con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg); |
| 474 | break; |
| 475 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 476 | default: |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 483 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 484 | /** |
| 485 | * af_alg_alloc_tsgl - allocate the TX SGL |
| 486 | * |
| 487 | * @sk socket of connection to user space |
| 488 | * @return: 0 upon success, < 0 upon error |
| 489 | */ |
Eric Biggers | 466e075 | 2019-01-10 12:18:00 -0800 | [diff] [blame^] | 490 | static int af_alg_alloc_tsgl(struct sock *sk) |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 491 | { |
| 492 | struct alg_sock *ask = alg_sk(sk); |
| 493 | struct af_alg_ctx *ctx = ask->private; |
| 494 | struct af_alg_tsgl *sgl; |
| 495 | struct scatterlist *sg = NULL; |
| 496 | |
| 497 | sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list); |
| 498 | if (!list_empty(&ctx->tsgl_list)) |
| 499 | sg = sgl->sg; |
| 500 | |
| 501 | if (!sg || sgl->cur >= MAX_SGL_ENTS) { |
Kees Cook | 0ed2dd0 | 2018-05-08 16:08:53 -0700 | [diff] [blame] | 502 | sgl = sock_kmalloc(sk, |
| 503 | struct_size(sgl, sg, (MAX_SGL_ENTS + 1)), |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 504 | GFP_KERNEL); |
| 505 | if (!sgl) |
| 506 | return -ENOMEM; |
| 507 | |
| 508 | sg_init_table(sgl->sg, MAX_SGL_ENTS + 1); |
| 509 | sgl->cur = 0; |
| 510 | |
| 511 | if (sg) |
| 512 | sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg); |
| 513 | |
| 514 | list_add_tail(&sgl->list, &ctx->tsgl_list); |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * aead_count_tsgl - Count number of TX SG entries |
| 522 | * |
| 523 | * The counting starts from the beginning of the SGL to @bytes. If |
| 524 | * an offset is provided, the counting of the SG entries starts at the offset. |
| 525 | * |
| 526 | * @sk socket of connection to user space |
| 527 | * @bytes Count the number of SG entries holding given number of bytes. |
| 528 | * @offset Start the counting of SG entries from the given offset. |
| 529 | * @return Number of TX SG entries found given the constraints |
| 530 | */ |
| 531 | unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset) |
| 532 | { |
| 533 | struct alg_sock *ask = alg_sk(sk); |
| 534 | struct af_alg_ctx *ctx = ask->private; |
| 535 | struct af_alg_tsgl *sgl, *tmp; |
| 536 | unsigned int i; |
| 537 | unsigned int sgl_count = 0; |
| 538 | |
| 539 | if (!bytes) |
| 540 | return 0; |
| 541 | |
| 542 | list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) { |
| 543 | struct scatterlist *sg = sgl->sg; |
| 544 | |
| 545 | for (i = 0; i < sgl->cur; i++) { |
| 546 | size_t bytes_count; |
| 547 | |
| 548 | /* Skip offset */ |
| 549 | if (offset >= sg[i].length) { |
| 550 | offset -= sg[i].length; |
| 551 | bytes -= sg[i].length; |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | bytes_count = sg[i].length - offset; |
| 556 | |
| 557 | offset = 0; |
| 558 | sgl_count++; |
| 559 | |
| 560 | /* If we have seen requested number of bytes, stop */ |
| 561 | if (bytes_count >= bytes) |
| 562 | return sgl_count; |
| 563 | |
| 564 | bytes -= bytes_count; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | return sgl_count; |
| 569 | } |
| 570 | EXPORT_SYMBOL_GPL(af_alg_count_tsgl); |
| 571 | |
| 572 | /** |
| 573 | * aead_pull_tsgl - Release the specified buffers from TX SGL |
| 574 | * |
| 575 | * If @dst is non-null, reassign the pages to dst. The caller must release |
| 576 | * the pages. If @dst_offset is given only reassign the pages to @dst starting |
| 577 | * at the @dst_offset (byte). The caller must ensure that @dst is large |
| 578 | * enough (e.g. by using af_alg_count_tsgl with the same offset). |
| 579 | * |
| 580 | * @sk socket of connection to user space |
| 581 | * @used Number of bytes to pull from TX SGL |
| 582 | * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The |
| 583 | * caller must release the buffers in dst. |
| 584 | * @dst_offset Reassign the TX SGL from given offset. All buffers before |
| 585 | * reaching the offset is released. |
| 586 | */ |
| 587 | void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst, |
| 588 | size_t dst_offset) |
| 589 | { |
| 590 | struct alg_sock *ask = alg_sk(sk); |
| 591 | struct af_alg_ctx *ctx = ask->private; |
| 592 | struct af_alg_tsgl *sgl; |
| 593 | struct scatterlist *sg; |
Stephan Mueller | e117765 | 2017-08-30 09:17:39 +0200 | [diff] [blame] | 594 | unsigned int i, j = 0; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 595 | |
| 596 | while (!list_empty(&ctx->tsgl_list)) { |
| 597 | sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl, |
| 598 | list); |
| 599 | sg = sgl->sg; |
| 600 | |
Stephan Mueller | e117765 | 2017-08-30 09:17:39 +0200 | [diff] [blame] | 601 | for (i = 0; i < sgl->cur; i++) { |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 602 | size_t plen = min_t(size_t, used, sg[i].length); |
| 603 | struct page *page = sg_page(sg + i); |
| 604 | |
| 605 | if (!page) |
| 606 | continue; |
| 607 | |
| 608 | /* |
| 609 | * Assumption: caller created af_alg_count_tsgl(len) |
| 610 | * SG entries in dst. |
| 611 | */ |
| 612 | if (dst) { |
| 613 | if (dst_offset >= plen) { |
| 614 | /* discard page before offset */ |
| 615 | dst_offset -= plen; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 616 | } else { |
| 617 | /* reassign page to dst after offset */ |
Stephan Mueller | 2d45a7e | 2017-08-10 16:40:03 +0200 | [diff] [blame] | 618 | get_page(page); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 619 | sg_set_page(dst + j, page, |
| 620 | plen - dst_offset, |
| 621 | sg[i].offset + dst_offset); |
| 622 | dst_offset = 0; |
| 623 | j++; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | sg[i].length -= plen; |
| 628 | sg[i].offset += plen; |
| 629 | |
| 630 | used -= plen; |
| 631 | ctx->used -= plen; |
| 632 | |
| 633 | if (sg[i].length) |
| 634 | return; |
| 635 | |
Stephan Mueller | 2d45a7e | 2017-08-10 16:40:03 +0200 | [diff] [blame] | 636 | put_page(page); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 637 | sg_assign_page(sg + i, NULL); |
| 638 | } |
| 639 | |
| 640 | list_del(&sgl->list); |
| 641 | sock_kfree_s(sk, sgl, sizeof(*sgl) + sizeof(sgl->sg[0]) * |
| 642 | (MAX_SGL_ENTS + 1)); |
| 643 | } |
| 644 | |
| 645 | if (!ctx->used) |
| 646 | ctx->merge = 0; |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(af_alg_pull_tsgl); |
| 649 | |
| 650 | /** |
| 651 | * af_alg_free_areq_sgls - Release TX and RX SGLs of the request |
| 652 | * |
| 653 | * @areq Request holding the TX and RX SGL |
| 654 | */ |
Eric Biggers | 466e075 | 2019-01-10 12:18:00 -0800 | [diff] [blame^] | 655 | static void af_alg_free_areq_sgls(struct af_alg_async_req *areq) |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 656 | { |
| 657 | struct sock *sk = areq->sk; |
| 658 | struct alg_sock *ask = alg_sk(sk); |
| 659 | struct af_alg_ctx *ctx = ask->private; |
| 660 | struct af_alg_rsgl *rsgl, *tmp; |
| 661 | struct scatterlist *tsgl; |
| 662 | struct scatterlist *sg; |
| 663 | unsigned int i; |
| 664 | |
| 665 | list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) { |
Jonathan Cameron | af955bf | 2017-12-19 10:27:24 +0000 | [diff] [blame] | 666 | atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 667 | af_alg_free_sg(&rsgl->sgl); |
| 668 | list_del(&rsgl->list); |
| 669 | if (rsgl != &areq->first_rsgl) |
| 670 | sock_kfree_s(sk, rsgl, sizeof(*rsgl)); |
| 671 | } |
| 672 | |
| 673 | tsgl = areq->tsgl; |
Eric Biggers | 887207e | 2017-11-28 00:46:24 -0800 | [diff] [blame] | 674 | if (tsgl) { |
| 675 | for_each_sg(tsgl, sg, areq->tsgl_entries, i) { |
| 676 | if (!sg_page(sg)) |
| 677 | continue; |
| 678 | put_page(sg_page(sg)); |
| 679 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 680 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 681 | sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl)); |
Eric Biggers | 887207e | 2017-11-28 00:46:24 -0800 | [diff] [blame] | 682 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 683 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 684 | |
| 685 | /** |
| 686 | * af_alg_wait_for_wmem - wait for availability of writable memory |
| 687 | * |
| 688 | * @sk socket of connection to user space |
| 689 | * @flags If MSG_DONTWAIT is set, then only report if function would sleep |
| 690 | * @return 0 when writable memory is available, < 0 upon error |
| 691 | */ |
Eric Biggers | 466e075 | 2019-01-10 12:18:00 -0800 | [diff] [blame^] | 692 | static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags) |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 693 | { |
| 694 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 695 | int err = -ERESTARTSYS; |
| 696 | long timeout; |
| 697 | |
| 698 | if (flags & MSG_DONTWAIT) |
| 699 | return -EAGAIN; |
| 700 | |
| 701 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 702 | |
| 703 | add_wait_queue(sk_sleep(sk), &wait); |
| 704 | for (;;) { |
| 705 | if (signal_pending(current)) |
| 706 | break; |
| 707 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 708 | if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) { |
| 709 | err = 0; |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | remove_wait_queue(sk_sleep(sk), &wait); |
| 714 | |
| 715 | return err; |
| 716 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 717 | |
| 718 | /** |
| 719 | * af_alg_wmem_wakeup - wakeup caller when writable memory is available |
| 720 | * |
| 721 | * @sk socket of connection to user space |
| 722 | */ |
| 723 | void af_alg_wmem_wakeup(struct sock *sk) |
| 724 | { |
| 725 | struct socket_wq *wq; |
| 726 | |
| 727 | if (!af_alg_writable(sk)) |
| 728 | return; |
| 729 | |
| 730 | rcu_read_lock(); |
| 731 | wq = rcu_dereference(sk->sk_wq); |
| 732 | if (skwq_has_sleeper(wq)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 733 | wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | |
| 734 | EPOLLRDNORM | |
| 735 | EPOLLRDBAND); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 736 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); |
| 737 | rcu_read_unlock(); |
| 738 | } |
| 739 | EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup); |
| 740 | |
| 741 | /** |
| 742 | * af_alg_wait_for_data - wait for availability of TX data |
| 743 | * |
| 744 | * @sk socket of connection to user space |
| 745 | * @flags If MSG_DONTWAIT is set, then only report if function would sleep |
| 746 | * @return 0 when writable memory is available, < 0 upon error |
| 747 | */ |
| 748 | int af_alg_wait_for_data(struct sock *sk, unsigned flags) |
| 749 | { |
| 750 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 751 | struct alg_sock *ask = alg_sk(sk); |
| 752 | struct af_alg_ctx *ctx = ask->private; |
| 753 | long timeout; |
| 754 | int err = -ERESTARTSYS; |
| 755 | |
| 756 | if (flags & MSG_DONTWAIT) |
| 757 | return -EAGAIN; |
| 758 | |
| 759 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 760 | |
| 761 | add_wait_queue(sk_sleep(sk), &wait); |
| 762 | for (;;) { |
| 763 | if (signal_pending(current)) |
| 764 | break; |
| 765 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 766 | if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more), |
| 767 | &wait)) { |
| 768 | err = 0; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | remove_wait_queue(sk_sleep(sk), &wait); |
| 773 | |
| 774 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 775 | |
| 776 | return err; |
| 777 | } |
| 778 | EXPORT_SYMBOL_GPL(af_alg_wait_for_data); |
| 779 | |
| 780 | /** |
| 781 | * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel |
| 782 | * |
| 783 | * @sk socket of connection to user space |
| 784 | */ |
Eric Biggers | 466e075 | 2019-01-10 12:18:00 -0800 | [diff] [blame^] | 785 | static void af_alg_data_wakeup(struct sock *sk) |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 786 | { |
| 787 | struct alg_sock *ask = alg_sk(sk); |
| 788 | struct af_alg_ctx *ctx = ask->private; |
| 789 | struct socket_wq *wq; |
| 790 | |
| 791 | if (!ctx->used) |
| 792 | return; |
| 793 | |
| 794 | rcu_read_lock(); |
| 795 | wq = rcu_dereference(sk->sk_wq); |
| 796 | if (skwq_has_sleeper(wq)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 797 | wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | |
| 798 | EPOLLRDNORM | |
| 799 | EPOLLRDBAND); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 800 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
| 801 | rcu_read_unlock(); |
| 802 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 803 | |
| 804 | /** |
| 805 | * af_alg_sendmsg - implementation of sendmsg system call handler |
| 806 | * |
| 807 | * The sendmsg system call handler obtains the user data and stores it |
| 808 | * in ctx->tsgl_list. This implies allocation of the required numbers of |
| 809 | * struct af_alg_tsgl. |
| 810 | * |
| 811 | * In addition, the ctx is filled with the information sent via CMSG. |
| 812 | * |
| 813 | * @sock socket of connection to user space |
| 814 | * @msg message from user space |
| 815 | * @size size of message from user space |
| 816 | * @ivsize the size of the IV for the cipher operation to verify that the |
| 817 | * user-space-provided IV has the right size |
| 818 | * @return the number of copied data upon success, < 0 upon error |
| 819 | */ |
| 820 | int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 821 | unsigned int ivsize) |
| 822 | { |
| 823 | struct sock *sk = sock->sk; |
| 824 | struct alg_sock *ask = alg_sk(sk); |
| 825 | struct af_alg_ctx *ctx = ask->private; |
| 826 | struct af_alg_tsgl *sgl; |
| 827 | struct af_alg_control con = {}; |
| 828 | long copied = 0; |
| 829 | bool enc = 0; |
| 830 | bool init = 0; |
| 831 | int err = 0; |
| 832 | |
| 833 | if (msg->msg_controllen) { |
| 834 | err = af_alg_cmsg_send(msg, &con); |
| 835 | if (err) |
| 836 | return err; |
| 837 | |
| 838 | init = 1; |
| 839 | switch (con.op) { |
| 840 | case ALG_OP_ENCRYPT: |
| 841 | enc = 1; |
| 842 | break; |
| 843 | case ALG_OP_DECRYPT: |
| 844 | enc = 0; |
| 845 | break; |
| 846 | default: |
| 847 | return -EINVAL; |
| 848 | } |
| 849 | |
| 850 | if (con.iv && con.iv->ivlen != ivsize) |
| 851 | return -EINVAL; |
| 852 | } |
| 853 | |
| 854 | lock_sock(sk); |
| 855 | if (!ctx->more && ctx->used) { |
| 856 | err = -EINVAL; |
| 857 | goto unlock; |
| 858 | } |
| 859 | |
| 860 | if (init) { |
| 861 | ctx->enc = enc; |
| 862 | if (con.iv) |
| 863 | memcpy(ctx->iv, con.iv->iv, ivsize); |
| 864 | |
| 865 | ctx->aead_assoclen = con.aead_assoclen; |
| 866 | } |
| 867 | |
| 868 | while (size) { |
| 869 | struct scatterlist *sg; |
| 870 | size_t len = size; |
| 871 | size_t plen; |
| 872 | |
| 873 | /* use the existing memory in an allocated page */ |
| 874 | if (ctx->merge) { |
| 875 | sgl = list_entry(ctx->tsgl_list.prev, |
| 876 | struct af_alg_tsgl, list); |
| 877 | sg = sgl->sg + sgl->cur - 1; |
| 878 | len = min_t(size_t, len, |
| 879 | PAGE_SIZE - sg->offset - sg->length); |
| 880 | |
| 881 | err = memcpy_from_msg(page_address(sg_page(sg)) + |
| 882 | sg->offset + sg->length, |
| 883 | msg, len); |
| 884 | if (err) |
| 885 | goto unlock; |
| 886 | |
| 887 | sg->length += len; |
| 888 | ctx->merge = (sg->offset + sg->length) & |
| 889 | (PAGE_SIZE - 1); |
| 890 | |
| 891 | ctx->used += len; |
| 892 | copied += len; |
| 893 | size -= len; |
| 894 | continue; |
| 895 | } |
| 896 | |
| 897 | if (!af_alg_writable(sk)) { |
| 898 | err = af_alg_wait_for_wmem(sk, msg->msg_flags); |
| 899 | if (err) |
| 900 | goto unlock; |
| 901 | } |
| 902 | |
| 903 | /* allocate a new page */ |
| 904 | len = min_t(unsigned long, len, af_alg_sndbuf(sk)); |
| 905 | |
| 906 | err = af_alg_alloc_tsgl(sk); |
| 907 | if (err) |
| 908 | goto unlock; |
| 909 | |
| 910 | sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, |
| 911 | list); |
| 912 | sg = sgl->sg; |
| 913 | if (sgl->cur) |
| 914 | sg_unmark_end(sg + sgl->cur - 1); |
| 915 | |
| 916 | do { |
| 917 | unsigned int i = sgl->cur; |
| 918 | |
| 919 | plen = min_t(size_t, len, PAGE_SIZE); |
| 920 | |
| 921 | sg_assign_page(sg + i, alloc_page(GFP_KERNEL)); |
| 922 | if (!sg_page(sg + i)) { |
| 923 | err = -ENOMEM; |
| 924 | goto unlock; |
| 925 | } |
| 926 | |
| 927 | err = memcpy_from_msg(page_address(sg_page(sg + i)), |
| 928 | msg, plen); |
| 929 | if (err) { |
| 930 | __free_page(sg_page(sg + i)); |
| 931 | sg_assign_page(sg + i, NULL); |
| 932 | goto unlock; |
| 933 | } |
| 934 | |
| 935 | sg[i].length = plen; |
| 936 | len -= plen; |
| 937 | ctx->used += plen; |
| 938 | copied += plen; |
| 939 | size -= plen; |
| 940 | sgl->cur++; |
| 941 | } while (len && sgl->cur < MAX_SGL_ENTS); |
| 942 | |
| 943 | if (!size) |
| 944 | sg_mark_end(sg + sgl->cur - 1); |
| 945 | |
| 946 | ctx->merge = plen & (PAGE_SIZE - 1); |
| 947 | } |
| 948 | |
| 949 | err = 0; |
| 950 | |
| 951 | ctx->more = msg->msg_flags & MSG_MORE; |
| 952 | |
| 953 | unlock: |
| 954 | af_alg_data_wakeup(sk); |
| 955 | release_sock(sk); |
| 956 | |
| 957 | return copied ?: err; |
| 958 | } |
| 959 | EXPORT_SYMBOL_GPL(af_alg_sendmsg); |
| 960 | |
| 961 | /** |
| 962 | * af_alg_sendpage - sendpage system call handler |
| 963 | * |
| 964 | * This is a generic implementation of sendpage to fill ctx->tsgl_list. |
| 965 | */ |
| 966 | ssize_t af_alg_sendpage(struct socket *sock, struct page *page, |
| 967 | int offset, size_t size, int flags) |
| 968 | { |
| 969 | struct sock *sk = sock->sk; |
| 970 | struct alg_sock *ask = alg_sk(sk); |
| 971 | struct af_alg_ctx *ctx = ask->private; |
| 972 | struct af_alg_tsgl *sgl; |
| 973 | int err = -EINVAL; |
| 974 | |
| 975 | if (flags & MSG_SENDPAGE_NOTLAST) |
| 976 | flags |= MSG_MORE; |
| 977 | |
| 978 | lock_sock(sk); |
| 979 | if (!ctx->more && ctx->used) |
| 980 | goto unlock; |
| 981 | |
| 982 | if (!size) |
| 983 | goto done; |
| 984 | |
| 985 | if (!af_alg_writable(sk)) { |
| 986 | err = af_alg_wait_for_wmem(sk, flags); |
| 987 | if (err) |
| 988 | goto unlock; |
| 989 | } |
| 990 | |
| 991 | err = af_alg_alloc_tsgl(sk); |
| 992 | if (err) |
| 993 | goto unlock; |
| 994 | |
| 995 | ctx->merge = 0; |
| 996 | sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list); |
| 997 | |
| 998 | if (sgl->cur) |
| 999 | sg_unmark_end(sgl->sg + sgl->cur - 1); |
| 1000 | |
| 1001 | sg_mark_end(sgl->sg + sgl->cur); |
| 1002 | |
| 1003 | get_page(page); |
| 1004 | sg_set_page(sgl->sg + sgl->cur, page, size, offset); |
| 1005 | sgl->cur++; |
| 1006 | ctx->used += size; |
| 1007 | |
| 1008 | done: |
| 1009 | ctx->more = flags & MSG_MORE; |
| 1010 | |
| 1011 | unlock: |
| 1012 | af_alg_data_wakeup(sk); |
| 1013 | release_sock(sk); |
| 1014 | |
| 1015 | return err ?: size; |
| 1016 | } |
| 1017 | EXPORT_SYMBOL_GPL(af_alg_sendpage); |
| 1018 | |
| 1019 | /** |
Stephan Mueller | 7d2c3f5 | 2017-11-10 13:20:55 +0100 | [diff] [blame] | 1020 | * af_alg_free_resources - release resources required for crypto request |
| 1021 | */ |
| 1022 | void af_alg_free_resources(struct af_alg_async_req *areq) |
| 1023 | { |
| 1024 | struct sock *sk = areq->sk; |
| 1025 | |
| 1026 | af_alg_free_areq_sgls(areq); |
| 1027 | sock_kfree_s(sk, areq, areq->areqlen); |
| 1028 | } |
| 1029 | EXPORT_SYMBOL_GPL(af_alg_free_resources); |
| 1030 | |
| 1031 | /** |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1032 | * af_alg_async_cb - AIO callback handler |
| 1033 | * |
| 1034 | * This handler cleans up the struct af_alg_async_req upon completion of the |
| 1035 | * AIO operation. |
| 1036 | * |
| 1037 | * The number of bytes to be generated with the AIO operation must be set |
| 1038 | * in areq->outlen before the AIO callback handler is invoked. |
| 1039 | */ |
| 1040 | void af_alg_async_cb(struct crypto_async_request *_req, int err) |
| 1041 | { |
| 1042 | struct af_alg_async_req *areq = _req->data; |
| 1043 | struct sock *sk = areq->sk; |
| 1044 | struct kiocb *iocb = areq->iocb; |
| 1045 | unsigned int resultlen; |
| 1046 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1047 | /* Buffer size written by crypto operation. */ |
| 1048 | resultlen = areq->outlen; |
| 1049 | |
Stephan Mueller | 7d2c3f5 | 2017-11-10 13:20:55 +0100 | [diff] [blame] | 1050 | af_alg_free_resources(areq); |
| 1051 | sock_put(sk); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1052 | |
| 1053 | iocb->ki_complete(iocb, err ? err : resultlen, 0); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1054 | } |
| 1055 | EXPORT_SYMBOL_GPL(af_alg_async_cb); |
| 1056 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1057 | /** |
| 1058 | * af_alg_poll - poll system call handler |
| 1059 | */ |
| 1060 | __poll_t af_alg_poll(struct file *file, struct socket *sock, |
| 1061 | poll_table *wait) |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1062 | { |
| 1063 | struct sock *sk = sock->sk; |
| 1064 | struct alg_sock *ask = alg_sk(sk); |
| 1065 | struct af_alg_ctx *ctx = ask->private; |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1066 | __poll_t mask; |
| 1067 | |
Karsten Graul | 89ab066 | 2018-10-23 13:40:39 +0200 | [diff] [blame] | 1068 | sock_poll_wait(file, sock, wait); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1069 | mask = 0; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1070 | |
| 1071 | if (!ctx->more || ctx->used) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1072 | mask |= EPOLLIN | EPOLLRDNORM; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1073 | |
| 1074 | if (af_alg_writable(sk)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1075 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1076 | |
| 1077 | return mask; |
| 1078 | } |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1079 | EXPORT_SYMBOL_GPL(af_alg_poll); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1080 | |
| 1081 | /** |
| 1082 | * af_alg_alloc_areq - allocate struct af_alg_async_req |
| 1083 | * |
| 1084 | * @sk socket of connection to user space |
| 1085 | * @areqlen size of struct af_alg_async_req + crypto_*_reqsize |
| 1086 | * @return allocated data structure or ERR_PTR upon error |
| 1087 | */ |
| 1088 | struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk, |
| 1089 | unsigned int areqlen) |
| 1090 | { |
| 1091 | struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL); |
| 1092 | |
| 1093 | if (unlikely(!areq)) |
| 1094 | return ERR_PTR(-ENOMEM); |
| 1095 | |
| 1096 | areq->areqlen = areqlen; |
| 1097 | areq->sk = sk; |
| 1098 | areq->last_rsgl = NULL; |
| 1099 | INIT_LIST_HEAD(&areq->rsgl_list); |
| 1100 | areq->tsgl = NULL; |
| 1101 | areq->tsgl_entries = 0; |
| 1102 | |
| 1103 | return areq; |
| 1104 | } |
| 1105 | EXPORT_SYMBOL_GPL(af_alg_alloc_areq); |
| 1106 | |
| 1107 | /** |
| 1108 | * af_alg_get_rsgl - create the RX SGL for the output data from the crypto |
| 1109 | * operation |
| 1110 | * |
| 1111 | * @sk socket of connection to user space |
| 1112 | * @msg user space message |
| 1113 | * @flags flags used to invoke recvmsg with |
| 1114 | * @areq instance of the cryptographic request that will hold the RX SGL |
| 1115 | * @maxsize maximum number of bytes to be pulled from user space |
| 1116 | * @outlen number of bytes in the RX SGL |
| 1117 | * @return 0 on success, < 0 upon error |
| 1118 | */ |
| 1119 | int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags, |
| 1120 | struct af_alg_async_req *areq, size_t maxsize, |
| 1121 | size_t *outlen) |
| 1122 | { |
| 1123 | struct alg_sock *ask = alg_sk(sk); |
| 1124 | struct af_alg_ctx *ctx = ask->private; |
| 1125 | size_t len = 0; |
| 1126 | |
| 1127 | while (maxsize > len && msg_data_left(msg)) { |
| 1128 | struct af_alg_rsgl *rsgl; |
| 1129 | size_t seglen; |
| 1130 | int err; |
| 1131 | |
| 1132 | /* limit the amount of readable buffers */ |
| 1133 | if (!af_alg_readable(sk)) |
| 1134 | break; |
| 1135 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1136 | seglen = min_t(size_t, (maxsize - len), |
| 1137 | msg_data_left(msg)); |
| 1138 | |
| 1139 | if (list_empty(&areq->rsgl_list)) { |
| 1140 | rsgl = &areq->first_rsgl; |
| 1141 | } else { |
| 1142 | rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL); |
| 1143 | if (unlikely(!rsgl)) |
| 1144 | return -ENOMEM; |
| 1145 | } |
| 1146 | |
| 1147 | rsgl->sgl.npages = 0; |
| 1148 | list_add_tail(&rsgl->list, &areq->rsgl_list); |
| 1149 | |
| 1150 | /* make one iovec available as scatterlist */ |
| 1151 | err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen); |
Stephan Mueller | 2546da9 | 2018-07-07 20:41:47 +0200 | [diff] [blame] | 1152 | if (err < 0) { |
| 1153 | rsgl->sg_num_bytes = 0; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1154 | return err; |
Stephan Mueller | 2546da9 | 2018-07-07 20:41:47 +0200 | [diff] [blame] | 1155 | } |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1156 | |
| 1157 | /* chain the new scatterlist with previous one */ |
| 1158 | if (areq->last_rsgl) |
| 1159 | af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl); |
| 1160 | |
| 1161 | areq->last_rsgl = rsgl; |
| 1162 | len += err; |
Jonathan Cameron | af955bf | 2017-12-19 10:27:24 +0000 | [diff] [blame] | 1163 | atomic_add(err, &ctx->rcvused); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 1164 | rsgl->sg_num_bytes = err; |
| 1165 | iov_iter_advance(&msg->msg_iter, err); |
| 1166 | } |
| 1167 | |
| 1168 | *outlen = len; |
| 1169 | return 0; |
| 1170 | } |
| 1171 | EXPORT_SYMBOL_GPL(af_alg_get_rsgl); |
| 1172 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 1173 | static int __init af_alg_init(void) |
| 1174 | { |
| 1175 | int err = proto_register(&alg_proto, 0); |
| 1176 | |
| 1177 | if (err) |
| 1178 | goto out; |
| 1179 | |
| 1180 | err = sock_register(&alg_family); |
| 1181 | if (err != 0) |
| 1182 | goto out_unregister_proto; |
| 1183 | |
| 1184 | out: |
| 1185 | return err; |
| 1186 | |
| 1187 | out_unregister_proto: |
| 1188 | proto_unregister(&alg_proto); |
| 1189 | goto out; |
| 1190 | } |
| 1191 | |
| 1192 | static void __exit af_alg_exit(void) |
| 1193 | { |
| 1194 | sock_unregister(PF_ALG); |
| 1195 | proto_unregister(&alg_proto); |
| 1196 | } |
| 1197 | |
| 1198 | module_init(af_alg_init); |
| 1199 | module_exit(af_alg_exit); |
| 1200 | MODULE_LICENSE("GPL"); |
| 1201 | MODULE_ALIAS_NETPROTO(AF_ALG); |