af_alg.c 9.4 KB

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