af_alg.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * af_alg: User-space algorithm interface
  4. *
  5. * This file provides the user-space API for algorithms.
  6. *
  7. * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
  8. */
  9. #include <linux/atomic.h>
  10. #include <crypto/if_alg.h>
  11. #include <linux/crypto.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/net.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/security.h>
  21. struct alg_type_list {
  22. const struct af_alg_type *type;
  23. struct list_head list;
  24. };
  25. static atomic_long_t alg_memory_allocated;
  26. static struct proto alg_proto = {
  27. .name = "ALG",
  28. .owner = THIS_MODULE,
  29. .memory_allocated = &alg_memory_allocated,
  30. .obj_size = sizeof(struct alg_sock),
  31. };
  32. static LIST_HEAD(alg_types);
  33. static DECLARE_RWSEM(alg_types_sem);
  34. static const struct af_alg_type *alg_get_type(const char *name)
  35. {
  36. const struct af_alg_type *type = ERR_PTR(-ENOENT);
  37. struct alg_type_list *node;
  38. down_read(&alg_types_sem);
  39. list_for_each_entry(node, &alg_types, list) {
  40. if (strcmp(node->type->name, name))
  41. continue;
  42. if (try_module_get(node->type->owner))
  43. type = node->type;
  44. break;
  45. }
  46. up_read(&alg_types_sem);
  47. return type;
  48. }
  49. int af_alg_register_type(const struct af_alg_type *type)
  50. {
  51. struct alg_type_list *node;
  52. int err = -EEXIST;
  53. down_write(&alg_types_sem);
  54. list_for_each_entry(node, &alg_types, list) {
  55. if (!strcmp(node->type->name, type->name))
  56. goto unlock;
  57. }
  58. node = kmalloc(sizeof(*node), GFP_KERNEL);
  59. err = -ENOMEM;
  60. if (!node)
  61. goto unlock;
  62. type->ops->owner = THIS_MODULE;
  63. if (type->ops_nokey)
  64. type->ops_nokey->owner = THIS_MODULE;
  65. node->type = type;
  66. list_add(&node->list, &alg_types);
  67. err = 0;
  68. unlock:
  69. up_write(&alg_types_sem);
  70. return err;
  71. }
  72. EXPORT_SYMBOL_GPL(af_alg_register_type);
  73. int af_alg_unregister_type(const struct af_alg_type *type)
  74. {
  75. struct alg_type_list *node;
  76. int err = -ENOENT;
  77. down_write(&alg_types_sem);
  78. list_for_each_entry(node, &alg_types, list) {
  79. if (strcmp(node->type->name, type->name))
  80. continue;
  81. list_del(&node->list);
  82. kfree(node);
  83. err = 0;
  84. break;
  85. }
  86. up_write(&alg_types_sem);
  87. return err;
  88. }
  89. EXPORT_SYMBOL_GPL(af_alg_unregister_type);
  90. static void alg_do_release(const struct af_alg_type *type, void *private)
  91. {
  92. if (!type)
  93. return;
  94. type->release(private);
  95. module_put(type->owner);
  96. }
  97. int af_alg_release(struct socket *sock)
  98. {
  99. if (sock->sk) {
  100. sock_put(sock->sk);
  101. sock->sk = NULL;
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(af_alg_release);
  106. void af_alg_release_parent(struct sock *sk)
  107. {
  108. struct alg_sock *ask = alg_sk(sk);
  109. unsigned int nokey = atomic_read(&ask->nokey_refcnt);
  110. sk = ask->parent;
  111. ask = alg_sk(sk);
  112. if (nokey)
  113. atomic_dec(&ask->nokey_refcnt);
  114. if (atomic_dec_and_test(&ask->refcnt))
  115. sock_put(sk);
  116. }
  117. EXPORT_SYMBOL_GPL(af_alg_release_parent);
  118. static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  119. {
  120. const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
  121. struct sock *sk = sock->sk;
  122. struct alg_sock *ask = alg_sk(sk);
  123. struct sockaddr_alg_new *sa = (void *)uaddr;
  124. const struct af_alg_type *type;
  125. void *private;
  126. int err;
  127. if (sock->state == SS_CONNECTED)
  128. return -EINVAL;
  129. BUILD_BUG_ON(offsetof(struct sockaddr_alg_new, salg_name) !=
  130. offsetof(struct sockaddr_alg, salg_name));
  131. BUILD_BUG_ON(offsetof(struct sockaddr_alg, salg_name) != sizeof(*sa));
  132. if (addr_len < sizeof(*sa) + 1)
  133. return -EINVAL;
  134. /* If caller uses non-allowed flag, return error. */
  135. if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
  136. return -EINVAL;
  137. sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
  138. sa->salg_name[addr_len - sizeof(*sa) - 1] = 0;
  139. type = alg_get_type(sa->salg_type);
  140. if (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, sa->salg_feat, sa->salg_mask);
  147. if (IS_ERR(private)) {
  148. module_put(type->owner);
  149. return PTR_ERR(private);
  150. }
  151. err = -EBUSY;
  152. lock_sock(sk);
  153. if (atomic_read(&ask->refcnt))
  154. goto unlock;
  155. swap(ask->type, type);
  156. swap(ask->private, private);
  157. err = 0;
  158. unlock:
  159. release_sock(sk);
  160. alg_do_release(type, private);
  161. return err;
  162. }
  163. static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen)
  164. {
  165. struct alg_sock *ask = alg_sk(sk);
  166. const struct af_alg_type *type = ask->type;
  167. u8 *key;
  168. int err;
  169. key = sock_kmalloc(sk, keylen, GFP_KERNEL);
  170. if (!key)
  171. return -ENOMEM;
  172. err = -EFAULT;
  173. if (copy_from_sockptr(key, ukey, keylen))
  174. goto out;
  175. err = type->setkey(ask->private, key, keylen);
  176. out:
  177. sock_kzfree_s(sk, key, keylen);
  178. return err;
  179. }
  180. static int alg_setsockopt(struct socket *sock, int level, int optname,
  181. sockptr_t optval, unsigned int optlen)
  182. {
  183. struct sock *sk = sock->sk;
  184. struct alg_sock *ask = alg_sk(sk);
  185. const struct af_alg_type *type;
  186. int err = -EBUSY;
  187. lock_sock(sk);
  188. if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
  189. goto unlock;
  190. type = ask->type;
  191. err = -ENOPROTOOPT;
  192. if (level != SOL_ALG || !type)
  193. goto unlock;
  194. switch (optname) {
  195. case ALG_SET_KEY:
  196. if (sock->state == SS_CONNECTED)
  197. goto unlock;
  198. if (!type->setkey)
  199. goto unlock;
  200. err = alg_setkey(sk, optval, optlen);
  201. break;
  202. case ALG_SET_AEAD_AUTHSIZE:
  203. if (sock->state == SS_CONNECTED)
  204. goto unlock;
  205. if (!type->setauthsize)
  206. goto unlock;
  207. err = type->setauthsize(ask->private, optlen);
  208. break;
  209. case ALG_SET_DRBG_ENTROPY:
  210. if (sock->state == SS_CONNECTED)
  211. goto unlock;
  212. if (!type->setentropy)
  213. goto unlock;
  214. err = type->setentropy(ask->private, optval, optlen);
  215. }
  216. unlock:
  217. release_sock(sk);
  218. return err;
  219. }
  220. int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
  221. {
  222. struct alg_sock *ask = alg_sk(sk);
  223. const struct af_alg_type *type;
  224. struct sock *sk2;
  225. unsigned int nokey;
  226. int err;
  227. lock_sock(sk);
  228. type = ask->type;
  229. err = -EINVAL;
  230. if (!type)
  231. goto unlock;
  232. sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
  233. err = -ENOMEM;
  234. if (!sk2)
  235. goto unlock;
  236. sock_init_data(newsock, sk2);
  237. security_sock_graft(sk2, newsock);
  238. security_sk_clone(sk, sk2);
  239. /*
  240. * newsock->ops assigned here to allow type->accept call to override
  241. * them when required.
  242. */
  243. newsock->ops = type->ops;
  244. err = type->accept(ask->private, sk2);
  245. nokey = err == -ENOKEY;
  246. if (nokey && type->accept_nokey)
  247. err = type->accept_nokey(ask->private, sk2);
  248. if (err)
  249. goto unlock;
  250. if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
  251. sock_hold(sk);
  252. if (nokey) {
  253. atomic_inc(&ask->nokey_refcnt);
  254. atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
  255. }
  256. alg_sk(sk2)->parent = sk;
  257. alg_sk(sk2)->type = type;
  258. newsock->state = SS_CONNECTED;
  259. if (nokey)
  260. newsock->ops = type->ops_nokey;
  261. err = 0;
  262. unlock:
  263. release_sock(sk);
  264. return err;
  265. }
  266. EXPORT_SYMBOL_GPL(af_alg_accept);
  267. static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
  268. bool kern)
  269. {
  270. return af_alg_accept(sock->sk, newsock, kern);
  271. }
  272. static const struct proto_ops alg_proto_ops = {
  273. .family = PF_ALG,
  274. .owner = THIS_MODULE,
  275. .connect = sock_no_connect,
  276. .socketpair = sock_no_socketpair,
  277. .getname = sock_no_getname,
  278. .ioctl = sock_no_ioctl,
  279. .listen = sock_no_listen,
  280. .shutdown = sock_no_shutdown,
  281. .mmap = sock_no_mmap,
  282. .sendpage = sock_no_sendpage,
  283. .sendmsg = sock_no_sendmsg,
  284. .recvmsg = sock_no_recvmsg,
  285. .bind = alg_bind,
  286. .release = af_alg_release,
  287. .setsockopt = alg_setsockopt,
  288. .accept = alg_accept,
  289. };
  290. static void alg_sock_destruct(struct sock *sk)
  291. {
  292. struct alg_sock *ask = alg_sk(sk);
  293. alg_do_release(ask->type, ask->private);
  294. }
  295. static int alg_create(struct net *net, struct socket *sock, int protocol,
  296. int kern)
  297. {
  298. struct sock *sk;
  299. int err;
  300. if (sock->type != SOCK_SEQPACKET)
  301. return -ESOCKTNOSUPPORT;
  302. if (protocol != 0)
  303. return -EPROTONOSUPPORT;
  304. err = -ENOMEM;
  305. sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
  306. if (!sk)
  307. goto out;
  308. sock->ops = &alg_proto_ops;
  309. sock_init_data(sock, sk);
  310. sk->sk_destruct = alg_sock_destruct;
  311. return 0;
  312. out:
  313. return err;
  314. }
  315. static const struct net_proto_family alg_family = {
  316. .family = PF_ALG,
  317. .create = alg_create,
  318. .owner = THIS_MODULE,
  319. };
  320. int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
  321. {
  322. size_t off;
  323. ssize_t n;
  324. int npages, i;
  325. n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
  326. if (n < 0)
  327. return n;
  328. npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
  329. if (WARN_ON(npages == 0))
  330. return -EINVAL;
  331. /* Add one extra for linking */
  332. sg_init_table(sgl->sg, npages + 1);
  333. for (i = 0, len = n; i < npages; i++) {
  334. int plen = min_t(int, len, PAGE_SIZE - off);
  335. sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
  336. off = 0;
  337. len -= plen;
  338. }
  339. sg_mark_end(sgl->sg + npages - 1);
  340. sgl->npages = npages;
  341. return n;
  342. }
  343. EXPORT_SYMBOL_GPL(af_alg_make_sg);
  344. static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
  345. struct af_alg_sgl *sgl_new)
  346. {
  347. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  348. sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
  349. }
  350. void af_alg_free_sg(struct af_alg_sgl *sgl)
  351. {
  352. int i;
  353. for (i = 0; i < sgl->npages; i++)
  354. put_page(sgl->pages[i]);
  355. }
  356. EXPORT_SYMBOL_GPL(af_alg_free_sg);
  357. static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
  358. {
  359. struct cmsghdr *cmsg;
  360. for_each_cmsghdr(cmsg, msg) {
  361. if (!CMSG_OK(msg, cmsg))
  362. return -EINVAL;
  363. if (cmsg->cmsg_level != SOL_ALG)
  364. continue;
  365. switch (cmsg->cmsg_type) {
  366. case ALG_SET_IV:
  367. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
  368. return -EINVAL;
  369. con->iv = (void *)CMSG_DATA(cmsg);
  370. if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
  371. sizeof(*con->iv)))
  372. return -EINVAL;
  373. break;
  374. case ALG_SET_OP:
  375. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  376. return -EINVAL;
  377. con->op = *(u32 *)CMSG_DATA(cmsg);
  378. break;
  379. case ALG_SET_AEAD_ASSOCLEN:
  380. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  381. return -EINVAL;
  382. con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
  383. break;
  384. default:
  385. return -EINVAL;
  386. }
  387. }
  388. return 0;
  389. }
  390. /**
  391. * af_alg_alloc_tsgl - allocate the TX SGL
  392. *
  393. * @sk socket of connection to user space
  394. * @return: 0 upon success, < 0 upon error
  395. */
  396. static int af_alg_alloc_tsgl(struct sock *sk)
  397. {
  398. struct alg_sock *ask = alg_sk(sk);
  399. struct af_alg_ctx *ctx = ask->private;
  400. struct af_alg_tsgl *sgl;
  401. struct scatterlist *sg = NULL;
  402. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
  403. if (!list_empty(&ctx->tsgl_list))
  404. sg = sgl->sg;
  405. if (!sg || sgl->cur >= MAX_SGL_ENTS) {
  406. sgl = sock_kmalloc(sk,
  407. struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
  408. GFP_KERNEL);
  409. if (!sgl)
  410. return -ENOMEM;
  411. sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
  412. sgl->cur = 0;
  413. if (sg)
  414. sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
  415. list_add_tail(&sgl->list, &ctx->tsgl_list);
  416. }
  417. return 0;
  418. }
  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. const struct alg_sock *ask = alg_sk(sk);
  433. const struct af_alg_ctx *ctx = ask->private;
  434. const struct af_alg_tsgl *sgl;
  435. unsigned int i;
  436. unsigned int sgl_count = 0;
  437. if (!bytes)
  438. return 0;
  439. list_for_each_entry(sgl, &ctx->tsgl_list, list) {
  440. const 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, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
  522. }
  523. if (!ctx->used)
  524. ctx->merge = 0;
  525. ctx->init = ctx->more;
  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. static 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. /**
  560. * af_alg_wait_for_wmem - wait for availability of writable memory
  561. *
  562. * @sk socket of connection to user space
  563. * @flags If MSG_DONTWAIT is set, then only report if function would sleep
  564. * @return 0 when writable memory is available, < 0 upon error
  565. */
  566. static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
  567. {
  568. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  569. int err = -ERESTARTSYS;
  570. long timeout;
  571. if (flags & MSG_DONTWAIT)
  572. return -EAGAIN;
  573. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  574. add_wait_queue(sk_sleep(sk), &wait);
  575. for (;;) {
  576. if (signal_pending(current))
  577. break;
  578. timeout = MAX_SCHEDULE_TIMEOUT;
  579. if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
  580. err = 0;
  581. break;
  582. }
  583. }
  584. remove_wait_queue(sk_sleep(sk), &wait);
  585. return err;
  586. }
  587. /**
  588. * af_alg_wmem_wakeup - wakeup caller when writable memory is available
  589. *
  590. * @sk socket of connection to user space
  591. */
  592. void af_alg_wmem_wakeup(struct sock *sk)
  593. {
  594. struct socket_wq *wq;
  595. if (!af_alg_writable(sk))
  596. return;
  597. rcu_read_lock();
  598. wq = rcu_dereference(sk->sk_wq);
  599. if (skwq_has_sleeper(wq))
  600. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  601. EPOLLRDNORM |
  602. EPOLLRDBAND);
  603. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  604. rcu_read_unlock();
  605. }
  606. EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
  607. /**
  608. * af_alg_wait_for_data - wait for availability of TX data
  609. *
  610. * @sk socket of connection to user space
  611. * @flags If MSG_DONTWAIT is set, then only report if function would sleep
  612. * @min Set to minimum request size if partial requests are allowed.
  613. * @return 0 when writable memory is available, < 0 upon error
  614. */
  615. int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
  616. {
  617. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  618. struct alg_sock *ask = alg_sk(sk);
  619. struct af_alg_ctx *ctx = ask->private;
  620. long timeout;
  621. int err = -ERESTARTSYS;
  622. if (flags & MSG_DONTWAIT)
  623. return -EAGAIN;
  624. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  625. add_wait_queue(sk_sleep(sk), &wait);
  626. for (;;) {
  627. if (signal_pending(current))
  628. break;
  629. timeout = MAX_SCHEDULE_TIMEOUT;
  630. if (sk_wait_event(sk, &timeout,
  631. ctx->init && (!ctx->more ||
  632. (min && ctx->used >= min)),
  633. &wait)) {
  634. err = 0;
  635. break;
  636. }
  637. }
  638. remove_wait_queue(sk_sleep(sk), &wait);
  639. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  640. return err;
  641. }
  642. EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
  643. /**
  644. * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
  645. *
  646. * @sk socket of connection to user space
  647. */
  648. static void af_alg_data_wakeup(struct sock *sk)
  649. {
  650. struct alg_sock *ask = alg_sk(sk);
  651. struct af_alg_ctx *ctx = ask->private;
  652. struct socket_wq *wq;
  653. if (!ctx->used)
  654. return;
  655. rcu_read_lock();
  656. wq = rcu_dereference(sk->sk_wq);
  657. if (skwq_has_sleeper(wq))
  658. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  659. EPOLLRDNORM |
  660. EPOLLRDBAND);
  661. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  662. rcu_read_unlock();
  663. }
  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 = false;
  690. bool init = false;
  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 = true;
  697. switch (con.op) {
  698. case ALG_OP_ENCRYPT:
  699. enc = true;
  700. break;
  701. case ALG_OP_DECRYPT:
  702. enc = false;
  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->init && !ctx->more) {
  712. if (ctx->used) {
  713. err = -EINVAL;
  714. goto unlock;
  715. }
  716. pr_info_once(
  717. "%s sent an empty control message without MSG_MORE.\n",
  718. current->comm);
  719. }
  720. ctx->init = true;
  721. if (init) {
  722. ctx->enc = enc;
  723. if (con.iv)
  724. memcpy(ctx->iv, con.iv->iv, ivsize);
  725. ctx->aead_assoclen = con.aead_assoclen;
  726. }
  727. while (size) {
  728. struct scatterlist *sg;
  729. size_t len = size;
  730. size_t plen;
  731. /* use the existing memory in an allocated page */
  732. if (ctx->merge) {
  733. sgl = list_entry(ctx->tsgl_list.prev,
  734. struct af_alg_tsgl, list);
  735. sg = sgl->sg + sgl->cur - 1;
  736. len = min_t(size_t, len,
  737. PAGE_SIZE - sg->offset - sg->length);
  738. err = memcpy_from_msg(page_address(sg_page(sg)) +
  739. sg->offset + sg->length,
  740. msg, len);
  741. if (err)
  742. goto unlock;
  743. sg->length += len;
  744. ctx->merge = (sg->offset + sg->length) &
  745. (PAGE_SIZE - 1);
  746. ctx->used += len;
  747. copied += len;
  748. size -= len;
  749. continue;
  750. }
  751. if (!af_alg_writable(sk)) {
  752. err = af_alg_wait_for_wmem(sk, msg->msg_flags);
  753. if (err)
  754. goto unlock;
  755. }
  756. /* allocate a new page */
  757. len = min_t(unsigned long, len, af_alg_sndbuf(sk));
  758. err = af_alg_alloc_tsgl(sk);
  759. if (err)
  760. goto unlock;
  761. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
  762. list);
  763. sg = sgl->sg;
  764. if (sgl->cur)
  765. sg_unmark_end(sg + sgl->cur - 1);
  766. do {
  767. unsigned int i = sgl->cur;
  768. plen = min_t(size_t, len, PAGE_SIZE);
  769. sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
  770. if (!sg_page(sg + i)) {
  771. err = -ENOMEM;
  772. goto unlock;
  773. }
  774. err = memcpy_from_msg(page_address(sg_page(sg + i)),
  775. msg, plen);
  776. if (err) {
  777. __free_page(sg_page(sg + i));
  778. sg_assign_page(sg + i, NULL);
  779. goto unlock;
  780. }
  781. sg[i].length = plen;
  782. len -= plen;
  783. ctx->used += plen;
  784. copied += plen;
  785. size -= plen;
  786. sgl->cur++;
  787. } while (len && sgl->cur < MAX_SGL_ENTS);
  788. if (!size)
  789. sg_mark_end(sg + sgl->cur - 1);
  790. ctx->merge = plen & (PAGE_SIZE - 1);
  791. }
  792. err = 0;
  793. ctx->more = msg->msg_flags & MSG_MORE;
  794. unlock:
  795. af_alg_data_wakeup(sk);
  796. release_sock(sk);
  797. return copied ?: err;
  798. }
  799. EXPORT_SYMBOL_GPL(af_alg_sendmsg);
  800. /**
  801. * af_alg_sendpage - sendpage system call handler
  802. *
  803. * This is a generic implementation of sendpage to fill ctx->tsgl_list.
  804. */
  805. ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
  806. int offset, size_t size, int flags)
  807. {
  808. struct sock *sk = sock->sk;
  809. struct alg_sock *ask = alg_sk(sk);
  810. struct af_alg_ctx *ctx = ask->private;
  811. struct af_alg_tsgl *sgl;
  812. int err = -EINVAL;
  813. if (flags & MSG_SENDPAGE_NOTLAST)
  814. flags |= MSG_MORE;
  815. lock_sock(sk);
  816. if (!ctx->more && ctx->used)
  817. goto unlock;
  818. if (!size)
  819. goto done;
  820. if (!af_alg_writable(sk)) {
  821. err = af_alg_wait_for_wmem(sk, flags);
  822. if (err)
  823. goto unlock;
  824. }
  825. err = af_alg_alloc_tsgl(sk);
  826. if (err)
  827. goto unlock;
  828. ctx->merge = 0;
  829. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
  830. if (sgl->cur)
  831. sg_unmark_end(sgl->sg + sgl->cur - 1);
  832. sg_mark_end(sgl->sg + sgl->cur);
  833. get_page(page);
  834. sg_set_page(sgl->sg + sgl->cur, page, size, offset);
  835. sgl->cur++;
  836. ctx->used += size;
  837. done:
  838. ctx->more = flags & MSG_MORE;
  839. unlock:
  840. af_alg_data_wakeup(sk);
  841. release_sock(sk);
  842. return err ?: size;
  843. }
  844. EXPORT_SYMBOL_GPL(af_alg_sendpage);
  845. /**
  846. * af_alg_free_resources - release resources required for crypto request
  847. */
  848. void af_alg_free_resources(struct af_alg_async_req *areq)
  849. {
  850. struct sock *sk = areq->sk;
  851. af_alg_free_areq_sgls(areq);
  852. sock_kfree_s(sk, areq, areq->areqlen);
  853. }
  854. EXPORT_SYMBOL_GPL(af_alg_free_resources);
  855. /**
  856. * af_alg_async_cb - AIO callback handler
  857. *
  858. * This handler cleans up the struct af_alg_async_req upon completion of the
  859. * AIO operation.
  860. *
  861. * The number of bytes to be generated with the AIO operation must be set
  862. * in areq->outlen before the AIO callback handler is invoked.
  863. */
  864. void af_alg_async_cb(struct crypto_async_request *_req, int err)
  865. {
  866. struct af_alg_async_req *areq = _req->data;
  867. struct sock *sk = areq->sk;
  868. struct kiocb *iocb = areq->iocb;
  869. unsigned int resultlen;
  870. /* Buffer size written by crypto operation. */
  871. resultlen = areq->outlen;
  872. af_alg_free_resources(areq);
  873. sock_put(sk);
  874. iocb->ki_complete(iocb, err ? err : (int)resultlen, 0);
  875. }
  876. EXPORT_SYMBOL_GPL(af_alg_async_cb);
  877. /**
  878. * af_alg_poll - poll system call handler
  879. */
  880. __poll_t af_alg_poll(struct file *file, struct socket *sock,
  881. poll_table *wait)
  882. {
  883. struct sock *sk = sock->sk;
  884. struct alg_sock *ask = alg_sk(sk);
  885. struct af_alg_ctx *ctx = ask->private;
  886. __poll_t mask;
  887. sock_poll_wait(file, sock, wait);
  888. mask = 0;
  889. if (!ctx->more || ctx->used)
  890. mask |= EPOLLIN | EPOLLRDNORM;
  891. if (af_alg_writable(sk))
  892. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  893. return mask;
  894. }
  895. EXPORT_SYMBOL_GPL(af_alg_poll);
  896. /**
  897. * af_alg_alloc_areq - allocate struct af_alg_async_req
  898. *
  899. * @sk socket of connection to user space
  900. * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
  901. * @return allocated data structure or ERR_PTR upon error
  902. */
  903. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  904. unsigned int areqlen)
  905. {
  906. struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
  907. if (unlikely(!areq))
  908. return ERR_PTR(-ENOMEM);
  909. areq->areqlen = areqlen;
  910. areq->sk = sk;
  911. areq->last_rsgl = NULL;
  912. INIT_LIST_HEAD(&areq->rsgl_list);
  913. areq->tsgl = NULL;
  914. areq->tsgl_entries = 0;
  915. return areq;
  916. }
  917. EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
  918. /**
  919. * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
  920. * operation
  921. *
  922. * @sk socket of connection to user space
  923. * @msg user space message
  924. * @flags flags used to invoke recvmsg with
  925. * @areq instance of the cryptographic request that will hold the RX SGL
  926. * @maxsize maximum number of bytes to be pulled from user space
  927. * @outlen number of bytes in the RX SGL
  928. * @return 0 on success, < 0 upon error
  929. */
  930. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  931. struct af_alg_async_req *areq, size_t maxsize,
  932. size_t *outlen)
  933. {
  934. struct alg_sock *ask = alg_sk(sk);
  935. struct af_alg_ctx *ctx = ask->private;
  936. size_t len = 0;
  937. while (maxsize > len && msg_data_left(msg)) {
  938. struct af_alg_rsgl *rsgl;
  939. size_t seglen;
  940. int err;
  941. /* limit the amount of readable buffers */
  942. if (!af_alg_readable(sk))
  943. break;
  944. seglen = min_t(size_t, (maxsize - len),
  945. msg_data_left(msg));
  946. if (list_empty(&areq->rsgl_list)) {
  947. rsgl = &areq->first_rsgl;
  948. } else {
  949. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  950. if (unlikely(!rsgl))
  951. return -ENOMEM;
  952. }
  953. rsgl->sgl.npages = 0;
  954. list_add_tail(&rsgl->list, &areq->rsgl_list);
  955. /* make one iovec available as scatterlist */
  956. err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
  957. if (err < 0) {
  958. rsgl->sg_num_bytes = 0;
  959. return err;
  960. }
  961. /* chain the new scatterlist with previous one */
  962. if (areq->last_rsgl)
  963. af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
  964. areq->last_rsgl = rsgl;
  965. len += err;
  966. atomic_add(err, &ctx->rcvused);
  967. rsgl->sg_num_bytes = err;
  968. iov_iter_advance(&msg->msg_iter, err);
  969. }
  970. *outlen = len;
  971. return 0;
  972. }
  973. EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
  974. static int __init af_alg_init(void)
  975. {
  976. int err = proto_register(&alg_proto, 0);
  977. if (err)
  978. goto out;
  979. err = sock_register(&alg_family);
  980. if (err != 0)
  981. goto out_unregister_proto;
  982. out:
  983. return err;
  984. out_unregister_proto:
  985. proto_unregister(&alg_proto);
  986. goto out;
  987. }
  988. static void __exit af_alg_exit(void)
  989. {
  990. sock_unregister(PF_ALG);
  991. proto_unregister(&alg_proto);
  992. }
  993. module_init(af_alg_init);
  994. module_exit(af_alg_exit);
  995. MODULE_LICENSE("GPL");
  996. MODULE_ALIAS_NETPROTO(AF_ALG);