af_alg.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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 forbidden = CRYPTO_ALG_INTERNAL;
  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. sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
  138. sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0;
  139. type = alg_get_type(sa->salg_type);
  140. if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
  141. request_module("algif-%s", sa->salg_type);
  142. type = alg_get_type(sa->salg_type);
  143. }
  144. if (IS_ERR(type))
  145. return PTR_ERR(type);
  146. private = type->bind(sa->salg_name,
  147. sa->salg_feat & ~forbidden,
  148. sa->salg_mask & ~forbidden);
  149. if (IS_ERR(private)) {
  150. module_put(type->owner);
  151. return PTR_ERR(private);
  152. }
  153. err = -EBUSY;
  154. lock_sock(sk);
  155. if (ask->refcnt | ask->nokey_refcnt)
  156. goto unlock;
  157. swap(ask->type, type);
  158. swap(ask->private, private);
  159. err = 0;
  160. unlock:
  161. release_sock(sk);
  162. alg_do_release(type, private);
  163. return err;
  164. }
  165. static int alg_setkey(struct sock *sk, char __user *ukey,
  166. unsigned int keylen)
  167. {
  168. struct alg_sock *ask = alg_sk(sk);
  169. const struct af_alg_type *type = ask->type;
  170. u8 *key;
  171. int err;
  172. key = sock_kmalloc(sk, keylen, GFP_KERNEL);
  173. if (!key)
  174. return -ENOMEM;
  175. err = -EFAULT;
  176. if (copy_from_user(key, ukey, keylen))
  177. goto out;
  178. err = type->setkey(ask->private, key, keylen);
  179. out:
  180. sock_kzfree_s(sk, key, keylen);
  181. return err;
  182. }
  183. static int alg_setsockopt(struct socket *sock, int level, int optname,
  184. char __user *optval, unsigned int optlen)
  185. {
  186. struct sock *sk = sock->sk;
  187. struct alg_sock *ask = alg_sk(sk);
  188. const struct af_alg_type *type;
  189. int err = -EBUSY;
  190. lock_sock(sk);
  191. if (ask->refcnt)
  192. goto unlock;
  193. type = ask->type;
  194. err = -ENOPROTOOPT;
  195. if (level != SOL_ALG || !type)
  196. goto unlock;
  197. switch (optname) {
  198. case ALG_SET_KEY:
  199. if (sock->state == SS_CONNECTED)
  200. goto unlock;
  201. if (!type->setkey)
  202. goto unlock;
  203. err = alg_setkey(sk, optval, optlen);
  204. break;
  205. case ALG_SET_AEAD_AUTHSIZE:
  206. if (sock->state == SS_CONNECTED)
  207. goto unlock;
  208. if (!type->setauthsize)
  209. goto unlock;
  210. err = type->setauthsize(ask->private, optlen);
  211. }
  212. unlock:
  213. release_sock(sk);
  214. return err;
  215. }
  216. int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
  217. {
  218. struct alg_sock *ask = alg_sk(sk);
  219. const struct af_alg_type *type;
  220. struct sock *sk2;
  221. unsigned int nokey;
  222. int err;
  223. lock_sock(sk);
  224. type = ask->type;
  225. err = -EINVAL;
  226. if (!type)
  227. goto unlock;
  228. sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
  229. err = -ENOMEM;
  230. if (!sk2)
  231. goto unlock;
  232. sock_init_data(newsock, sk2);
  233. security_sock_graft(sk2, newsock);
  234. security_sk_clone(sk, sk2);
  235. err = type->accept(ask->private, sk2);
  236. nokey = err == -ENOKEY;
  237. if (nokey && type->accept_nokey)
  238. err = type->accept_nokey(ask->private, sk2);
  239. if (err)
  240. goto unlock;
  241. sk2->sk_family = PF_ALG;
  242. if (nokey || !ask->refcnt++)
  243. sock_hold(sk);
  244. ask->nokey_refcnt += nokey;
  245. alg_sk(sk2)->parent = sk;
  246. alg_sk(sk2)->type = type;
  247. alg_sk(sk2)->nokey_refcnt = nokey;
  248. newsock->ops = type->ops;
  249. newsock->state = SS_CONNECTED;
  250. if (nokey)
  251. newsock->ops = type->ops_nokey;
  252. err = 0;
  253. unlock:
  254. release_sock(sk);
  255. return err;
  256. }
  257. EXPORT_SYMBOL_GPL(af_alg_accept);
  258. static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
  259. bool kern)
  260. {
  261. return af_alg_accept(sock->sk, newsock, kern);
  262. }
  263. static const struct proto_ops alg_proto_ops = {
  264. .family = PF_ALG,
  265. .owner = THIS_MODULE,
  266. .connect = sock_no_connect,
  267. .socketpair = sock_no_socketpair,
  268. .getname = sock_no_getname,
  269. .ioctl = sock_no_ioctl,
  270. .listen = sock_no_listen,
  271. .shutdown = sock_no_shutdown,
  272. .getsockopt = sock_no_getsockopt,
  273. .mmap = sock_no_mmap,
  274. .sendpage = sock_no_sendpage,
  275. .sendmsg = sock_no_sendmsg,
  276. .recvmsg = sock_no_recvmsg,
  277. .poll = sock_no_poll,
  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, sizeof(*sgl) +
  402. sizeof(sgl->sg[0]) * (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. ctx->rcvused -= rsgl->sg_num_bytes;
  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, POLLIN |
  599. POLLRDNORM |
  600. POLLRDBAND);
  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, POLLOUT |
  654. POLLRDNORM |
  655. POLLRDBAND);
  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_async_cb - AIO callback handler
  837. *
  838. * This handler cleans up the struct af_alg_async_req upon completion of the
  839. * AIO operation.
  840. *
  841. * The number of bytes to be generated with the AIO operation must be set
  842. * in areq->outlen before the AIO callback handler is invoked.
  843. */
  844. void af_alg_async_cb(struct crypto_async_request *_req, int err)
  845. {
  846. struct af_alg_async_req *areq = _req->data;
  847. struct sock *sk = areq->sk;
  848. struct kiocb *iocb = areq->iocb;
  849. unsigned int resultlen;
  850. lock_sock(sk);
  851. /* Buffer size written by crypto operation. */
  852. resultlen = areq->outlen;
  853. af_alg_free_areq_sgls(areq);
  854. sock_kfree_s(sk, areq, areq->areqlen);
  855. __sock_put(sk);
  856. iocb->ki_complete(iocb, err ? err : resultlen, 0);
  857. release_sock(sk);
  858. }
  859. EXPORT_SYMBOL_GPL(af_alg_async_cb);
  860. /**
  861. * af_alg_poll - poll system call handler
  862. */
  863. unsigned int af_alg_poll(struct file *file, struct socket *sock,
  864. poll_table *wait)
  865. {
  866. struct sock *sk = sock->sk;
  867. struct alg_sock *ask = alg_sk(sk);
  868. struct af_alg_ctx *ctx = ask->private;
  869. unsigned int mask;
  870. sock_poll_wait(file, sk_sleep(sk), wait);
  871. mask = 0;
  872. if (!ctx->more || ctx->used)
  873. mask |= POLLIN | POLLRDNORM;
  874. if (af_alg_writable(sk))
  875. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  876. return mask;
  877. }
  878. EXPORT_SYMBOL_GPL(af_alg_poll);
  879. /**
  880. * af_alg_alloc_areq - allocate struct af_alg_async_req
  881. *
  882. * @sk socket of connection to user space
  883. * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
  884. * @return allocated data structure or ERR_PTR upon error
  885. */
  886. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  887. unsigned int areqlen)
  888. {
  889. struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
  890. if (unlikely(!areq))
  891. return ERR_PTR(-ENOMEM);
  892. areq->areqlen = areqlen;
  893. areq->sk = sk;
  894. areq->last_rsgl = NULL;
  895. INIT_LIST_HEAD(&areq->rsgl_list);
  896. areq->tsgl = NULL;
  897. areq->tsgl_entries = 0;
  898. return areq;
  899. }
  900. EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
  901. /**
  902. * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
  903. * operation
  904. *
  905. * @sk socket of connection to user space
  906. * @msg user space message
  907. * @flags flags used to invoke recvmsg with
  908. * @areq instance of the cryptographic request that will hold the RX SGL
  909. * @maxsize maximum number of bytes to be pulled from user space
  910. * @outlen number of bytes in the RX SGL
  911. * @return 0 on success, < 0 upon error
  912. */
  913. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  914. struct af_alg_async_req *areq, size_t maxsize,
  915. size_t *outlen)
  916. {
  917. struct alg_sock *ask = alg_sk(sk);
  918. struct af_alg_ctx *ctx = ask->private;
  919. size_t len = 0;
  920. while (maxsize > len && msg_data_left(msg)) {
  921. struct af_alg_rsgl *rsgl;
  922. size_t seglen;
  923. int err;
  924. /* limit the amount of readable buffers */
  925. if (!af_alg_readable(sk))
  926. break;
  927. if (!ctx->used) {
  928. err = af_alg_wait_for_data(sk, flags);
  929. if (err)
  930. return err;
  931. }
  932. seglen = min_t(size_t, (maxsize - len),
  933. msg_data_left(msg));
  934. if (list_empty(&areq->rsgl_list)) {
  935. rsgl = &areq->first_rsgl;
  936. } else {
  937. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  938. if (unlikely(!rsgl))
  939. return -ENOMEM;
  940. }
  941. rsgl->sgl.npages = 0;
  942. list_add_tail(&rsgl->list, &areq->rsgl_list);
  943. /* make one iovec available as scatterlist */
  944. err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
  945. if (err < 0)
  946. return err;
  947. /* chain the new scatterlist with previous one */
  948. if (areq->last_rsgl)
  949. af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
  950. areq->last_rsgl = rsgl;
  951. len += err;
  952. ctx->rcvused += err;
  953. rsgl->sg_num_bytes = err;
  954. iov_iter_advance(&msg->msg_iter, err);
  955. }
  956. *outlen = len;
  957. return 0;
  958. }
  959. EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
  960. static int __init af_alg_init(void)
  961. {
  962. int err = proto_register(&alg_proto, 0);
  963. if (err)
  964. goto out;
  965. err = sock_register(&alg_family);
  966. if (err != 0)
  967. goto out_unregister_proto;
  968. out:
  969. return err;
  970. out_unregister_proto:
  971. proto_unregister(&alg_proto);
  972. goto out;
  973. }
  974. static void __exit af_alg_exit(void)
  975. {
  976. sock_unregister(PF_ALG);
  977. proto_unregister(&alg_proto);
  978. }
  979. module_init(af_alg_init);
  980. module_exit(af_alg_exit);
  981. MODULE_LICENSE("GPL");
  982. MODULE_ALIAS_NETPROTO(AF_ALG);