blob: 1b1b411adcf1e075f3a87acc788edf4277fd707a [file] [log] [blame]
Wang Lei1a4240f2010-08-04 15:16:33 +01001/* Key type used to cache DNS lookups made by the kernel
2 *
3 * See Documentation/networking/dns_resolver.txt
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
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/kernel.h>
30#include <linux/keyctl.h>
31#include <keys/dns_resolver-type.h>
32#include <keys/user-type.h>
33#include "internal.h"
34
35MODULE_DESCRIPTION("DNS Resolver");
36MODULE_AUTHOR("Wang Lei");
37MODULE_LICENSE("GPL");
38
39unsigned dns_resolver_debug;
40module_param_named(debug, dns_resolver_debug, uint, S_IWUSR | S_IRUGO);
41MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
42
43const struct cred *dns_resolver_cache;
44
45/*
46 * Instantiate a user defined key for dns_resolver.
47 *
48 * The data must be a NUL-terminated string, with the NUL char accounted in
49 * datalen.
50 *
51 * If the data contains a '#' characters, then we take the clause after each
52 * one to be an option of the form 'key=value'. The actual data of interest is
53 * the string leading up to the first '#'. For instance:
54 *
55 * "ip1,ip2,...#foo=bar"
56 */
57static int
58dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen)
59{
60 struct user_key_payload *upayload;
61 int ret;
62 size_t result_len = 0;
63 const char *data = _data, *opt;
64
65 kenter("%%%d,%s,'%s',%zu",
66 key->serial, key->description, data, datalen);
67
68 if (datalen <= 1 || !data || data[datalen - 1] != '\0')
69 return -EINVAL;
70 datalen--;
71
72 /* deal with any options embedded in the data */
73 opt = memchr(data, '#', datalen);
74 if (!opt) {
75 kdebug("no options currently supported");
76 return -EINVAL;
77 }
78
79 result_len = datalen;
80 ret = key_payload_reserve(key, result_len);
81 if (ret < 0)
82 return -EINVAL;
83
84 upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL);
85 if (!upayload) {
86 kleave(" = -ENOMEM");
87 return -ENOMEM;
88 }
89
90 upayload->datalen = result_len;
91 memcpy(upayload->data, data, result_len);
92 upayload->data[result_len] = '\0';
93 rcu_assign_pointer(key->payload.data, upayload);
94
95 kleave(" = 0");
96 return 0;
97}
98
99/*
100 * The description is of the form "[<type>:]<domain_name>"
101 *
102 * The domain name may be a simple name or an absolute domain name (which
103 * should end with a period). The domain name is case-independent.
104 */
105static int
106dns_resolver_match(const struct key *key, const void *description)
107{
108 int slen, dlen, ret = 0;
109 const char *src = key->description, *dsp = description;
110
111 kenter("%s,%s", src, dsp);
112
113 if (!src || !dsp)
114 goto no_match;
115
116 if (strcasecmp(src, dsp) == 0)
117 goto matched;
118
119 slen = strlen(src);
120 dlen = strlen(dsp);
121 if (slen <= 0 || dlen <= 0)
122 goto no_match;
123 if (src[slen - 1] == '.')
124 slen--;
125 if (dsp[dlen - 1] == '.')
126 dlen--;
127 if (slen != dlen || strncasecmp(src, dsp, slen) != 0)
128 goto no_match;
129
130matched:
131 ret = 1;
132no_match:
133 kleave(" = %d", ret);
134 return ret;
135}
136
137struct key_type key_type_dns_resolver = {
138 .name = "dns_resolver",
139 .instantiate = dns_resolver_instantiate,
140 .match = dns_resolver_match,
141 .revoke = user_revoke,
142 .destroy = user_destroy,
143 .describe = user_describe,
144 .read = user_read,
145};
146
147static int __init init_dns_resolver(void)
148{
149 struct cred *cred;
150 struct key *keyring;
151 int ret;
152
153 printk(KERN_NOTICE "Registering the %s key type\n",
154 key_type_dns_resolver.name);
155
156 /* create an override credential set with a special thread keyring in
157 * which DNS requests are cached
158 *
159 * this is used to prevent malicious redirections from being installed
160 * with add_key().
161 */
162 cred = prepare_kernel_cred(NULL);
163 if (!cred)
164 return -ENOMEM;
165
166 keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
167 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
168 KEY_USR_VIEW | KEY_USR_READ,
169 KEY_ALLOC_NOT_IN_QUOTA);
170 if (IS_ERR(keyring)) {
171 ret = PTR_ERR(keyring);
172 goto failed_put_cred;
173 }
174
175 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
176 if (ret < 0)
177 goto failed_put_key;
178
179 ret = register_key_type(&key_type_dns_resolver);
180 if (ret < 0)
181 goto failed_put_key;
182
183 /* instruct request_key() to use this special keyring as a cache for
184 * the results it looks up */
185 cred->thread_keyring = keyring;
186 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
187 dns_resolver_cache = cred;
188
189 kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
190 return 0;
191
192failed_put_key:
193 key_put(keyring);
194failed_put_cred:
195 put_cred(cred);
196 return ret;
197}
198
199static void __exit exit_dns_resolver(void)
200{
201 key_revoke(dns_resolver_cache->thread_keyring);
202 unregister_key_type(&key_type_dns_resolver);
203 put_cred(dns_resolver_cache);
204 printk(KERN_NOTICE "Unregistered %s key type\n",
205 key_type_dns_resolver.name);
206}
207
208module_init(init_dns_resolver)
209module_exit(exit_dns_resolver)
210MODULE_LICENSE("GPL");