af_alg.c 26 KB

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