keys, dns: Allow key types (eg. DNS) to be reclaimed immediately on expiry
[linux-2.6-block.git] / net / dns_resolver / dns_key.c
CommitLineData
1a4240f4
WL
1/* Key type used to cache DNS lookups made by the kernel
2 *
9dfe1361 3 * See Documentation/networking/dns_resolver.rst
1a4240f4
WL
4 *
5 * Copyright (c) 2007 Igor Mammedov
6 * Author(s): Igor Mammedov (niallain@gmail.com)
7 * Steve French (sfrench@us.ibm.com)
8 * Wang Lei (wang840925@gmail.com)
9 * David Howells (dhowells@redhat.com)
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
c057b190 22 * along with this library; if not, see <http://www.gnu.org/licenses/>.
1a4240f4
WL
23 */
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/slab.h>
27#include <linux/string.h>
28#include <linux/kernel.h>
29#include <linux/keyctl.h>
af352fe9 30#include <linux/err.h>
4a2d7892 31#include <linux/seq_file.h>
bbb4c432 32#include <linux/dns_resolver.h>
1a4240f4
WL
33#include <keys/dns_resolver-type.h>
34#include <keys/user-type.h>
35#include "internal.h"
36
37MODULE_DESCRIPTION("DNS Resolver");
38MODULE_AUTHOR("Wang Lei");
39MODULE_LICENSE("GPL");
40
95c96174 41unsigned int dns_resolver_debug;
d6444062 42module_param_named(debug, dns_resolver_debug, uint, 0644);
1a4240f4
WL
43MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
44
45const struct cred *dns_resolver_cache;
46
4a2d7892
WL
47#define DNS_ERRORNO_OPTION "dnserror"
48
1a4240f4 49/*
d46d4942 50 * Preparse instantiation data for a dns_resolver key.
1a4240f4 51 *
bbb4c432
DH
52 * For normal hostname lookups, the data must be a NUL-terminated string, with
53 * the NUL char accounted in datalen.
1a4240f4
WL
54 *
55 * If the data contains a '#' characters, then we take the clause after each
56 * one to be an option of the form 'key=value'. The actual data of interest is
57 * the string leading up to the first '#'. For instance:
58 *
59 * "ip1,ip2,...#foo=bar"
bbb4c432
DH
60 *
61 * For server list requests, the data must begin with a NUL char and be
62 * followed by a byte indicating the version of the data format. Version 1
63 * looks something like (note this is packed):
64 *
65 * u8 Non-string marker (ie. 0)
66 * u8 Content (DNS_PAYLOAD_IS_*)
67 * u8 Version (e.g. 1)
68 * u8 Source of server list
69 * u8 Lookup status of server list
70 * u8 Number of servers
71 * foreach-server {
72 * __le16 Name length
73 * __le16 Priority (as per SRV record, low first)
74 * __le16 Weight (as per SRV record, higher first)
75 * __le16 Port
76 * u8 Source of address list
77 * u8 Lookup status of address list
78 * u8 Protocol (DNS_SERVER_PROTOCOL_*)
79 * u8 Number of addresses
80 * char[] Name (not NUL-terminated)
81 * foreach-address {
82 * u8 Family (DNS_ADDRESS_IS_*)
83 * union {
84 * u8[4] ipv4_addr
85 * u8[16] ipv6_addr
86 * }
87 * }
88 * }
89 *
1a4240f4
WL
90 */
91static int
d46d4942 92dns_resolver_preparse(struct key_preparsed_payload *prep)
1a4240f4 93{
39299bdd 94 const struct dns_server_list_v1_header *v1;
bbb4c432 95 const struct dns_payload_header *bin;
1a4240f4 96 struct user_key_payload *upayload;
4a2d7892 97 unsigned long derrno;
1a4240f4 98 int ret;
d46d4942 99 int datalen = prep->datalen, result_len = 0;
cf7f601c 100 const char *data = prep->data, *end, *opt;
1a4240f4 101
bbb4c432
DH
102 if (datalen <= 1 || !data)
103 return -EINVAL;
104
105 if (data[0] == 0) {
106 /* It may be a server list. */
107 if (datalen <= sizeof(*bin))
108 return -EINVAL;
109
110 bin = (const struct dns_payload_header *)data;
111 kenter("[%u,%u],%u", bin->content, bin->version, datalen);
112 if (bin->content != DNS_PAYLOAD_IS_SERVER_LIST) {
113 pr_warn_ratelimited(
114 "dns_resolver: Unsupported content type (%u)\n",
115 bin->content);
116 return -EINVAL;
117 }
118
119 if (bin->version != 1) {
120 pr_warn_ratelimited(
121 "dns_resolver: Unsupported server list version (%u)\n",
122 bin->version);
123 return -EINVAL;
124 }
125
39299bdd
DH
126 v1 = (const struct dns_server_list_v1_header *)bin;
127 if ((v1->status != DNS_LOOKUP_GOOD &&
128 v1->status != DNS_LOOKUP_GOOD_WITH_BAD)) {
129 if (prep->expiry == TIME64_MAX)
130 prep->expiry = ktime_get_real_seconds() + 1;
131 }
132
bbb4c432
DH
133 result_len = datalen;
134 goto store_result;
135 }
136
d46d4942 137 kenter("'%*.*s',%u", datalen, datalen, data, datalen);
1a4240f4 138
bbb4c432 139 if (!data || data[datalen - 1] != '\0')
1a4240f4
WL
140 return -EINVAL;
141 datalen--;
142
143 /* deal with any options embedded in the data */
4a2d7892 144 end = data + datalen;
1a4240f4
WL
145 opt = memchr(data, '#', datalen);
146 if (!opt) {
4a2d7892
WL
147 /* no options: the entire data is the result */
148 kdebug("no options");
149 result_len = datalen;
150 } else {
151 const char *next_opt;
152
153 result_len = opt - data;
154 opt++;
155 kdebug("options: '%s'", opt);
156 do {
c604cb76 157 int opt_len, opt_nlen;
4a2d7892 158 const char *eq;
c604cb76 159 char optval[128];
4a2d7892
WL
160
161 next_opt = memchr(opt, '#', end - opt) ?: end;
162 opt_len = next_opt - opt;
c604cb76 163 if (opt_len <= 0 || opt_len > sizeof(optval)) {
9c438d7a
EB
164 pr_warn_ratelimited("Invalid option length (%d) for dns_resolver key\n",
165 opt_len);
4a2d7892
WL
166 return -EINVAL;
167 }
168
c604cb76
EB
169 eq = memchr(opt, '=', opt_len);
170 if (eq) {
171 opt_nlen = eq - opt;
172 eq++;
173 memcpy(optval, eq, next_opt - eq);
174 optval[next_opt - eq] = '\0';
175 } else {
176 opt_nlen = opt_len;
177 optval[0] = '\0';
178 }
4a2d7892 179
c604cb76
EB
180 kdebug("option '%*.*s' val '%s'",
181 opt_nlen, opt_nlen, opt, optval);
4a2d7892
WL
182
183 /* see if it's an error number representing a DNS error
184 * that's to be recorded as the result in this key */
185 if (opt_nlen == sizeof(DNS_ERRORNO_OPTION) - 1 &&
186 memcmp(opt, DNS_ERRORNO_OPTION, opt_nlen) == 0) {
187 kdebug("dns error number option");
4a2d7892 188
c604cb76 189 ret = kstrtoul(optval, 10, &derrno);
4a2d7892
WL
190 if (ret < 0)
191 goto bad_option_value;
192
193 if (derrno < 1 || derrno > 511)
194 goto bad_option_value;
195
196 kdebug("dns error no. = %lu", derrno);
146aa8b1 197 prep->payload.data[dns_key_error] = ERR_PTR(-derrno);
4a2d7892
WL
198 continue;
199 }
200
201 bad_option_value:
9c438d7a
EB
202 pr_warn_ratelimited("Option '%*.*s' to dns_resolver key: bad/missing value\n",
203 opt_nlen, opt_nlen, opt);
4a2d7892
WL
204 return -EINVAL;
205 } while (opt = next_opt + 1, opt < end);
1a4240f4
WL
206 }
207
4a2d7892
WL
208 /* don't cache the result if we're caching an error saying there's no
209 * result */
146aa8b1
DH
210 if (prep->payload.data[dns_key_error]) {
211 kleave(" = 0 [h_error %ld]", PTR_ERR(prep->payload.data[dns_key_error]));
4a2d7892
WL
212 return 0;
213 }
214
bbb4c432 215store_result:
4a2d7892 216 kdebug("store result");
d46d4942 217 prep->quotalen = result_len;
1a4240f4
WL
218
219 upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
220 if (!upayload) {
221 kleave(" = -ENOMEM");
222 return -ENOMEM;
223 }
224
225 upayload->datalen = result_len;
226 memcpy(upayload->data, data, result_len);
227 upayload->data[result_len] = '\0';
1a4240f4 228
146aa8b1 229 prep->payload.data[dns_key_data] = upayload;
1a4240f4
WL
230 kleave(" = 0");
231 return 0;
232}
233
d46d4942
DH
234/*
235 * Clean up the preparse data
236 */
237static void dns_resolver_free_preparse(struct key_preparsed_payload *prep)
238{
239 pr_devel("==>%s()\n", __func__);
240
146aa8b1 241 kfree(prep->payload.data[dns_key_data]);
d46d4942
DH
242}
243
1a4240f4
WL
244/*
245 * The description is of the form "[<type>:]<domain_name>"
246 *
247 * The domain name may be a simple name or an absolute domain name (which
248 * should end with a period). The domain name is case-independent.
249 */
0c903ab6
DH
250static bool dns_resolver_cmp(const struct key *key,
251 const struct key_match_data *match_data)
1a4240f4
WL
252{
253 int slen, dlen, ret = 0;
46291959 254 const char *src = key->description, *dsp = match_data->raw_data;
1a4240f4
WL
255
256 kenter("%s,%s", src, dsp);
257
258 if (!src || !dsp)
259 goto no_match;
260
261 if (strcasecmp(src, dsp) == 0)
262 goto matched;
263
264 slen = strlen(src);
265 dlen = strlen(dsp);
266 if (slen <= 0 || dlen <= 0)
267 goto no_match;
268 if (src[slen - 1] == '.')
269 slen--;
270 if (dsp[dlen - 1] == '.')
271 dlen--;
272 if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
273 goto no_match;
274
275matched:
276 ret = 1;
277no_match:
278 kleave(" = %d", ret);
279 return ret;
280}
281
c06cfb08
DH
282/*
283 * Preparse the match criterion.
284 */
285static int dns_resolver_match_preparse(struct key_match_data *match_data)
286{
287 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
288 match_data->cmp = dns_resolver_cmp;
289 return 0;
290}
291
4a2d7892
WL
292/*
293 * Describe a DNS key
294 */
295static void dns_resolver_describe(const struct key *key, struct seq_file *m)
296{
4a2d7892 297 seq_puts(m, key->description);
363b02da 298 if (key_is_positive(key)) {
146aa8b1
DH
299 int err = PTR_ERR(key->payload.data[dns_key_error]);
300
78b7280c
DH
301 if (err)
302 seq_printf(m, ": %d", err);
303 else
304 seq_printf(m, ": %u", key->datalen);
305 }
4a2d7892
WL
306}
307
1362fa07
DH
308/*
309 * read the DNS data
310 * - the key's semaphore is read-locked
311 */
312static long dns_resolver_read(const struct key *key,
d3ec10aa 313 char *buffer, size_t buflen)
1362fa07 314{
146aa8b1
DH
315 int err = PTR_ERR(key->payload.data[dns_key_error]);
316
317 if (err)
318 return err;
1362fa07
DH
319
320 return user_read(key, buffer, buflen);
321}
322
1a4240f4
WL
323struct key_type key_type_dns_resolver = {
324 .name = "dns_resolver",
39299bdd 325 .flags = KEY_TYPE_NET_DOMAIN | KEY_TYPE_INSTANT_REAP,
d46d4942
DH
326 .preparse = dns_resolver_preparse,
327 .free_preparse = dns_resolver_free_preparse,
328 .instantiate = generic_key_instantiate,
c06cfb08 329 .match_preparse = dns_resolver_match_preparse,
1a4240f4
WL
330 .revoke = user_revoke,
331 .destroy = user_destroy,
4a2d7892 332 .describe = dns_resolver_describe,
1362fa07 333 .read = dns_resolver_read,
1a4240f4
WL
334};
335
336static int __init init_dns_resolver(void)
337{
338 struct cred *cred;
339 struct key *keyring;
340 int ret;
341
1a4240f4
WL
342 /* create an override credential set with a special thread keyring in
343 * which DNS requests are cached
344 *
345 * this is used to prevent malicious redirections from being installed
346 * with add_key().
347 */
5a17f040 348 cred = prepare_kernel_cred(&init_task);
1a4240f4
WL
349 if (!cred)
350 return -ENOMEM;
351
2a74dbb9
LT
352 keyring = keyring_alloc(".dns_resolver",
353 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
028db3e2
LT
354 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
355 KEY_USR_VIEW | KEY_USR_READ,
5ac7eace 356 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
1a4240f4
WL
357 if (IS_ERR(keyring)) {
358 ret = PTR_ERR(keyring);
359 goto failed_put_cred;
360 }
361
1a4240f4
WL
362 ret = register_key_type(&key_type_dns_resolver);
363 if (ret < 0)
364 goto failed_put_key;
365
366 /* instruct request_key() to use this special keyring as a cache for
367 * the results it looks up */
700920eb 368 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
1a4240f4
WL
369 cred->thread_keyring = keyring;
370 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
371 dns_resolver_cache = cred;
372
373 kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
374 return 0;
375
376failed_put_key:
377 key_put(keyring);
378failed_put_cred:
379 put_cred(cred);
380 return ret;
381}
382
383static void __exit exit_dns_resolver(void)
384{
385 key_revoke(dns_resolver_cache->thread_keyring);
386 unregister_key_type(&key_type_dns_resolver);
387 put_cred(dns_resolver_cache);
1a4240f4
WL
388}
389
390module_init(init_dns_resolver)
391module_exit(exit_dns_resolver)
392MODULE_LICENSE("GPL");