18c2ecf20Sopenharmony_ci/* Key type used to cache DNS lookups made by the kernel 28c2ecf20Sopenharmony_ci * 38c2ecf20Sopenharmony_ci * See Documentation/networking/dns_resolver.rst 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2007 Igor Mammedov 68c2ecf20Sopenharmony_ci * Author(s): Igor Mammedov (niallain@gmail.com) 78c2ecf20Sopenharmony_ci * Steve French (sfrench@us.ibm.com) 88c2ecf20Sopenharmony_ci * Wang Lei (wang840925@gmail.com) 98c2ecf20Sopenharmony_ci * David Howells (dhowells@redhat.com) 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This library is free software; you can redistribute it and/or modify 128c2ecf20Sopenharmony_ci * it under the terms of the GNU Lesser General Public License as published 138c2ecf20Sopenharmony_ci * by the Free Software Foundation; either version 2.1 of the License, or 148c2ecf20Sopenharmony_ci * (at your option) any later version. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * This library is distributed in the hope that it will be useful, 178c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 188c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 198c2ecf20Sopenharmony_ci * the GNU Lesser General Public License for more details. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * You should have received a copy of the GNU Lesser General Public License 228c2ecf20Sopenharmony_ci * along with this library; if not, see <http://www.gnu.org/licenses/>. 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci#include <linux/module.h> 258c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 268c2ecf20Sopenharmony_ci#include <linux/slab.h> 278c2ecf20Sopenharmony_ci#include <linux/string.h> 288c2ecf20Sopenharmony_ci#include <linux/kernel.h> 298c2ecf20Sopenharmony_ci#include <linux/keyctl.h> 308c2ecf20Sopenharmony_ci#include <linux/err.h> 318c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 328c2ecf20Sopenharmony_ci#include <linux/dns_resolver.h> 338c2ecf20Sopenharmony_ci#include <keys/dns_resolver-type.h> 348c2ecf20Sopenharmony_ci#include <keys/user-type.h> 358c2ecf20Sopenharmony_ci#include "internal.h" 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DNS Resolver"); 388c2ecf20Sopenharmony_ciMODULE_AUTHOR("Wang Lei"); 398c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciunsigned int dns_resolver_debug; 428c2ecf20Sopenharmony_cimodule_param_named(debug, dns_resolver_debug, uint, 0644); 438c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "DNS Resolver debugging mask"); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ciconst struct cred *dns_resolver_cache; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define DNS_ERRORNO_OPTION "dnserror" 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/* 508c2ecf20Sopenharmony_ci * Preparse instantiation data for a dns_resolver key. 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci * For normal hostname lookups, the data must be a NUL-terminated string, with 538c2ecf20Sopenharmony_ci * the NUL char accounted in datalen. 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * If the data contains a '#' characters, then we take the clause after each 568c2ecf20Sopenharmony_ci * one to be an option of the form 'key=value'. The actual data of interest is 578c2ecf20Sopenharmony_ci * the string leading up to the first '#'. For instance: 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * "ip1,ip2,...#foo=bar" 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * For server list requests, the data must begin with a NUL char and be 628c2ecf20Sopenharmony_ci * followed by a byte indicating the version of the data format. Version 1 638c2ecf20Sopenharmony_ci * looks something like (note this is packed): 648c2ecf20Sopenharmony_ci * 658c2ecf20Sopenharmony_ci * u8 Non-string marker (ie. 0) 668c2ecf20Sopenharmony_ci * u8 Content (DNS_PAYLOAD_IS_*) 678c2ecf20Sopenharmony_ci * u8 Version (e.g. 1) 688c2ecf20Sopenharmony_ci * u8 Source of server list 698c2ecf20Sopenharmony_ci * u8 Lookup status of server list 708c2ecf20Sopenharmony_ci * u8 Number of servers 718c2ecf20Sopenharmony_ci * foreach-server { 728c2ecf20Sopenharmony_ci * __le16 Name length 738c2ecf20Sopenharmony_ci * __le16 Priority (as per SRV record, low first) 748c2ecf20Sopenharmony_ci * __le16 Weight (as per SRV record, higher first) 758c2ecf20Sopenharmony_ci * __le16 Port 768c2ecf20Sopenharmony_ci * u8 Source of address list 778c2ecf20Sopenharmony_ci * u8 Lookup status of address list 788c2ecf20Sopenharmony_ci * u8 Protocol (DNS_SERVER_PROTOCOL_*) 798c2ecf20Sopenharmony_ci * u8 Number of addresses 808c2ecf20Sopenharmony_ci * char[] Name (not NUL-terminated) 818c2ecf20Sopenharmony_ci * foreach-address { 828c2ecf20Sopenharmony_ci * u8 Family (DNS_ADDRESS_IS_*) 838c2ecf20Sopenharmony_ci * union { 848c2ecf20Sopenharmony_ci * u8[4] ipv4_addr 858c2ecf20Sopenharmony_ci * u8[16] ipv6_addr 868c2ecf20Sopenharmony_ci * } 878c2ecf20Sopenharmony_ci * } 888c2ecf20Sopenharmony_ci * } 898c2ecf20Sopenharmony_ci * 908c2ecf20Sopenharmony_ci */ 918c2ecf20Sopenharmony_cistatic int 928c2ecf20Sopenharmony_cidns_resolver_preparse(struct key_preparsed_payload *prep) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci struct user_key_payload *upayload; 958c2ecf20Sopenharmony_ci unsigned long derrno; 968c2ecf20Sopenharmony_ci int ret; 978c2ecf20Sopenharmony_ci int datalen = prep->datalen, result_len = 0; 988c2ecf20Sopenharmony_ci const char *data = prep->data, *end, *opt; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci if (datalen <= 1 || !data) 1018c2ecf20Sopenharmony_ci return -EINVAL; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci if (data[0] == 0) { 1048c2ecf20Sopenharmony_ci const struct dns_server_list_v1_header *v1; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* It may be a server list. */ 1078c2ecf20Sopenharmony_ci if (datalen < sizeof(*v1)) 1088c2ecf20Sopenharmony_ci return -EINVAL; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci v1 = (const struct dns_server_list_v1_header *)data; 1118c2ecf20Sopenharmony_ci kenter("[%u,%u],%u", v1->hdr.content, v1->hdr.version, datalen); 1128c2ecf20Sopenharmony_ci if (v1->hdr.content != DNS_PAYLOAD_IS_SERVER_LIST) { 1138c2ecf20Sopenharmony_ci pr_warn_ratelimited( 1148c2ecf20Sopenharmony_ci "dns_resolver: Unsupported content type (%u)\n", 1158c2ecf20Sopenharmony_ci v1->hdr.content); 1168c2ecf20Sopenharmony_ci return -EINVAL; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci if (v1->hdr.version != 1) { 1208c2ecf20Sopenharmony_ci pr_warn_ratelimited( 1218c2ecf20Sopenharmony_ci "dns_resolver: Unsupported server list version (%u)\n", 1228c2ecf20Sopenharmony_ci v1->hdr.version); 1238c2ecf20Sopenharmony_ci return -EINVAL; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci if ((v1->status != DNS_LOOKUP_GOOD && 1278c2ecf20Sopenharmony_ci v1->status != DNS_LOOKUP_GOOD_WITH_BAD)) { 1288c2ecf20Sopenharmony_ci if (prep->expiry == TIME64_MAX) 1298c2ecf20Sopenharmony_ci prep->expiry = ktime_get_real_seconds() + 1; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci result_len = datalen; 1338c2ecf20Sopenharmony_ci goto store_result; 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci kenter("'%*.*s',%u", datalen, datalen, data, datalen); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if (!data || data[datalen - 1] != '\0') 1398c2ecf20Sopenharmony_ci return -EINVAL; 1408c2ecf20Sopenharmony_ci datalen--; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci /* deal with any options embedded in the data */ 1438c2ecf20Sopenharmony_ci end = data + datalen; 1448c2ecf20Sopenharmony_ci opt = memchr(data, '#', datalen); 1458c2ecf20Sopenharmony_ci if (!opt) { 1468c2ecf20Sopenharmony_ci /* no options: the entire data is the result */ 1478c2ecf20Sopenharmony_ci kdebug("no options"); 1488c2ecf20Sopenharmony_ci result_len = datalen; 1498c2ecf20Sopenharmony_ci } else { 1508c2ecf20Sopenharmony_ci const char *next_opt; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci result_len = opt - data; 1538c2ecf20Sopenharmony_ci opt++; 1548c2ecf20Sopenharmony_ci kdebug("options: '%s'", opt); 1558c2ecf20Sopenharmony_ci do { 1568c2ecf20Sopenharmony_ci int opt_len, opt_nlen; 1578c2ecf20Sopenharmony_ci const char *eq; 1588c2ecf20Sopenharmony_ci char optval[128]; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci next_opt = memchr(opt, '#', end - opt) ?: end; 1618c2ecf20Sopenharmony_ci opt_len = next_opt - opt; 1628c2ecf20Sopenharmony_ci if (opt_len <= 0 || opt_len > sizeof(optval)) { 1638c2ecf20Sopenharmony_ci pr_warn_ratelimited("Invalid option length (%d) for dns_resolver key\n", 1648c2ecf20Sopenharmony_ci opt_len); 1658c2ecf20Sopenharmony_ci return -EINVAL; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci eq = memchr(opt, '=', opt_len); 1698c2ecf20Sopenharmony_ci if (eq) { 1708c2ecf20Sopenharmony_ci opt_nlen = eq - opt; 1718c2ecf20Sopenharmony_ci eq++; 1728c2ecf20Sopenharmony_ci memcpy(optval, eq, next_opt - eq); 1738c2ecf20Sopenharmony_ci optval[next_opt - eq] = '\0'; 1748c2ecf20Sopenharmony_ci } else { 1758c2ecf20Sopenharmony_ci opt_nlen = opt_len; 1768c2ecf20Sopenharmony_ci optval[0] = '\0'; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci kdebug("option '%*.*s' val '%s'", 1808c2ecf20Sopenharmony_ci opt_nlen, opt_nlen, opt, optval); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* see if it's an error number representing a DNS error 1838c2ecf20Sopenharmony_ci * that's to be recorded as the result in this key */ 1848c2ecf20Sopenharmony_ci if (opt_nlen == sizeof(DNS_ERRORNO_OPTION) - 1 && 1858c2ecf20Sopenharmony_ci memcmp(opt, DNS_ERRORNO_OPTION, opt_nlen) == 0) { 1868c2ecf20Sopenharmony_ci kdebug("dns error number option"); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci ret = kstrtoul(optval, 10, &derrno); 1898c2ecf20Sopenharmony_ci if (ret < 0) 1908c2ecf20Sopenharmony_ci goto bad_option_value; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (derrno < 1 || derrno > 511) 1938c2ecf20Sopenharmony_ci goto bad_option_value; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci kdebug("dns error no. = %lu", derrno); 1968c2ecf20Sopenharmony_ci prep->payload.data[dns_key_error] = ERR_PTR(-derrno); 1978c2ecf20Sopenharmony_ci continue; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci bad_option_value: 2018c2ecf20Sopenharmony_ci pr_warn_ratelimited("Option '%*.*s' to dns_resolver key: bad/missing value\n", 2028c2ecf20Sopenharmony_ci opt_nlen, opt_nlen, opt); 2038c2ecf20Sopenharmony_ci return -EINVAL; 2048c2ecf20Sopenharmony_ci } while (opt = next_opt + 1, opt < end); 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci /* don't cache the result if we're caching an error saying there's no 2088c2ecf20Sopenharmony_ci * result */ 2098c2ecf20Sopenharmony_ci if (prep->payload.data[dns_key_error]) { 2108c2ecf20Sopenharmony_ci kleave(" = 0 [h_error %ld]", PTR_ERR(prep->payload.data[dns_key_error])); 2118c2ecf20Sopenharmony_ci return 0; 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistore_result: 2158c2ecf20Sopenharmony_ci kdebug("store result"); 2168c2ecf20Sopenharmony_ci prep->quotalen = result_len; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL); 2198c2ecf20Sopenharmony_ci if (!upayload) { 2208c2ecf20Sopenharmony_ci kleave(" = -ENOMEM"); 2218c2ecf20Sopenharmony_ci return -ENOMEM; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci upayload->datalen = result_len; 2258c2ecf20Sopenharmony_ci memcpy(upayload->data, data, result_len); 2268c2ecf20Sopenharmony_ci upayload->data[result_len] = '\0'; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci prep->payload.data[dns_key_data] = upayload; 2298c2ecf20Sopenharmony_ci kleave(" = 0"); 2308c2ecf20Sopenharmony_ci return 0; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci/* 2348c2ecf20Sopenharmony_ci * Clean up the preparse data 2358c2ecf20Sopenharmony_ci */ 2368c2ecf20Sopenharmony_cistatic void dns_resolver_free_preparse(struct key_preparsed_payload *prep) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci pr_devel("==>%s()\n", __func__); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci kfree(prep->payload.data[dns_key_data]); 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci/* 2448c2ecf20Sopenharmony_ci * The description is of the form "[<type>:]<domain_name>" 2458c2ecf20Sopenharmony_ci * 2468c2ecf20Sopenharmony_ci * The domain name may be a simple name or an absolute domain name (which 2478c2ecf20Sopenharmony_ci * should end with a period). The domain name is case-independent. 2488c2ecf20Sopenharmony_ci */ 2498c2ecf20Sopenharmony_cistatic bool dns_resolver_cmp(const struct key *key, 2508c2ecf20Sopenharmony_ci const struct key_match_data *match_data) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci int slen, dlen, ret = 0; 2538c2ecf20Sopenharmony_ci const char *src = key->description, *dsp = match_data->raw_data; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci kenter("%s,%s", src, dsp); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci if (!src || !dsp) 2588c2ecf20Sopenharmony_ci goto no_match; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if (strcasecmp(src, dsp) == 0) 2618c2ecf20Sopenharmony_ci goto matched; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci slen = strlen(src); 2648c2ecf20Sopenharmony_ci dlen = strlen(dsp); 2658c2ecf20Sopenharmony_ci if (slen <= 0 || dlen <= 0) 2668c2ecf20Sopenharmony_ci goto no_match; 2678c2ecf20Sopenharmony_ci if (src[slen - 1] == '.') 2688c2ecf20Sopenharmony_ci slen--; 2698c2ecf20Sopenharmony_ci if (dsp[dlen - 1] == '.') 2708c2ecf20Sopenharmony_ci dlen--; 2718c2ecf20Sopenharmony_ci if (slen != dlen || strncasecmp(src, dsp, slen) != 0) 2728c2ecf20Sopenharmony_ci goto no_match; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_cimatched: 2758c2ecf20Sopenharmony_ci ret = 1; 2768c2ecf20Sopenharmony_cino_match: 2778c2ecf20Sopenharmony_ci kleave(" = %d", ret); 2788c2ecf20Sopenharmony_ci return ret; 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci/* 2828c2ecf20Sopenharmony_ci * Preparse the match criterion. 2838c2ecf20Sopenharmony_ci */ 2848c2ecf20Sopenharmony_cistatic int dns_resolver_match_preparse(struct key_match_data *match_data) 2858c2ecf20Sopenharmony_ci{ 2868c2ecf20Sopenharmony_ci match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE; 2878c2ecf20Sopenharmony_ci match_data->cmp = dns_resolver_cmp; 2888c2ecf20Sopenharmony_ci return 0; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci/* 2928c2ecf20Sopenharmony_ci * Describe a DNS key 2938c2ecf20Sopenharmony_ci */ 2948c2ecf20Sopenharmony_cistatic void dns_resolver_describe(const struct key *key, struct seq_file *m) 2958c2ecf20Sopenharmony_ci{ 2968c2ecf20Sopenharmony_ci seq_puts(m, key->description); 2978c2ecf20Sopenharmony_ci if (key_is_positive(key)) { 2988c2ecf20Sopenharmony_ci int err = PTR_ERR(key->payload.data[dns_key_error]); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci if (err) 3018c2ecf20Sopenharmony_ci seq_printf(m, ": %d", err); 3028c2ecf20Sopenharmony_ci else 3038c2ecf20Sopenharmony_ci seq_printf(m, ": %u", key->datalen); 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci} 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci/* 3088c2ecf20Sopenharmony_ci * read the DNS data 3098c2ecf20Sopenharmony_ci * - the key's semaphore is read-locked 3108c2ecf20Sopenharmony_ci */ 3118c2ecf20Sopenharmony_cistatic long dns_resolver_read(const struct key *key, 3128c2ecf20Sopenharmony_ci char *buffer, size_t buflen) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci int err = PTR_ERR(key->payload.data[dns_key_error]); 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci if (err) 3178c2ecf20Sopenharmony_ci return err; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci return user_read(key, buffer, buflen); 3208c2ecf20Sopenharmony_ci} 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_cistruct key_type key_type_dns_resolver = { 3238c2ecf20Sopenharmony_ci .name = "dns_resolver", 3248c2ecf20Sopenharmony_ci .flags = KEY_TYPE_NET_DOMAIN | KEY_TYPE_INSTANT_REAP, 3258c2ecf20Sopenharmony_ci .preparse = dns_resolver_preparse, 3268c2ecf20Sopenharmony_ci .free_preparse = dns_resolver_free_preparse, 3278c2ecf20Sopenharmony_ci .instantiate = generic_key_instantiate, 3288c2ecf20Sopenharmony_ci .match_preparse = dns_resolver_match_preparse, 3298c2ecf20Sopenharmony_ci .revoke = user_revoke, 3308c2ecf20Sopenharmony_ci .destroy = user_destroy, 3318c2ecf20Sopenharmony_ci .describe = dns_resolver_describe, 3328c2ecf20Sopenharmony_ci .read = dns_resolver_read, 3338c2ecf20Sopenharmony_ci}; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_cistatic int __init init_dns_resolver(void) 3368c2ecf20Sopenharmony_ci{ 3378c2ecf20Sopenharmony_ci struct cred *cred; 3388c2ecf20Sopenharmony_ci struct key *keyring; 3398c2ecf20Sopenharmony_ci int ret; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci /* create an override credential set with a special thread keyring in 3428c2ecf20Sopenharmony_ci * which DNS requests are cached 3438c2ecf20Sopenharmony_ci * 3448c2ecf20Sopenharmony_ci * this is used to prevent malicious redirections from being installed 3458c2ecf20Sopenharmony_ci * with add_key(). 3468c2ecf20Sopenharmony_ci */ 3478c2ecf20Sopenharmony_ci cred = prepare_kernel_cred(NULL); 3488c2ecf20Sopenharmony_ci if (!cred) 3498c2ecf20Sopenharmony_ci return -ENOMEM; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci keyring = keyring_alloc(".dns_resolver", 3528c2ecf20Sopenharmony_ci GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 3538c2ecf20Sopenharmony_ci (KEY_POS_ALL & ~KEY_POS_SETATTR) | 3548c2ecf20Sopenharmony_ci KEY_USR_VIEW | KEY_USR_READ, 3558c2ecf20Sopenharmony_ci KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); 3568c2ecf20Sopenharmony_ci if (IS_ERR(keyring)) { 3578c2ecf20Sopenharmony_ci ret = PTR_ERR(keyring); 3588c2ecf20Sopenharmony_ci goto failed_put_cred; 3598c2ecf20Sopenharmony_ci } 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci ret = register_key_type(&key_type_dns_resolver); 3628c2ecf20Sopenharmony_ci if (ret < 0) 3638c2ecf20Sopenharmony_ci goto failed_put_key; 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci /* instruct request_key() to use this special keyring as a cache for 3668c2ecf20Sopenharmony_ci * the results it looks up */ 3678c2ecf20Sopenharmony_ci set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags); 3688c2ecf20Sopenharmony_ci cred->thread_keyring = keyring; 3698c2ecf20Sopenharmony_ci cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; 3708c2ecf20Sopenharmony_ci dns_resolver_cache = cred; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci kdebug("DNS resolver keyring: %d\n", key_serial(keyring)); 3738c2ecf20Sopenharmony_ci return 0; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_cifailed_put_key: 3768c2ecf20Sopenharmony_ci key_put(keyring); 3778c2ecf20Sopenharmony_cifailed_put_cred: 3788c2ecf20Sopenharmony_ci put_cred(cred); 3798c2ecf20Sopenharmony_ci return ret; 3808c2ecf20Sopenharmony_ci} 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_cistatic void __exit exit_dns_resolver(void) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci key_revoke(dns_resolver_cache->thread_keyring); 3858c2ecf20Sopenharmony_ci unregister_key_type(&key_type_dns_resolver); 3868c2ecf20Sopenharmony_ci put_cred(dns_resolver_cache); 3878c2ecf20Sopenharmony_ci} 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_cimodule_init(init_dns_resolver) 3908c2ecf20Sopenharmony_cimodule_exit(exit_dns_resolver) 3918c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 392