af_alg.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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_obj(*node);
  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_unsized *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_unmark_end(sg + MAX_SGL_ENTS - 1);
  499. sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
  500. }
  501. list_add_tail(&sgl->list, &ctx->tsgl_list);
  502. }
  503. return 0;
  504. }
  505. /**
  506. * af_alg_count_tsgl - Count number of TX SG entries
  507. *
  508. * The counting starts from the beginning of the SGL to @bytes.
  509. *
  510. * @sk: socket of connection to user space
  511. * @bytes: Count the number of SG entries holding given number of bytes.
  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)
  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. sgl_count++;
  527. if (sg[i].length >= bytes)
  528. return sgl_count;
  529. bytes -= sg[i].length;
  530. }
  531. }
  532. return sgl_count;
  533. }
  534. EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
  535. /**
  536. * af_alg_pull_tsgl - Release the specified buffers from TX SGL
  537. *
  538. * If @dst is non-null, reassign the pages to @dst. The caller must release
  539. * the pages.
  540. *
  541. * @sk: socket of connection to user space
  542. * @used: Number of bytes to pull from TX SGL
  543. * @dst: If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
  544. * caller must release the buffers in dst.
  545. */
  546. void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst)
  547. {
  548. struct alg_sock *ask = alg_sk(sk);
  549. struct af_alg_ctx *ctx = ask->private;
  550. struct af_alg_tsgl *sgl;
  551. struct scatterlist *sg;
  552. unsigned int i, j = 0;
  553. while (!list_empty(&ctx->tsgl_list)) {
  554. sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
  555. list);
  556. sg = sgl->sg;
  557. for (i = 0; i < sgl->cur; i++) {
  558. size_t plen = min_t(size_t, used, sg[i].length);
  559. struct page *page = sg_page(sg + i);
  560. if (!page)
  561. continue;
  562. /*
  563. * Assumption: caller created af_alg_count_tsgl(len)
  564. * SG entries in dst.
  565. */
  566. if (dst && plen) {
  567. /* reassign page to dst */
  568. get_page(page);
  569. sg_set_page(dst + j, page, plen, sg[i].offset);
  570. j++;
  571. }
  572. sg[i].length -= plen;
  573. sg[i].offset += plen;
  574. used -= plen;
  575. ctx->used -= plen;
  576. if (sg[i].length)
  577. return;
  578. put_page(page);
  579. sg_assign_page(sg + i, NULL);
  580. }
  581. list_del(&sgl->list);
  582. sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
  583. }
  584. if (!ctx->used)
  585. ctx->merge = 0;
  586. ctx->init = ctx->more;
  587. }
  588. EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
  589. /**
  590. * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
  591. *
  592. * @areq: Request holding the TX and RX SGL
  593. */
  594. static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
  595. {
  596. struct sock *sk = areq->sk;
  597. struct alg_sock *ask = alg_sk(sk);
  598. struct af_alg_ctx *ctx = ask->private;
  599. struct af_alg_rsgl *rsgl, *tmp;
  600. struct scatterlist *tsgl;
  601. struct scatterlist *sg;
  602. unsigned int i;
  603. list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
  604. atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
  605. af_alg_free_sg(&rsgl->sgl);
  606. list_del(&rsgl->list);
  607. if (rsgl != &areq->first_rsgl)
  608. sock_kfree_s(sk, rsgl, sizeof(*rsgl));
  609. }
  610. tsgl = areq->tsgl;
  611. if (tsgl) {
  612. for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
  613. if (!sg_page(sg))
  614. continue;
  615. put_page(sg_page(sg));
  616. }
  617. sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
  618. }
  619. }
  620. /**
  621. * af_alg_wait_for_wmem - wait for availability of writable memory
  622. *
  623. * @sk: socket of connection to user space
  624. * @flags: If MSG_DONTWAIT is set, then only report if function would sleep
  625. * Return: 0 when writable memory is available, < 0 upon error
  626. */
  627. static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
  628. {
  629. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  630. int err = -ERESTARTSYS;
  631. long timeout;
  632. if (flags & MSG_DONTWAIT)
  633. return -EAGAIN;
  634. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  635. add_wait_queue(sk_sleep(sk), &wait);
  636. for (;;) {
  637. if (signal_pending(current))
  638. break;
  639. timeout = MAX_SCHEDULE_TIMEOUT;
  640. if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
  641. err = 0;
  642. break;
  643. }
  644. }
  645. remove_wait_queue(sk_sleep(sk), &wait);
  646. return err;
  647. }
  648. /**
  649. * af_alg_wmem_wakeup - wakeup caller when writable memory is available
  650. *
  651. * @sk: socket of connection to user space
  652. */
  653. void af_alg_wmem_wakeup(struct sock *sk)
  654. {
  655. struct socket_wq *wq;
  656. if (!af_alg_writable(sk))
  657. return;
  658. rcu_read_lock();
  659. wq = rcu_dereference(sk->sk_wq);
  660. if (skwq_has_sleeper(wq))
  661. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  662. EPOLLRDNORM |
  663. EPOLLRDBAND);
  664. sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN);
  665. rcu_read_unlock();
  666. }
  667. EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
  668. /**
  669. * af_alg_wait_for_data - wait for availability of TX data
  670. *
  671. * @sk: socket of connection to user space
  672. * @flags: If MSG_DONTWAIT is set, then only report if function would sleep
  673. * @min: Set to minimum request size if partial requests are allowed.
  674. * Return: 0 when writable memory is available, < 0 upon error
  675. */
  676. int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
  677. {
  678. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  679. struct alg_sock *ask = alg_sk(sk);
  680. struct af_alg_ctx *ctx = ask->private;
  681. long timeout;
  682. int err = -ERESTARTSYS;
  683. if (flags & MSG_DONTWAIT)
  684. return -EAGAIN;
  685. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  686. add_wait_queue(sk_sleep(sk), &wait);
  687. for (;;) {
  688. if (signal_pending(current))
  689. break;
  690. timeout = MAX_SCHEDULE_TIMEOUT;
  691. if (sk_wait_event(sk, &timeout,
  692. ctx->init && (!ctx->more ||
  693. (min && ctx->used >= min)),
  694. &wait)) {
  695. err = 0;
  696. break;
  697. }
  698. }
  699. remove_wait_queue(sk_sleep(sk), &wait);
  700. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  701. return err;
  702. }
  703. EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
  704. /**
  705. * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
  706. *
  707. * @sk: socket of connection to user space
  708. */
  709. static void af_alg_data_wakeup(struct sock *sk)
  710. {
  711. struct alg_sock *ask = alg_sk(sk);
  712. struct af_alg_ctx *ctx = ask->private;
  713. struct socket_wq *wq;
  714. if (!ctx->used)
  715. return;
  716. rcu_read_lock();
  717. wq = rcu_dereference(sk->sk_wq);
  718. if (skwq_has_sleeper(wq))
  719. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  720. EPOLLRDNORM |
  721. EPOLLRDBAND);
  722. sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT);
  723. rcu_read_unlock();
  724. }
  725. /**
  726. * af_alg_sendmsg - implementation of sendmsg system call handler
  727. *
  728. * The sendmsg system call handler obtains the user data and stores it
  729. * in ctx->tsgl_list. This implies allocation of the required numbers of
  730. * struct af_alg_tsgl.
  731. *
  732. * In addition, the ctx is filled with the information sent via CMSG.
  733. *
  734. * @sock: socket of connection to user space
  735. * @msg: message from user space
  736. * @size: size of message from user space
  737. * @ivsize: the size of the IV for the cipher operation to verify that the
  738. * user-space-provided IV has the right size
  739. * Return: the number of copied data upon success, < 0 upon error
  740. */
  741. int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
  742. unsigned int ivsize)
  743. {
  744. struct sock *sk = sock->sk;
  745. struct alg_sock *ask = alg_sk(sk);
  746. struct af_alg_ctx *ctx = ask->private;
  747. struct af_alg_tsgl *sgl;
  748. struct af_alg_control con = {};
  749. long copied = 0;
  750. bool enc = false;
  751. bool init = false;
  752. int err = 0;
  753. if (msg->msg_controllen) {
  754. err = af_alg_cmsg_send(msg, &con);
  755. if (err)
  756. return err;
  757. init = true;
  758. switch (con.op) {
  759. case ALG_OP_ENCRYPT:
  760. enc = true;
  761. break;
  762. case ALG_OP_DECRYPT:
  763. enc = false;
  764. break;
  765. default:
  766. return -EINVAL;
  767. }
  768. if (con.iv && con.iv->ivlen != ivsize)
  769. return -EINVAL;
  770. }
  771. lock_sock(sk);
  772. if (ctx->write) {
  773. release_sock(sk);
  774. return -EBUSY;
  775. }
  776. ctx->write = true;
  777. if (ctx->init && !ctx->more) {
  778. if (ctx->used) {
  779. err = -EINVAL;
  780. goto unlock;
  781. }
  782. pr_info_once(
  783. "%s sent an empty control message without MSG_MORE.\n",
  784. current->comm);
  785. }
  786. ctx->init = true;
  787. if (init) {
  788. ctx->enc = enc;
  789. if (con.iv)
  790. memcpy(ctx->iv, con.iv->iv, ivsize);
  791. ctx->aead_assoclen = con.aead_assoclen;
  792. }
  793. while (size) {
  794. struct scatterlist *sg;
  795. size_t len = size;
  796. ssize_t plen;
  797. /* use the existing memory in an allocated page */
  798. if (ctx->merge && !(msg->msg_flags & MSG_SPLICE_PAGES)) {
  799. sgl = list_entry(ctx->tsgl_list.prev,
  800. struct af_alg_tsgl, list);
  801. sg = sgl->sg + sgl->cur - 1;
  802. len = min_t(size_t, len,
  803. PAGE_SIZE - sg->offset - sg->length);
  804. err = memcpy_from_msg(page_address(sg_page(sg)) +
  805. sg->offset + sg->length,
  806. msg, len);
  807. if (err)
  808. goto unlock;
  809. sg->length += len;
  810. ctx->merge = (sg->offset + sg->length) &
  811. (PAGE_SIZE - 1);
  812. ctx->used += len;
  813. copied += len;
  814. size -= len;
  815. continue;
  816. }
  817. ctx->merge = 0;
  818. if (!af_alg_writable(sk)) {
  819. err = af_alg_wait_for_wmem(sk, msg->msg_flags);
  820. if (err)
  821. goto unlock;
  822. }
  823. /* allocate a new page */
  824. len = min_t(unsigned long, len, af_alg_sndbuf(sk));
  825. err = af_alg_alloc_tsgl(sk);
  826. if (err)
  827. goto unlock;
  828. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
  829. list);
  830. sg = sgl->sg;
  831. if (sgl->cur)
  832. sg_unmark_end(sg + sgl->cur - 1);
  833. if (msg->msg_flags & MSG_SPLICE_PAGES) {
  834. struct sg_table sgtable = {
  835. .sgl = sg,
  836. .nents = sgl->cur,
  837. .orig_nents = sgl->cur,
  838. };
  839. plen = extract_iter_to_sg(&msg->msg_iter, len, &sgtable,
  840. MAX_SGL_ENTS - sgl->cur, 0);
  841. if (plen < 0) {
  842. err = plen;
  843. goto unlock;
  844. }
  845. for (; sgl->cur < sgtable.nents; sgl->cur++)
  846. get_page(sg_page(&sg[sgl->cur]));
  847. len -= plen;
  848. ctx->used += plen;
  849. copied += plen;
  850. size -= plen;
  851. } else {
  852. do {
  853. struct page *pg;
  854. unsigned int i = sgl->cur;
  855. plen = min_t(size_t, len, PAGE_SIZE);
  856. pg = alloc_page(GFP_KERNEL);
  857. if (!pg) {
  858. err = -ENOMEM;
  859. goto unlock;
  860. }
  861. sg_assign_page(sg + i, pg);
  862. err = memcpy_from_msg(
  863. page_address(sg_page(sg + i)),
  864. msg, plen);
  865. if (err) {
  866. __free_page(sg_page(sg + i));
  867. sg_assign_page(sg + i, NULL);
  868. goto unlock;
  869. }
  870. sg[i].length = plen;
  871. len -= plen;
  872. ctx->used += plen;
  873. copied += plen;
  874. size -= plen;
  875. sgl->cur++;
  876. } while (len && sgl->cur < MAX_SGL_ENTS);
  877. ctx->merge = plen & (PAGE_SIZE - 1);
  878. }
  879. if (!size)
  880. sg_mark_end(sg + sgl->cur - 1);
  881. }
  882. err = 0;
  883. ctx->more = msg->msg_flags & MSG_MORE;
  884. unlock:
  885. af_alg_data_wakeup(sk);
  886. ctx->write = false;
  887. release_sock(sk);
  888. return copied ?: err;
  889. }
  890. EXPORT_SYMBOL_GPL(af_alg_sendmsg);
  891. /**
  892. * af_alg_free_resources - release resources required for crypto request
  893. * @areq: Request holding the TX and RX SGL
  894. */
  895. void af_alg_free_resources(struct af_alg_async_req *areq)
  896. {
  897. struct sock *sk = areq->sk;
  898. struct af_alg_ctx *ctx;
  899. af_alg_free_areq_sgls(areq);
  900. sock_kfree_s(sk, areq, areq->areqlen);
  901. ctx = alg_sk(sk)->private;
  902. ctx->inflight = false;
  903. }
  904. EXPORT_SYMBOL_GPL(af_alg_free_resources);
  905. /**
  906. * af_alg_async_cb - AIO callback handler
  907. * @data: async request completion data
  908. * @err: if non-zero, error result to be returned via ki_complete();
  909. * otherwise return the AIO output length via ki_complete().
  910. *
  911. * This handler cleans up the struct af_alg_async_req upon completion of the
  912. * AIO operation.
  913. *
  914. * The number of bytes to be generated with the AIO operation must be set
  915. * in areq->outlen before the AIO callback handler is invoked.
  916. */
  917. void af_alg_async_cb(void *data, int err)
  918. {
  919. struct af_alg_async_req *areq = data;
  920. struct sock *sk = areq->sk;
  921. struct kiocb *iocb = areq->iocb;
  922. unsigned int resultlen;
  923. /* Buffer size written by crypto operation. */
  924. resultlen = areq->outlen;
  925. af_alg_free_resources(areq);
  926. sock_put(sk);
  927. iocb->ki_complete(iocb, err ? err : (int)resultlen);
  928. }
  929. EXPORT_SYMBOL_GPL(af_alg_async_cb);
  930. /**
  931. * af_alg_poll - poll system call handler
  932. * @file: file pointer
  933. * @sock: socket to poll
  934. * @wait: poll_table
  935. */
  936. __poll_t af_alg_poll(struct file *file, struct socket *sock,
  937. poll_table *wait)
  938. {
  939. struct sock *sk = sock->sk;
  940. struct alg_sock *ask = alg_sk(sk);
  941. struct af_alg_ctx *ctx = ask->private;
  942. __poll_t mask;
  943. sock_poll_wait(file, sock, wait);
  944. mask = 0;
  945. if (!ctx->more || ctx->used)
  946. mask |= EPOLLIN | EPOLLRDNORM;
  947. if (af_alg_writable(sk))
  948. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  949. return mask;
  950. }
  951. EXPORT_SYMBOL_GPL(af_alg_poll);
  952. /**
  953. * af_alg_alloc_areq - allocate struct af_alg_async_req
  954. *
  955. * @sk: socket of connection to user space
  956. * @areqlen: size of struct af_alg_async_req + crypto_*_reqsize
  957. * Return: allocated data structure or ERR_PTR upon error
  958. */
  959. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  960. unsigned int areqlen)
  961. {
  962. struct af_alg_ctx *ctx = alg_sk(sk)->private;
  963. struct af_alg_async_req *areq;
  964. /* Only one AIO request can be in flight. */
  965. if (ctx->inflight)
  966. return ERR_PTR(-EBUSY);
  967. areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
  968. if (unlikely(!areq))
  969. return ERR_PTR(-ENOMEM);
  970. memset(areq, 0, areqlen);
  971. ctx->inflight = true;
  972. areq->areqlen = areqlen;
  973. areq->sk = sk;
  974. areq->first_rsgl.sgl.sgt.sgl = areq->first_rsgl.sgl.sgl;
  975. INIT_LIST_HEAD(&areq->rsgl_list);
  976. return areq;
  977. }
  978. EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
  979. /**
  980. * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
  981. * operation
  982. *
  983. * @sk: socket of connection to user space
  984. * @msg: user space message
  985. * @flags: flags used to invoke recvmsg with
  986. * @areq: instance of the cryptographic request that will hold the RX SGL
  987. * @maxsize: maximum number of bytes to be pulled from user space
  988. * @outlen: number of bytes in the RX SGL
  989. * Return: 0 on success, < 0 upon error
  990. */
  991. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  992. struct af_alg_async_req *areq, size_t maxsize,
  993. size_t *outlen)
  994. {
  995. struct alg_sock *ask = alg_sk(sk);
  996. struct af_alg_ctx *ctx = ask->private;
  997. size_t len = 0;
  998. while (maxsize > len && msg_data_left(msg)) {
  999. struct af_alg_rsgl *rsgl;
  1000. ssize_t err;
  1001. size_t seglen;
  1002. /* limit the amount of readable buffers */
  1003. if (!af_alg_readable(sk))
  1004. break;
  1005. seglen = min_t(size_t, (maxsize - len),
  1006. msg_data_left(msg));
  1007. /* Never pin more pages than the remaining RX accounting budget. */
  1008. seglen = min_t(size_t, seglen, af_alg_rcvbuf(sk));
  1009. if (list_empty(&areq->rsgl_list)) {
  1010. rsgl = &areq->first_rsgl;
  1011. } else {
  1012. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  1013. if (unlikely(!rsgl))
  1014. return -ENOMEM;
  1015. }
  1016. rsgl->sgl.need_unpin =
  1017. iov_iter_extract_will_pin(&msg->msg_iter);
  1018. rsgl->sgl.sgt.sgl = rsgl->sgl.sgl;
  1019. rsgl->sgl.sgt.nents = 0;
  1020. rsgl->sgl.sgt.orig_nents = 0;
  1021. list_add_tail(&rsgl->list, &areq->rsgl_list);
  1022. sg_init_table(rsgl->sgl.sgt.sgl, ALG_MAX_PAGES);
  1023. err = extract_iter_to_sg(&msg->msg_iter, seglen, &rsgl->sgl.sgt,
  1024. ALG_MAX_PAGES, 0);
  1025. if (err < 0) {
  1026. rsgl->sg_num_bytes = 0;
  1027. return err;
  1028. }
  1029. sg_mark_end(rsgl->sgl.sgt.sgl + rsgl->sgl.sgt.nents - 1);
  1030. /* chain the new scatterlist with previous one */
  1031. if (areq->last_rsgl)
  1032. af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
  1033. areq->last_rsgl = rsgl;
  1034. len += err;
  1035. atomic_add(err, &ctx->rcvused);
  1036. rsgl->sg_num_bytes = err;
  1037. }
  1038. *outlen = len;
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
  1042. static int __init af_alg_init(void)
  1043. {
  1044. int err = proto_register(&alg_proto, 0);
  1045. if (err)
  1046. goto out;
  1047. err = sock_register(&alg_family);
  1048. if (err != 0)
  1049. goto out_unregister_proto;
  1050. out:
  1051. return err;
  1052. out_unregister_proto:
  1053. proto_unregister(&alg_proto);
  1054. goto out;
  1055. }
  1056. static void __exit af_alg_exit(void)
  1057. {
  1058. sock_unregister(PF_ALG);
  1059. proto_unregister(&alg_proto);
  1060. }
  1061. module_init(af_alg_init);
  1062. module_exit(af_alg_exit);
  1063. MODULE_DESCRIPTION("Crypto userspace interface");
  1064. MODULE_LICENSE("GPL");
  1065. MODULE_ALIAS_NETPROTO(AF_ALG);