dns_query.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.txt
  5. *
  6. * Copyright (c) 2007 Igor Mammedov
  7. * Author(s): Igor Mammedov (niallain@gmail.com)
  8. * Steve French (sfrench@us.ibm.com)
  9. * Wang Lei (wang840925@gmail.com)
  10. * David Howells (dhowells@redhat.com)
  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, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  37. */
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/dns_resolver.h>
  41. #include <linux/err.h>
  42. #include <keys/dns_resolver-type.h>
  43. #include <keys/user-type.h>
  44. #include "internal.h"
  45. /**
  46. * dns_query - Query the DNS
  47. * @type: Query type (or NULL for straight host->IP lookup)
  48. * @name: Name to look up
  49. * @namelen: Length of name
  50. * @options: Request options (or NULL if no options)
  51. * @_result: Where to place the returned data.
  52. * @_expiry: Where to store the result expiry time (or NULL)
  53. *
  54. * The data will be returned in the pointer at *result, and the caller is
  55. * responsible for freeing it.
  56. *
  57. * The description should be of the form "[<query_type>:]<domain_name>", and
  58. * the options need to be appropriate for the query type requested. If no
  59. * query_type is given, then the query is a straight hostname to IP address
  60. * lookup.
  61. *
  62. * The DNS resolution lookup is performed by upcalling to userspace by way of
  63. * requesting a key of type dns_resolver.
  64. *
  65. * Returns the size of the result on success, -ve error code otherwise.
  66. */
  67. int dns_query(const char *type, const char *name, size_t namelen,
  68. const char *options, char **_result, time_t *_expiry)
  69. {
  70. struct key *rkey;
  71. struct user_key_payload *upayload;
  72. const struct cred *saved_cred;
  73. size_t typelen, desclen;
  74. char *desc, *cp;
  75. int ret, len;
  76. kenter("%s,%*.*s,%zu,%s",
  77. type, (int)namelen, (int)namelen, name, namelen, options);
  78. if (!name || namelen == 0 || !_result)
  79. return -EINVAL;
  80. /* construct the query key description as "[<type>:]<name>" */
  81. typelen = 0;
  82. desclen = 0;
  83. if (type) {
  84. typelen = strlen(type);
  85. if (typelen < 1)
  86. return -EINVAL;
  87. desclen += typelen + 1;
  88. }
  89. if (!namelen)
  90. namelen = strlen(name);
  91. if (namelen < 3)
  92. return -EINVAL;
  93. desclen += namelen + 1;
  94. desc = kmalloc(desclen, GFP_KERNEL);
  95. if (!desc)
  96. return -ENOMEM;
  97. cp = desc;
  98. if (type) {
  99. memcpy(cp, type, typelen);
  100. cp += typelen;
  101. *cp++ = ':';
  102. }
  103. memcpy(cp, name, namelen);
  104. cp += namelen;
  105. *cp = '\0';
  106. if (!options)
  107. options = "";
  108. kdebug("call request_key(,%s,%s)", desc, options);
  109. /* make the upcall, using special credentials to prevent the use of
  110. * add_key() to preinstall malicious redirections
  111. */
  112. saved_cred = override_creds(dns_resolver_cache);
  113. rkey = request_key(&key_type_dns_resolver, desc, options);
  114. revert_creds(saved_cred);
  115. kfree(desc);
  116. if (IS_ERR(rkey)) {
  117. ret = PTR_ERR(rkey);
  118. goto out;
  119. }
  120. down_read(&rkey->sem);
  121. rkey->perm |= KEY_USR_VIEW;
  122. ret = key_validate(rkey);
  123. if (ret < 0)
  124. goto put;
  125. /* If the DNS server gave an error, return that to the caller */
  126. ret = rkey->type_data.x[0];
  127. if (ret)
  128. goto put;
  129. upayload = rcu_dereference_protected(rkey->payload.data,
  130. lockdep_is_held(&rkey->sem));
  131. len = upayload->datalen;
  132. ret = -ENOMEM;
  133. *_result = kmalloc(len + 1, GFP_KERNEL);
  134. if (!*_result)
  135. goto put;
  136. memcpy(*_result, upayload->data, len);
  137. (*_result)[len] = '\0';
  138. if (_expiry)
  139. *_expiry = rkey->expiry;
  140. ret = len;
  141. put:
  142. up_read(&rkey->sem);
  143. key_put(rkey);
  144. out:
  145. kleave(" = %d", ret);
  146. return ret;
  147. }
  148. EXPORT_SYMBOL(dns_query);