dns_resolve.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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(const char *name, int len)
  41. {
  42. struct sockaddr_storage ss;
  43. return cifs_convert_address((struct sockaddr *)&ss, name, len);
  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. /* make sure this looks like an address */
  52. if (!is_ip(data, datalen))
  53. return -EINVAL;
  54. ip = kmalloc(datalen + 1, GFP_KERNEL);
  55. if (!ip)
  56. return -ENOMEM;
  57. memcpy(ip, data, datalen);
  58. ip[datalen] = '\0';
  59. key->type_data.x[0] = datalen;
  60. key->payload.data = ip;
  61. return rc;
  62. }
  63. static void
  64. dns_resolver_destroy(struct key *key)
  65. {
  66. kfree(key->payload.data);
  67. }
  68. struct key_type key_type_dns_resolver = {
  69. .name = "dns_resolver",
  70. .def_datalen = sizeof(struct in_addr),
  71. .describe = user_describe,
  72. .instantiate = dns_resolver_instantiate,
  73. .destroy = dns_resolver_destroy,
  74. .match = user_match,
  75. };
  76. /* Resolves server name to ip address.
  77. * input:
  78. * unc - server UNC
  79. * output:
  80. * *ip_addr - pointer to server ip, caller responcible for freeing it.
  81. * return the length of the returned string on success
  82. */
  83. int
  84. dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
  85. {
  86. const struct cred *saved_cred;
  87. int rc = -EAGAIN;
  88. struct key *rkey = ERR_PTR(-EAGAIN);
  89. char *name;
  90. char *data = NULL;
  91. int len;
  92. if (!ip_addr || !unc)
  93. return -EINVAL;
  94. /* search for server name delimiter */
  95. len = strlen(unc);
  96. if (len < 3) {
  97. cFYI(1, "%s: unc is too short: %s", __func__, unc);
  98. return -EINVAL;
  99. }
  100. len -= 2;
  101. name = memchr(unc+2, '\\', len);
  102. if (!name) {
  103. cFYI(1, "%s: probably server name is whole unc: %s",
  104. __func__, unc);
  105. } else {
  106. len = (name - unc) - 2/* leading // */;
  107. }
  108. name = kmalloc(len+1, GFP_KERNEL);
  109. if (!name) {
  110. rc = -ENOMEM;
  111. return rc;
  112. }
  113. memcpy(name, unc+2, len);
  114. name[len] = 0;
  115. if (is_ip(name, len)) {
  116. cFYI(1, "%s: it is IP, skipping dns upcall: %s",
  117. __func__, name);
  118. data = name;
  119. goto skip_upcall;
  120. }
  121. saved_cred = override_creds(dns_resolver_cache);
  122. rkey = request_key(&key_type_dns_resolver, name, "");
  123. revert_creds(saved_cred);
  124. if (!IS_ERR(rkey)) {
  125. if (!(rkey->perm & KEY_USR_VIEW)) {
  126. down_read(&rkey->sem);
  127. rkey->perm |= KEY_USR_VIEW;
  128. up_read(&rkey->sem);
  129. }
  130. len = rkey->type_data.x[0];
  131. data = rkey->payload.data;
  132. } else {
  133. cERROR(1, "%s: unable to resolve: %s", __func__, name);
  134. goto out;
  135. }
  136. skip_upcall:
  137. if (data) {
  138. *ip_addr = kmalloc(len + 1, GFP_KERNEL);
  139. if (*ip_addr) {
  140. memcpy(*ip_addr, data, len + 1);
  141. if (!IS_ERR(rkey))
  142. cFYI(1, "%s: resolved: %s to %s", __func__,
  143. name,
  144. *ip_addr
  145. );
  146. rc = len;
  147. } else {
  148. rc = -ENOMEM;
  149. }
  150. if (!IS_ERR(rkey))
  151. key_put(rkey);
  152. }
  153. out:
  154. kfree(name);
  155. return rc;
  156. }
  157. int __init cifs_init_dns_resolver(void)
  158. {
  159. struct cred *cred;
  160. struct key *keyring;
  161. int ret;
  162. printk(KERN_NOTICE "Registering the %s key type\n",
  163. key_type_dns_resolver.name);
  164. /* create an override credential set with a special thread keyring in
  165. * which DNS requests are cached
  166. *
  167. * this is used to prevent malicious redirections from being installed
  168. * with add_key().
  169. */
  170. cred = prepare_kernel_cred(NULL);
  171. if (!cred)
  172. return -ENOMEM;
  173. keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
  174. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  175. KEY_USR_VIEW | KEY_USR_READ,
  176. KEY_ALLOC_NOT_IN_QUOTA);
  177. if (IS_ERR(keyring)) {
  178. ret = PTR_ERR(keyring);
  179. goto failed_put_cred;
  180. }
  181. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  182. if (ret < 0)
  183. goto failed_put_key;
  184. ret = register_key_type(&key_type_dns_resolver);
  185. if (ret < 0)
  186. goto failed_put_key;
  187. /* instruct request_key() to use this special keyring as a cache for
  188. * the results it looks up */
  189. cred->thread_keyring = keyring;
  190. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  191. dns_resolver_cache = cred;
  192. return 0;
  193. failed_put_key:
  194. key_put(keyring);
  195. failed_put_cred:
  196. put_cred(cred);
  197. return ret;
  198. }
  199. void cifs_exit_dns_resolver(void)
  200. {
  201. key_revoke(dns_resolver_cache->thread_keyring);
  202. unregister_key_type(&key_type_dns_resolver);
  203. put_cred(dns_resolver_cache);
  204. printk(KERN_NOTICE "Unregistered %s key type\n",
  205. key_type_dns_resolver.name);
  206. }