dns_resolve.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * fs/cifs/dns_resolve.c
  3. *
  4. * Copyright (c) 2007 Igor Mammedov
  5. * Author(s): Igor Mammedov (niallain@gmail.com)
  6. * Steve French (sfrench@us.ibm.com)
  7. *
  8. * Contains the CIFS DFS upcall routines used for hostname to
  9. * IP address translation.
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/keyctl.h>
  27. #include <linux/key-type.h>
  28. #include <keys/user-type.h>
  29. #include "dns_resolve.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. static const struct cred *dns_resolver_cache;
  34. /* Checks if supplied name is IP address
  35. * returns:
  36. * 1 - name is IP
  37. * 0 - name is not IP
  38. */
  39. static int
  40. is_ip(char *name)
  41. {
  42. struct sockaddr_storage ss;
  43. return cifs_convert_address((struct sockaddr *)&ss, name);
  44. }
  45. static int
  46. dns_resolver_instantiate(struct key *key, const void *data,
  47. size_t datalen)
  48. {
  49. int rc = 0;
  50. char *ip;
  51. ip = kmalloc(datalen + 1, GFP_KERNEL);
  52. if (!ip)
  53. return -ENOMEM;
  54. memcpy(ip, data, datalen);
  55. ip[datalen] = '\0';
  56. /* make sure this looks like an address */
  57. if (!is_ip(ip)) {
  58. kfree(ip);
  59. return -EINVAL;
  60. }
  61. key->type_data.x[0] = datalen;
  62. key->payload.data = ip;
  63. return rc;
  64. }
  65. static void
  66. dns_resolver_destroy(struct key *key)
  67. {
  68. kfree(key->payload.data);
  69. }
  70. struct key_type key_type_dns_resolver = {
  71. .name = "dns_resolver",
  72. .def_datalen = sizeof(struct in_addr),
  73. .describe = user_describe,
  74. .instantiate = dns_resolver_instantiate,
  75. .destroy = dns_resolver_destroy,
  76. .match = user_match,
  77. };
  78. /* Resolves server name to ip address.
  79. * input:
  80. * unc - server UNC
  81. * output:
  82. * *ip_addr - pointer to server ip, caller responcible for freeing it.
  83. * return 0 on success
  84. */
  85. int
  86. dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
  87. {
  88. const struct cred *saved_cred;
  89. int rc = -EAGAIN;
  90. struct key *rkey = ERR_PTR(-EAGAIN);
  91. char *name;
  92. char *data = NULL;
  93. int len;
  94. if (!ip_addr || !unc)
  95. return -EINVAL;
  96. /* search for server name delimiter */
  97. len = strlen(unc);
  98. if (len < 3) {
  99. cFYI(1, "%s: unc is too short: %s", __func__, unc);
  100. return -EINVAL;
  101. }
  102. len -= 2;
  103. name = memchr(unc+2, '\\', len);
  104. if (!name) {
  105. cFYI(1, "%s: probably server name is whole unc: %s",
  106. __func__, unc);
  107. } else {
  108. len = (name - unc) - 2/* leading // */;
  109. }
  110. name = kmalloc(len+1, GFP_KERNEL);
  111. if (!name) {
  112. rc = -ENOMEM;
  113. return rc;
  114. }
  115. memcpy(name, unc+2, len);
  116. name[len] = 0;
  117. if (is_ip(name)) {
  118. cFYI(1, "%s: it is IP, skipping dns upcall: %s",
  119. __func__, name);
  120. data = name;
  121. goto skip_upcall;
  122. }
  123. saved_cred = override_creds(dns_resolver_cache);
  124. rkey = request_key(&key_type_dns_resolver, name, "");
  125. revert_creds(saved_cred);
  126. if (!IS_ERR(rkey)) {
  127. if (!(rkey->perm & KEY_USR_VIEW)) {
  128. down_read(&rkey->sem);
  129. rkey->perm |= KEY_USR_VIEW;
  130. up_read(&rkey->sem);
  131. }
  132. len = rkey->type_data.x[0];
  133. data = rkey->payload.data;
  134. } else {
  135. cERROR(1, "%s: unable to resolve: %s", __func__, name);
  136. goto out;
  137. }
  138. skip_upcall:
  139. if (data) {
  140. *ip_addr = kmalloc(len + 1, GFP_KERNEL);
  141. if (*ip_addr) {
  142. memcpy(*ip_addr, data, len + 1);
  143. if (!IS_ERR(rkey))
  144. cFYI(1, "%s: resolved: %s to %s", __func__,
  145. name,
  146. *ip_addr
  147. );
  148. rc = 0;
  149. } else {
  150. rc = -ENOMEM;
  151. }
  152. if (!IS_ERR(rkey))
  153. key_put(rkey);
  154. }
  155. out:
  156. kfree(name);
  157. return rc;
  158. }
  159. int __init cifs_init_dns_resolver(void)
  160. {
  161. struct cred *cred;
  162. struct key *keyring;
  163. int ret;
  164. printk(KERN_NOTICE "Registering the %s key type\n",
  165. key_type_dns_resolver.name);
  166. /* create an override credential set with a special thread keyring in
  167. * which DNS requests are cached
  168. *
  169. * this is used to prevent malicious redirections from being installed
  170. * with add_key().
  171. */
  172. cred = prepare_kernel_cred(NULL);
  173. if (!cred)
  174. return -ENOMEM;
  175. keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
  176. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  177. KEY_USR_VIEW | KEY_USR_READ,
  178. KEY_ALLOC_NOT_IN_QUOTA);
  179. if (IS_ERR(keyring)) {
  180. ret = PTR_ERR(keyring);
  181. goto failed_put_cred;
  182. }
  183. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  184. if (ret < 0)
  185. goto failed_put_key;
  186. ret = register_key_type(&key_type_dns_resolver);
  187. if (ret < 0)
  188. goto failed_put_key;
  189. /* instruct request_key() to use this special keyring as a cache for
  190. * the results it looks up */
  191. cred->thread_keyring = keyring;
  192. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  193. dns_resolver_cache = cred;
  194. return 0;
  195. failed_put_key:
  196. key_put(keyring);
  197. failed_put_cred:
  198. put_cred(cred);
  199. return ret;
  200. }
  201. void cifs_exit_dns_resolver(void)
  202. {
  203. key_revoke(dns_resolver_cache->thread_keyring);
  204. unregister_key_type(&key_type_dns_resolver);
  205. put_cred(dns_resolver_cache);
  206. printk(KERN_NOTICE "Unregistered %s key type\n",
  207. key_type_dns_resolver.name);
  208. }