dns_key.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <linux/seq_file.h>
  33. #include <keys/dns_resolver-type.h>
  34. #include <keys/user-type.h>
  35. #include "internal.h"
  36. MODULE_DESCRIPTION("DNS Resolver");
  37. MODULE_AUTHOR("Wang Lei");
  38. MODULE_LICENSE("GPL");
  39. unsigned int dns_resolver_debug;
  40. module_param_named(debug, dns_resolver_debug, uint, S_IWUSR | S_IRUGO);
  41. MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
  42. const struct cred *dns_resolver_cache;
  43. #define DNS_ERRORNO_OPTION "dnserror"
  44. /*
  45. * Instantiate a user defined key for dns_resolver.
  46. *
  47. * The data must be a NUL-terminated string, with the NUL char accounted in
  48. * datalen.
  49. *
  50. * If the data contains a '#' characters, then we take the clause after each
  51. * one to be an option of the form 'key=value'. The actual data of interest is
  52. * the string leading up to the first '#'. For instance:
  53. *
  54. * "ip1,ip2,...#foo=bar"
  55. */
  56. static int
  57. dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen)
  58. {
  59. struct user_key_payload *upayload;
  60. unsigned long derrno;
  61. int ret;
  62. size_t result_len = 0;
  63. const char *data = _data, *end, *opt;
  64. kenter("%%%d,%s,'%*.*s',%zu",
  65. key->serial, key->description,
  66. (int)datalen, (int)datalen, data, datalen);
  67. if (datalen <= 1 || !data || data[datalen - 1] != '\0')
  68. return -EINVAL;
  69. datalen--;
  70. /* deal with any options embedded in the data */
  71. end = data + datalen;
  72. opt = memchr(data, '#', datalen);
  73. if (!opt) {
  74. /* no options: the entire data is the result */
  75. kdebug("no options");
  76. result_len = datalen;
  77. } else {
  78. const char *next_opt;
  79. result_len = opt - data;
  80. opt++;
  81. kdebug("options: '%s'", opt);
  82. do {
  83. const char *eq;
  84. int opt_len, opt_nlen, opt_vlen, tmp;
  85. next_opt = memchr(opt, '#', end - opt) ?: end;
  86. opt_len = next_opt - opt;
  87. if (!opt_len) {
  88. printk(KERN_WARNING
  89. "Empty option to dns_resolver key %d\n",
  90. key->serial);
  91. return -EINVAL;
  92. }
  93. eq = memchr(opt, '=', opt_len) ?: end;
  94. opt_nlen = eq - opt;
  95. eq++;
  96. opt_vlen = next_opt - eq; /* will be -1 if no value */
  97. tmp = opt_vlen >= 0 ? opt_vlen : 0;
  98. kdebug("option '%*.*s' val '%*.*s'",
  99. opt_nlen, opt_nlen, opt, tmp, tmp, eq);
  100. /* see if it's an error number representing a DNS error
  101. * that's to be recorded as the result in this key */
  102. if (opt_nlen == sizeof(DNS_ERRORNO_OPTION) - 1 &&
  103. memcmp(opt, DNS_ERRORNO_OPTION, opt_nlen) == 0) {
  104. kdebug("dns error number option");
  105. if (opt_vlen <= 0)
  106. goto bad_option_value;
  107. ret = strict_strtoul(eq, 10, &derrno);
  108. if (ret < 0)
  109. goto bad_option_value;
  110. if (derrno < 1 || derrno > 511)
  111. goto bad_option_value;
  112. kdebug("dns error no. = %lu", derrno);
  113. key->type_data.x[0] = -derrno;
  114. continue;
  115. }
  116. bad_option_value:
  117. printk(KERN_WARNING
  118. "Option '%*.*s' to dns_resolver key %d:"
  119. " bad/missing value\n",
  120. opt_nlen, opt_nlen, opt, key->serial);
  121. return -EINVAL;
  122. } while (opt = next_opt + 1, opt < end);
  123. }
  124. /* don't cache the result if we're caching an error saying there's no
  125. * result */
  126. if (key->type_data.x[0]) {
  127. kleave(" = 0 [h_error %ld]", key->type_data.x[0]);
  128. return 0;
  129. }
  130. kdebug("store result");
  131. ret = key_payload_reserve(key, result_len);
  132. if (ret < 0)
  133. return -EINVAL;
  134. upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
  135. if (!upayload) {
  136. kleave(" = -ENOMEM");
  137. return -ENOMEM;
  138. }
  139. upayload->datalen = result_len;
  140. memcpy(upayload->data, data, result_len);
  141. upayload->data[result_len] = '\0';
  142. rcu_assign_pointer(key->payload.data, upayload);
  143. kleave(" = 0");
  144. return 0;
  145. }
  146. /*
  147. * The description is of the form "[<type>:]<domain_name>"
  148. *
  149. * The domain name may be a simple name or an absolute domain name (which
  150. * should end with a period). The domain name is case-independent.
  151. */
  152. static int
  153. dns_resolver_match(const struct key *key, const void *description)
  154. {
  155. int slen, dlen, ret = 0;
  156. const char *src = key->description, *dsp = description;
  157. kenter("%s,%s", src, dsp);
  158. if (!src || !dsp)
  159. goto no_match;
  160. if (strcasecmp(src, dsp) == 0)
  161. goto matched;
  162. slen = strlen(src);
  163. dlen = strlen(dsp);
  164. if (slen <= 0 || dlen <= 0)
  165. goto no_match;
  166. if (src[slen - 1] == '.')
  167. slen--;
  168. if (dsp[dlen - 1] == '.')
  169. dlen--;
  170. if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
  171. goto no_match;
  172. matched:
  173. ret = 1;
  174. no_match:
  175. kleave(" = %d", ret);
  176. return ret;
  177. }
  178. /*
  179. * Describe a DNS key
  180. */
  181. static void dns_resolver_describe(const struct key *key, struct seq_file *m)
  182. {
  183. int err = key->type_data.x[0];
  184. seq_puts(m, key->description);
  185. if (key_is_instantiated(key)) {
  186. if (err)
  187. seq_printf(m, ": %d", err);
  188. else
  189. seq_printf(m, ": %u", key->datalen);
  190. }
  191. }
  192. /*
  193. * read the DNS data
  194. * - the key's semaphore is read-locked
  195. */
  196. static long dns_resolver_read(const struct key *key,
  197. char __user *buffer, size_t buflen)
  198. {
  199. if (key->type_data.x[0])
  200. return key->type_data.x[0];
  201. return user_read(key, buffer, buflen);
  202. }
  203. struct key_type key_type_dns_resolver = {
  204. .name = "dns_resolver",
  205. .instantiate = dns_resolver_instantiate,
  206. .match = dns_resolver_match,
  207. .revoke = user_revoke,
  208. .destroy = user_destroy,
  209. .describe = dns_resolver_describe,
  210. .read = dns_resolver_read,
  211. };
  212. static int __init init_dns_resolver(void)
  213. {
  214. struct cred *cred;
  215. struct key *keyring;
  216. int ret;
  217. printk(KERN_NOTICE "Registering the %s key type\n",
  218. key_type_dns_resolver.name);
  219. /* create an override credential set with a special thread keyring in
  220. * which DNS requests are cached
  221. *
  222. * this is used to prevent malicious redirections from being installed
  223. * with add_key().
  224. */
  225. cred = prepare_kernel_cred(NULL);
  226. if (!cred)
  227. return -ENOMEM;
  228. keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
  229. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  230. KEY_USR_VIEW | KEY_USR_READ,
  231. KEY_ALLOC_NOT_IN_QUOTA);
  232. if (IS_ERR(keyring)) {
  233. ret = PTR_ERR(keyring);
  234. goto failed_put_cred;
  235. }
  236. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  237. if (ret < 0)
  238. goto failed_put_key;
  239. ret = register_key_type(&key_type_dns_resolver);
  240. if (ret < 0)
  241. goto failed_put_key;
  242. /* instruct request_key() to use this special keyring as a cache for
  243. * the results it looks up */
  244. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  245. cred->thread_keyring = keyring;
  246. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  247. dns_resolver_cache = cred;
  248. kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
  249. return 0;
  250. failed_put_key:
  251. key_put(keyring);
  252. failed_put_cred:
  253. put_cred(cred);
  254. return ret;
  255. }
  256. static void __exit exit_dns_resolver(void)
  257. {
  258. key_revoke(dns_resolver_cache->thread_keyring);
  259. unregister_key_type(&key_type_dns_resolver);
  260. put_cred(dns_resolver_cache);
  261. printk(KERN_NOTICE "Unregistered %s key type\n",
  262. key_type_dns_resolver.name);
  263. }
  264. module_init(init_dns_resolver)
  265. module_exit(exit_dns_resolver)
  266. MODULE_LICENSE("GPL");