Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 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 |
Jeff Kirsher | c057b19 | 2013-12-06 09:13:44 -0800 | [diff] [blame] | 35 | * along with this library; if not, see <http://www.gnu.org/licenses/>. |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/dns_resolver.h> |
Stephen Rothwell | af352fe | 2010-08-06 03:13:47 +0100 | [diff] [blame] | 41 | #include <linux/err.h> |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 42 | #include <keys/dns_resolver-type.h> |
| 43 | #include <keys/user-type.h> |
| 44 | |
| 45 | #include "internal.h" |
| 46 | |
David Howells | ff9517a | 2010-08-06 03:13:52 +0100 | [diff] [blame] | 47 | /** |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 48 | * dns_query - Query the DNS |
| 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. |
| 54 | * @_expiry: Where to store the result expiry time (or NULL) |
| 55 | * |
| 56 | * The data will be returned in the pointer at *result, and the caller is |
| 57 | * responsible for freeing it. |
| 58 | * |
| 59 | * The description should be of the form "[<query_type>:]<domain_name>", and |
| 60 | * the options need to be appropriate for the query type requested. If no |
| 61 | * query_type is given, then the query is a straight hostname to IP address |
| 62 | * lookup. |
| 63 | * |
| 64 | * The DNS resolution lookup is performed by upcalling to userspace by way of |
| 65 | * requesting a key of type dns_resolver. |
| 66 | * |
| 67 | * Returns the size of the result on success, -ve error code otherwise. |
| 68 | */ |
| 69 | int dns_query(const char *type, const char *name, size_t namelen, |
Aya Mahfouz | 451c2b5 | 2015-11-18 08:36:44 +0200 | [diff] [blame] | 70 | const char *options, char **_result, time64_t *_expiry) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 71 | { |
| 72 | struct key *rkey; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 73 | const struct user_key_payload *upayload; |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 74 | const struct cred *saved_cred; |
| 75 | size_t typelen, desclen; |
| 76 | char *desc, *cp; |
| 77 | int ret, len; |
| 78 | |
| 79 | kenter("%s,%*.*s,%zu,%s", |
| 80 | type, (int)namelen, (int)namelen, name, namelen, options); |
| 81 | |
| 82 | if (!name || namelen == 0 || !_result) |
| 83 | return -EINVAL; |
| 84 | |
| 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 | |
| 95 | if (!namelen) |
Manuel Schölling | 9638f67 | 2014-05-31 23:37:40 +0200 | [diff] [blame] | 96 | namelen = strnlen(name, 256); |
| 97 | if (namelen < 3 || namelen > 255) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | desclen += namelen + 1; |
| 100 | |
| 101 | desc = kmalloc(desclen, GFP_KERNEL); |
| 102 | if (!desc) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | cp = desc; |
| 106 | if (type) { |
| 107 | memcpy(cp, type, typelen); |
| 108 | cp += typelen; |
| 109 | *cp++ = ':'; |
| 110 | } |
| 111 | memcpy(cp, name, namelen); |
| 112 | cp += namelen; |
| 113 | *cp = '\0'; |
| 114 | |
| 115 | if (!options) |
| 116 | options = ""; |
| 117 | kdebug("call request_key(,%s,%s)", desc, options); |
| 118 | |
| 119 | /* make the upcall, using special credentials to prevent the use of |
| 120 | * add_key() to preinstall malicious redirections |
| 121 | */ |
| 122 | saved_cred = override_creds(dns_resolver_cache); |
| 123 | rkey = request_key(&key_type_dns_resolver, desc, options); |
| 124 | revert_creds(saved_cred); |
| 125 | kfree(desc); |
| 126 | if (IS_ERR(rkey)) { |
| 127 | ret = PTR_ERR(rkey); |
| 128 | goto out; |
| 129 | } |
| 130 | |
| 131 | down_read(&rkey->sem); |
David Howells | 0c7774a | 2014-07-17 20:45:08 +0100 | [diff] [blame] | 132 | set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 133 | rkey->perm |= KEY_USR_VIEW; |
| 134 | |
| 135 | ret = key_validate(rkey); |
| 136 | if (ret < 0) |
| 137 | goto put; |
| 138 | |
Wang Lei | 4a2d789 | 2010-08-11 09:37:58 +0100 | [diff] [blame] | 139 | /* If the DNS server gave an error, return that to the caller */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 140 | ret = PTR_ERR(rkey->payload.data[dns_key_error]); |
Wang Lei | 4a2d789 | 2010-08-11 09:37:58 +0100 | [diff] [blame] | 141 | if (ret) |
| 142 | goto put; |
| 143 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 144 | upayload = user_key_payload(rkey); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 145 | len = upayload->datalen; |
| 146 | |
| 147 | ret = -ENOMEM; |
| 148 | *_result = kmalloc(len + 1, GFP_KERNEL); |
| 149 | if (!*_result) |
| 150 | goto put; |
| 151 | |
Manuel Schölling | 84a7c0b | 2014-06-07 23:57:25 +0200 | [diff] [blame] | 152 | memcpy(*_result, upayload->data, len); |
Ben Hutchings | 640d7ef | 2014-07-21 00:06:48 +0100 | [diff] [blame] | 153 | (*_result)[len] = '\0'; |
Manuel Schölling | 84a7c0b | 2014-06-07 23:57:25 +0200 | [diff] [blame] | 154 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 155 | if (_expiry) |
| 156 | *_expiry = rkey->expiry; |
| 157 | |
| 158 | ret = len; |
| 159 | put: |
| 160 | up_read(&rkey->sem); |
| 161 | key_put(rkey); |
| 162 | out: |
| 163 | kleave(" = %d", ret); |
| 164 | return ret; |
| 165 | } |
| 166 | EXPORT_SYMBOL(dns_query); |