dns_query.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Upcall routine, designed to work as a key type and working through
  2. * /sbin/request-key to contact userspace when handling DNS queries.
  3. *
  4. * See Documentation/networking/dns_resolver.rst
  5. *
  6. * Copyright (c) 2007 Igor Mammedov
  7. * Author(s): Igor Mammedov ([email protected])
  8. * Steve French ([email protected])
  9. * Wang Lei ([email protected])
  10. * David Howells ([email protected])
  11. *
  12. * The upcall wrapper used to make an arbitrary DNS query.
  13. *
  14. * This function requires the appropriate userspace tool dns.upcall to be
  15. * installed and something like the following lines should be added to the
  16. * /etc/request-key.conf file:
  17. *
  18. * create dns_resolver * * /sbin/dns.upcall %k
  19. *
  20. * For example to use this module to query AFSDB RR:
  21. *
  22. * create dns_resolver afsdb:* * /sbin/dns.afsdb %k
  23. *
  24. * This library is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU Lesser General Public License as published
  26. * by the Free Software Foundation; either version 2.1 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This library is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. * the GNU Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public License
  35. * along with this library; if not, see <http://www.gnu.org/licenses/>.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/slab.h>
  39. #include <linux/cred.h>
  40. #include <linux/dns_resolver.h>
  41. #include <linux/err.h>
  42. #include <net/net_namespace.h>
  43. #include <keys/dns_resolver-type.h>
  44. #include <keys/user-type.h>
  45. #include "internal.h"
  46. /**
  47. * dns_query - Query the DNS
  48. * @net: The network namespace to operate in.
  49. * @type: Query type (or NULL for straight host->IP lookup)
  50. * @name: Name to look up
  51. * @namelen: Length of name
  52. * @options: Request options (or NULL if no options)
  53. * @_result: Where to place the returned data (or NULL)
  54. * @_expiry: Where to store the result expiry time (or NULL)
  55. * @invalidate: Always invalidate the key after use
  56. *
  57. * The data will be returned in the pointer at *result, if provided, and the
  58. * caller is responsible for freeing it.
  59. *
  60. * The description should be of the form "[<query_type>:]<domain_name>", and
  61. * the options need to be appropriate for the query type requested. If no
  62. * query_type is given, then the query is a straight hostname to IP address
  63. * lookup.
  64. *
  65. * The DNS resolution lookup is performed by upcalling to userspace by way of
  66. * requesting a key of type dns_resolver.
  67. *
  68. * Returns the size of the result on success, -ve error code otherwise.
  69. */
  70. int dns_query(struct net *net,
  71. const char *type, const char *name, size_t namelen,
  72. const char *options, char **_result, time64_t *_expiry,
  73. bool invalidate)
  74. {
  75. struct key *rkey;
  76. struct user_key_payload *upayload;
  77. const struct cred *saved_cred;
  78. size_t typelen, desclen;
  79. char *desc, *cp;
  80. int ret, len;
  81. kenter("%s,%*.*s,%zu,%s",
  82. type, (int)namelen, (int)namelen, name, namelen, options);
  83. if (!name || namelen == 0)
  84. return -EINVAL;
  85. /* construct the query key description as "[<type>:]<name>" */
  86. typelen = 0;
  87. desclen = 0;
  88. if (type) {
  89. typelen = strlen(type);
  90. if (typelen < 1)
  91. return -EINVAL;
  92. desclen += typelen + 1;
  93. }
  94. if (namelen < 3 || namelen > 255)
  95. return -EINVAL;
  96. desclen += namelen + 1;
  97. desc = kmalloc(desclen, GFP_KERNEL);
  98. if (!desc)
  99. return -ENOMEM;
  100. cp = desc;
  101. if (type) {
  102. memcpy(cp, type, typelen);
  103. cp += typelen;
  104. *cp++ = ':';
  105. }
  106. memcpy(cp, name, namelen);
  107. cp += namelen;
  108. *cp = '\0';
  109. if (!options)
  110. options = "";
  111. kdebug("call request_key(,%s,%s)", desc, options);
  112. /* make the upcall, using special credentials to prevent the use of
  113. * add_key() to preinstall malicious redirections
  114. */
  115. saved_cred = override_creds(dns_resolver_cache);
  116. rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
  117. revert_creds(saved_cred);
  118. kfree(desc);
  119. if (IS_ERR(rkey)) {
  120. ret = PTR_ERR(rkey);
  121. goto out;
  122. }
  123. down_read(&rkey->sem);
  124. set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
  125. rkey->perm |= KEY_USR_VIEW;
  126. ret = key_validate(rkey);
  127. if (ret < 0)
  128. goto put;
  129. /* If the DNS server gave an error, return that to the caller */
  130. ret = PTR_ERR(rkey->payload.data[dns_key_error]);
  131. if (ret)
  132. goto put;
  133. upayload = user_key_payload_locked(rkey);
  134. len = upayload->datalen;
  135. if (_result) {
  136. ret = -ENOMEM;
  137. *_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
  138. if (!*_result)
  139. goto put;
  140. }
  141. if (_expiry)
  142. *_expiry = rkey->expiry;
  143. ret = len;
  144. put:
  145. up_read(&rkey->sem);
  146. if (invalidate)
  147. key_invalidate(rkey);
  148. key_put(rkey);
  149. out:
  150. kleave(" = %d", ret);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL(dns_query);