af_alg.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. node->type = type;
  67. list_add(&node->list, &alg_types);
  68. err = 0;
  69. unlock:
  70. up_write(&alg_types_sem);
  71. return err;
  72. }
  73. EXPORT_SYMBOL_GPL(af_alg_register_type);
  74. int af_alg_unregister_type(const struct af_alg_type *type)
  75. {
  76. struct alg_type_list *node;
  77. int err = -ENOENT;
  78. down_write(&alg_types_sem);
  79. list_for_each_entry(node, &alg_types, list) {
  80. if (strcmp(node->type->name, type->name))
  81. continue;
  82. list_del(&node->list);
  83. kfree(node);
  84. err = 0;
  85. break;
  86. }
  87. up_write(&alg_types_sem);
  88. return err;
  89. }
  90. EXPORT_SYMBOL_GPL(af_alg_unregister_type);
  91. static void alg_do_release(const struct af_alg_type *type, void *private)
  92. {
  93. if (!type)
  94. return;
  95. type->release(private);
  96. module_put(type->owner);
  97. }
  98. int af_alg_release(struct socket *sock)
  99. {
  100. if (sock->sk)
  101. sock_put(sock->sk);
  102. return 0;
  103. }
  104. EXPORT_SYMBOL_GPL(af_alg_release);
  105. static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  106. {
  107. struct sock *sk = sock->sk;
  108. struct alg_sock *ask = alg_sk(sk);
  109. struct sockaddr_alg *sa = (void *)uaddr;
  110. const struct af_alg_type *type;
  111. void *private;
  112. if (sock->state == SS_CONNECTED)
  113. return -EINVAL;
  114. if (addr_len != sizeof(*sa))
  115. return -EINVAL;
  116. sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
  117. sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
  118. type = alg_get_type(sa->salg_type);
  119. if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
  120. request_module("algif-%s", sa->salg_type);
  121. type = alg_get_type(sa->salg_type);
  122. }
  123. if (IS_ERR(type))
  124. return PTR_ERR(type);
  125. private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
  126. if (IS_ERR(private)) {
  127. module_put(type->owner);
  128. return PTR_ERR(private);
  129. }
  130. lock_sock(sk);
  131. swap(ask->type, type);
  132. swap(ask->private, private);
  133. release_sock(sk);
  134. alg_do_release(type, private);
  135. return 0;
  136. }
  137. static int alg_setkey(struct sock *sk, char __user *ukey,
  138. unsigned int keylen)
  139. {
  140. struct alg_sock *ask = alg_sk(sk);
  141. const struct af_alg_type *type = ask->type;
  142. u8 *key;
  143. int err;
  144. key = sock_kmalloc(sk, keylen, GFP_KERNEL);
  145. if (!key)
  146. return -ENOMEM;
  147. err = -EFAULT;
  148. if (copy_from_user(key, ukey, keylen))
  149. goto out;
  150. err = type->setkey(ask->private, key, keylen);
  151. out:
  152. sock_kfree_s(sk, key, keylen);
  153. return err;
  154. }
  155. static int alg_setsockopt(struct socket *sock, int level, int optname,
  156. char __user *optval, unsigned int optlen)
  157. {
  158. struct sock *sk = sock->sk;
  159. struct alg_sock *ask = alg_sk(sk);
  160. const struct af_alg_type *type;
  161. int err = -ENOPROTOOPT;
  162. lock_sock(sk);
  163. type = ask->type;
  164. if (level != SOL_ALG || !type)
  165. goto unlock;
  166. switch (optname) {
  167. case ALG_SET_KEY:
  168. if (sock->state == SS_CONNECTED)
  169. goto unlock;
  170. if (!type->setkey)
  171. goto unlock;
  172. err = alg_setkey(sk, optval, optlen);
  173. }
  174. unlock:
  175. release_sock(sk);
  176. return err;
  177. }
  178. int af_alg_accept(struct sock *sk, struct socket *newsock)
  179. {
  180. struct alg_sock *ask = alg_sk(sk);
  181. const struct af_alg_type *type;
  182. struct sock *sk2;
  183. int err;
  184. lock_sock(sk);
  185. type = ask->type;
  186. err = -EINVAL;
  187. if (!type)
  188. goto unlock;
  189. sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto);
  190. err = -ENOMEM;
  191. if (!sk2)
  192. goto unlock;
  193. sock_init_data(newsock, sk2);
  194. sock_graft(sk2, newsock);
  195. security_sk_clone(sk, sk2);
  196. err = type->accept(ask->private, sk2);
  197. if (err) {
  198. sk_free(sk2);
  199. goto unlock;
  200. }
  201. sk2->sk_family = PF_ALG;
  202. sock_hold(sk);
  203. alg_sk(sk2)->parent = sk;
  204. alg_sk(sk2)->type = type;
  205. newsock->ops = type->ops;
  206. newsock->state = SS_CONNECTED;
  207. err = 0;
  208. unlock:
  209. release_sock(sk);
  210. return err;
  211. }
  212. EXPORT_SYMBOL_GPL(af_alg_accept);
  213. static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
  214. {
  215. return af_alg_accept(sock->sk, newsock);
  216. }
  217. static const struct proto_ops alg_proto_ops = {
  218. .family = PF_ALG,
  219. .owner = THIS_MODULE,
  220. .connect = sock_no_connect,
  221. .socketpair = sock_no_socketpair,
  222. .getname = sock_no_getname,
  223. .ioctl = sock_no_ioctl,
  224. .listen = sock_no_listen,
  225. .shutdown = sock_no_shutdown,
  226. .getsockopt = sock_no_getsockopt,
  227. .mmap = sock_no_mmap,
  228. .sendpage = sock_no_sendpage,
  229. .sendmsg = sock_no_sendmsg,
  230. .recvmsg = sock_no_recvmsg,
  231. .poll = sock_no_poll,
  232. .bind = alg_bind,
  233. .release = af_alg_release,
  234. .setsockopt = alg_setsockopt,
  235. .accept = alg_accept,
  236. };
  237. static void alg_sock_destruct(struct sock *sk)
  238. {
  239. struct alg_sock *ask = alg_sk(sk);
  240. alg_do_release(ask->type, ask->private);
  241. }
  242. static int alg_create(struct net *net, struct socket *sock, int protocol,
  243. int kern)
  244. {
  245. struct sock *sk;
  246. int err;
  247. if (sock->type != SOCK_SEQPACKET)
  248. return -ESOCKTNOSUPPORT;
  249. if (protocol != 0)
  250. return -EPROTONOSUPPORT;
  251. err = -ENOMEM;
  252. sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
  253. if (!sk)
  254. goto out;
  255. sock->ops = &alg_proto_ops;
  256. sock_init_data(sock, sk);
  257. sk->sk_family = PF_ALG;
  258. sk->sk_destruct = alg_sock_destruct;
  259. return 0;
  260. out:
  261. return err;
  262. }
  263. static const struct net_proto_family alg_family = {
  264. .family = PF_ALG,
  265. .create = alg_create,
  266. .owner = THIS_MODULE,
  267. };
  268. int af_alg_make_sg(struct af_alg_sgl *sgl, void __user *addr, int len,
  269. int write)
  270. {
  271. unsigned long from = (unsigned long)addr;
  272. unsigned long npages;
  273. unsigned off;
  274. int err;
  275. int i;
  276. err = -EFAULT;
  277. if (!access_ok(write ? VERIFY_READ : VERIFY_WRITE, addr, len))
  278. goto out;
  279. off = from & ~PAGE_MASK;
  280. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  281. if (npages > ALG_MAX_PAGES)
  282. npages = ALG_MAX_PAGES;
  283. err = get_user_pages_fast(from, npages, write, sgl->pages);
  284. if (err < 0)
  285. goto out;
  286. npages = err;
  287. err = -EINVAL;
  288. if (WARN_ON(npages == 0))
  289. goto out;
  290. err = 0;
  291. sg_init_table(sgl->sg, npages);
  292. for (i = 0; i < npages; i++) {
  293. int plen = min_t(int, len, PAGE_SIZE - off);
  294. sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
  295. off = 0;
  296. len -= plen;
  297. err += plen;
  298. }
  299. out:
  300. return err;
  301. }
  302. EXPORT_SYMBOL_GPL(af_alg_make_sg);
  303. void af_alg_free_sg(struct af_alg_sgl *sgl)
  304. {
  305. int i;
  306. i = 0;
  307. do {
  308. put_page(sgl->pages[i]);
  309. } while (!sg_is_last(sgl->sg + (i++)));
  310. }
  311. EXPORT_SYMBOL_GPL(af_alg_free_sg);
  312. int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
  313. {
  314. struct cmsghdr *cmsg;
  315. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  316. if (!CMSG_OK(msg, cmsg))
  317. return -EINVAL;
  318. if (cmsg->cmsg_level != SOL_ALG)
  319. continue;
  320. switch(cmsg->cmsg_type) {
  321. case ALG_SET_IV:
  322. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
  323. return -EINVAL;
  324. con->iv = (void *)CMSG_DATA(cmsg);
  325. if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
  326. sizeof(*con->iv)))
  327. return -EINVAL;
  328. break;
  329. case ALG_SET_OP:
  330. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  331. return -EINVAL;
  332. con->op = *(u32 *)CMSG_DATA(cmsg);
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. }
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
  341. int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
  342. {
  343. switch (err) {
  344. case -EINPROGRESS:
  345. case -EBUSY:
  346. wait_for_completion(&completion->completion);
  347. reinit_completion(&completion->completion);
  348. err = completion->err;
  349. break;
  350. };
  351. return err;
  352. }
  353. EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
  354. void af_alg_complete(struct crypto_async_request *req, int err)
  355. {
  356. struct af_alg_completion *completion = req->data;
  357. completion->err = err;
  358. complete(&completion->completion);
  359. }
  360. EXPORT_SYMBOL_GPL(af_alg_complete);
  361. static int __init af_alg_init(void)
  362. {
  363. int err = proto_register(&alg_proto, 0);
  364. if (err)
  365. goto out;
  366. err = sock_register(&alg_family);
  367. if (err != 0)
  368. goto out_unregister_proto;
  369. out:
  370. return err;
  371. out_unregister_proto:
  372. proto_unregister(&alg_proto);
  373. goto out;
  374. }
  375. static void __exit af_alg_exit(void)
  376. {
  377. sock_unregister(PF_ALG);
  378. proto_unregister(&alg_proto);
  379. }
  380. module_init(af_alg_init);
  381. module_exit(af_alg_exit);
  382. MODULE_LICENSE("GPL");
  383. MODULE_ALIAS_NETPROTO(AF_ALG);