dns_key.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/ratelimit.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. * Preparse instantiation data for a dns_resolver key.
  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_preparse(struct key_preparsed_payload *prep)
  58. {
  59. struct user_key_payload *upayload;
  60. unsigned long derrno;
  61. int ret;
  62. int datalen = prep->datalen, result_len = 0;
  63. const char *data = prep->data, *end, *opt;
  64. kenter("'%*.*s',%u", datalen, datalen, data, datalen);
  65. if (datalen <= 1 || !data || data[datalen - 1] != '\0')
  66. return -EINVAL;
  67. datalen--;
  68. /* deal with any options embedded in the data */
  69. end = data + datalen;
  70. opt = memchr(data, '#', datalen);
  71. if (!opt) {
  72. /* no options: the entire data is the result */
  73. kdebug("no options");
  74. result_len = datalen;
  75. } else {
  76. const char *next_opt;
  77. result_len = opt - data;
  78. opt++;
  79. kdebug("options: '%s'", opt);
  80. do {
  81. int opt_len, opt_nlen;
  82. const char *eq;
  83. char optval[128];
  84. next_opt = memchr(opt, '#', end - opt) ?: end;
  85. opt_len = next_opt - opt;
  86. if (opt_len <= 0 || opt_len > sizeof(optval)) {
  87. pr_warn_ratelimited("Invalid option length (%d) for dns_resolver key\n",
  88. opt_len);
  89. return -EINVAL;
  90. }
  91. eq = memchr(opt, '=', opt_len);
  92. if (eq) {
  93. opt_nlen = eq - opt;
  94. eq++;
  95. memcpy(optval, eq, next_opt - eq);
  96. optval[next_opt - eq] = '\0';
  97. } else {
  98. opt_nlen = opt_len;
  99. optval[0] = '\0';
  100. }
  101. kdebug("option '%*.*s' val '%s'",
  102. opt_nlen, opt_nlen, opt, optval);
  103. /* see if it's an error number representing a DNS error
  104. * that's to be recorded as the result in this key */
  105. if (opt_nlen == sizeof(DNS_ERRORNO_OPTION) - 1 &&
  106. memcmp(opt, DNS_ERRORNO_OPTION, opt_nlen) == 0) {
  107. kdebug("dns error number option");
  108. ret = kstrtoul(optval, 10, &derrno);
  109. if (ret < 0)
  110. goto bad_option_value;
  111. if (derrno < 1 || derrno > 511)
  112. goto bad_option_value;
  113. kdebug("dns error no. = %lu", derrno);
  114. prep->payload.data[dns_key_error] = ERR_PTR(-derrno);
  115. continue;
  116. }
  117. bad_option_value:
  118. pr_warn_ratelimited("Option '%*.*s' to dns_resolver key: bad/missing value\n",
  119. opt_nlen, opt_nlen, opt);
  120. return -EINVAL;
  121. } while (opt = next_opt + 1, opt < end);
  122. }
  123. /* don't cache the result if we're caching an error saying there's no
  124. * result */
  125. if (prep->payload.data[dns_key_error]) {
  126. kleave(" = 0 [h_error %ld]", PTR_ERR(prep->payload.data[dns_key_error]));
  127. return 0;
  128. }
  129. kdebug("store result");
  130. prep->quotalen = result_len;
  131. upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
  132. if (!upayload) {
  133. kleave(" = -ENOMEM");
  134. return -ENOMEM;
  135. }
  136. upayload->datalen = result_len;
  137. memcpy(upayload->data, data, result_len);
  138. upayload->data[result_len] = '\0';
  139. prep->payload.data[dns_key_data] = upayload;
  140. kleave(" = 0");
  141. return 0;
  142. }
  143. /*
  144. * Clean up the preparse data
  145. */
  146. static void dns_resolver_free_preparse(struct key_preparsed_payload *prep)
  147. {
  148. pr_devel("==>%s()\n", __func__);
  149. kfree(prep->payload.data[dns_key_data]);
  150. }
  151. /*
  152. * The description is of the form "[<type>:]<domain_name>"
  153. *
  154. * The domain name may be a simple name or an absolute domain name (which
  155. * should end with a period). The domain name is case-independent.
  156. */
  157. static bool dns_resolver_cmp(const struct key *key,
  158. const struct key_match_data *match_data)
  159. {
  160. int slen, dlen, ret = 0;
  161. const char *src = key->description, *dsp = match_data->raw_data;
  162. kenter("%s,%s", src, dsp);
  163. if (!src || !dsp)
  164. goto no_match;
  165. if (strcasecmp(src, dsp) == 0)
  166. goto matched;
  167. slen = strlen(src);
  168. dlen = strlen(dsp);
  169. if (slen <= 0 || dlen <= 0)
  170. goto no_match;
  171. if (src[slen - 1] == '.')
  172. slen--;
  173. if (dsp[dlen - 1] == '.')
  174. dlen--;
  175. if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
  176. goto no_match;
  177. matched:
  178. ret = 1;
  179. no_match:
  180. kleave(" = %d", ret);
  181. return ret;
  182. }
  183. /*
  184. * Preparse the match criterion.
  185. */
  186. static int dns_resolver_match_preparse(struct key_match_data *match_data)
  187. {
  188. match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
  189. match_data->cmp = dns_resolver_cmp;
  190. return 0;
  191. }
  192. /*
  193. * Describe a DNS key
  194. */
  195. static void dns_resolver_describe(const struct key *key, struct seq_file *m)
  196. {
  197. seq_puts(m, key->description);
  198. if (key_is_positive(key)) {
  199. int err = PTR_ERR(key->payload.data[dns_key_error]);
  200. if (err)
  201. seq_printf(m, ": %d", err);
  202. else
  203. seq_printf(m, ": %u", key->datalen);
  204. }
  205. }
  206. /*
  207. * read the DNS data
  208. * - the key's semaphore is read-locked
  209. */
  210. static long dns_resolver_read(const struct key *key,
  211. char __user *buffer, size_t buflen)
  212. {
  213. int err = PTR_ERR(key->payload.data[dns_key_error]);
  214. if (err)
  215. return err;
  216. return user_read(key, buffer, buflen);
  217. }
  218. struct key_type key_type_dns_resolver = {
  219. .name = "dns_resolver",
  220. .preparse = dns_resolver_preparse,
  221. .free_preparse = dns_resolver_free_preparse,
  222. .instantiate = generic_key_instantiate,
  223. .match_preparse = dns_resolver_match_preparse,
  224. .revoke = user_revoke,
  225. .destroy = user_destroy,
  226. .describe = dns_resolver_describe,
  227. .read = dns_resolver_read,
  228. };
  229. static int __init init_dns_resolver(void)
  230. {
  231. struct cred *cred;
  232. struct key *keyring;
  233. int ret;
  234. /* create an override credential set with a special thread keyring in
  235. * which DNS requests are cached
  236. *
  237. * this is used to prevent malicious redirections from being installed
  238. * with add_key().
  239. */
  240. cred = prepare_kernel_cred(NULL);
  241. if (!cred)
  242. return -ENOMEM;
  243. keyring = keyring_alloc(".dns_resolver",
  244. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  245. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  246. KEY_USR_VIEW | KEY_USR_READ,
  247. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  248. if (IS_ERR(keyring)) {
  249. ret = PTR_ERR(keyring);
  250. goto failed_put_cred;
  251. }
  252. ret = register_key_type(&key_type_dns_resolver);
  253. if (ret < 0)
  254. goto failed_put_key;
  255. /* instruct request_key() to use this special keyring as a cache for
  256. * the results it looks up */
  257. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  258. cred->thread_keyring = keyring;
  259. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  260. dns_resolver_cache = cred;
  261. kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
  262. return 0;
  263. failed_put_key:
  264. key_put(keyring);
  265. failed_put_cred:
  266. put_cred(cred);
  267. return ret;
  268. }
  269. static void __exit exit_dns_resolver(void)
  270. {
  271. key_revoke(dns_resolver_cache->thread_keyring);
  272. unregister_key_type(&key_type_dns_resolver);
  273. put_cred(dns_resolver_cache);
  274. }
  275. module_init(init_dns_resolver)
  276. module_exit(exit_dns_resolver)
  277. MODULE_LICENSE("GPL");