keys: Replace uid/gid/perm permissions checking with an ACL
[linux-2.6-block.git] / net / dns_resolver / dns_query.c
CommitLineData
1a4240f4
WL
1/* Upcall routine, designed to work as a key type and working through
2 * /sbin/request-key to contact userspace when handling DNS queries.
3 *
4 * See Documentation/networking/dns_resolver.txt
5 *
6 * Copyright (c) 2007 Igor Mammedov
7 * Author(s): Igor Mammedov (niallain@gmail.com)
8 * Steve French (sfrench@us.ibm.com)
9 * Wang Lei (wang840925@gmail.com)
10 * David Howells (dhowells@redhat.com)
11 *
12 * The upcall wrapper used to make an arbitrary DNS query.
13 *
14 * This function requires the appropriate userspace tool dns.upcall to be
15 * installed and something like the following lines should be added to the
16 * /etc/request-key.conf file:
17 *
18 * create dns_resolver * * /sbin/dns.upcall %k
19 *
20 * For example to use this module to query AFSDB RR:
21 *
22 * create dns_resolver afsdb:* * /sbin/dns.afsdb %k
23 *
24 * This library is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU Lesser General Public License as published
26 * by the Free Software Foundation; either version 2.1 of the License, or
27 * (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
32 * the GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
c057b190 35 * along with this library; if not, see <http://www.gnu.org/licenses/>.
1a4240f4
WL
36 */
37
38#include <linux/module.h>
39#include <linux/slab.h>
5b825c3a 40#include <linux/cred.h>
1a4240f4 41#include <linux/dns_resolver.h>
af352fe9 42#include <linux/err.h>
a58946c1 43#include <net/net_namespace.h>
5b825c3a 44
1a4240f4
WL
45#include <keys/dns_resolver-type.h>
46#include <keys/user-type.h>
47
48#include "internal.h"
49
2e12256b
DH
50static struct key_acl dns_key_acl = {
51 .usage = REFCOUNT_INIT(1),
52 .nr_ace = 2,
53 .possessor_viewable = true,
54 .aces = {
55 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_SEARCH | KEY_ACE_READ),
56 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_INVAL),
57 }
58};
59
ff9517a6 60/**
1a4240f4 61 * dns_query - Query the DNS
a58946c1 62 * @net: The network namespace to operate in.
1a4240f4
WL
63 * @type: Query type (or NULL for straight host->IP lookup)
64 * @name: Name to look up
65 * @namelen: Length of name
66 * @options: Request options (or NULL if no options)
4d673da1 67 * @_result: Where to place the returned data (or NULL)
1a4240f4 68 * @_expiry: Where to store the result expiry time (or NULL)
d0660f0b 69 * @invalidate: Always invalidate the key after use
1a4240f4 70 *
4d673da1
DH
71 * The data will be returned in the pointer at *result, if provided, and the
72 * caller is responsible for freeing it.
1a4240f4
WL
73 *
74 * The description should be of the form "[<query_type>:]<domain_name>", and
75 * the options need to be appropriate for the query type requested. If no
76 * query_type is given, then the query is a straight hostname to IP address
77 * lookup.
78 *
79 * The DNS resolution lookup is performed by upcalling to userspace by way of
80 * requesting a key of type dns_resolver.
81 *
82 * Returns the size of the result on success, -ve error code otherwise.
83 */
a58946c1
DH
84int dns_query(struct net *net,
85 const char *type, const char *name, size_t namelen,
d0660f0b
DH
86 const char *options, char **_result, time64_t *_expiry,
87 bool invalidate)
1a4240f4
WL
88{
89 struct key *rkey;
0837e49a 90 struct user_key_payload *upayload;
1a4240f4
WL
91 const struct cred *saved_cred;
92 size_t typelen, desclen;
93 char *desc, *cp;
94 int ret, len;
95
96 kenter("%s,%*.*s,%zu,%s",
97 type, (int)namelen, (int)namelen, name, namelen, options);
98
4d673da1 99 if (!name || namelen == 0)
1a4240f4
WL
100 return -EINVAL;
101
102 /* construct the query key description as "[<type>:]<name>" */
103 typelen = 0;
104 desclen = 0;
105 if (type) {
106 typelen = strlen(type);
107 if (typelen < 1)
108 return -EINVAL;
109 desclen += typelen + 1;
110 }
111
9638f671 112 if (namelen < 3 || namelen > 255)
1a4240f4
WL
113 return -EINVAL;
114 desclen += namelen + 1;
115
116 desc = kmalloc(desclen, GFP_KERNEL);
117 if (!desc)
118 return -ENOMEM;
119
120 cp = desc;
121 if (type) {
122 memcpy(cp, type, typelen);
123 cp += typelen;
124 *cp++ = ':';
125 }
126 memcpy(cp, name, namelen);
127 cp += namelen;
128 *cp = '\0';
129
130 if (!options)
131 options = "";
132 kdebug("call request_key(,%s,%s)", desc, options);
133
134 /* make the upcall, using special credentials to prevent the use of
135 * add_key() to preinstall malicious redirections
136 */
137 saved_cred = override_creds(dns_resolver_cache);
2e12256b
DH
138 rkey = request_key_net(&key_type_dns_resolver, desc, net, options,
139 &dns_key_acl);
1a4240f4
WL
140 revert_creds(saved_cred);
141 kfree(desc);
142 if (IS_ERR(rkey)) {
143 ret = PTR_ERR(rkey);
144 goto out;
145 }
146
147 down_read(&rkey->sem);
0c7774ab 148 set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
1a4240f4
WL
149 ret = key_validate(rkey);
150 if (ret < 0)
151 goto put;
152
4a2d7892 153 /* If the DNS server gave an error, return that to the caller */
146aa8b1 154 ret = PTR_ERR(rkey->payload.data[dns_key_error]);
4a2d7892
WL
155 if (ret)
156 goto put;
157
0837e49a 158 upayload = user_key_payload_locked(rkey);
1a4240f4
WL
159 len = upayload->datalen;
160
4d673da1
DH
161 if (_result) {
162 ret = -ENOMEM;
bbb4c432 163 *_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
4d673da1
DH
164 if (!*_result)
165 goto put;
4d673da1 166 }
84a7c0b1 167
1a4240f4
WL
168 if (_expiry)
169 *_expiry = rkey->expiry;
170
171 ret = len;
172put:
173 up_read(&rkey->sem);
d0660f0b
DH
174 if (invalidate)
175 key_invalidate(rkey);
1a4240f4
WL
176 key_put(rkey);
177out:
178 kleave(" = %d", ret);
179 return ret;
180}
181EXPORT_SYMBOL(dns_query);