af_alg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * af_alg: User-space algorithm interface
  3. *
  4. * This file provides the user-space API for algorithms.
  5. *
  6. * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <linux/atomic.h>
  15. #include <crypto/if_alg.h>
  16. #include <linux/crypto.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/net.h>
  22. #include <linux/rwsem.h>
  23. #include <linux/security.h>
  24. struct alg_type_list {
  25. const struct af_alg_type *type;
  26. struct list_head list;
  27. };
  28. static atomic_long_t alg_memory_allocated;
  29. static struct proto alg_proto = {
  30. .name = "ALG",
  31. .owner = THIS_MODULE,
  32. .memory_allocated = &alg_memory_allocated,
  33. .obj_size = sizeof(struct alg_sock),
  34. };
  35. static LIST_HEAD(alg_types);
  36. static DECLARE_RWSEM(alg_types_sem);
  37. static const struct af_alg_type *alg_get_type(const char *name)
  38. {
  39. const struct af_alg_type *type = ERR_PTR(-ENOENT);
  40. struct alg_type_list *node;
  41. down_read(&alg_types_sem);
  42. list_for_each_entry(node, &alg_types, list) {
  43. if (strcmp(node->type->name, name))
  44. continue;
  45. if (try_module_get(node->type->owner))
  46. type = node->type;
  47. break;
  48. }
  49. up_read(&alg_types_sem);
  50. return type;
  51. }
  52. int af_alg_register_type(const struct af_alg_type *type)
  53. {
  54. struct alg_type_list *node;
  55. int err = -EEXIST;
  56. down_write(&alg_types_sem);
  57. list_for_each_entry(node, &alg_types, list) {
  58. if (!strcmp(node->type->name, type->name))
  59. goto unlock;
  60. }
  61. node = kmalloc(sizeof(*node), GFP_KERNEL);
  62. err = -ENOMEM;
  63. if (!node)
  64. goto unlock;
  65. type->ops->owner = THIS_MODULE;
  66. if (type->ops_nokey)
  67. type->ops_nokey->owner = THIS_MODULE;
  68. node->type = type;
  69. list_add(&node->list, &alg_types);
  70. err = 0;
  71. unlock:
  72. up_write(&alg_types_sem);
  73. return err;
  74. }
  75. EXPORT_SYMBOL_GPL(af_alg_register_type);
  76. int af_alg_unregister_type(const struct af_alg_type *type)
  77. {
  78. struct alg_type_list *node;
  79. int err = -ENOENT;
  80. down_write(&alg_types_sem);
  81. list_for_each_entry(node, &alg_types, list) {
  82. if (strcmp(node->type->name, type->name))
  83. continue;
  84. list_del(&node->list);
  85. kfree(node);
  86. err = 0;
  87. break;
  88. }
  89. up_write(&alg_types_sem);
  90. return err;
  91. }
  92. EXPORT_SYMBOL_GPL(af_alg_unregister_type);
  93. static void alg_do_release(const struct af_alg_type *type, void *private)
  94. {
  95. if (!type)
  96. return;
  97. type->release(private);
  98. module_put(type->owner);
  99. }
  100. int af_alg_release(struct socket *sock)
  101. {
  102. if (sock->sk)
  103. sock_put(sock->sk);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(af_alg_release);
  107. void af_alg_release_parent(struct sock *sk)
  108. {
  109. struct alg_sock *ask = alg_sk(sk);
  110. unsigned int nokey = ask->nokey_refcnt;
  111. bool last = nokey && !ask->refcnt;
  112. sk = ask->parent;
  113. ask = alg_sk(sk);
  114. lock_sock(sk);
  115. ask->nokey_refcnt -= nokey;
  116. if (!last)
  117. last = !--ask->refcnt;
  118. release_sock(sk);
  119. if (last)
  120. sock_put(sk);
  121. }
  122. EXPORT_SYMBOL_GPL(af_alg_release_parent);
  123. static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  124. {
  125. const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
  126. struct sock *sk = sock->sk;
  127. struct alg_sock *ask = alg_sk(sk);
  128. struct sockaddr_alg *sa = (void *)uaddr;
  129. const struct af_alg_type *type;
  130. void *private;
  131. int err;
  132. if (sock->state == SS_CONNECTED)
  133. return -EINVAL;
  134. if (addr_len != sizeof(*sa))
  135. return -EINVAL;
  136. /* If caller uses non-allowed flag, return error. */
  137. if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
  138. return -EINVAL;
  139. sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
  140. sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
  141. type = alg_get_type(sa->salg_type);
  142. if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
  143. request_module("algif-%s", sa->salg_type);
  144. type = alg_get_type(sa->salg_type);
  145. }
  146. if (IS_ERR(type))
  147. return PTR_ERR(type);
  148. private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
  149. if (IS_ERR(private)) {
  150. module_put(type->owner);
  151. return PTR_ERR(private);
  152. }
  153. err = -EBUSY;
  154. lock_sock(sk);
  155. if (ask->refcnt | ask->nokey_refcnt)
  156. goto unlock;
  157. swap(ask->type, type);
  158. swap(ask->private, private);
  159. err = 0;
  160. unlock:
  161. release_sock(sk);
  162. alg_do_release(type, private);
  163. return err;
  164. }
  165. static int alg_setkey(struct sock *sk, char __user *ukey,
  166. unsigned int keylen)
  167. {
  168. struct alg_sock *ask = alg_sk(sk);
  169. const struct af_alg_type *type = ask->type;
  170. u8 *key;
  171. int err;
  172. key = sock_kmalloc(sk, keylen, GFP_KERNEL);
  173. if (!key)
  174. return -ENOMEM;
  175. err = -EFAULT;
  176. if (copy_from_user(key, ukey, keylen))
  177. goto out;
  178. err = type->setkey(ask->private, key, keylen);
  179. out:
  180. sock_kzfree_s(sk, key, keylen);
  181. return err;
  182. }
  183. static int alg_setsockopt(struct socket *sock, int level, int optname,
  184. char __user *optval, unsigned int optlen)
  185. {
  186. struct sock *sk = sock->sk;
  187. struct alg_sock *ask = alg_sk(sk);
  188. const struct af_alg_type *type;
  189. int err = -EBUSY;
  190. lock_sock(sk);
  191. if (ask->refcnt)
  192. goto unlock;
  193. type = ask->type;
  194. err = -ENOPROTOOPT;
  195. if (level != SOL_ALG || !type)
  196. goto unlock;
  197. switch (optname) {
  198. case ALG_SET_KEY:
  199. if (sock->state == SS_CONNECTED)
  200. goto unlock;
  201. if (!type->setkey)
  202. goto unlock;
  203. err = alg_setkey(sk, optval, optlen);
  204. break;
  205. case ALG_SET_AEAD_AUTHSIZE:
  206. if (sock->state == SS_CONNECTED)
  207. goto unlock;
  208. if (!type->setauthsize)
  209. goto unlock;
  210. err = type->setauthsize(ask->private, optlen);
  211. }
  212. unlock:
  213. release_sock(sk);
  214. return err;
  215. }
  216. int af_alg_accept(struct sock *sk, struct socket *newsock)
  217. {
  218. struct alg_sock *ask = alg_sk(sk);
  219. const struct af_alg_type *type;
  220. struct sock *sk2;
  221. unsigned int nokey;
  222. int err;
  223. lock_sock(sk);
  224. type = ask->type;
  225. err = -EINVAL;
  226. if (!type)
  227. goto unlock;
  228. sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, 0);
  229. err = -ENOMEM;
  230. if (!sk2)
  231. goto unlock;
  232. sock_init_data(newsock, sk2);
  233. sock_graft(sk2, newsock);
  234. security_sk_clone(sk, sk2);
  235. err = type->accept(ask->private, sk2);
  236. nokey = err == -ENOKEY;
  237. if (nokey && type->accept_nokey)
  238. err = type->accept_nokey(ask->private, sk2);
  239. if (err)
  240. goto unlock;
  241. sk2->sk_family = PF_ALG;
  242. if (nokey || !ask->refcnt++)
  243. sock_hold(sk);
  244. ask->nokey_refcnt += nokey;
  245. alg_sk(sk2)->parent = sk;
  246. alg_sk(sk2)->type = type;
  247. alg_sk(sk2)->nokey_refcnt = nokey;
  248. newsock->ops = type->ops;
  249. newsock->state = SS_CONNECTED;
  250. if (nokey)
  251. newsock->ops = type->ops_nokey;
  252. err = 0;
  253. unlock:
  254. release_sock(sk);
  255. return err;
  256. }
  257. EXPORT_SYMBOL_GPL(af_alg_accept);
  258. static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
  259. {
  260. return af_alg_accept(sock->sk, newsock);
  261. }
  262. static const struct proto_ops alg_proto_ops = {
  263. .family = PF_ALG,
  264. .owner = THIS_MODULE,
  265. .connect = sock_no_connect,
  266. .socketpair = sock_no_socketpair,
  267. .getname = sock_no_getname,
  268. .ioctl = sock_no_ioctl,
  269. .listen = sock_no_listen,
  270. .shutdown = sock_no_shutdown,
  271. .getsockopt = sock_no_getsockopt,
  272. .mmap = sock_no_mmap,
  273. .sendpage = sock_no_sendpage,
  274. .sendmsg = sock_no_sendmsg,
  275. .recvmsg = sock_no_recvmsg,
  276. .poll = sock_no_poll,
  277. .bind = alg_bind,
  278. .release = af_alg_release,
  279. .setsockopt = alg_setsockopt,
  280. .accept = alg_accept,
  281. };
  282. static void alg_sock_destruct(struct sock *sk)
  283. {
  284. struct alg_sock *ask = alg_sk(sk);
  285. alg_do_release(ask->type, ask->private);
  286. }
  287. static int alg_create(struct net *net, struct socket *sock, int protocol,
  288. int kern)
  289. {
  290. struct sock *sk;
  291. int err;
  292. if (sock->type != SOCK_SEQPACKET)
  293. return -ESOCKTNOSUPPORT;
  294. if (protocol != 0)
  295. return -EPROTONOSUPPORT;
  296. err = -ENOMEM;
  297. sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
  298. if (!sk)
  299. goto out;
  300. sock->ops = &alg_proto_ops;
  301. sock_init_data(sock, sk);
  302. sk->sk_family = PF_ALG;
  303. sk->sk_destruct = alg_sock_destruct;
  304. return 0;
  305. out:
  306. return err;
  307. }
  308. static const struct net_proto_family alg_family = {
  309. .family = PF_ALG,
  310. .create = alg_create,
  311. .owner = THIS_MODULE,
  312. };
  313. int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
  314. {
  315. size_t off;
  316. ssize_t n;
  317. int npages, i;
  318. n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
  319. if (n < 0)
  320. return n;
  321. npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
  322. if (WARN_ON(npages == 0))
  323. return -EINVAL;
  324. /* Add one extra for linking */
  325. sg_init_table(sgl->sg, npages + 1);
  326. for (i = 0, len = n; i < npages; i++) {
  327. int plen = min_t(int, len, PAGE_SIZE - off);
  328. sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
  329. off = 0;
  330. len -= plen;
  331. }
  332. sg_mark_end(sgl->sg + npages - 1);
  333. sgl->npages = npages;
  334. return n;
  335. }
  336. EXPORT_SYMBOL_GPL(af_alg_make_sg);
  337. void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new)
  338. {
  339. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  340. sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
  341. }
  342. EXPORT_SYMBOL_GPL(af_alg_link_sg);
  343. void af_alg_free_sg(struct af_alg_sgl *sgl)
  344. {
  345. int i;
  346. for (i = 0; i < sgl->npages; i++)
  347. put_page(sgl->pages[i]);
  348. }
  349. EXPORT_SYMBOL_GPL(af_alg_free_sg);
  350. int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
  351. {
  352. struct cmsghdr *cmsg;
  353. for_each_cmsghdr(cmsg, msg) {
  354. if (!CMSG_OK(msg, cmsg))
  355. return -EINVAL;
  356. if (cmsg->cmsg_level != SOL_ALG)
  357. continue;
  358. switch (cmsg->cmsg_type) {
  359. case ALG_SET_IV:
  360. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
  361. return -EINVAL;
  362. con->iv = (void *)CMSG_DATA(cmsg);
  363. if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
  364. sizeof(*con->iv)))
  365. return -EINVAL;
  366. break;
  367. case ALG_SET_OP:
  368. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  369. return -EINVAL;
  370. con->op = *(u32 *)CMSG_DATA(cmsg);
  371. break;
  372. case ALG_SET_AEAD_ASSOCLEN:
  373. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  374. return -EINVAL;
  375. con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. }
  381. return 0;
  382. }
  383. EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
  384. int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
  385. {
  386. switch (err) {
  387. case -EINPROGRESS:
  388. case -EBUSY:
  389. wait_for_completion(&completion->completion);
  390. reinit_completion(&completion->completion);
  391. err = completion->err;
  392. break;
  393. };
  394. return err;
  395. }
  396. EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
  397. void af_alg_complete(struct crypto_async_request *req, int err)
  398. {
  399. struct af_alg_completion *completion = req->data;
  400. if (err == -EINPROGRESS)
  401. return;
  402. completion->err = err;
  403. complete(&completion->completion);
  404. }
  405. EXPORT_SYMBOL_GPL(af_alg_complete);
  406. static int __init af_alg_init(void)
  407. {
  408. int err = proto_register(&alg_proto, 0);
  409. if (err)
  410. goto out;
  411. err = sock_register(&alg_family);
  412. if (err != 0)
  413. goto out_unregister_proto;
  414. out:
  415. return err;
  416. out_unregister_proto:
  417. proto_unregister(&alg_proto);
  418. goto out;
  419. }
  420. static void __exit af_alg_exit(void)
  421. {
  422. sock_unregister(PF_ALG);
  423. proto_unregister(&alg_proto);
  424. }
  425. module_init(af_alg_init);
  426. module_exit(af_alg_exit);
  427. MODULE_LICENSE("GPL");
  428. MODULE_ALIAS_NETPROTO(AF_ALG);