nfs4idmap.c 20.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * fs/nfs/idmap.c
 *
 *  UID and GID to name mapping for clients.
 *
 *  Copyright (c) 2002 The Regents of the University of Michigan.
 *  All rights reserved.
 *
 *  Marius Aamodt Eriksen <marius@umich.edu>
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of the University nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
36
#include <linux/types.h>
37 38 39 40
#include <linux/parser.h>
#include <linux/fs.h>
#include <net/net_namespace.h>
#include <linux/sunrpc/rpc_pipe_fs.h>
41
#include <linux/nfs_fs.h>
42
#include <linux/nfs_fs_sb.h>
43
#include <linux/key.h>
44 45 46
#include <linux/keyctl.h>
#include <linux/key-type.h>
#include <keys/user-type.h>
47
#include <keys/request_key_auth-type.h>
48
#include <linux/module.h>
49

50
#include "internal.h"
51
#include "netns.h"
52
#include "nfs4idmap.h"
53
#include "nfs4trace.h"
54 55 56

#define NFS_UINT_MAXLEN 11

57 58
static const struct cred *id_resolver_cache;
static struct key_type key_type_id_resolver_legacy;
59

60 61 62
struct idmap_legacy_upcalldata {
	struct rpc_pipe_msg pipe_msg;
	struct idmap_msg idmap_msg;
63
	struct key	*authkey;
64 65 66
	struct idmap *idmap;
};

67
struct idmap {
68
	struct rpc_pipe_dir_object idmap_pdo;
69 70 71
	struct rpc_pipe		*idmap_pipe;
	struct idmap_legacy_upcalldata *idmap_upcall_data;
	struct mutex		idmap_mutex;
72
	const struct cred	*cred;
73 74
};

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static struct key_acl nfs_idmap_key_acl = {
	.usage	= REFCOUNT_INIT(1),
	.nr_ace	= 2,
	.possessor_viewable = true,
	.aces = {
		KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_SEARCH | KEY_ACE_READ),
		KEY_OWNER_ACE(KEY_ACE_VIEW),
	}
};

static struct key_acl nfs_idmap_keyring_acl = {
	.usage	= REFCOUNT_INIT(1),
	.nr_ace	= 2,
	.aces = {
		KEY_POSSESSOR_ACE(KEY_ACE_SEARCH | KEY_ACE_WRITE),
		KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
	}
};

94 95 96 97 98 99 100
static struct user_namespace *idmap_userns(const struct idmap *idmap)
{
	if (idmap && idmap->cred)
		return idmap->cred->user_ns;
	return &init_user_ns;
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/**
 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
 * @fattr: fully initialised struct nfs_fattr
 * @owner_name: owner name string cache
 * @group_name: group name string cache
 */
void nfs_fattr_init_names(struct nfs_fattr *fattr,
		struct nfs4_string *owner_name,
		struct nfs4_string *group_name)
{
	fattr->owner_name = owner_name;
	fattr->group_name = group_name;
}

static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
{
	fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
	kfree(fattr->owner_name->data);
}

static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
{
	fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
	kfree(fattr->group_name->data);
}

static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
{
	struct nfs4_string *owner = fattr->owner_name;
130
	kuid_t uid;
131 132 133 134 135 136 137 138 139 140 141 142 143

	if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
		return false;
	if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
		fattr->uid = uid;
		fattr->valid |= NFS_ATTR_FATTR_OWNER;
	}
	return true;
}

static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
{
	struct nfs4_string *group = fattr->group_name;
144
	kgid_t gid;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

	if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
		return false;
	if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
		fattr->gid = gid;
		fattr->valid |= NFS_ATTR_FATTR_GROUP;
	}
	return true;
}

/**
 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
 * @fattr: a fully initialised nfs_fattr structure
 */
void nfs_fattr_free_names(struct nfs_fattr *fattr)
{
	if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
		nfs_fattr_free_owner_name(fattr);
	if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
		nfs_fattr_free_group_name(fattr);
}

/**
 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
 * @server: pointer to the filesystem nfs_server structure
 * @fattr: a fully initialised nfs_fattr structure
 *
 * This helper maps the cached NFSv4 owner/group strings in fattr into
 * their numeric uid/gid equivalents, and then frees the cached strings.
 */
void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
{
	if (nfs_fattr_map_owner_name(server, fattr))
		nfs_fattr_free_owner_name(fattr);
	if (nfs_fattr_map_group_name(server, fattr))
		nfs_fattr_free_group_name(fattr);
}
182

183
int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
184 185 186 187 188 189 190 191
{
	unsigned long val;
	char buf[16];

	if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
		return 0;
	memcpy(buf, name, namelen);
	buf[namelen] = '\0';
192
	if (kstrtoul(buf, 0, &val) != 0)
193 194 195 196
		return 0;
	*res = val;
	return 1;
}
197
EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
L
Linus Torvalds 已提交
198

199 200 201 202 203
static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
{
	return snprintf(buf, buflen, "%u", id);
}

204
static struct key_type key_type_id_resolver = {
B
Bryan Schumaker 已提交
205
	.name		= "id_resolver",
D
David Howells 已提交
206 207 208
	.preparse	= user_preparse,
	.free_preparse	= user_free_preparse,
	.instantiate	= generic_key_instantiate,
B
Bryan Schumaker 已提交
209 210 211 212 213 214
	.revoke		= user_revoke,
	.destroy	= user_destroy,
	.describe	= user_describe,
	.read		= user_read,
};

215
int nfs_idmap_init(void)
B
Bryan Schumaker 已提交
216 217 218 219 220
{
	struct cred *cred;
	struct key *keyring;
	int ret = 0;

221 222
	printk(KERN_NOTICE "NFS: Registering the %s key type\n",
		key_type_id_resolver.name);
B
Bryan Schumaker 已提交
223 224 225 226 227

	cred = prepare_kernel_cred(NULL);
	if (!cred)
		return -ENOMEM;

228 229
	keyring = keyring_alloc(".id_resolver",
				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
230
				&nfs_idmap_keyring_acl,
231
				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
B
Bryan Schumaker 已提交
232 233 234 235 236 237 238 239 240
	if (IS_ERR(keyring)) {
		ret = PTR_ERR(keyring);
		goto failed_put_cred;
	}

	ret = register_key_type(&key_type_id_resolver);
	if (ret < 0)
		goto failed_put_key;

241 242 243 244
	ret = register_key_type(&key_type_id_resolver_legacy);
	if (ret < 0)
		goto failed_reg_legacy;

245
	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
B
Bryan Schumaker 已提交
246 247 248 249 250
	cred->thread_keyring = keyring;
	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
	id_resolver_cache = cred;
	return 0;

251 252
failed_reg_legacy:
	unregister_key_type(&key_type_id_resolver);
B
Bryan Schumaker 已提交
253 254 255 256 257 258 259
failed_put_key:
	key_put(keyring);
failed_put_cred:
	put_cred(cred);
	return ret;
}

260
void nfs_idmap_quit(void)
B
Bryan Schumaker 已提交
261 262 263
{
	key_revoke(id_resolver_cache->thread_keyring);
	unregister_key_type(&key_type_id_resolver);
264
	unregister_key_type(&key_type_id_resolver_legacy);
B
Bryan Schumaker 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	put_cred(id_resolver_cache);
}

/*
 * Assemble the description to pass to request_key()
 * This function will allocate a new string and update dest to point
 * at it.  The caller is responsible for freeing dest.
 *
 * On error 0 is returned.  Otherwise, the length of dest is returned.
 */
static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
				const char *type, size_t typelen, char **desc)
{
	char *cp;
	size_t desclen = typelen + namelen + 2;

	*desc = kmalloc(desclen, GFP_KERNEL);
D
Dan Carpenter 已提交
282
	if (!*desc)
B
Bryan Schumaker 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
		return -ENOMEM;

	cp = *desc;
	memcpy(cp, type, typelen);
	cp += typelen;
	*cp++ = ':';

	memcpy(cp, name, namelen);
	cp += namelen;
	*cp = '\0';
	return desclen;
}

296 297
static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
					 const char *type, struct idmap *idmap)
B
Bryan Schumaker 已提交
298 299
{
	char *desc;
300
	struct key *rkey = ERR_PTR(-EAGAIN);
B
Bryan Schumaker 已提交
301 302 303
	ssize_t ret;

	ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
304
	if (ret < 0)
305 306
		return ERR_PTR(ret);

307
	if (!idmap->cred || idmap->cred->user_ns == &init_user_ns)
308 309
		rkey = request_key(&key_type_id_resolver, desc, "",
				   &nfs_idmap_key_acl);
310 311 312
	if (IS_ERR(rkey)) {
		mutex_lock(&idmap->idmap_mutex);
		rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
313 314
						desc, NULL, "", 0, idmap,
						&nfs_idmap_key_acl);
315 316
		mutex_unlock(&idmap->idmap_mutex);
	}
317 318
	if (!IS_ERR(rkey))
		set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
319 320 321 322 323 324 325 326 327 328 329

	kfree(desc);
	return rkey;
}

static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
				 const char *type, void *data,
				 size_t data_size, struct idmap *idmap)
{
	const struct cred *saved_cred;
	struct key *rkey;
330
	const struct user_key_payload *payload;
331
	ssize_t ret;
B
Bryan Schumaker 已提交
332 333

	saved_cred = override_creds(id_resolver_cache);
334
	rkey = nfs_idmap_request_key(name, namelen, type, idmap);
B
Bryan Schumaker 已提交
335
	revert_creds(saved_cred);
336

B
Bryan Schumaker 已提交
337 338 339 340 341 342 343 344 345 346
	if (IS_ERR(rkey)) {
		ret = PTR_ERR(rkey);
		goto out;
	}

	rcu_read_lock();
	ret = key_validate(rkey);
	if (ret < 0)
		goto out_up;

347
	payload = user_key_payload_rcu(rkey);
B
Bryan Schumaker 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	if (IS_ERR_OR_NULL(payload)) {
		ret = PTR_ERR(payload);
		goto out_up;
	}

	ret = payload->datalen;
	if (ret > 0 && ret <= data_size)
		memcpy(data, payload->data, ret);
	else
		ret = -EINVAL;

out_up:
	rcu_read_unlock();
	key_put(rkey);
out:
	return ret;
}

/* ID -> Name */
367 368
static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
				     size_t buflen, struct idmap *idmap)
B
Bryan Schumaker 已提交
369 370 371 372 373
{
	char id_str[NFS_UINT_MAXLEN];
	int id_len;
	ssize_t ret;

374
	id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
375
	ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
B
Bryan Schumaker 已提交
376 377 378 379 380 381
	if (ret < 0)
		return -EINVAL;
	return ret;
}

/* Name -> ID */
382 383
static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
			       __u32 *id, struct idmap *idmap)
B
Bryan Schumaker 已提交
384 385 386 387 388 389
{
	char id_str[NFS_UINT_MAXLEN];
	long id_long;
	ssize_t data_size;
	int ret = 0;

390
	data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
B
Bryan Schumaker 已提交
391 392 393
	if (data_size <= 0) {
		ret = -EINVAL;
	} else {
394
		ret = kstrtol(id_str, 10, &id_long);
395 396
		if (!ret)
			*id = (__u32)id_long;
B
Bryan Schumaker 已提交
397 398 399 400
	}
	return ret;
}

401
/* idmap classic begins here */
402

403 404
enum {
	Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
L
Linus Torvalds 已提交
405 406
};

407 408 409 410 411 412
static const match_table_t nfs_idmap_tokens = {
	{ Opt_find_uid, "uid:%s" },
	{ Opt_find_gid, "gid:%s" },
	{ Opt_find_user, "user:%s" },
	{ Opt_find_group, "group:%s" },
	{ Opt_find_err, NULL }
L
Linus Torvalds 已提交
413 414
};

415
static int nfs_idmap_legacy_upcall(struct key *, void *);
C
Chuck Lever 已提交
416 417
static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
				   size_t);
418
static void idmap_release_pipe(struct inode *);
C
Chuck Lever 已提交
419
static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
L
Linus Torvalds 已提交
420

421
static const struct rpc_pipe_ops idmap_upcall_ops = {
422
	.upcall		= rpc_pipe_generic_upcall,
C
Chuck Lever 已提交
423
	.downcall	= idmap_pipe_downcall,
424
	.release_pipe	= idmap_release_pipe,
C
Chuck Lever 已提交
425
	.destroy_msg	= idmap_pipe_destroy_msg,
L
Linus Torvalds 已提交
426 427
};

428
static struct key_type key_type_id_resolver_legacy = {
429
	.name		= "id_legacy",
D
David Howells 已提交
430 431 432
	.preparse	= user_preparse,
	.free_preparse	= user_free_preparse,
	.instantiate	= generic_key_instantiate,
433 434 435 436 437 438 439
	.revoke		= user_revoke,
	.destroy	= user_destroy,
	.describe	= user_describe,
	.read		= user_read,
	.request_key	= nfs_idmap_legacy_upcall,
};

440 441
static void nfs_idmap_pipe_destroy(struct dentry *dir,
		struct rpc_pipe_dir_object *pdo)
442
{
443 444 445
	struct idmap *idmap = pdo->pdo_data;
	struct rpc_pipe *pipe = idmap->idmap_pipe;

446
	if (pipe->dentry) {
447
		rpc_unlink(pipe->dentry);
448 449
		pipe->dentry = NULL;
	}
450 451
}

452 453
static int nfs_idmap_pipe_create(struct dentry *dir,
		struct rpc_pipe_dir_object *pdo)
454
{
455 456
	struct idmap *idmap = pdo->pdo_data;
	struct rpc_pipe *pipe = idmap->idmap_pipe;
457 458 459 460 461 462 463 464 465
	struct dentry *dentry;

	dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
	pipe->dentry = dentry;
	return 0;
}

466 467 468 469
static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
	.create = nfs_idmap_pipe_create,
	.destroy = nfs_idmap_pipe_destroy,
};
470

471
int
472
nfs_idmap_new(struct nfs_client *clp)
L
Linus Torvalds 已提交
473 474
{
	struct idmap *idmap;
475
	struct rpc_pipe *pipe;
476
	int error;
L
Linus Torvalds 已提交
477

C
Chuck Lever 已提交
478 479 480
	idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
	if (idmap == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
481

482 483 484
	mutex_init(&idmap->idmap_mutex);
	idmap->cred = get_cred(clp->cl_rpcclient->cl_cred);

485 486 487 488
	rpc_init_pipe_dir_object(&idmap->idmap_pdo,
			&nfs_idmap_pipe_dir_object_ops,
			idmap);

489 490 491
	pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
	if (IS_ERR(pipe)) {
		error = PTR_ERR(pipe);
492
		goto err;
493 494
	}
	idmap->idmap_pipe = pipe;
L
Linus Torvalds 已提交
495

496 497 498 499 500 501
	error = rpc_add_pipe_dir_object(clp->cl_net,
			&clp->cl_rpcclient->cl_pipedir_objects,
			&idmap->idmap_pdo);
	if (error)
		goto err_destroy_pipe;

L
Linus Torvalds 已提交
502
	clp->cl_idmap = idmap;
503
	return 0;
504 505 506
err_destroy_pipe:
	rpc_destroy_pipe_data(idmap->idmap_pipe);
err:
507
	put_cred(idmap->cred);
508 509
	kfree(idmap);
	return error;
L
Linus Torvalds 已提交
510 511 512
}

void
513
nfs_idmap_delete(struct nfs_client *clp)
L
Linus Torvalds 已提交
514 515 516 517 518 519
{
	struct idmap *idmap = clp->cl_idmap;

	if (!idmap)
		return;
	clp->cl_idmap = NULL;
520 521 522 523
	rpc_remove_pipe_dir_object(clp->cl_net,
			&clp->cl_rpcclient->cl_pipedir_objects,
			&idmap->idmap_pdo);
	rpc_destroy_pipe_data(idmap->idmap_pipe);
524
	put_cred(idmap->cred);
L
Linus Torvalds 已提交
525 526 527
	kfree(idmap);
}

528 529
static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
				     struct idmap_msg *im,
530
				     struct rpc_pipe_msg *msg)
L
Linus Torvalds 已提交
531
{
532 533
	substring_t substr;
	int token, ret;
L
Linus Torvalds 已提交
534

535 536
	im->im_type = IDMAP_TYPE_GROUP;
	token = match_token(desc, nfs_idmap_tokens, &substr);
L
Linus Torvalds 已提交
537

538 539 540
	switch (token) {
	case Opt_find_uid:
		im->im_type = IDMAP_TYPE_USER;
541
		/* Fall through */
542 543 544 545
	case Opt_find_gid:
		im->im_conv = IDMAP_CONV_NAMETOID;
		ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
		break;
L
Linus Torvalds 已提交
546

547 548
	case Opt_find_user:
		im->im_type = IDMAP_TYPE_USER;
549
		/* Fall through */
550 551 552
	case Opt_find_group:
		im->im_conv = IDMAP_CONV_IDTONAME;
		ret = match_int(&substr, &im->im_id);
553 554
		if (ret)
			goto out;
555
		break;
L
Linus Torvalds 已提交
556

557 558
	default:
		ret = -EINVAL;
L
Linus Torvalds 已提交
559 560 561
		goto out;
	}

562 563
	msg->data = im;
	msg->len  = sizeof(struct idmap_msg);
L
Linus Torvalds 已提交
564

565
out:
C
Chuck Lever 已提交
566
	return ret;
L
Linus Torvalds 已提交
567 568
}

569 570
static bool
nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
571
		struct idmap_legacy_upcalldata *data)
572
{
573
	if (idmap->idmap_upcall_data != NULL) {
574 575 576
		WARN_ON_ONCE(1);
		return false;
	}
577
	idmap->idmap_upcall_data = data;
578 579 580 581 582 583
	return true;
}

static void
nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
{
584
	struct key *authkey = idmap->idmap_upcall_data->authkey;
585

586 587
	kfree(idmap->idmap_upcall_data);
	idmap->idmap_upcall_data = NULL;
588 589
	complete_request_key(authkey, ret);
	key_put(authkey);
590 591 592 593 594
}

static void
nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
{
595
	if (idmap->idmap_upcall_data != NULL)
596 597 598
		nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
}

599
static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
L
Linus Torvalds 已提交
600
{
601
	struct idmap_legacy_upcalldata *data;
602
	struct request_key_auth *rka = get_request_key_auth(authkey);
603
	struct rpc_pipe_msg *msg;
L
Linus Torvalds 已提交
604
	struct idmap_msg *im;
605
	struct idmap *idmap = (struct idmap *)aux;
606
	struct key *key = rka->target_key;
607 608 609 610
	int ret = -ENOKEY;

	if (!aux)
		goto out1;
L
Linus Torvalds 已提交
611

612
	/* msg and im are freed in idmap_pipe_destroy_msg */
613
	ret = -ENOMEM;
614
	data = kzalloc(sizeof(*data), GFP_KERNEL);
615
	if (!data)
616
		goto out1;
L
Linus Torvalds 已提交
617

618 619 620
	msg = &data->pipe_msg;
	im = &data->idmap_msg;
	data->idmap = idmap;
621
	data->authkey = key_get(authkey);
622 623

	ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
624 625
	if (ret < 0)
		goto out2;
L
Linus Torvalds 已提交
626

627
	ret = -EAGAIN;
628
	if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
629
		goto out2;
L
Linus Torvalds 已提交
630

631 632
	ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
	if (ret < 0)
633
		nfs_idmap_abort_pipe_upcall(idmap, ret);
L
Linus Torvalds 已提交
634

635
	return ret;
636
out2:
637
	kfree(data);
638
out1:
639
	complete_request_key(authkey, ret);
C
Chuck Lever 已提交
640
	return ret;
L
Linus Torvalds 已提交
641 642
}

643
static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
L
Linus Torvalds 已提交
644
{
645
	return key_instantiate_and_link(key, data, datalen,
646 647 648
					id_resolver_cache->thread_keyring,
					authkey);
}
L
Linus Torvalds 已提交
649

650 651 652
static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
		struct idmap_msg *upcall,
		struct key *key, struct key *authkey)
653 654
{
	char id_str[NFS_UINT_MAXLEN];
655
	size_t len;
656
	int ret = -ENOKEY;
L
Linus Torvalds 已提交
657

658 659 660
	/* ret = -ENOKEY */
	if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
		goto out;
661 662
	switch (im->im_conv) {
	case IDMAP_CONV_NAMETOID:
663 664
		if (strcmp(upcall->im_name, im->im_name) != 0)
			break;
665
		/* Note: here we store the NUL terminator too */
666 667
		len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
						    sizeof(id_str));
668
		ret = nfs_idmap_instantiate(key, authkey, id_str, len);
669 670
		break;
	case IDMAP_CONV_IDTONAME:
671 672
		if (upcall->im_id != im->im_id)
			break;
673 674
		len = strlen(im->im_name);
		ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
675
		break;
676 677
	default:
		ret = -EINVAL;
L
Linus Torvalds 已提交
678
	}
679
out:
L
Linus Torvalds 已提交
680 681 682 683 684 685
	return ret;
}

static ssize_t
idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
{
686
	struct request_key_auth *rka;
A
Al Viro 已提交
687
	struct rpc_inode *rpci = RPC_I(file_inode(filp));
L
Linus Torvalds 已提交
688
	struct idmap *idmap = (struct idmap *)rpci->private;
689
	struct key *authkey;
690
	struct idmap_msg im;
691
	size_t namelen_in;
692
	int ret = -ENOKEY;
L
Linus Torvalds 已提交
693

694 695 696 697
	/* If instantiation is successful, anyone waiting for key construction
	 * will have been woken up and someone else may now have used
	 * idmap_key_cons - so after this point we may no longer touch it.
	 */
698
	if (idmap->idmap_upcall_data == NULL)
699
		goto out_noupcall;
700

701 702
	authkey = idmap->idmap_upcall_data->authkey;
	rka = get_request_key_auth(authkey);
703

704 705
	if (mlen != sizeof(im)) {
		ret = -ENOSPC;
L
Linus Torvalds 已提交
706 707 708
		goto out;
	}

709 710
	if (copy_from_user(&im, src, mlen) != 0) {
		ret = -EFAULT;
L
Linus Torvalds 已提交
711
		goto out;
712
	}
L
Linus Torvalds 已提交
713

714
	if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
715 716
		ret = -ENOKEY;
		goto out;
L
Linus Torvalds 已提交
717 718
	}

719 720 721
	namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
	if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
		ret = -EINVAL;
L
Linus Torvalds 已提交
722
		goto out;
723
}
L
Linus Torvalds 已提交
724

725 726
	ret = nfs_idmap_read_and_verify_message(&im,
			&idmap->idmap_upcall_data->idmap_msg,
727
			rka->target_key, authkey);
728
	if (ret >= 0) {
729
		key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
730 731 732
		ret = mlen;
	}

L
Linus Torvalds 已提交
733
out:
734 735
	nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
out_noupcall:
L
Linus Torvalds 已提交
736 737 738
	return ret;
}

A
Adrian Bunk 已提交
739
static void
L
Linus Torvalds 已提交
740 741
idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
{
742 743 744 745
	struct idmap_legacy_upcalldata *data = container_of(msg,
			struct idmap_legacy_upcalldata,
			pipe_msg);
	struct idmap *idmap = data->idmap;
746 747 748

	if (msg->errno)
		nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
749 750 751 752 753 754 755
}

static void
idmap_release_pipe(struct inode *inode)
{
	struct rpc_inode *rpci = RPC_I(inode);
	struct idmap *idmap = (struct idmap *)rpci->private;
756 757

	nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
L
Linus Torvalds 已提交
758 759
}

760
int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
L
Linus Torvalds 已提交
761
{
762
	struct idmap *idmap = server->nfs_client->cl_idmap;
763 764
	__u32 id = -1;
	int ret = 0;
L
Linus Torvalds 已提交
765

766 767 768
	if (!nfs_map_string_to_numeric(name, namelen, &id))
		ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
	if (ret == 0) {
769
		*uid = make_kuid(idmap_userns(idmap), id);
770 771 772
		if (!uid_valid(*uid))
			ret = -ERANGE;
	}
773
	trace_nfs4_map_name_to_uid(name, namelen, id, ret);
774
	return ret;
L
Linus Torvalds 已提交
775 776
}

777
int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
L
Linus Torvalds 已提交
778
{
779
	struct idmap *idmap = server->nfs_client->cl_idmap;
780 781
	__u32 id = -1;
	int ret = 0;
L
Linus Torvalds 已提交
782

783 784 785
	if (!nfs_map_string_to_numeric(name, namelen, &id))
		ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
	if (ret == 0) {
786
		*gid = make_kgid(idmap_userns(idmap), id);
787 788 789
		if (!gid_valid(*gid))
			ret = -ERANGE;
	}
790
	trace_nfs4_map_group_to_gid(name, namelen, id, ret);
791
	return ret;
L
Linus Torvalds 已提交
792 793
}

794
int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
L
Linus Torvalds 已提交
795
{
796
	struct idmap *idmap = server->nfs_client->cl_idmap;
797
	int ret = -EINVAL;
798
	__u32 id;
L
Linus Torvalds 已提交
799

800
	id = from_kuid_munged(idmap_userns(idmap), uid);
801
	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
802
		ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
803
	if (ret < 0)
804
		ret = nfs_map_numeric_to_string(id, buf, buflen);
805
	trace_nfs4_map_uid_to_name(buf, ret, id, ret);
806
	return ret;
L
Linus Torvalds 已提交
807
}
808
int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
L
Linus Torvalds 已提交
809
{
810
	struct idmap *idmap = server->nfs_client->cl_idmap;
811
	int ret = -EINVAL;
812
	__u32 id;
L
Linus Torvalds 已提交
813

814
	id = from_kgid_munged(idmap_userns(idmap), gid);
815
	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
816
		ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
817
	if (ret < 0)
818
		ret = nfs_map_numeric_to_string(id, buf, buflen);
819
	trace_nfs4_map_gid_to_group(buf, ret, id, ret);
820
	return ret;
L
Linus Torvalds 已提交
821
}