af_alg.c 26 KB

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