af_alg.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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->init && !ctx->more) {
  796. if (ctx->used) {
  797. err = -EINVAL;
  798. goto unlock;
  799. }
  800. pr_info_once(
  801. "%s sent an empty control message without MSG_MORE.\n",
  802. current->comm);
  803. }
  804. ctx->init = true;
  805. if (init) {
  806. ctx->enc = enc;
  807. if (con.iv)
  808. memcpy(ctx->iv, con.iv->iv, ivsize);
  809. ctx->aead_assoclen = con.aead_assoclen;
  810. }
  811. while (size) {
  812. struct scatterlist *sg;
  813. size_t len = size;
  814. ssize_t plen;
  815. /* use the existing memory in an allocated page */
  816. if (ctx->merge && !(msg->msg_flags & MSG_SPLICE_PAGES)) {
  817. sgl = list_entry(ctx->tsgl_list.prev,
  818. struct af_alg_tsgl, list);
  819. sg = sgl->sg + sgl->cur - 1;
  820. len = min_t(size_t, len,
  821. PAGE_SIZE - sg->offset - sg->length);
  822. err = memcpy_from_msg(page_address(sg_page(sg)) +
  823. sg->offset + sg->length,
  824. msg, len);
  825. if (err)
  826. goto unlock;
  827. sg->length += len;
  828. ctx->merge = (sg->offset + sg->length) &
  829. (PAGE_SIZE - 1);
  830. ctx->used += len;
  831. copied += len;
  832. size -= len;
  833. continue;
  834. }
  835. if (!af_alg_writable(sk)) {
  836. err = af_alg_wait_for_wmem(sk, msg->msg_flags);
  837. if (err)
  838. goto unlock;
  839. }
  840. /* allocate a new page */
  841. len = min_t(unsigned long, len, af_alg_sndbuf(sk));
  842. err = af_alg_alloc_tsgl(sk);
  843. if (err)
  844. goto unlock;
  845. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
  846. list);
  847. sg = sgl->sg;
  848. if (sgl->cur)
  849. sg_unmark_end(sg + sgl->cur - 1);
  850. if (msg->msg_flags & MSG_SPLICE_PAGES) {
  851. struct sg_table sgtable = {
  852. .sgl = sg,
  853. .nents = sgl->cur,
  854. .orig_nents = sgl->cur,
  855. };
  856. plen = extract_iter_to_sg(&msg->msg_iter, len, &sgtable,
  857. MAX_SGL_ENTS - sgl->cur, 0);
  858. if (plen < 0) {
  859. err = plen;
  860. goto unlock;
  861. }
  862. for (; sgl->cur < sgtable.nents; sgl->cur++)
  863. get_page(sg_page(&sg[sgl->cur]));
  864. len -= plen;
  865. ctx->used += plen;
  866. copied += plen;
  867. size -= plen;
  868. ctx->merge = 0;
  869. } else {
  870. do {
  871. struct page *pg;
  872. unsigned int i = sgl->cur;
  873. plen = min_t(size_t, len, PAGE_SIZE);
  874. pg = alloc_page(GFP_KERNEL);
  875. if (!pg) {
  876. err = -ENOMEM;
  877. goto unlock;
  878. }
  879. sg_assign_page(sg + i, pg);
  880. err = memcpy_from_msg(
  881. page_address(sg_page(sg + i)),
  882. msg, plen);
  883. if (err) {
  884. __free_page(sg_page(sg + i));
  885. sg_assign_page(sg + i, NULL);
  886. goto unlock;
  887. }
  888. sg[i].length = plen;
  889. len -= plen;
  890. ctx->used += plen;
  891. copied += plen;
  892. size -= plen;
  893. sgl->cur++;
  894. } while (len && sgl->cur < MAX_SGL_ENTS);
  895. ctx->merge = plen & (PAGE_SIZE - 1);
  896. }
  897. if (!size)
  898. sg_mark_end(sg + sgl->cur - 1);
  899. }
  900. err = 0;
  901. ctx->more = msg->msg_flags & MSG_MORE;
  902. unlock:
  903. af_alg_data_wakeup(sk);
  904. release_sock(sk);
  905. return copied ?: err;
  906. }
  907. EXPORT_SYMBOL_GPL(af_alg_sendmsg);
  908. /**
  909. * af_alg_free_resources - release resources required for crypto request
  910. * @areq: Request holding the TX and RX SGL
  911. */
  912. void af_alg_free_resources(struct af_alg_async_req *areq)
  913. {
  914. struct sock *sk = areq->sk;
  915. struct af_alg_ctx *ctx;
  916. af_alg_free_areq_sgls(areq);
  917. sock_kfree_s(sk, areq, areq->areqlen);
  918. ctx = alg_sk(sk)->private;
  919. ctx->inflight = false;
  920. }
  921. EXPORT_SYMBOL_GPL(af_alg_free_resources);
  922. /**
  923. * af_alg_async_cb - AIO callback handler
  924. * @data: async request completion data
  925. * @err: if non-zero, error result to be returned via ki_complete();
  926. * otherwise return the AIO output length via ki_complete().
  927. *
  928. * This handler cleans up the struct af_alg_async_req upon completion of the
  929. * AIO operation.
  930. *
  931. * The number of bytes to be generated with the AIO operation must be set
  932. * in areq->outlen before the AIO callback handler is invoked.
  933. */
  934. void af_alg_async_cb(void *data, int err)
  935. {
  936. struct af_alg_async_req *areq = data;
  937. struct sock *sk = areq->sk;
  938. struct kiocb *iocb = areq->iocb;
  939. unsigned int resultlen;
  940. /* Buffer size written by crypto operation. */
  941. resultlen = areq->outlen;
  942. af_alg_free_resources(areq);
  943. sock_put(sk);
  944. iocb->ki_complete(iocb, err ? err : (int)resultlen);
  945. }
  946. EXPORT_SYMBOL_GPL(af_alg_async_cb);
  947. /**
  948. * af_alg_poll - poll system call handler
  949. * @file: file pointer
  950. * @sock: socket to poll
  951. * @wait: poll_table
  952. */
  953. __poll_t af_alg_poll(struct file *file, struct socket *sock,
  954. poll_table *wait)
  955. {
  956. struct sock *sk = sock->sk;
  957. struct alg_sock *ask = alg_sk(sk);
  958. struct af_alg_ctx *ctx = ask->private;
  959. __poll_t mask;
  960. sock_poll_wait(file, sock, wait);
  961. mask = 0;
  962. if (!ctx->more || ctx->used)
  963. mask |= EPOLLIN | EPOLLRDNORM;
  964. if (af_alg_writable(sk))
  965. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  966. return mask;
  967. }
  968. EXPORT_SYMBOL_GPL(af_alg_poll);
  969. /**
  970. * af_alg_alloc_areq - allocate struct af_alg_async_req
  971. *
  972. * @sk: socket of connection to user space
  973. * @areqlen: size of struct af_alg_async_req + crypto_*_reqsize
  974. * Return: allocated data structure or ERR_PTR upon error
  975. */
  976. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  977. unsigned int areqlen)
  978. {
  979. struct af_alg_ctx *ctx = alg_sk(sk)->private;
  980. struct af_alg_async_req *areq;
  981. /* Only one AIO request can be in flight. */
  982. if (ctx->inflight)
  983. return ERR_PTR(-EBUSY);
  984. areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
  985. if (unlikely(!areq))
  986. return ERR_PTR(-ENOMEM);
  987. ctx->inflight = true;
  988. areq->areqlen = areqlen;
  989. areq->sk = sk;
  990. areq->first_rsgl.sgl.sgt.sgl = areq->first_rsgl.sgl.sgl;
  991. areq->last_rsgl = NULL;
  992. INIT_LIST_HEAD(&areq->rsgl_list);
  993. areq->tsgl = NULL;
  994. areq->tsgl_entries = 0;
  995. return areq;
  996. }
  997. EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
  998. /**
  999. * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
  1000. * operation
  1001. *
  1002. * @sk: socket of connection to user space
  1003. * @msg: user space message
  1004. * @flags: flags used to invoke recvmsg with
  1005. * @areq: instance of the cryptographic request that will hold the RX SGL
  1006. * @maxsize: maximum number of bytes to be pulled from user space
  1007. * @outlen: number of bytes in the RX SGL
  1008. * Return: 0 on success, < 0 upon error
  1009. */
  1010. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  1011. struct af_alg_async_req *areq, size_t maxsize,
  1012. size_t *outlen)
  1013. {
  1014. struct alg_sock *ask = alg_sk(sk);
  1015. struct af_alg_ctx *ctx = ask->private;
  1016. size_t len = 0;
  1017. while (maxsize > len && msg_data_left(msg)) {
  1018. struct af_alg_rsgl *rsgl;
  1019. ssize_t err;
  1020. size_t seglen;
  1021. /* limit the amount of readable buffers */
  1022. if (!af_alg_readable(sk))
  1023. break;
  1024. seglen = min_t(size_t, (maxsize - len),
  1025. msg_data_left(msg));
  1026. if (list_empty(&areq->rsgl_list)) {
  1027. rsgl = &areq->first_rsgl;
  1028. } else {
  1029. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  1030. if (unlikely(!rsgl))
  1031. return -ENOMEM;
  1032. }
  1033. rsgl->sgl.need_unpin =
  1034. iov_iter_extract_will_pin(&msg->msg_iter);
  1035. rsgl->sgl.sgt.sgl = rsgl->sgl.sgl;
  1036. rsgl->sgl.sgt.nents = 0;
  1037. rsgl->sgl.sgt.orig_nents = 0;
  1038. list_add_tail(&rsgl->list, &areq->rsgl_list);
  1039. sg_init_table(rsgl->sgl.sgt.sgl, ALG_MAX_PAGES);
  1040. err = extract_iter_to_sg(&msg->msg_iter, seglen, &rsgl->sgl.sgt,
  1041. ALG_MAX_PAGES, 0);
  1042. if (err < 0) {
  1043. rsgl->sg_num_bytes = 0;
  1044. return err;
  1045. }
  1046. sg_mark_end(rsgl->sgl.sgt.sgl + rsgl->sgl.sgt.nents - 1);
  1047. /* chain the new scatterlist with previous one */
  1048. if (areq->last_rsgl)
  1049. af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
  1050. areq->last_rsgl = rsgl;
  1051. len += err;
  1052. atomic_add(err, &ctx->rcvused);
  1053. rsgl->sg_num_bytes = err;
  1054. }
  1055. *outlen = len;
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
  1059. static int __init af_alg_init(void)
  1060. {
  1061. int err = proto_register(&alg_proto, 0);
  1062. if (err)
  1063. goto out;
  1064. err = sock_register(&alg_family);
  1065. if (err != 0)
  1066. goto out_unregister_proto;
  1067. out:
  1068. return err;
  1069. out_unregister_proto:
  1070. proto_unregister(&alg_proto);
  1071. goto out;
  1072. }
  1073. static void __exit af_alg_exit(void)
  1074. {
  1075. sock_unregister(PF_ALG);
  1076. proto_unregister(&alg_proto);
  1077. }
  1078. module_init(af_alg_init);
  1079. module_exit(af_alg_exit);
  1080. MODULE_DESCRIPTION("Crypto userspace interface");
  1081. MODULE_LICENSE("GPL");
  1082. MODULE_ALIAS_NETPROTO(AF_ALG);