/* 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;80size_t typelen, desclen;81char *desc, *cp;82int ret, len;8384kenter("%s,%*.*s,%zu,%s",85type, (int)namelen, (int)namelen, name, namelen, options);8687if (!name || namelen == 0)88return -EINVAL;8990/* construct the query key description as "[<type>:]<name>" */91typelen = 0;92desclen = 0;93if (type) {94typelen = strlen(type);95if (typelen < 1)96return -EINVAL;97desclen += typelen + 1;98}99100if (namelen < 3 || namelen > 255)101return -EINVAL;102desclen += namelen + 1;103104desc = kmalloc(desclen, GFP_KERNEL);105if (!desc)106return -ENOMEM;107108cp = desc;109if (type) {110memcpy(cp, type, typelen);111cp += typelen;112*cp++ = ':';113}114memcpy(cp, name, namelen);115cp += namelen;116*cp = '\0';117118if (!options)119options = "";120kdebug("call request_key(,%s,%s)", desc, options);121122/* make the upcall, using special credentials to prevent the use of123* add_key() to preinstall malicious redirections124*/125scoped_with_creds(dns_resolver_cache)126rkey = request_key_net(&key_type_dns_resolver, desc, net, options);127kfree(desc);128if (IS_ERR(rkey)) {129ret = PTR_ERR(rkey);130goto out;131}132133down_read(&rkey->sem);134set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);135rkey->perm |= KEY_USR_VIEW;136137ret = key_validate(rkey);138if (ret < 0)139goto put;140141/* If the DNS server gave an error, return that to the caller */142ret = PTR_ERR(rkey->payload.data[dns_key_error]);143if (ret)144goto put;145146upayload = user_key_payload_locked(rkey);147len = upayload->datalen;148149if (_result) {150ret = -ENOMEM;151*_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);152if (!*_result)153goto put;154}155156if (_expiry)157*_expiry = rkey->expiry;158159ret = len;160put:161up_read(&rkey->sem);162if (invalidate)163key_invalidate(rkey);164key_put(rkey);165out:166kleave(" = %d", ret);167return ret;168}169EXPORT_SYMBOL(dns_query);170171172