af_alg.c 29 KB

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