xref: /openbmc/linux/net/dns_resolver/dns_query.c (revision 9dfe1361)
11a4240f4SWang Lei /* Upcall routine, designed to work as a key type and working through
21a4240f4SWang Lei  * /sbin/request-key to contact userspace when handling DNS queries.
31a4240f4SWang Lei  *
49dfe1361SMauro Carvalho Chehab  * See Documentation/networking/dns_resolver.rst
51a4240f4SWang Lei  *
61a4240f4SWang Lei  *   Copyright (c) 2007 Igor Mammedov
71a4240f4SWang Lei  *   Author(s): Igor Mammedov (niallain@gmail.com)
81a4240f4SWang Lei  *              Steve French (sfrench@us.ibm.com)
91a4240f4SWang Lei  *              Wang Lei (wang840925@gmail.com)
101a4240f4SWang Lei  *		David Howells (dhowells@redhat.com)
111a4240f4SWang Lei  *
121a4240f4SWang Lei  *   The upcall wrapper used to make an arbitrary DNS query.
131a4240f4SWang Lei  *
141a4240f4SWang Lei  *   This function requires the appropriate userspace tool dns.upcall to be
151a4240f4SWang Lei  *   installed and something like the following lines should be added to the
161a4240f4SWang Lei  *   /etc/request-key.conf file:
171a4240f4SWang Lei  *
181a4240f4SWang Lei  *	create dns_resolver * * /sbin/dns.upcall %k
191a4240f4SWang Lei  *
201a4240f4SWang Lei  *   For example to use this module to query AFSDB RR:
211a4240f4SWang Lei  *
221a4240f4SWang Lei  *	create dns_resolver afsdb:* * /sbin/dns.afsdb %k
231a4240f4SWang Lei  *
241a4240f4SWang Lei  *   This library is free software; you can redistribute it and/or modify
251a4240f4SWang Lei  *   it under the terms of the GNU Lesser General Public License as published
261a4240f4SWang Lei  *   by the Free Software Foundation; either version 2.1 of the License, or
271a4240f4SWang Lei  *   (at your option) any later version.
281a4240f4SWang Lei  *
291a4240f4SWang Lei  *   This library is distributed in the hope that it will be useful,
301a4240f4SWang Lei  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
311a4240f4SWang Lei  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
321a4240f4SWang Lei  *   the GNU Lesser General Public License for more details.
331a4240f4SWang Lei  *
341a4240f4SWang Lei  *   You should have received a copy of the GNU Lesser General Public License
35c057b190SJeff Kirsher  *   along with this library; if not, see <http://www.gnu.org/licenses/>.
361a4240f4SWang Lei  */
371a4240f4SWang Lei 
381a4240f4SWang Lei #include <linux/module.h>
391a4240f4SWang Lei #include <linux/slab.h>
405b825c3aSIngo Molnar #include <linux/cred.h>
411a4240f4SWang Lei #include <linux/dns_resolver.h>
42af352fe9SStephen Rothwell #include <linux/err.h>
43a58946c1SDavid Howells #include <net/net_namespace.h>
445b825c3aSIngo Molnar 
451a4240f4SWang Lei #include <keys/dns_resolver-type.h>
461a4240f4SWang Lei #include <keys/user-type.h>
471a4240f4SWang Lei 
481a4240f4SWang Lei #include "internal.h"
491a4240f4SWang Lei 
50ff9517a6SDavid Howells /**
511a4240f4SWang Lei  * dns_query - Query the DNS
52a58946c1SDavid Howells  * @net: The network namespace to operate in.
531a4240f4SWang Lei  * @type: Query type (or NULL for straight host->IP lookup)
541a4240f4SWang Lei  * @name: Name to look up
551a4240f4SWang Lei  * @namelen: Length of name
561a4240f4SWang Lei  * @options: Request options (or NULL if no options)
574d673da1SDavid Howells  * @_result: Where to place the returned data (or NULL)
581a4240f4SWang Lei  * @_expiry: Where to store the result expiry time (or NULL)
59d0660f0bSDavid Howells  * @invalidate: Always invalidate the key after use
601a4240f4SWang Lei  *
614d673da1SDavid Howells  * The data will be returned in the pointer at *result, if provided, and the
624d673da1SDavid Howells  * caller is responsible for freeing it.
631a4240f4SWang Lei  *
641a4240f4SWang Lei  * The description should be of the form "[<query_type>:]<domain_name>", and
651a4240f4SWang Lei  * the options need to be appropriate for the query type requested.  If no
661a4240f4SWang Lei  * query_type is given, then the query is a straight hostname to IP address
671a4240f4SWang Lei  * lookup.
681a4240f4SWang Lei  *
691a4240f4SWang Lei  * The DNS resolution lookup is performed by upcalling to userspace by way of
701a4240f4SWang Lei  * requesting a key of type dns_resolver.
711a4240f4SWang Lei  *
721a4240f4SWang Lei  * Returns the size of the result on success, -ve error code otherwise.
731a4240f4SWang Lei  */
dns_query(struct net * net,const char * type,const char * name,size_t namelen,const char * options,char ** _result,time64_t * _expiry,bool invalidate)74a58946c1SDavid Howells int dns_query(struct net *net,
75a58946c1SDavid Howells 	      const char *type, const char *name, size_t namelen,
76d0660f0bSDavid Howells 	      const char *options, char **_result, time64_t *_expiry,
77d0660f0bSDavid Howells 	      bool invalidate)
781a4240f4SWang Lei {
791a4240f4SWang Lei 	struct key *rkey;
800837e49aSDavid Howells 	struct user_key_payload *upayload;
811a4240f4SWang Lei 	const struct cred *saved_cred;
821a4240f4SWang Lei 	size_t typelen, desclen;
831a4240f4SWang Lei 	char *desc, *cp;
841a4240f4SWang Lei 	int ret, len;
851a4240f4SWang Lei 
861a4240f4SWang Lei 	kenter("%s,%*.*s,%zu,%s",
871a4240f4SWang Lei 	       type, (int)namelen, (int)namelen, name, namelen, options);
881a4240f4SWang Lei 
894d673da1SDavid Howells 	if (!name || namelen == 0)
901a4240f4SWang Lei 		return -EINVAL;
911a4240f4SWang Lei 
921a4240f4SWang Lei 	/* construct the query key description as "[<type>:]<name>" */
931a4240f4SWang Lei 	typelen = 0;
941a4240f4SWang Lei 	desclen = 0;
951a4240f4SWang Lei 	if (type) {
961a4240f4SWang Lei 		typelen = strlen(type);
971a4240f4SWang Lei 		if (typelen < 1)
981a4240f4SWang Lei 			return -EINVAL;
991a4240f4SWang Lei 		desclen += typelen + 1;
1001a4240f4SWang Lei 	}
1011a4240f4SWang Lei 
1029638f671SManuel Schölling 	if (namelen < 3 || namelen > 255)
1031a4240f4SWang Lei 		return -EINVAL;
1041a4240f4SWang Lei 	desclen += namelen + 1;
1051a4240f4SWang Lei 
1061a4240f4SWang Lei 	desc = kmalloc(desclen, GFP_KERNEL);
1071a4240f4SWang Lei 	if (!desc)
1081a4240f4SWang Lei 		return -ENOMEM;
1091a4240f4SWang Lei 
1101a4240f4SWang Lei 	cp = desc;
1111a4240f4SWang Lei 	if (type) {
1121a4240f4SWang Lei 		memcpy(cp, type, typelen);
1131a4240f4SWang Lei 		cp += typelen;
1141a4240f4SWang Lei 		*cp++ = ':';
1151a4240f4SWang Lei 	}
1161a4240f4SWang Lei 	memcpy(cp, name, namelen);
1171a4240f4SWang Lei 	cp += namelen;
1181a4240f4SWang Lei 	*cp = '\0';
1191a4240f4SWang Lei 
1201a4240f4SWang Lei 	if (!options)
1211a4240f4SWang Lei 		options = "";
1221a4240f4SWang Lei 	kdebug("call request_key(,%s,%s)", desc, options);
1231a4240f4SWang Lei 
1241a4240f4SWang Lei 	/* make the upcall, using special credentials to prevent the use of
1251a4240f4SWang Lei 	 * add_key() to preinstall malicious redirections
1261a4240f4SWang Lei 	 */
1271a4240f4SWang Lei 	saved_cred = override_creds(dns_resolver_cache);
128028db3e2SLinus Torvalds 	rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
1291a4240f4SWang Lei 	revert_creds(saved_cred);
1301a4240f4SWang Lei 	kfree(desc);
1311a4240f4SWang Lei 	if (IS_ERR(rkey)) {
1321a4240f4SWang Lei 		ret = PTR_ERR(rkey);
1331a4240f4SWang Lei 		goto out;
1341a4240f4SWang Lei 	}
1351a4240f4SWang Lei 
1361a4240f4SWang Lei 	down_read(&rkey->sem);
1370c7774abSDavid Howells 	set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
138028db3e2SLinus Torvalds 	rkey->perm |= KEY_USR_VIEW;
139028db3e2SLinus Torvalds 
1401a4240f4SWang Lei 	ret = key_validate(rkey);
1411a4240f4SWang Lei 	if (ret < 0)
1421a4240f4SWang Lei 		goto put;
1431a4240f4SWang Lei 
1444a2d7892SWang Lei 	/* If the DNS server gave an error, return that to the caller */
145146aa8b1SDavid Howells 	ret = PTR_ERR(rkey->payload.data[dns_key_error]);
1464a2d7892SWang Lei 	if (ret)
1474a2d7892SWang Lei 		goto put;
1484a2d7892SWang Lei 
1490837e49aSDavid Howells 	upayload = user_key_payload_locked(rkey);
1501a4240f4SWang Lei 	len = upayload->datalen;
1511a4240f4SWang Lei 
1524d673da1SDavid Howells 	if (_result) {
1531a4240f4SWang Lei 		ret = -ENOMEM;
154bbb4c432SDavid Howells 		*_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
1551a4240f4SWang Lei 		if (!*_result)
1561a4240f4SWang Lei 			goto put;
1574d673da1SDavid Howells 	}
15884a7c0b1SManuel Schölling 
1591a4240f4SWang Lei 	if (_expiry)
1601a4240f4SWang Lei 		*_expiry = rkey->expiry;
1611a4240f4SWang Lei 
1621a4240f4SWang Lei 	ret = len;
1631a4240f4SWang Lei put:
1641a4240f4SWang Lei 	up_read(&rkey->sem);
165d0660f0bSDavid Howells 	if (invalidate)
166d0660f0bSDavid Howells 		key_invalidate(rkey);
1671a4240f4SWang Lei 	key_put(rkey);
1681a4240f4SWang Lei out:
1691a4240f4SWang Lei 	kleave(" = %d", ret);
1701a4240f4SWang Lei 	return ret;
1711a4240f4SWang Lei }
1721a4240f4SWang Lei EXPORT_SYMBOL(dns_query);
173