dns_key.c 10.0 KB

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