dns_key.c 8.2 KB

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