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> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 40 | #include <linux/cred.h> |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 41 | #include <linux/dns_resolver.h> |
Stephen Rothwell | af352fe | 2010-08-06 03:13:47 +0100 | [diff] [blame] | 42 | #include <linux/err.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 43 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 44 | #include <keys/dns_resolver-type.h> |
| 45 | #include <keys/user-type.h> |
| 46 | |
| 47 | #include "internal.h" |
| 48 | |
David Howells | ff9517a | 2010-08-06 03:13:52 +0100 | [diff] [blame] | 49 | /** |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 50 | * dns_query - Query the DNS |
| 51 | * @type: Query type (or NULL for straight host->IP lookup) |
| 52 | * @name: Name to look up |
| 53 | * @namelen: Length of name |
| 54 | * @options: Request options (or NULL if no options) |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 55 | * @_result: Where to place the returned data (or NULL) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 56 | * @_expiry: Where to store the result expiry time (or NULL) |
David Howells | d0660f0 | 2019-05-03 18:26:55 +0100 | [diff] [blame^] | 57 | * @invalidate: Always invalidate the key after use |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 58 | * |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 59 | * The data will be returned in the pointer at *result, if provided, and the |
| 60 | * caller is responsible for freeing it. |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 61 | * |
| 62 | * The description should be of the form "[<query_type>:]<domain_name>", and |
| 63 | * the options need to be appropriate for the query type requested. If no |
| 64 | * query_type is given, then the query is a straight hostname to IP address |
| 65 | * lookup. |
| 66 | * |
| 67 | * The DNS resolution lookup is performed by upcalling to userspace by way of |
| 68 | * requesting a key of type dns_resolver. |
| 69 | * |
| 70 | * Returns the size of the result on success, -ve error code otherwise. |
| 71 | */ |
| 72 | int dns_query(const char *type, const char *name, size_t namelen, |
David Howells | d0660f0 | 2019-05-03 18:26:55 +0100 | [diff] [blame^] | 73 | const char *options, char **_result, time64_t *_expiry, |
| 74 | bool invalidate) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 75 | { |
| 76 | struct key *rkey; |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 77 | struct user_key_payload *upayload; |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 78 | const struct cred *saved_cred; |
| 79 | size_t typelen, desclen; |
| 80 | char *desc, *cp; |
| 81 | int ret, len; |
| 82 | |
| 83 | kenter("%s,%*.*s,%zu,%s", |
| 84 | type, (int)namelen, (int)namelen, name, namelen, options); |
| 85 | |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 86 | if (!name || namelen == 0) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 87 | return -EINVAL; |
| 88 | |
| 89 | /* construct the query key description as "[<type>:]<name>" */ |
| 90 | typelen = 0; |
| 91 | desclen = 0; |
| 92 | if (type) { |
| 93 | typelen = strlen(type); |
| 94 | if (typelen < 1) |
| 95 | return -EINVAL; |
| 96 | desclen += typelen + 1; |
| 97 | } |
| 98 | |
Manuel Schölling | 9638f67 | 2014-05-31 23:37:40 +0200 | [diff] [blame] | 99 | if (namelen < 3 || namelen > 255) |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 100 | return -EINVAL; |
| 101 | desclen += namelen + 1; |
| 102 | |
| 103 | desc = kmalloc(desclen, GFP_KERNEL); |
| 104 | if (!desc) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | cp = desc; |
| 108 | if (type) { |
| 109 | memcpy(cp, type, typelen); |
| 110 | cp += typelen; |
| 111 | *cp++ = ':'; |
| 112 | } |
| 113 | memcpy(cp, name, namelen); |
| 114 | cp += namelen; |
| 115 | *cp = '\0'; |
| 116 | |
| 117 | if (!options) |
| 118 | options = ""; |
| 119 | kdebug("call request_key(,%s,%s)", desc, options); |
| 120 | |
| 121 | /* make the upcall, using special credentials to prevent the use of |
| 122 | * add_key() to preinstall malicious redirections |
| 123 | */ |
| 124 | saved_cred = override_creds(dns_resolver_cache); |
| 125 | rkey = request_key(&key_type_dns_resolver, desc, options); |
| 126 | revert_creds(saved_cred); |
| 127 | kfree(desc); |
| 128 | if (IS_ERR(rkey)) { |
| 129 | ret = PTR_ERR(rkey); |
| 130 | goto out; |
| 131 | } |
| 132 | |
| 133 | down_read(&rkey->sem); |
David Howells | 0c7774a | 2014-07-17 20:45:08 +0100 | [diff] [blame] | 134 | set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 135 | rkey->perm |= KEY_USR_VIEW; |
| 136 | |
| 137 | ret = key_validate(rkey); |
| 138 | if (ret < 0) |
| 139 | goto put; |
| 140 | |
Wang Lei | 4a2d789 | 2010-08-11 09:37:58 +0100 | [diff] [blame] | 141 | /* If the DNS server gave an error, return that to the caller */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 142 | ret = PTR_ERR(rkey->payload.data[dns_key_error]); |
Wang Lei | 4a2d789 | 2010-08-11 09:37:58 +0100 | [diff] [blame] | 143 | if (ret) |
| 144 | goto put; |
| 145 | |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 146 | upayload = user_key_payload_locked(rkey); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 147 | len = upayload->datalen; |
| 148 | |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 149 | if (_result) { |
| 150 | ret = -ENOMEM; |
David Howells | bbb4c43 | 2018-10-04 14:27:55 +0100 | [diff] [blame] | 151 | *_result = kmemdup_nul(upayload->data, len, GFP_KERNEL); |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 152 | if (!*_result) |
| 153 | goto put; |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 154 | } |
Manuel Schölling | 84a7c0b | 2014-06-07 23:57:25 +0200 | [diff] [blame] | 155 | |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 156 | if (_expiry) |
| 157 | *_expiry = rkey->expiry; |
| 158 | |
| 159 | ret = len; |
| 160 | put: |
| 161 | up_read(&rkey->sem); |
David Howells | d0660f0 | 2019-05-03 18:26:55 +0100 | [diff] [blame^] | 162 | if (invalidate) |
| 163 | key_invalidate(rkey); |
Wang Lei | 1a4240f | 2010-08-04 15:16:33 +0100 | [diff] [blame] | 164 | key_put(rkey); |
| 165 | out: |
| 166 | kleave(" = %d", ret); |
| 167 | return ret; |
| 168 | } |
| 169 | EXPORT_SYMBOL(dns_query); |