| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * fs/nfs/idmap.c | 
|  | 3 | * | 
|  | 4 | *  UID and GID to name mapping for clients. | 
|  | 5 | * | 
|  | 6 | *  Copyright (c) 2002 The Regents of the University of Michigan. | 
|  | 7 | *  All rights reserved. | 
|  | 8 | * | 
|  | 9 | *  Marius Aamodt Eriksen <marius@umich.edu> | 
|  | 10 | * | 
|  | 11 | *  Redistribution and use in source and binary forms, with or without | 
|  | 12 | *  modification, are permitted provided that the following conditions | 
|  | 13 | *  are met: | 
|  | 14 | * | 
|  | 15 | *  1. Redistributions of source code must retain the above copyright | 
|  | 16 | *     notice, this list of conditions and the following disclaimer. | 
|  | 17 | *  2. Redistributions in binary form must reproduce the above copyright | 
|  | 18 | *     notice, this list of conditions and the following disclaimer in the | 
|  | 19 | *     documentation and/or other materials provided with the distribution. | 
|  | 20 | *  3. Neither the name of the University nor the names of its | 
|  | 21 | *     contributors may be used to endorse or promote products derived | 
|  | 22 | *     from this software without specific prior written permission. | 
|  | 23 | * | 
|  | 24 | *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | 
|  | 25 | *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 
|  | 26 | *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
|  | 27 | *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|  | 28 | *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
|  | 29 | *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
|  | 30 | *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | 
|  | 31 | *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 
|  | 32 | *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 
|  | 33 | *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
|  | 34 | *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|  | 35 | */ | 
|  | 36 | #include <linux/types.h> | 
|  | 37 | #include <linux/parser.h> | 
|  | 38 | #include <linux/fs.h> | 
|  | 39 | #include <linux/nfs_idmap.h> | 
|  | 40 | #include <net/net_namespace.h> | 
|  | 41 | #include <linux/sunrpc/rpc_pipe_fs.h> | 
|  | 42 | #include <linux/nfs_fs.h> | 
|  | 43 | #include <linux/nfs_fs_sb.h> | 
|  | 44 | #include <linux/key.h> | 
|  | 45 | #include <linux/keyctl.h> | 
|  | 46 | #include <linux/key-type.h> | 
|  | 47 | #include <keys/user-type.h> | 
|  | 48 | #include <linux/module.h> | 
|  | 49 |  | 
|  | 50 | #include "internal.h" | 
|  | 51 | #include "netns.h" | 
|  | 52 |  | 
|  | 53 | #define NFS_UINT_MAXLEN 11 | 
|  | 54 |  | 
|  | 55 | /* Default cache timeout is 10 minutes */ | 
|  | 56 | unsigned int nfs_idmap_cache_timeout = 600; | 
|  | 57 | static const struct cred *id_resolver_cache; | 
|  | 58 | static struct key_type key_type_id_resolver_legacy; | 
|  | 59 |  | 
|  | 60 | struct idmap { | 
|  | 61 | struct rpc_pipe		*idmap_pipe; | 
|  | 62 | struct key_construction	*idmap_key_cons; | 
|  | 63 | struct mutex		idmap_mutex; | 
|  | 64 | }; | 
|  | 65 |  | 
|  | 66 | struct idmap_legacy_upcalldata { | 
|  | 67 | struct rpc_pipe_msg pipe_msg; | 
|  | 68 | struct idmap_msg idmap_msg; | 
|  | 69 | struct idmap *idmap; | 
|  | 70 | }; | 
|  | 71 |  | 
|  | 72 | /** | 
|  | 73 | * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields | 
|  | 74 | * @fattr: fully initialised struct nfs_fattr | 
|  | 75 | * @owner_name: owner name string cache | 
|  | 76 | * @group_name: group name string cache | 
|  | 77 | */ | 
|  | 78 | void nfs_fattr_init_names(struct nfs_fattr *fattr, | 
|  | 79 | struct nfs4_string *owner_name, | 
|  | 80 | struct nfs4_string *group_name) | 
|  | 81 | { | 
|  | 82 | fattr->owner_name = owner_name; | 
|  | 83 | fattr->group_name = group_name; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr) | 
|  | 87 | { | 
|  | 88 | fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME; | 
|  | 89 | kfree(fattr->owner_name->data); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | static void nfs_fattr_free_group_name(struct nfs_fattr *fattr) | 
|  | 93 | { | 
|  | 94 | fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME; | 
|  | 95 | kfree(fattr->group_name->data); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr) | 
|  | 99 | { | 
|  | 100 | struct nfs4_string *owner = fattr->owner_name; | 
|  | 101 | __u32 uid; | 
|  | 102 |  | 
|  | 103 | if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)) | 
|  | 104 | return false; | 
|  | 105 | if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) { | 
|  | 106 | fattr->uid = uid; | 
|  | 107 | fattr->valid |= NFS_ATTR_FATTR_OWNER; | 
|  | 108 | } | 
|  | 109 | return true; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr) | 
|  | 113 | { | 
|  | 114 | struct nfs4_string *group = fattr->group_name; | 
|  | 115 | __u32 gid; | 
|  | 116 |  | 
|  | 117 | if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)) | 
|  | 118 | return false; | 
|  | 119 | if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) { | 
|  | 120 | fattr->gid = gid; | 
|  | 121 | fattr->valid |= NFS_ATTR_FATTR_GROUP; | 
|  | 122 | } | 
|  | 123 | return true; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | /** | 
|  | 127 | * nfs_fattr_free_names - free up the NFSv4 owner and group strings | 
|  | 128 | * @fattr: a fully initialised nfs_fattr structure | 
|  | 129 | */ | 
|  | 130 | void nfs_fattr_free_names(struct nfs_fattr *fattr) | 
|  | 131 | { | 
|  | 132 | if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME) | 
|  | 133 | nfs_fattr_free_owner_name(fattr); | 
|  | 134 | if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME) | 
|  | 135 | nfs_fattr_free_group_name(fattr); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | /** | 
|  | 139 | * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free | 
|  | 140 | * @server: pointer to the filesystem nfs_server structure | 
|  | 141 | * @fattr: a fully initialised nfs_fattr structure | 
|  | 142 | * | 
|  | 143 | * This helper maps the cached NFSv4 owner/group strings in fattr into | 
|  | 144 | * their numeric uid/gid equivalents, and then frees the cached strings. | 
|  | 145 | */ | 
|  | 146 | void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr) | 
|  | 147 | { | 
|  | 148 | if (nfs_fattr_map_owner_name(server, fattr)) | 
|  | 149 | nfs_fattr_free_owner_name(fattr); | 
|  | 150 | if (nfs_fattr_map_group_name(server, fattr)) | 
|  | 151 | nfs_fattr_free_group_name(fattr); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res) | 
|  | 155 | { | 
|  | 156 | unsigned long val; | 
|  | 157 | char buf[16]; | 
|  | 158 |  | 
|  | 159 | if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf)) | 
|  | 160 | return 0; | 
|  | 161 | memcpy(buf, name, namelen); | 
|  | 162 | buf[namelen] = '\0'; | 
|  | 163 | if (strict_strtoul(buf, 0, &val) != 0) | 
|  | 164 | return 0; | 
|  | 165 | *res = val; | 
|  | 166 | return 1; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen) | 
|  | 170 | { | 
|  | 171 | return snprintf(buf, buflen, "%u", id); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | static struct key_type key_type_id_resolver = { | 
|  | 175 | .name		= "id_resolver", | 
|  | 176 | .instantiate	= user_instantiate, | 
|  | 177 | .match		= user_match, | 
|  | 178 | .revoke		= user_revoke, | 
|  | 179 | .destroy	= user_destroy, | 
|  | 180 | .describe	= user_describe, | 
|  | 181 | .read		= user_read, | 
|  | 182 | }; | 
|  | 183 |  | 
|  | 184 | static int nfs_idmap_init_keyring(void) | 
|  | 185 | { | 
|  | 186 | struct cred *cred; | 
|  | 187 | struct key *keyring; | 
|  | 188 | int ret = 0; | 
|  | 189 |  | 
|  | 190 | printk(KERN_NOTICE "NFS: Registering the %s key type\n", | 
|  | 191 | key_type_id_resolver.name); | 
|  | 192 |  | 
|  | 193 | cred = prepare_kernel_cred(NULL); | 
|  | 194 | if (!cred) | 
|  | 195 | return -ENOMEM; | 
|  | 196 |  | 
|  | 197 | keyring = key_alloc(&key_type_keyring, ".id_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_id_resolver); | 
|  | 211 | if (ret < 0) | 
|  | 212 | goto failed_put_key; | 
|  | 213 |  | 
|  | 214 | ret = register_key_type(&key_type_id_resolver_legacy); | 
|  | 215 | if (ret < 0) | 
|  | 216 | goto failed_reg_legacy; | 
|  | 217 |  | 
|  | 218 | set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags); | 
|  | 219 | cred->thread_keyring = keyring; | 
|  | 220 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; | 
|  | 221 | id_resolver_cache = cred; | 
|  | 222 | return 0; | 
|  | 223 |  | 
|  | 224 | failed_reg_legacy: | 
|  | 225 | unregister_key_type(&key_type_id_resolver); | 
|  | 226 | failed_put_key: | 
|  | 227 | key_put(keyring); | 
|  | 228 | failed_put_cred: | 
|  | 229 | put_cred(cred); | 
|  | 230 | return ret; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | static void nfs_idmap_quit_keyring(void) | 
|  | 234 | { | 
|  | 235 | key_revoke(id_resolver_cache->thread_keyring); | 
|  | 236 | unregister_key_type(&key_type_id_resolver); | 
|  | 237 | unregister_key_type(&key_type_id_resolver_legacy); | 
|  | 238 | put_cred(id_resolver_cache); | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | /* | 
|  | 242 | * Assemble the description to pass to request_key() | 
|  | 243 | * This function will allocate a new string and update dest to point | 
|  | 244 | * at it.  The caller is responsible for freeing dest. | 
|  | 245 | * | 
|  | 246 | * On error 0 is returned.  Otherwise, the length of dest is returned. | 
|  | 247 | */ | 
|  | 248 | static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen, | 
|  | 249 | const char *type, size_t typelen, char **desc) | 
|  | 250 | { | 
|  | 251 | char *cp; | 
|  | 252 | size_t desclen = typelen + namelen + 2; | 
|  | 253 |  | 
|  | 254 | *desc = kmalloc(desclen, GFP_KERNEL); | 
|  | 255 | if (!*desc) | 
|  | 256 | return -ENOMEM; | 
|  | 257 |  | 
|  | 258 | cp = *desc; | 
|  | 259 | memcpy(cp, type, typelen); | 
|  | 260 | cp += typelen; | 
|  | 261 | *cp++ = ':'; | 
|  | 262 |  | 
|  | 263 | memcpy(cp, name, namelen); | 
|  | 264 | cp += namelen; | 
|  | 265 | *cp = '\0'; | 
|  | 266 | return desclen; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | static ssize_t nfs_idmap_request_key(struct key_type *key_type, | 
|  | 270 | const char *name, size_t namelen, | 
|  | 271 | const char *type, void *data, | 
|  | 272 | size_t data_size, struct idmap *idmap) | 
|  | 273 | { | 
|  | 274 | const struct cred *saved_cred; | 
|  | 275 | struct key *rkey; | 
|  | 276 | char *desc; | 
|  | 277 | struct user_key_payload *payload; | 
|  | 278 | ssize_t ret; | 
|  | 279 |  | 
|  | 280 | ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc); | 
|  | 281 | if (ret <= 0) | 
|  | 282 | goto out; | 
|  | 283 |  | 
|  | 284 | saved_cred = override_creds(id_resolver_cache); | 
|  | 285 | if (idmap) | 
|  | 286 | rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap); | 
|  | 287 | else | 
|  | 288 | rkey = request_key(&key_type_id_resolver, desc, ""); | 
|  | 289 | revert_creds(saved_cred); | 
|  | 290 |  | 
|  | 291 | kfree(desc); | 
|  | 292 | if (IS_ERR(rkey)) { | 
|  | 293 | ret = PTR_ERR(rkey); | 
|  | 294 | goto out; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | rcu_read_lock(); | 
|  | 298 | rkey->perm |= KEY_USR_VIEW; | 
|  | 299 |  | 
|  | 300 | ret = key_validate(rkey); | 
|  | 301 | if (ret < 0) | 
|  | 302 | goto out_up; | 
|  | 303 |  | 
|  | 304 | payload = rcu_dereference(rkey->payload.data); | 
|  | 305 | if (IS_ERR_OR_NULL(payload)) { | 
|  | 306 | ret = PTR_ERR(payload); | 
|  | 307 | goto out_up; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | ret = payload->datalen; | 
|  | 311 | if (ret > 0 && ret <= data_size) | 
|  | 312 | memcpy(data, payload->data, ret); | 
|  | 313 | else | 
|  | 314 | ret = -EINVAL; | 
|  | 315 |  | 
|  | 316 | out_up: | 
|  | 317 | rcu_read_unlock(); | 
|  | 318 | key_put(rkey); | 
|  | 319 | out: | 
|  | 320 | return ret; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | static ssize_t nfs_idmap_get_key(const char *name, size_t namelen, | 
|  | 324 | const char *type, void *data, | 
|  | 325 | size_t data_size, struct idmap *idmap) | 
|  | 326 | { | 
|  | 327 | ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver, | 
|  | 328 | name, namelen, type, data, | 
|  | 329 | data_size, NULL); | 
|  | 330 | if (ret < 0) { | 
|  | 331 | mutex_lock(&idmap->idmap_mutex); | 
|  | 332 | ret = nfs_idmap_request_key(&key_type_id_resolver_legacy, | 
|  | 333 | name, namelen, type, data, | 
|  | 334 | data_size, idmap); | 
|  | 335 | idmap->idmap_key_cons = NULL; | 
|  | 336 | mutex_unlock(&idmap->idmap_mutex); | 
|  | 337 | } | 
|  | 338 | return ret; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | /* ID -> Name */ | 
|  | 342 | static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, | 
|  | 343 | size_t buflen, struct idmap *idmap) | 
|  | 344 | { | 
|  | 345 | char id_str[NFS_UINT_MAXLEN]; | 
|  | 346 | int id_len; | 
|  | 347 | ssize_t ret; | 
|  | 348 |  | 
|  | 349 | id_len = snprintf(id_str, sizeof(id_str), "%u", id); | 
|  | 350 | ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap); | 
|  | 351 | if (ret < 0) | 
|  | 352 | return -EINVAL; | 
|  | 353 | return ret; | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | /* Name -> ID */ | 
|  | 357 | static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type, | 
|  | 358 | __u32 *id, struct idmap *idmap) | 
|  | 359 | { | 
|  | 360 | char id_str[NFS_UINT_MAXLEN]; | 
|  | 361 | long id_long; | 
|  | 362 | ssize_t data_size; | 
|  | 363 | int ret = 0; | 
|  | 364 |  | 
|  | 365 | data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap); | 
|  | 366 | if (data_size <= 0) { | 
|  | 367 | ret = -EINVAL; | 
|  | 368 | } else { | 
|  | 369 | ret = strict_strtol(id_str, 10, &id_long); | 
|  | 370 | *id = (__u32)id_long; | 
|  | 371 | } | 
|  | 372 | return ret; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | /* idmap classic begins here */ | 
|  | 376 | module_param(nfs_idmap_cache_timeout, int, 0644); | 
|  | 377 |  | 
|  | 378 | enum { | 
|  | 379 | Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err | 
|  | 380 | }; | 
|  | 381 |  | 
|  | 382 | static const match_table_t nfs_idmap_tokens = { | 
|  | 383 | { Opt_find_uid, "uid:%s" }, | 
|  | 384 | { Opt_find_gid, "gid:%s" }, | 
|  | 385 | { Opt_find_user, "user:%s" }, | 
|  | 386 | { Opt_find_group, "group:%s" }, | 
|  | 387 | { Opt_find_err, NULL } | 
|  | 388 | }; | 
|  | 389 |  | 
|  | 390 | static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *); | 
|  | 391 | static ssize_t idmap_pipe_downcall(struct file *, const char __user *, | 
|  | 392 | size_t); | 
|  | 393 | static void idmap_release_pipe(struct inode *); | 
|  | 394 | static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *); | 
|  | 395 |  | 
|  | 396 | static const struct rpc_pipe_ops idmap_upcall_ops = { | 
|  | 397 | .upcall		= rpc_pipe_generic_upcall, | 
|  | 398 | .downcall	= idmap_pipe_downcall, | 
|  | 399 | .release_pipe	= idmap_release_pipe, | 
|  | 400 | .destroy_msg	= idmap_pipe_destroy_msg, | 
|  | 401 | }; | 
|  | 402 |  | 
|  | 403 | static struct key_type key_type_id_resolver_legacy = { | 
|  | 404 | .name		= "id_legacy", | 
|  | 405 | .instantiate	= user_instantiate, | 
|  | 406 | .match		= user_match, | 
|  | 407 | .revoke		= user_revoke, | 
|  | 408 | .destroy	= user_destroy, | 
|  | 409 | .describe	= user_describe, | 
|  | 410 | .read		= user_read, | 
|  | 411 | .request_key	= nfs_idmap_legacy_upcall, | 
|  | 412 | }; | 
|  | 413 |  | 
|  | 414 | static void __nfs_idmap_unregister(struct rpc_pipe *pipe) | 
|  | 415 | { | 
|  | 416 | if (pipe->dentry) | 
|  | 417 | rpc_unlink(pipe->dentry); | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | static int __nfs_idmap_register(struct dentry *dir, | 
|  | 421 | struct idmap *idmap, | 
|  | 422 | struct rpc_pipe *pipe) | 
|  | 423 | { | 
|  | 424 | struct dentry *dentry; | 
|  | 425 |  | 
|  | 426 | dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe); | 
|  | 427 | if (IS_ERR(dentry)) | 
|  | 428 | return PTR_ERR(dentry); | 
|  | 429 | pipe->dentry = dentry; | 
|  | 430 | return 0; | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | static void nfs_idmap_unregister(struct nfs_client *clp, | 
|  | 434 | struct rpc_pipe *pipe) | 
|  | 435 | { | 
|  | 436 | struct net *net = clp->net; | 
|  | 437 | struct super_block *pipefs_sb; | 
|  | 438 |  | 
|  | 439 | pipefs_sb = rpc_get_sb_net(net); | 
|  | 440 | if (pipefs_sb) { | 
|  | 441 | __nfs_idmap_unregister(pipe); | 
|  | 442 | rpc_put_sb_net(net); | 
|  | 443 | } | 
|  | 444 | } | 
|  | 445 |  | 
|  | 446 | static int nfs_idmap_register(struct nfs_client *clp, | 
|  | 447 | struct idmap *idmap, | 
|  | 448 | struct rpc_pipe *pipe) | 
|  | 449 | { | 
|  | 450 | struct net *net = clp->net; | 
|  | 451 | struct super_block *pipefs_sb; | 
|  | 452 | int err = 0; | 
|  | 453 |  | 
|  | 454 | pipefs_sb = rpc_get_sb_net(net); | 
|  | 455 | if (pipefs_sb) { | 
|  | 456 | if (clp->cl_rpcclient->cl_dentry) | 
|  | 457 | err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry, | 
|  | 458 | idmap, pipe); | 
|  | 459 | rpc_put_sb_net(net); | 
|  | 460 | } | 
|  | 461 | return err; | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | int | 
|  | 465 | nfs_idmap_new(struct nfs_client *clp) | 
|  | 466 | { | 
|  | 467 | struct idmap *idmap; | 
|  | 468 | struct rpc_pipe *pipe; | 
|  | 469 | int error; | 
|  | 470 |  | 
|  | 471 | BUG_ON(clp->cl_idmap != NULL); | 
|  | 472 |  | 
|  | 473 | idmap = kzalloc(sizeof(*idmap), GFP_KERNEL); | 
|  | 474 | if (idmap == NULL) | 
|  | 475 | return -ENOMEM; | 
|  | 476 |  | 
|  | 477 | pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0); | 
|  | 478 | if (IS_ERR(pipe)) { | 
|  | 479 | error = PTR_ERR(pipe); | 
|  | 480 | kfree(idmap); | 
|  | 481 | return error; | 
|  | 482 | } | 
|  | 483 | error = nfs_idmap_register(clp, idmap, pipe); | 
|  | 484 | if (error) { | 
|  | 485 | rpc_destroy_pipe_data(pipe); | 
|  | 486 | kfree(idmap); | 
|  | 487 | return error; | 
|  | 488 | } | 
|  | 489 | idmap->idmap_pipe = pipe; | 
|  | 490 | mutex_init(&idmap->idmap_mutex); | 
|  | 491 |  | 
|  | 492 | clp->cl_idmap = idmap; | 
|  | 493 | return 0; | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | void | 
|  | 497 | nfs_idmap_delete(struct nfs_client *clp) | 
|  | 498 | { | 
|  | 499 | struct idmap *idmap = clp->cl_idmap; | 
|  | 500 |  | 
|  | 501 | if (!idmap) | 
|  | 502 | return; | 
|  | 503 | nfs_idmap_unregister(clp, idmap->idmap_pipe); | 
|  | 504 | rpc_destroy_pipe_data(idmap->idmap_pipe); | 
|  | 505 | clp->cl_idmap = NULL; | 
|  | 506 | kfree(idmap); | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event, | 
|  | 510 | struct super_block *sb) | 
|  | 511 | { | 
|  | 512 | int err = 0; | 
|  | 513 |  | 
|  | 514 | switch (event) { | 
|  | 515 | case RPC_PIPEFS_MOUNT: | 
|  | 516 | BUG_ON(clp->cl_rpcclient->cl_dentry == NULL); | 
|  | 517 | err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry, | 
|  | 518 | clp->cl_idmap, | 
|  | 519 | clp->cl_idmap->idmap_pipe); | 
|  | 520 | break; | 
|  | 521 | case RPC_PIPEFS_UMOUNT: | 
|  | 522 | if (clp->cl_idmap->idmap_pipe) { | 
|  | 523 | struct dentry *parent; | 
|  | 524 |  | 
|  | 525 | parent = clp->cl_idmap->idmap_pipe->dentry->d_parent; | 
|  | 526 | __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe); | 
|  | 527 | /* | 
|  | 528 | * Note: This is a dirty hack. SUNRPC hook has been | 
|  | 529 | * called already but simple_rmdir() call for the | 
|  | 530 | * directory returned with error because of idmap pipe | 
|  | 531 | * inside. Thus now we have to remove this directory | 
|  | 532 | * here. | 
|  | 533 | */ | 
|  | 534 | if (rpc_rmdir(parent)) | 
|  | 535 | printk(KERN_ERR "NFS: %s: failed to remove " | 
|  | 536 | "clnt dir!\n", __func__); | 
|  | 537 | } | 
|  | 538 | break; | 
|  | 539 | default: | 
|  | 540 | printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__, | 
|  | 541 | event); | 
|  | 542 | return -ENOTSUPP; | 
|  | 543 | } | 
|  | 544 | return err; | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | static struct nfs_client *nfs_get_client_for_event(struct net *net, int event) | 
|  | 548 | { | 
|  | 549 | struct nfs_net *nn = net_generic(net, nfs_net_id); | 
|  | 550 | struct dentry *cl_dentry; | 
|  | 551 | struct nfs_client *clp; | 
|  | 552 |  | 
|  | 553 | spin_lock(&nn->nfs_client_lock); | 
|  | 554 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { | 
|  | 555 | if (clp->rpc_ops != &nfs_v4_clientops) | 
|  | 556 | continue; | 
|  | 557 | cl_dentry = clp->cl_idmap->idmap_pipe->dentry; | 
|  | 558 | if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) || | 
|  | 559 | ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry)) | 
|  | 560 | continue; | 
|  | 561 | atomic_inc(&clp->cl_count); | 
|  | 562 | spin_unlock(&nn->nfs_client_lock); | 
|  | 563 | return clp; | 
|  | 564 | } | 
|  | 565 | spin_unlock(&nn->nfs_client_lock); | 
|  | 566 | return NULL; | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, | 
|  | 570 | void *ptr) | 
|  | 571 | { | 
|  | 572 | struct super_block *sb = ptr; | 
|  | 573 | struct nfs_client *clp; | 
|  | 574 | int error = 0; | 
|  | 575 |  | 
|  | 576 | if (!try_module_get(THIS_MODULE)) | 
|  | 577 | return 0; | 
|  | 578 |  | 
|  | 579 | while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) { | 
|  | 580 | error = __rpc_pipefs_event(clp, event, sb); | 
|  | 581 | nfs_put_client(clp); | 
|  | 582 | if (error) | 
|  | 583 | break; | 
|  | 584 | } | 
|  | 585 | module_put(THIS_MODULE); | 
|  | 586 | return error; | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | #define PIPEFS_NFS_PRIO		1 | 
|  | 590 |  | 
|  | 591 | static struct notifier_block nfs_idmap_block = { | 
|  | 592 | .notifier_call	= rpc_pipefs_event, | 
|  | 593 | .priority	= SUNRPC_PIPEFS_NFS_PRIO, | 
|  | 594 | }; | 
|  | 595 |  | 
|  | 596 | int nfs_idmap_init(void) | 
|  | 597 | { | 
|  | 598 | int ret; | 
|  | 599 | ret = nfs_idmap_init_keyring(); | 
|  | 600 | if (ret != 0) | 
|  | 601 | goto out; | 
|  | 602 | ret = rpc_pipefs_notifier_register(&nfs_idmap_block); | 
|  | 603 | if (ret != 0) | 
|  | 604 | nfs_idmap_quit_keyring(); | 
|  | 605 | out: | 
|  | 606 | return ret; | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | void nfs_idmap_quit(void) | 
|  | 610 | { | 
|  | 611 | rpc_pipefs_notifier_unregister(&nfs_idmap_block); | 
|  | 612 | nfs_idmap_quit_keyring(); | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap, | 
|  | 616 | struct idmap_msg *im, | 
|  | 617 | struct rpc_pipe_msg *msg) | 
|  | 618 | { | 
|  | 619 | substring_t substr; | 
|  | 620 | int token, ret; | 
|  | 621 |  | 
|  | 622 | memset(im,  0, sizeof(*im)); | 
|  | 623 | memset(msg, 0, sizeof(*msg)); | 
|  | 624 |  | 
|  | 625 | im->im_type = IDMAP_TYPE_GROUP; | 
|  | 626 | token = match_token(desc, nfs_idmap_tokens, &substr); | 
|  | 627 |  | 
|  | 628 | switch (token) { | 
|  | 629 | case Opt_find_uid: | 
|  | 630 | im->im_type = IDMAP_TYPE_USER; | 
|  | 631 | case Opt_find_gid: | 
|  | 632 | im->im_conv = IDMAP_CONV_NAMETOID; | 
|  | 633 | ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ); | 
|  | 634 | break; | 
|  | 635 |  | 
|  | 636 | case Opt_find_user: | 
|  | 637 | im->im_type = IDMAP_TYPE_USER; | 
|  | 638 | case Opt_find_group: | 
|  | 639 | im->im_conv = IDMAP_CONV_IDTONAME; | 
|  | 640 | ret = match_int(&substr, &im->im_id); | 
|  | 641 | break; | 
|  | 642 |  | 
|  | 643 | default: | 
|  | 644 | ret = -EINVAL; | 
|  | 645 | goto out; | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | msg->data = im; | 
|  | 649 | msg->len  = sizeof(struct idmap_msg); | 
|  | 650 |  | 
|  | 651 | out: | 
|  | 652 | return ret; | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | static int nfs_idmap_legacy_upcall(struct key_construction *cons, | 
|  | 656 | const char *op, | 
|  | 657 | void *aux) | 
|  | 658 | { | 
|  | 659 | struct idmap_legacy_upcalldata *data; | 
|  | 660 | struct rpc_pipe_msg *msg; | 
|  | 661 | struct idmap_msg *im; | 
|  | 662 | struct idmap *idmap = (struct idmap *)aux; | 
|  | 663 | struct key *key = cons->key; | 
|  | 664 | int ret = -ENOMEM; | 
|  | 665 |  | 
|  | 666 | /* msg and im are freed in idmap_pipe_destroy_msg */ | 
|  | 667 | data = kmalloc(sizeof(*data), GFP_KERNEL); | 
|  | 668 | if (!data) | 
|  | 669 | goto out1; | 
|  | 670 |  | 
|  | 671 | msg = &data->pipe_msg; | 
|  | 672 | im = &data->idmap_msg; | 
|  | 673 | data->idmap = idmap; | 
|  | 674 |  | 
|  | 675 | ret = nfs_idmap_prepare_message(key->description, idmap, im, msg); | 
|  | 676 | if (ret < 0) | 
|  | 677 | goto out2; | 
|  | 678 |  | 
|  | 679 | BUG_ON(idmap->idmap_key_cons != NULL); | 
|  | 680 | idmap->idmap_key_cons = cons; | 
|  | 681 |  | 
|  | 682 | ret = rpc_queue_upcall(idmap->idmap_pipe, msg); | 
|  | 683 | if (ret < 0) | 
|  | 684 | goto out3; | 
|  | 685 |  | 
|  | 686 | return ret; | 
|  | 687 |  | 
|  | 688 | out3: | 
|  | 689 | idmap->idmap_key_cons = NULL; | 
|  | 690 | out2: | 
|  | 691 | kfree(data); | 
|  | 692 | out1: | 
|  | 693 | complete_request_key(cons, ret); | 
|  | 694 | return ret; | 
|  | 695 | } | 
|  | 696 |  | 
|  | 697 | static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data) | 
|  | 698 | { | 
|  | 699 | return key_instantiate_and_link(key, data, strlen(data) + 1, | 
|  | 700 | id_resolver_cache->thread_keyring, | 
|  | 701 | authkey); | 
|  | 702 | } | 
|  | 703 |  | 
|  | 704 | static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey) | 
|  | 705 | { | 
|  | 706 | char id_str[NFS_UINT_MAXLEN]; | 
|  | 707 | int ret = -EINVAL; | 
|  | 708 |  | 
|  | 709 | switch (im->im_conv) { | 
|  | 710 | case IDMAP_CONV_NAMETOID: | 
|  | 711 | sprintf(id_str, "%d", im->im_id); | 
|  | 712 | ret = nfs_idmap_instantiate(key, authkey, id_str); | 
|  | 713 | break; | 
|  | 714 | case IDMAP_CONV_IDTONAME: | 
|  | 715 | ret = nfs_idmap_instantiate(key, authkey, im->im_name); | 
|  | 716 | break; | 
|  | 717 | } | 
|  | 718 |  | 
|  | 719 | return ret; | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | static ssize_t | 
|  | 723 | idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) | 
|  | 724 | { | 
|  | 725 | struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode); | 
|  | 726 | struct idmap *idmap = (struct idmap *)rpci->private; | 
|  | 727 | struct key_construction *cons; | 
|  | 728 | struct idmap_msg im; | 
|  | 729 | size_t namelen_in; | 
|  | 730 | int ret; | 
|  | 731 |  | 
|  | 732 | /* If instantiation is successful, anyone waiting for key construction | 
|  | 733 | * will have been woken up and someone else may now have used | 
|  | 734 | * idmap_key_cons - so after this point we may no longer touch it. | 
|  | 735 | */ | 
|  | 736 | cons = ACCESS_ONCE(idmap->idmap_key_cons); | 
|  | 737 | idmap->idmap_key_cons = NULL; | 
|  | 738 |  | 
|  | 739 | if (mlen != sizeof(im)) { | 
|  | 740 | ret = -ENOSPC; | 
|  | 741 | goto out; | 
|  | 742 | } | 
|  | 743 |  | 
|  | 744 | if (copy_from_user(&im, src, mlen) != 0) { | 
|  | 745 | ret = -EFAULT; | 
|  | 746 | goto out; | 
|  | 747 | } | 
|  | 748 |  | 
|  | 749 | if (!(im.im_status & IDMAP_STATUS_SUCCESS)) { | 
|  | 750 | ret = -ENOKEY; | 
|  | 751 | goto out; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | namelen_in = strnlen(im.im_name, IDMAP_NAMESZ); | 
|  | 755 | if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) { | 
|  | 756 | ret = -EINVAL; | 
|  | 757 | goto out; | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | ret = nfs_idmap_read_message(&im, cons->key, cons->authkey); | 
|  | 761 | if (ret >= 0) { | 
|  | 762 | key_set_timeout(cons->key, nfs_idmap_cache_timeout); | 
|  | 763 | ret = mlen; | 
|  | 764 | } | 
|  | 765 |  | 
|  | 766 | out: | 
|  | 767 | complete_request_key(cons, ret); | 
|  | 768 | return ret; | 
|  | 769 | } | 
|  | 770 |  | 
|  | 771 | static void | 
|  | 772 | idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg) | 
|  | 773 | { | 
|  | 774 | struct idmap_legacy_upcalldata *data = container_of(msg, | 
|  | 775 | struct idmap_legacy_upcalldata, | 
|  | 776 | pipe_msg); | 
|  | 777 | struct idmap *idmap = data->idmap; | 
|  | 778 | struct key_construction *cons; | 
|  | 779 | if (msg->errno) { | 
|  | 780 | cons = ACCESS_ONCE(idmap->idmap_key_cons); | 
|  | 781 | idmap->idmap_key_cons = NULL; | 
|  | 782 | complete_request_key(cons, msg->errno); | 
|  | 783 | } | 
|  | 784 | /* Free memory allocated in nfs_idmap_legacy_upcall() */ | 
|  | 785 | kfree(data); | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | static void | 
|  | 789 | idmap_release_pipe(struct inode *inode) | 
|  | 790 | { | 
|  | 791 | struct rpc_inode *rpci = RPC_I(inode); | 
|  | 792 | struct idmap *idmap = (struct idmap *)rpci->private; | 
|  | 793 | idmap->idmap_key_cons = NULL; | 
|  | 794 | } | 
|  | 795 |  | 
|  | 796 | int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid) | 
|  | 797 | { | 
|  | 798 | struct idmap *idmap = server->nfs_client->cl_idmap; | 
|  | 799 |  | 
|  | 800 | if (nfs_map_string_to_numeric(name, namelen, uid)) | 
|  | 801 | return 0; | 
|  | 802 | return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap); | 
|  | 803 | } | 
|  | 804 |  | 
|  | 805 | int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid) | 
|  | 806 | { | 
|  | 807 | struct idmap *idmap = server->nfs_client->cl_idmap; | 
|  | 808 |  | 
|  | 809 | if (nfs_map_string_to_numeric(name, namelen, gid)) | 
|  | 810 | return 0; | 
|  | 811 | return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap); | 
|  | 812 | } | 
|  | 813 |  | 
|  | 814 | int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen) | 
|  | 815 | { | 
|  | 816 | struct idmap *idmap = server->nfs_client->cl_idmap; | 
|  | 817 | int ret = -EINVAL; | 
|  | 818 |  | 
|  | 819 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) | 
|  | 820 | ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap); | 
|  | 821 | if (ret < 0) | 
|  | 822 | ret = nfs_map_numeric_to_string(uid, buf, buflen); | 
|  | 823 | return ret; | 
|  | 824 | } | 
|  | 825 | int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen) | 
|  | 826 | { | 
|  | 827 | struct idmap *idmap = server->nfs_client->cl_idmap; | 
|  | 828 | int ret = -EINVAL; | 
|  | 829 |  | 
|  | 830 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) | 
|  | 831 | ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap); | 
|  | 832 | if (ret < 0) | 
|  | 833 | ret = nfs_map_numeric_to_string(gid, buf, buflen); | 
|  | 834 | return ret; | 
|  | 835 | } |