xref: /linux/net/dns_resolver/dns_query.c (revision 05e352444b2430de4b183b4a988085381e5fd6ad)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /* Upcall routine, designed to work as a key type and working through
3  * /sbin/request-key to contact userspace when handling DNS queries.
4  *
5  * See Documentation/networking/dns_resolver.rst
6  *
7  *   Copyright (c) 2007 Igor Mammedov
8  *   Author(s): Igor Mammedov (niallain@gmail.com)
9  *              Steve French (sfrench@us.ibm.com)
10  *              Wang Lei (wang840925@gmail.com)
11  *		David Howells (dhowells@redhat.com)
12  *
13  *   The upcall wrapper used to make an arbitrary DNS query.
14  *
15  *   This function requires the appropriate userspace tool dns.upcall to be
16  *   installed and something like the following lines should be added to the
17  *   /etc/request-key.conf file:
18  *
19  *	create dns_resolver * * /sbin/dns.upcall %k
20  *
21  *   For example to use this module to query AFSDB RR:
22  *
23  *	create dns_resolver afsdb:* * /sbin/dns.afsdb %k
24  */
25 
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/cred.h>
29 #include <linux/dns_resolver.h>
30 #include <linux/err.h>
31 #include <net/net_namespace.h>
32 
33 #include <keys/dns_resolver-type.h>
34 #include <keys/user-type.h>
35 
36 #include "internal.h"
37 
38 /**
39  * dns_query - Query the DNS
40  * @net: The network namespace to operate in.
41  * @type: Query type (or NULL for straight host->IP lookup)
42  * @name: Name to look up
43  * @namelen: Length of name
44  * @options: Request options (or NULL if no options)
45  * @_result: Where to place the returned data (or NULL)
46  * @_expiry: Where to store the result expiry time (or NULL)
47  * @invalidate: Always invalidate the key after use
48  *
49  * The data will be returned in the pointer at *result, if provided, and the
50  * caller is responsible for freeing it.
51  *
52  * The description should be of the form "[<query_type>:]<domain_name>", and
53  * the options need to be appropriate for the query type requested.  If no
54  * query_type is given, then the query is a straight hostname to IP address
55  * lookup.
56  *
57  * The DNS resolution lookup is performed by upcalling to userspace by way of
58  * requesting a key of type dns_resolver.
59  *
60  * Returns the size of the result on success, -ve error code otherwise.
61  */
62 int dns_query(struct net *net,
63 	      const char *type, const char *name, size_t namelen,
64 	      const char *options, char **_result, time64_t *_expiry,
65 	      bool invalidate)
66 {
67 	struct key *rkey;
68 	struct user_key_payload *upayload;
69 	size_t typelen, desclen;
70 	char *desc, *cp;
71 	int ret, len;
72 
73 	kenter("%s,%*.*s,%zu,%s",
74 	       type, (int)namelen, (int)namelen, name, namelen, options);
75 
76 	if (!name || namelen == 0)
77 		return -EINVAL;
78 
79 	/* construct the query key description as "[<type>:]<name>" */
80 	typelen = 0;
81 	desclen = 0;
82 	if (type) {
83 		typelen = strlen(type);
84 		if (typelen < 1)
85 			return -EINVAL;
86 		desclen += typelen + 1;
87 	}
88 
89 	if (namelen < 3 || namelen > 255)
90 		return -EINVAL;
91 	desclen += namelen + 1;
92 
93 	desc = kmalloc(desclen, GFP_KERNEL);
94 	if (!desc)
95 		return -ENOMEM;
96 
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 
107 	if (!options)
108 		options = "";
109 	kdebug("call request_key(,%s,%s)", desc, options);
110 
111 	/* make the upcall, using special credentials to prevent the use of
112 	 * add_key() to preinstall malicious redirections
113 	 */
114 	scoped_with_creds(dns_resolver_cache)
115 		rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
116 	kfree(desc);
117 	if (IS_ERR(rkey)) {
118 		ret = PTR_ERR(rkey);
119 		goto out;
120 	}
121 
122 	down_read(&rkey->sem);
123 	set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
124 	rkey->perm |= KEY_USR_VIEW;
125 
126 	ret = key_validate(rkey);
127 	if (ret < 0)
128 		goto put;
129 
130 	/* If the DNS server gave an error, return that to the caller */
131 	ret = PTR_ERR(rkey->payload.data[dns_key_error]);
132 	if (ret)
133 		goto put;
134 
135 	upayload = user_key_payload_locked(rkey);
136 	len = upayload->datalen;
137 
138 	if (_result) {
139 		ret = -ENOMEM;
140 		*_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
141 		if (!*_result)
142 			goto put;
143 	}
144 
145 	if (_expiry)
146 		*_expiry = rkey->expiry;
147 
148 	ret = len;
149 put:
150 	up_read(&rkey->sem);
151 	if (invalidate)
152 		key_invalidate(rkey);
153 	key_put(rkey);
154 out:
155 	kleave(" = %d", ret);
156 	return ret;
157 }
158 EXPORT_SYMBOL(dns_query);
159