dns_key.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* Key type used to cache DNS lookups made by the kernel
  2. *
  3. * See Documentation/networking/dns_resolver.rst
  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 <linux/dns_resolver.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, 0644);
  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. * For normal hostname lookups, the data must be a NUL-terminated string, with
  48. * the NUL char accounted in 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. * For server list requests, the data must begin with a NUL char and be
  57. * followed by a byte indicating the version of the data format. Version 1
  58. * looks something like (note this is packed):
  59. *
  60. * u8 Non-string marker (ie. 0)
  61. * u8 Content (DNS_PAYLOAD_IS_*)
  62. * u8 Version (e.g. 1)
  63. * u8 Source of server list
  64. * u8 Lookup status of server list
  65. * u8 Number of servers
  66. * foreach-server {
  67. * __le16 Name length
  68. * __le16 Priority (as per SRV record, low first)
  69. * __le16 Weight (as per SRV record, higher first)
  70. * __le16 Port
  71. * u8 Source of address list
  72. * u8 Lookup status of address list
  73. * u8 Protocol (DNS_SERVER_PROTOCOL_*)
  74. * u8 Number of addresses
  75. * char[] Name (not NUL-terminated)
  76. * foreach-address {
  77. * u8 Family (DNS_ADDRESS_IS_*)
  78. * union {
  79. * u8[4] ipv4_addr
  80. * u8[16] ipv6_addr
  81. * }
  82. * }
  83. * }
  84. *
  85. */
  86. static int
  87. dns_resolver_preparse(struct key_preparsed_payload *prep)
  88. {
  89. const struct dns_payload_header *bin;
  90. struct user_key_payload *upayload;
  91. unsigned long derrno;
  92. int ret;
  93. int datalen = prep->datalen, result_len = 0;
  94. const char *data = prep->data, *end, *opt;
  95. if (datalen <= 1 || !data)
  96. return -EINVAL;
  97. if (data[0] == 0) {
  98. /* It may be a server list. */
  99. if (datalen <= sizeof(*bin))
  100. return -EINVAL;
  101. bin = (const struct dns_payload_header *)data;
  102. kenter("[%u,%u],%u", bin->content, bin->version, datalen);
  103. if (bin->content != DNS_PAYLOAD_IS_SERVER_LIST) {
  104. pr_warn_ratelimited(
  105. "dns_resolver: Unsupported content type (%u)\n",
  106. bin->content);
  107. return -EINVAL;
  108. }
  109. if (bin->version != 1) {
  110. pr_warn_ratelimited(
  111. "dns_resolver: Unsupported server list version (%u)\n",
  112. bin->version);
  113. return -EINVAL;
  114. }
  115. result_len = datalen;
  116. goto store_result;
  117. }
  118. kenter("'%*.*s',%u", datalen, datalen, data, datalen);
  119. if (!data || data[datalen - 1] != '\0')
  120. return -EINVAL;
  121. datalen--;
  122. /* deal with any options embedded in the data */
  123. end = data + datalen;
  124. opt = memchr(data, '#', datalen);
  125. if (!opt) {
  126. /* no options: the entire data is the result */
  127. kdebug("no options");
  128. result_len = datalen;
  129. } else {
  130. const char *next_opt;
  131. result_len = opt - data;
  132. opt++;
  133. kdebug("options: '%s'", opt);
  134. do {
  135. int opt_len, opt_nlen;
  136. const char *eq;
  137. char optval[128];
  138. next_opt = memchr(opt, '#', end - opt) ?: end;
  139. opt_len = next_opt - opt;
  140. if (opt_len <= 0 || opt_len > sizeof(optval)) {
  141. pr_warn_ratelimited("Invalid option length (%d) for dns_resolver key\n",
  142. opt_len);
  143. return -EINVAL;
  144. }
  145. eq = memchr(opt, '=', opt_len);
  146. if (eq) {
  147. opt_nlen = eq - opt;
  148. eq++;
  149. memcpy(optval, eq, next_opt - eq);
  150. optval[next_opt - eq] = '\0';
  151. } else {
  152. opt_nlen = opt_len;
  153. optval[0] = '\0';
  154. }
  155. kdebug("option '%*.*s' val '%s'",
  156. opt_nlen, opt_nlen, opt, optval);
  157. /* see if it's an error number representing a DNS error
  158. * that's to be recorded as the result in this key */
  159. if (opt_nlen == sizeof(DNS_ERRORNO_OPTION) - 1 &&
  160. memcmp(opt, DNS_ERRORNO_OPTION, opt_nlen) == 0) {
  161. kdebug("dns error number option");
  162. ret = kstrtoul(optval, 10, &derrno);
  163. if (ret < 0)
  164. goto bad_option_value;
  165. if (derrno < 1 || derrno > 511)
  166. goto bad_option_value;
  167. kdebug("dns error no. = %lu", derrno);
  168. prep->payload.data[dns_key_error] = ERR_PTR(-derrno);
  169. continue;
  170. }
  171. bad_option_value:
  172. pr_warn_ratelimited("Option '%*.*s' to dns_resolver key: bad/missing value\n",
  173. opt_nlen, opt_nlen, opt);
  174. return -EINVAL;
  175. } while (opt = next_opt + 1, opt < end);
  176. }
  177. /* don't cache the result if we're caching an error saying there's no
  178. * result */
  179. if (prep->payload.data[dns_key_error]) {
  180. kleave(" = 0 [h_error %ld]", PTR_ERR(prep->payload.data[dns_key_error]));
  181. return 0;
  182. }
  183. store_result:
  184. kdebug("store result");
  185. prep->quotalen = result_len;
  186. upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
  187. if (!upayload) {
  188. kleave(" = -ENOMEM");
  189. return -ENOMEM;
  190. }
  191. upayload->datalen = result_len;
  192. memcpy(upayload->data, data, result_len);
  193. upayload->data[result_len] = '\0';
  194. prep->payload.data[dns_key_data] = upayload;
  195. kleave(" = 0");
  196. return 0;
  197. }
  198. /*
  199. * Clean up the preparse data
  200. */
  201. static void dns_resolver_free_preparse(struct key_preparsed_payload *prep)
  202. {
  203. pr_devel("==>%s()\n", __func__);
  204. kfree(prep->payload.data[dns_key_data]);
  205. }
  206. /*
  207. * The description is of the form "[<type>:]<domain_name>"
  208. *
  209. * The domain name may be a simple name or an absolute domain name (which
  210. * should end with a period). The domain name is case-independent.
  211. */
  212. static bool dns_resolver_cmp(const struct key *key,
  213. const struct key_match_data *match_data)
  214. {
  215. int slen, dlen, ret = 0;
  216. const char *src = key->description, *dsp = match_data->raw_data;
  217. kenter("%s,%s", src, dsp);
  218. if (!src || !dsp)
  219. goto no_match;
  220. if (strcasecmp(src, dsp) == 0)
  221. goto matched;
  222. slen = strlen(src);
  223. dlen = strlen(dsp);
  224. if (slen <= 0 || dlen <= 0)
  225. goto no_match;
  226. if (src[slen - 1] == '.')
  227. slen--;
  228. if (dsp[dlen - 1] == '.')
  229. dlen--;
  230. if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
  231. goto no_match;
  232. matched:
  233. ret = 1;
  234. no_match:
  235. kleave(" = %d", ret);
  236. return ret;
  237. }
  238. /*
  239. * Preparse the match criterion.
  240. */
  241. static int dns_resolver_match_preparse(struct key_match_data *match_data)
  242. {
  243. match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
  244. match_data->cmp = dns_resolver_cmp;
  245. return 0;
  246. }
  247. /*
  248. * Describe a DNS key
  249. */
  250. static void dns_resolver_describe(const struct key *key, struct seq_file *m)
  251. {
  252. seq_puts(m, key->description);
  253. if (key_is_positive(key)) {
  254. int err = PTR_ERR(key->payload.data[dns_key_error]);
  255. if (err)
  256. seq_printf(m, ": %d", err);
  257. else
  258. seq_printf(m, ": %u", key->datalen);
  259. }
  260. }
  261. /*
  262. * read the DNS data
  263. * - the key's semaphore is read-locked
  264. */
  265. static long dns_resolver_read(const struct key *key,
  266. char *buffer, size_t buflen)
  267. {
  268. int err = PTR_ERR(key->payload.data[dns_key_error]);
  269. if (err)
  270. return err;
  271. return user_read(key, buffer, buflen);
  272. }
  273. struct key_type key_type_dns_resolver = {
  274. .name = "dns_resolver",
  275. .flags = KEY_TYPE_NET_DOMAIN,
  276. .preparse = dns_resolver_preparse,
  277. .free_preparse = dns_resolver_free_preparse,
  278. .instantiate = generic_key_instantiate,
  279. .match_preparse = dns_resolver_match_preparse,
  280. .revoke = user_revoke,
  281. .destroy = user_destroy,
  282. .describe = dns_resolver_describe,
  283. .read = dns_resolver_read,
  284. };
  285. static int __init init_dns_resolver(void)
  286. {
  287. struct cred *cred;
  288. struct key *keyring;
  289. int ret;
  290. /* create an override credential set with a special thread keyring in
  291. * which DNS requests are cached
  292. *
  293. * this is used to prevent malicious redirections from being installed
  294. * with add_key().
  295. */
  296. cred = prepare_kernel_cred(NULL);
  297. if (!cred)
  298. return -ENOMEM;
  299. keyring = keyring_alloc(".dns_resolver",
  300. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  301. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  302. KEY_USR_VIEW | KEY_USR_READ,
  303. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  304. if (IS_ERR(keyring)) {
  305. ret = PTR_ERR(keyring);
  306. goto failed_put_cred;
  307. }
  308. ret = register_key_type(&key_type_dns_resolver);
  309. if (ret < 0)
  310. goto failed_put_key;
  311. /* instruct request_key() to use this special keyring as a cache for
  312. * the results it looks up */
  313. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  314. cred->thread_keyring = keyring;
  315. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  316. dns_resolver_cache = cred;
  317. kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
  318. return 0;
  319. failed_put_key:
  320. key_put(keyring);
  321. failed_put_cred:
  322. put_cred(cred);
  323. return ret;
  324. }
  325. static void __exit exit_dns_resolver(void)
  326. {
  327. key_revoke(dns_resolver_cache->thread_keyring);
  328. unregister_key_type(&key_type_dns_resolver);
  329. put_cred(dns_resolver_cache);
  330. }
  331. module_init(init_dns_resolver)
  332. module_exit(exit_dns_resolver)
  333. MODULE_LICENSE("GPL");