/* Upcall routine, designed to work as a key type and working through1* /sbin/request-key to contact userspace when handling DNS queries.2*3* See Documentation/networking/dns_resolver.rst4*5* Copyright (c) 2007 Igor Mammedov6* Author(s): Igor Mammedov ([email protected])7* Steve French ([email protected])8* Wang Lei ([email protected])9* David Howells ([email protected])10*11* The upcall wrapper used to make an arbitrary DNS query.12*13* This function requires the appropriate userspace tool dns.upcall to be14* installed and something like the following lines should be added to the15* /etc/request-key.conf file:16*17* create dns_resolver * * /sbin/dns.upcall %k18*19* For example to use this module to query AFSDB RR:20*21* create dns_resolver afsdb:* * /sbin/dns.afsdb %k22*23* This library is free software; you can redistribute it and/or modify24* it under the terms of the GNU Lesser General Public License as published25* by the Free Software Foundation; either version 2.1 of the License, or26* (at your option) any later version.27*28* This library is distributed in the hope that it will be useful,29* but WITHOUT ANY WARRANTY; without even the implied warranty of30* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See31* the GNU Lesser General Public License for more details.32*33* You should have received a copy of the GNU Lesser General Public License34* along with this library; if not, see <http://www.gnu.org/licenses/>.35*/3637#include <linux/module.h>38#include <linux/slab.h>39#include <linux/cred.h>40#include <linux/dns_resolver.h>41#include <linux/err.h>42#include <net/net_namespace.h>4344#include <keys/dns_resolver-type.h>45#include <keys/user-type.h>4647#include "internal.h"4849/**50* dns_query - Query the DNS51* @net: The network namespace to operate in.52* @type: Query type (or NULL for straight host->IP lookup)53* @name: Name to look up54* @namelen: Length of name55* @options: Request options (or NULL if no options)56* @_result: Where to place the returned data (or NULL)57* @_expiry: Where to store the result expiry time (or NULL)58* @invalidate: Always invalidate the key after use59*60* The data will be returned in the pointer at *result, if provided, and the61* caller is responsible for freeing it.62*63* The description should be of the form "[<query_type>:]<domain_name>", and64* the options need to be appropriate for the query type requested. If no65* query_type is given, then the query is a straight hostname to IP address66* lookup.67*68* The DNS resolution lookup is performed by upcalling to userspace by way of69* requesting a key of type dns_resolver.70*71* Returns the size of the result on success, -ve error code otherwise.72*/73int dns_query(struct net *net,74const char *type, const char *name, size_t namelen,75const char *options, char **_result, time64_t *_expiry,76bool invalidate)77{78struct key *rkey;79struct user_key_payload *upayload;80const struct cred *saved_cred;81size_t typelen, desclen;82char *desc, *cp;83int ret, len;8485kenter("%s,%*.*s,%zu,%s",86type, (int)namelen, (int)namelen, name, namelen, options);8788if (!name || namelen == 0)89return -EINVAL;9091/* construct the query key description as "[<type>:]<name>" */92typelen = 0;93desclen = 0;94if (type) {95typelen = strlen(type);96if (typelen < 1)97return -EINVAL;98desclen += typelen + 1;99}100101if (namelen < 3 || namelen > 255)102return -EINVAL;103desclen += namelen + 1;104105desc = kmalloc(desclen, GFP_KERNEL);106if (!desc)107return -ENOMEM;108109cp = desc;110if (type) {111memcpy(cp, type, typelen);112cp += typelen;113*cp++ = ':';114}115memcpy(cp, name, namelen);116cp += namelen;117*cp = '\0';118119if (!options)120options = "";121kdebug("call request_key(,%s,%s)", desc, options);122123/* make the upcall, using special credentials to prevent the use of124* add_key() to preinstall malicious redirections125*/126saved_cred = override_creds(dns_resolver_cache);127rkey = request_key_net(&key_type_dns_resolver, desc, net, options);128revert_creds(saved_cred);129kfree(desc);130if (IS_ERR(rkey)) {131ret = PTR_ERR(rkey);132goto out;133}134135down_read(&rkey->sem);136set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);137rkey->perm |= KEY_USR_VIEW;138139ret = key_validate(rkey);140if (ret < 0)141goto put;142143/* If the DNS server gave an error, return that to the caller */144ret = PTR_ERR(rkey->payload.data[dns_key_error]);145if (ret)146goto put;147148upayload = user_key_payload_locked(rkey);149len = upayload->datalen;150151if (_result) {152ret = -ENOMEM;153*_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);154if (!*_result)155goto put;156}157158if (_expiry)159*_expiry = rkey->expiry;160161ret = len;162put:163up_read(&rkey->sem);164if (invalidate)165key_invalidate(rkey);166key_put(rkey);167out:168kleave(" = %d", ret);169return ret;170}171EXPORT_SYMBOL(dns_query);172173174