Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/dns_resolve.c |
| 3 | * |
| 4 | * Copyright (c) 2007 Igor Mammedov |
| 5 | * Author(s): Igor Mammedov (niallain@gmail.com) |
| 6 | * Steve French (sfrench@us.ibm.com) |
| 7 | * |
| 8 | * Contains the CIFS DFS upcall routines used for hostname to |
| 9 | * IP address translation. |
| 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 27 | #include <linux/keyctl.h> |
| 28 | #include <linux/key-type.h> |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 29 | #include <keys/user-type.h> |
| 30 | #include "dns_resolve.h" |
| 31 | #include "cifsglob.h" |
| 32 | #include "cifsproto.h" |
| 33 | #include "cifs_debug.h" |
| 34 | |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 35 | static const struct cred *dns_resolver_cache; |
| 36 | |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 37 | /* Checks if supplied name is IP address |
| 38 | * returns: |
| 39 | * 1 - name is IP |
| 40 | * 0 - name is not IP |
| 41 | */ |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 42 | static int |
David Howells | 67b7626 | 2010-07-22 18:33:01 +0100 | [diff] [blame] | 43 | is_ip(const char *name, int len) |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 44 | { |
Jeff Layton | 1e68b2b | 2009-06-11 10:27:30 -0400 | [diff] [blame] | 45 | struct sockaddr_storage ss; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 46 | |
David Howells | 67b7626 | 2010-07-22 18:33:01 +0100 | [diff] [blame] | 47 | return cifs_convert_address((struct sockaddr *)&ss, name, len); |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 48 | } |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 49 | |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 50 | static int |
| 51 | dns_resolver_instantiate(struct key *key, const void *data, |
| 52 | size_t datalen) |
| 53 | { |
| 54 | int rc = 0; |
| 55 | char *ip; |
| 56 | |
David Howells | 67b7626 | 2010-07-22 18:33:01 +0100 | [diff] [blame] | 57 | /* make sure this looks like an address */ |
| 58 | if (!is_ip(data, datalen)) |
| 59 | return -EINVAL; |
| 60 | |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 61 | ip = kmalloc(datalen + 1, GFP_KERNEL); |
| 62 | if (!ip) |
| 63 | return -ENOMEM; |
| 64 | |
| 65 | memcpy(ip, data, datalen); |
| 66 | ip[datalen] = '\0'; |
| 67 | |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 68 | key->type_data.x[0] = datalen; |
Jeff Layton | d9fb5c0 | 2009-03-23 01:47:11 -0400 | [diff] [blame] | 69 | key->payload.data = ip; |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 70 | |
| 71 | return rc; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | dns_resolver_destroy(struct key *key) |
| 76 | { |
| 77 | kfree(key->payload.data); |
| 78 | } |
| 79 | |
| 80 | struct key_type key_type_dns_resolver = { |
| 81 | .name = "dns_resolver", |
| 82 | .def_datalen = sizeof(struct in_addr), |
| 83 | .describe = user_describe, |
| 84 | .instantiate = dns_resolver_instantiate, |
| 85 | .destroy = dns_resolver_destroy, |
| 86 | .match = user_match, |
| 87 | }; |
| 88 | |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 89 | /* Resolves server name to ip address. |
| 90 | * input: |
| 91 | * unc - server UNC |
| 92 | * output: |
| 93 | * *ip_addr - pointer to server ip, caller responcible for freeing it. |
David Howells | 67b7626 | 2010-07-22 18:33:01 +0100 | [diff] [blame] | 94 | * return the length of the returned string on success |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 95 | */ |
| 96 | int |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 97 | dns_resolve_server_name_to_ip(const char *unc, char **ip_addr) |
| 98 | { |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 99 | const struct cred *saved_cred; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 100 | int rc = -EAGAIN; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 101 | struct key *rkey = ERR_PTR(-EAGAIN); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 102 | char *name; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 103 | char *data = NULL; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 104 | int len; |
| 105 | |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 106 | if (!ip_addr || !unc) |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 107 | return -EINVAL; |
| 108 | |
| 109 | /* search for server name delimiter */ |
| 110 | len = strlen(unc); |
| 111 | if (len < 3) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 112 | cFYI(1, "%s: unc is too short: %s", __func__, unc); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 113 | return -EINVAL; |
| 114 | } |
| 115 | len -= 2; |
| 116 | name = memchr(unc+2, '\\', len); |
| 117 | if (!name) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 118 | cFYI(1, "%s: probably server name is whole unc: %s", |
| 119 | __func__, unc); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 120 | } else { |
| 121 | len = (name - unc) - 2/* leading // */; |
| 122 | } |
| 123 | |
| 124 | name = kmalloc(len+1, GFP_KERNEL); |
| 125 | if (!name) { |
| 126 | rc = -ENOMEM; |
| 127 | return rc; |
| 128 | } |
| 129 | memcpy(name, unc+2, len); |
| 130 | name[len] = 0; |
| 131 | |
David Howells | 67b7626 | 2010-07-22 18:33:01 +0100 | [diff] [blame] | 132 | if (is_ip(name, len)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 133 | cFYI(1, "%s: it is IP, skipping dns upcall: %s", |
| 134 | __func__, name); |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 135 | data = name; |
| 136 | goto skip_upcall; |
| 137 | } |
| 138 | |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 139 | saved_cred = override_creds(dns_resolver_cache); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 140 | rkey = request_key(&key_type_dns_resolver, name, ""); |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 141 | revert_creds(saved_cred); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 142 | if (!IS_ERR(rkey)) { |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 143 | if (!(rkey->perm & KEY_USR_VIEW)) { |
| 144 | down_read(&rkey->sem); |
| 145 | rkey->perm |= KEY_USR_VIEW; |
| 146 | up_read(&rkey->sem); |
| 147 | } |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 148 | len = rkey->type_data.x[0]; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 149 | data = rkey->payload.data; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 150 | } else { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 151 | cERROR(1, "%s: unable to resolve: %s", __func__, name); |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | skip_upcall: |
| 156 | if (data) { |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 157 | *ip_addr = kmalloc(len + 1, GFP_KERNEL); |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 158 | if (*ip_addr) { |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 159 | memcpy(*ip_addr, data, len + 1); |
Igor Mammedov | 5651ced | 2008-05-20 13:02:01 +0400 | [diff] [blame] | 160 | if (!IS_ERR(rkey)) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 161 | cFYI(1, "%s: resolved: %s to %s", __func__, |
Igor Mammedov | 5651ced | 2008-05-20 13:02:01 +0400 | [diff] [blame] | 162 | name, |
| 163 | *ip_addr |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 164 | ); |
David Howells | 67b7626 | 2010-07-22 18:33:01 +0100 | [diff] [blame] | 165 | rc = len; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 166 | } else { |
| 167 | rc = -ENOMEM; |
| 168 | } |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 169 | if (!IS_ERR(rkey)) |
| 170 | key_put(rkey); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 173 | out: |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 174 | kfree(name); |
| 175 | return rc; |
| 176 | } |
| 177 | |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 178 | int __init cifs_init_dns_resolver(void) |
| 179 | { |
| 180 | struct cred *cred; |
| 181 | struct key *keyring; |
| 182 | int ret; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 183 | |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 184 | printk(KERN_NOTICE "Registering the %s key type\n", |
| 185 | key_type_dns_resolver.name); |
| 186 | |
| 187 | /* create an override credential set with a special thread keyring in |
| 188 | * which DNS requests are cached |
| 189 | * |
| 190 | * this is used to prevent malicious redirections from being installed |
| 191 | * with add_key(). |
| 192 | */ |
| 193 | cred = prepare_kernel_cred(NULL); |
| 194 | if (!cred) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred, |
| 198 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | |
| 199 | KEY_USR_VIEW | KEY_USR_READ, |
| 200 | KEY_ALLOC_NOT_IN_QUOTA); |
| 201 | if (IS_ERR(keyring)) { |
| 202 | ret = PTR_ERR(keyring); |
| 203 | goto failed_put_cred; |
| 204 | } |
| 205 | |
| 206 | ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL); |
| 207 | if (ret < 0) |
| 208 | goto failed_put_key; |
| 209 | |
| 210 | ret = register_key_type(&key_type_dns_resolver); |
| 211 | if (ret < 0) |
| 212 | goto failed_put_key; |
| 213 | |
| 214 | /* instruct request_key() to use this special keyring as a cache for |
| 215 | * the results it looks up */ |
| 216 | cred->thread_keyring = keyring; |
| 217 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; |
| 218 | dns_resolver_cache = cred; |
| 219 | return 0; |
| 220 | |
| 221 | failed_put_key: |
| 222 | key_put(keyring); |
| 223 | failed_put_cred: |
| 224 | put_cred(cred); |
| 225 | return ret; |
| 226 | } |
| 227 | |
David Howells | 51c20fc | 2010-07-30 15:25:19 +0100 | [diff] [blame] | 228 | void cifs_exit_dns_resolver(void) |
David Howells | 4c0c03c | 2010-07-22 12:53:18 +0100 | [diff] [blame] | 229 | { |
| 230 | key_revoke(dns_resolver_cache->thread_keyring); |
| 231 | unregister_key_type(&key_type_dns_resolver); |
| 232 | put_cred(dns_resolver_cache); |
| 233 | printk(KERN_NOTICE "Unregistered %s key type\n", |
| 234 | key_type_dns_resolver.name); |
| 235 | } |