dns_key.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Key type used to cache DNS lookups made by the kernel
  2. *
  3. * See Documentation/networking/dns_resolver.txt
  4. *
  5. * Copyright (c) 2007 Igor Mammedov
  6. * Author(s): Igor Mammedov (niallain@gmail.com)
  7. * Steve French (sfrench@us.ibm.com)
  8. * Wang Lei (wang840925@gmail.com)
  9. * David Howells (dhowells@redhat.com)
  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/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/kernel.h>
  30. #include <linux/keyctl.h>
  31. #include <linux/err.h>
  32. #include <keys/dns_resolver-type.h>
  33. #include <keys/user-type.h>
  34. #include "internal.h"
  35. MODULE_DESCRIPTION("DNS Resolver");
  36. MODULE_AUTHOR("Wang Lei");
  37. MODULE_LICENSE("GPL");
  38. unsigned dns_resolver_debug;
  39. module_param_named(debug, dns_resolver_debug, uint, S_IWUSR | S_IRUGO);
  40. MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
  41. const struct cred *dns_resolver_cache;
  42. /*
  43. * Instantiate a user defined key for dns_resolver.
  44. *
  45. * The data must be a NUL-terminated string, with the NUL char accounted in
  46. * datalen.
  47. *
  48. * If the data contains a '#' characters, then we take the clause after each
  49. * one to be an option of the form 'key=value'. The actual data of interest is
  50. * the string leading up to the first '#'. For instance:
  51. *
  52. * "ip1,ip2,...#foo=bar"
  53. */
  54. static int
  55. dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen)
  56. {
  57. struct user_key_payload *upayload;
  58. int ret;
  59. size_t result_len = 0;
  60. const char *data = _data, *opt;
  61. kenter("%%%d,%s,'%s',%zu",
  62. key->serial, key->description, data, datalen);
  63. if (datalen <= 1 || !data || data[datalen - 1] != '\0')
  64. return -EINVAL;
  65. datalen--;
  66. /* deal with any options embedded in the data */
  67. opt = memchr(data, '#', datalen);
  68. if (!opt) {
  69. kdebug("no options currently supported");
  70. return -EINVAL;
  71. }
  72. result_len = datalen;
  73. ret = key_payload_reserve(key, result_len);
  74. if (ret < 0)
  75. return -EINVAL;
  76. upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
  77. if (!upayload) {
  78. kleave(" = -ENOMEM");
  79. return -ENOMEM;
  80. }
  81. upayload->datalen = result_len;
  82. memcpy(upayload->data, data, result_len);
  83. upayload->data[result_len] = '\0';
  84. rcu_assign_pointer(key->payload.data, upayload);
  85. kleave(" = 0");
  86. return 0;
  87. }
  88. /*
  89. * The description is of the form "[<type>:]<domain_name>"
  90. *
  91. * The domain name may be a simple name or an absolute domain name (which
  92. * should end with a period). The domain name is case-independent.
  93. */
  94. static int
  95. dns_resolver_match(const struct key *key, const void *description)
  96. {
  97. int slen, dlen, ret = 0;
  98. const char *src = key->description, *dsp = description;
  99. kenter("%s,%s", src, dsp);
  100. if (!src || !dsp)
  101. goto no_match;
  102. if (strcasecmp(src, dsp) == 0)
  103. goto matched;
  104. slen = strlen(src);
  105. dlen = strlen(dsp);
  106. if (slen <= 0 || dlen <= 0)
  107. goto no_match;
  108. if (src[slen - 1] == '.')
  109. slen--;
  110. if (dsp[dlen - 1] == '.')
  111. dlen--;
  112. if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
  113. goto no_match;
  114. matched:
  115. ret = 1;
  116. no_match:
  117. kleave(" = %d", ret);
  118. return ret;
  119. }
  120. struct key_type key_type_dns_resolver = {
  121. .name = "dns_resolver",
  122. .instantiate = dns_resolver_instantiate,
  123. .match = dns_resolver_match,
  124. .revoke = user_revoke,
  125. .destroy = user_destroy,
  126. .describe = user_describe,
  127. .read = user_read,
  128. };
  129. static int __init init_dns_resolver(void)
  130. {
  131. struct cred *cred;
  132. struct key *keyring;
  133. int ret;
  134. printk(KERN_NOTICE "Registering the %s key type\n",
  135. key_type_dns_resolver.name);
  136. /* create an override credential set with a special thread keyring in
  137. * which DNS requests are cached
  138. *
  139. * this is used to prevent malicious redirections from being installed
  140. * with add_key().
  141. */
  142. cred = prepare_kernel_cred(NULL);
  143. if (!cred)
  144. return -ENOMEM;
  145. keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
  146. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  147. KEY_USR_VIEW | KEY_USR_READ,
  148. KEY_ALLOC_NOT_IN_QUOTA);
  149. if (IS_ERR(keyring)) {
  150. ret = PTR_ERR(keyring);
  151. goto failed_put_cred;
  152. }
  153. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  154. if (ret < 0)
  155. goto failed_put_key;
  156. ret = register_key_type(&key_type_dns_resolver);
  157. if (ret < 0)
  158. goto failed_put_key;
  159. /* instruct request_key() to use this special keyring as a cache for
  160. * the results it looks up */
  161. cred->thread_keyring = keyring;
  162. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  163. dns_resolver_cache = cred;
  164. kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
  165. return 0;
  166. failed_put_key:
  167. key_put(keyring);
  168. failed_put_cred:
  169. put_cred(cred);
  170. return ret;
  171. }
  172. static void __exit exit_dns_resolver(void)
  173. {
  174. key_revoke(dns_resolver_cache->thread_keyring);
  175. unregister_key_type(&key_type_dns_resolver);
  176. put_cred(dns_resolver_cache);
  177. printk(KERN_NOTICE "Unregistered %s key type\n",
  178. key_type_dns_resolver.name);
  179. }
  180. module_init(init_dns_resolver)
  181. module_exit(exit_dns_resolver)
  182. MODULE_LICENSE("GPL");