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 <net/net_namespace.h>
40 #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 #include <linux/keyctl.h>
45 #include <linux/key-type.h>
46 #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 #define NFS_UINT_MAXLEN 11
56 
57 static const struct cred *id_resolver_cache;
58 static struct key_type key_type_id_resolver_legacy;
59 
60 struct idmap_legacy_upcalldata {
61 	struct rpc_pipe_msg pipe_msg;
62 	struct idmap_msg idmap_msg;
63 	struct key	*authkey;
64 	struct idmap *idmap;
65 };
66 
67 struct idmap {
68 	struct rpc_pipe_dir_object idmap_pdo;
69 	struct rpc_pipe		*idmap_pipe;
70 	struct idmap_legacy_upcalldata *idmap_upcall_data;
71 	struct mutex		idmap_mutex;
72 };
73 
74 /**
75  * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
76  * @fattr: fully initialised struct nfs_fattr
77  * @owner_name: owner name string cache
78  * @group_name: group name string cache
79  */
nfs_fattr_init_names(struct nfs_fattr * fattr,struct nfs4_string * owner_name,struct nfs4_string * group_name)80 void nfs_fattr_init_names(struct nfs_fattr *fattr,
81 		struct nfs4_string *owner_name,
82 		struct nfs4_string *group_name)
83 {
84 	fattr->owner_name = owner_name;
85 	fattr->group_name = group_name;
86 }
87 
nfs_fattr_free_owner_name(struct nfs_fattr * fattr)88 static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
89 {
90 	fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
91 	kfree(fattr->owner_name->data);
92 }
93 
nfs_fattr_free_group_name(struct nfs_fattr * fattr)94 static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
95 {
96 	fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
97 	kfree(fattr->group_name->data);
98 }
99 
nfs_fattr_map_owner_name(struct nfs_server * server,struct nfs_fattr * fattr)100 static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
101 {
102 	struct nfs4_string *owner = fattr->owner_name;
103 	kuid_t uid;
104 
105 	if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
106 		return false;
107 	if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
108 		fattr->uid = uid;
109 		fattr->valid |= NFS_ATTR_FATTR_OWNER;
110 	}
111 	return true;
112 }
113 
nfs_fattr_map_group_name(struct nfs_server * server,struct nfs_fattr * fattr)114 static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
115 {
116 	struct nfs4_string *group = fattr->group_name;
117 	kgid_t gid;
118 
119 	if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
120 		return false;
121 	if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
122 		fattr->gid = gid;
123 		fattr->valid |= NFS_ATTR_FATTR_GROUP;
124 	}
125 	return true;
126 }
127 
128 /**
129  * nfs_fattr_free_names - free up the NFSv4 owner and group strings
130  * @fattr: a fully initialised nfs_fattr structure
131  */
nfs_fattr_free_names(struct nfs_fattr * fattr)132 void nfs_fattr_free_names(struct nfs_fattr *fattr)
133 {
134 	if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
135 		nfs_fattr_free_owner_name(fattr);
136 	if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
137 		nfs_fattr_free_group_name(fattr);
138 }
139 
140 /**
141  * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
142  * @server: pointer to the filesystem nfs_server structure
143  * @fattr: a fully initialised nfs_fattr structure
144  *
145  * This helper maps the cached NFSv4 owner/group strings in fattr into
146  * their numeric uid/gid equivalents, and then frees the cached strings.
147  */
nfs_fattr_map_and_free_names(struct nfs_server * server,struct nfs_fattr * fattr)148 void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
149 {
150 	if (nfs_fattr_map_owner_name(server, fattr))
151 		nfs_fattr_free_owner_name(fattr);
152 	if (nfs_fattr_map_group_name(server, fattr))
153 		nfs_fattr_free_group_name(fattr);
154 }
155 
nfs_map_string_to_numeric(const char * name,size_t namelen,__u32 * res)156 int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
157 {
158 	unsigned long val;
159 	char buf[16];
160 
161 	if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
162 		return 0;
163 	memcpy(buf, name, namelen);
164 	buf[namelen] = '\0';
165 	if (kstrtoul(buf, 0, &val) != 0)
166 		return 0;
167 	*res = val;
168 	return 1;
169 }
170 EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
171 
nfs_map_numeric_to_string(__u32 id,char * buf,size_t buflen)172 static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
173 {
174 	return snprintf(buf, buflen, "%u", id);
175 }
176 
177 static struct key_type key_type_id_resolver = {
178 	.name		= "id_resolver",
179 	.preparse	= user_preparse,
180 	.free_preparse	= user_free_preparse,
181 	.instantiate	= generic_key_instantiate,
182 	.revoke		= user_revoke,
183 	.destroy	= user_destroy,
184 	.describe	= user_describe,
185 	.read		= user_read,
186 };
187 
nfs_idmap_init(void)188 int nfs_idmap_init(void)
189 {
190 	struct cred *cred;
191 	struct key *keyring;
192 	int ret = 0;
193 
194 	printk(KERN_NOTICE "NFS: Registering the %s key type\n",
195 		key_type_id_resolver.name);
196 
197 	cred = prepare_kernel_cred(NULL);
198 	if (!cred)
199 		return -ENOMEM;
200 
201 	keyring = keyring_alloc(".id_resolver",
202 				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
203 				(KEY_POS_ALL & ~KEY_POS_SETATTR) |
204 				KEY_USR_VIEW | KEY_USR_READ,
205 				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
206 	if (IS_ERR(keyring)) {
207 		ret = PTR_ERR(keyring);
208 		goto failed_put_cred;
209 	}
210 
211 	ret = register_key_type(&key_type_id_resolver);
212 	if (ret < 0)
213 		goto failed_put_key;
214 
215 	ret = register_key_type(&key_type_id_resolver_legacy);
216 	if (ret < 0)
217 		goto failed_reg_legacy;
218 
219 	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
220 	cred->thread_keyring = keyring;
221 	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
222 	id_resolver_cache = cred;
223 	return 0;
224 
225 failed_reg_legacy:
226 	unregister_key_type(&key_type_id_resolver);
227 failed_put_key:
228 	key_put(keyring);
229 failed_put_cred:
230 	put_cred(cred);
231 	return ret;
232 }
233 
nfs_idmap_quit(void)234 void nfs_idmap_quit(void)
235 {
236 	key_revoke(id_resolver_cache->thread_keyring);
237 	unregister_key_type(&key_type_id_resolver);
238 	unregister_key_type(&key_type_id_resolver_legacy);
239 	put_cred(id_resolver_cache);
240 }
241 
242 /*
243  * Assemble the description to pass to request_key()
244  * This function will allocate a new string and update dest to point
245  * at it.  The caller is responsible for freeing dest.
246  *
247  * On error 0 is returned.  Otherwise, the length of dest is returned.
248  */
nfs_idmap_get_desc(const char * name,size_t namelen,const char * type,size_t typelen,char ** desc)249 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
250 				const char *type, size_t typelen, char **desc)
251 {
252 	char *cp;
253 	size_t desclen = typelen + namelen + 2;
254 
255 	*desc = kmalloc(desclen, GFP_KERNEL);
256 	if (!*desc)
257 		return -ENOMEM;
258 
259 	cp = *desc;
260 	memcpy(cp, type, typelen);
261 	cp += typelen;
262 	*cp++ = ':';
263 
264 	memcpy(cp, name, namelen);
265 	cp += namelen;
266 	*cp = '\0';
267 	return desclen;
268 }
269 
nfs_idmap_request_key(const char * name,size_t namelen,const char * type,struct idmap * idmap)270 static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
271 					 const char *type, struct idmap *idmap)
272 {
273 	char *desc;
274 	struct key *rkey;
275 	ssize_t ret;
276 
277 	ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
278 	if (ret < 0)
279 		return ERR_PTR(ret);
280 
281 	rkey = request_key(&key_type_id_resolver, desc, "");
282 	if (IS_ERR(rkey)) {
283 		mutex_lock(&idmap->idmap_mutex);
284 		rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
285 						desc, "", 0, idmap);
286 		mutex_unlock(&idmap->idmap_mutex);
287 	}
288 	if (!IS_ERR(rkey))
289 		set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
290 
291 	kfree(desc);
292 	return rkey;
293 }
294 
nfs_idmap_get_key(const char * name,size_t namelen,const char * type,void * data,size_t data_size,struct idmap * idmap)295 static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
296 				 const char *type, void *data,
297 				 size_t data_size, struct idmap *idmap)
298 {
299 	const struct cred *saved_cred;
300 	struct key *rkey;
301 	const struct user_key_payload *payload;
302 	ssize_t ret;
303 
304 	saved_cred = override_creds(id_resolver_cache);
305 	rkey = nfs_idmap_request_key(name, namelen, type, idmap);
306 	revert_creds(saved_cred);
307 
308 	if (IS_ERR(rkey)) {
309 		ret = PTR_ERR(rkey);
310 		goto out;
311 	}
312 
313 	rcu_read_lock();
314 	rkey->perm |= KEY_USR_VIEW;
315 
316 	ret = key_validate(rkey);
317 	if (ret < 0)
318 		goto out_up;
319 
320 	payload = user_key_payload_rcu(rkey);
321 	if (IS_ERR_OR_NULL(payload)) {
322 		ret = PTR_ERR(payload);
323 		goto out_up;
324 	}
325 
326 	ret = payload->datalen;
327 	if (ret > 0 && ret <= data_size)
328 		memcpy(data, payload->data, ret);
329 	else
330 		ret = -EINVAL;
331 
332 out_up:
333 	rcu_read_unlock();
334 	key_put(rkey);
335 out:
336 	return ret;
337 }
338 
339 /* ID -> Name */
nfs_idmap_lookup_name(__u32 id,const char * type,char * buf,size_t buflen,struct idmap * idmap)340 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
341 				     size_t buflen, struct idmap *idmap)
342 {
343 	char id_str[NFS_UINT_MAXLEN];
344 	int id_len;
345 	ssize_t ret;
346 
347 	id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
348 	ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
349 	if (ret < 0)
350 		return -EINVAL;
351 	return ret;
352 }
353 
354 /* Name -> ID */
nfs_idmap_lookup_id(const char * name,size_t namelen,const char * type,__u32 * id,struct idmap * idmap)355 static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
356 			       __u32 *id, struct idmap *idmap)
357 {
358 	char id_str[NFS_UINT_MAXLEN];
359 	long id_long;
360 	ssize_t data_size;
361 	int ret = 0;
362 
363 	data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
364 	if (data_size <= 0) {
365 		ret = -EINVAL;
366 	} else {
367 		ret = kstrtol(id_str, 10, &id_long);
368 		if (!ret)
369 			*id = (__u32)id_long;
370 	}
371 	return ret;
372 }
373 
374 /* idmap classic begins here */
375 
376 enum {
377 	Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
378 };
379 
380 static const match_table_t nfs_idmap_tokens = {
381 	{ Opt_find_uid, "uid:%s" },
382 	{ Opt_find_gid, "gid:%s" },
383 	{ Opt_find_user, "user:%s" },
384 	{ Opt_find_group, "group:%s" },
385 	{ Opt_find_err, NULL }
386 };
387 
388 static int nfs_idmap_legacy_upcall(struct key *, void *);
389 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
390 				   size_t);
391 static void idmap_release_pipe(struct inode *);
392 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
393 
394 static const struct rpc_pipe_ops idmap_upcall_ops = {
395 	.upcall		= rpc_pipe_generic_upcall,
396 	.downcall	= idmap_pipe_downcall,
397 	.release_pipe	= idmap_release_pipe,
398 	.destroy_msg	= idmap_pipe_destroy_msg,
399 };
400 
401 static struct key_type key_type_id_resolver_legacy = {
402 	.name		= "id_legacy",
403 	.preparse	= user_preparse,
404 	.free_preparse	= user_free_preparse,
405 	.instantiate	= generic_key_instantiate,
406 	.revoke		= user_revoke,
407 	.destroy	= user_destroy,
408 	.describe	= user_describe,
409 	.read		= user_read,
410 	.request_key	= nfs_idmap_legacy_upcall,
411 };
412 
nfs_idmap_pipe_destroy(struct dentry * dir,struct rpc_pipe_dir_object * pdo)413 static void nfs_idmap_pipe_destroy(struct dentry *dir,
414 		struct rpc_pipe_dir_object *pdo)
415 {
416 	struct idmap *idmap = pdo->pdo_data;
417 	struct rpc_pipe *pipe = idmap->idmap_pipe;
418 
419 	if (pipe->dentry) {
420 		rpc_unlink(pipe->dentry);
421 		pipe->dentry = NULL;
422 	}
423 }
424 
nfs_idmap_pipe_create(struct dentry * dir,struct rpc_pipe_dir_object * pdo)425 static int nfs_idmap_pipe_create(struct dentry *dir,
426 		struct rpc_pipe_dir_object *pdo)
427 {
428 	struct idmap *idmap = pdo->pdo_data;
429 	struct rpc_pipe *pipe = idmap->idmap_pipe;
430 	struct dentry *dentry;
431 
432 	dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
433 	if (IS_ERR(dentry))
434 		return PTR_ERR(dentry);
435 	pipe->dentry = dentry;
436 	return 0;
437 }
438 
439 static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
440 	.create = nfs_idmap_pipe_create,
441 	.destroy = nfs_idmap_pipe_destroy,
442 };
443 
444 int
nfs_idmap_new(struct nfs_client * clp)445 nfs_idmap_new(struct nfs_client *clp)
446 {
447 	struct idmap *idmap;
448 	struct rpc_pipe *pipe;
449 	int error;
450 
451 	idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
452 	if (idmap == NULL)
453 		return -ENOMEM;
454 
455 	rpc_init_pipe_dir_object(&idmap->idmap_pdo,
456 			&nfs_idmap_pipe_dir_object_ops,
457 			idmap);
458 
459 	pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
460 	if (IS_ERR(pipe)) {
461 		error = PTR_ERR(pipe);
462 		goto err;
463 	}
464 	idmap->idmap_pipe = pipe;
465 	mutex_init(&idmap->idmap_mutex);
466 
467 	error = rpc_add_pipe_dir_object(clp->cl_net,
468 			&clp->cl_rpcclient->cl_pipedir_objects,
469 			&idmap->idmap_pdo);
470 	if (error)
471 		goto err_destroy_pipe;
472 
473 	clp->cl_idmap = idmap;
474 	return 0;
475 err_destroy_pipe:
476 	rpc_destroy_pipe_data(idmap->idmap_pipe);
477 err:
478 	kfree(idmap);
479 	return error;
480 }
481 
482 void
nfs_idmap_delete(struct nfs_client * clp)483 nfs_idmap_delete(struct nfs_client *clp)
484 {
485 	struct idmap *idmap = clp->cl_idmap;
486 
487 	if (!idmap)
488 		return;
489 	clp->cl_idmap = NULL;
490 	rpc_remove_pipe_dir_object(clp->cl_net,
491 			&clp->cl_rpcclient->cl_pipedir_objects,
492 			&idmap->idmap_pdo);
493 	rpc_destroy_pipe_data(idmap->idmap_pipe);
494 	kfree(idmap);
495 }
496 
nfs_idmap_prepare_message(char * desc,struct idmap * idmap,struct idmap_msg * im,struct rpc_pipe_msg * msg)497 static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
498 				     struct idmap_msg *im,
499 				     struct rpc_pipe_msg *msg)
500 {
501 	substring_t substr;
502 	int token, ret;
503 
504 	im->im_type = IDMAP_TYPE_GROUP;
505 	token = match_token(desc, nfs_idmap_tokens, &substr);
506 
507 	switch (token) {
508 	case Opt_find_uid:
509 		im->im_type = IDMAP_TYPE_USER;
510 		/* Fall through */
511 	case Opt_find_gid:
512 		im->im_conv = IDMAP_CONV_NAMETOID;
513 		ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
514 		break;
515 
516 	case Opt_find_user:
517 		im->im_type = IDMAP_TYPE_USER;
518 		/* Fall through */
519 	case Opt_find_group:
520 		im->im_conv = IDMAP_CONV_IDTONAME;
521 		ret = match_int(&substr, &im->im_id);
522 		if (ret)
523 			goto out;
524 		break;
525 
526 	default:
527 		ret = -EINVAL;
528 		goto out;
529 	}
530 
531 	msg->data = im;
532 	msg->len  = sizeof(struct idmap_msg);
533 
534 out:
535 	return ret;
536 }
537 
538 static bool
nfs_idmap_prepare_pipe_upcall(struct idmap * idmap,struct idmap_legacy_upcalldata * data)539 nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
540 		struct idmap_legacy_upcalldata *data)
541 {
542 	if (idmap->idmap_upcall_data != NULL) {
543 		WARN_ON_ONCE(1);
544 		return false;
545 	}
546 	idmap->idmap_upcall_data = data;
547 	return true;
548 }
549 
nfs_idmap_complete_pipe_upcall(struct idmap_legacy_upcalldata * data,int ret)550 static void nfs_idmap_complete_pipe_upcall(struct idmap_legacy_upcalldata *data,
551 					   int ret)
552 {
553 	complete_request_key(data->authkey, ret);
554 	key_put(data->authkey);
555 	kfree(data);
556 }
557 
nfs_idmap_abort_pipe_upcall(struct idmap * idmap,struct idmap_legacy_upcalldata * data,int ret)558 static void nfs_idmap_abort_pipe_upcall(struct idmap *idmap,
559 					struct idmap_legacy_upcalldata *data,
560 					int ret)
561 {
562 	if (cmpxchg(&idmap->idmap_upcall_data, data, NULL) == data)
563 		nfs_idmap_complete_pipe_upcall(data, ret);
564 }
565 
nfs_idmap_legacy_upcall(struct key * authkey,void * aux)566 static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
567 {
568 	struct idmap_legacy_upcalldata *data;
569 	struct request_key_auth *rka = get_request_key_auth(authkey);
570 	struct rpc_pipe_msg *msg;
571 	struct idmap_msg *im;
572 	struct idmap *idmap = (struct idmap *)aux;
573 	struct key *key = rka->target_key;
574 	int ret = -ENOKEY;
575 
576 	if (!aux)
577 		goto out1;
578 
579 	/* msg and im are freed in idmap_pipe_destroy_msg */
580 	ret = -ENOMEM;
581 	data = kzalloc(sizeof(*data), GFP_KERNEL);
582 	if (!data)
583 		goto out1;
584 
585 	msg = &data->pipe_msg;
586 	im = &data->idmap_msg;
587 	data->idmap = idmap;
588 	data->authkey = key_get(authkey);
589 
590 	ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
591 	if (ret < 0)
592 		goto out2;
593 
594 	ret = -EAGAIN;
595 	if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
596 		goto out2;
597 
598 	ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
599 	if (ret < 0)
600 		nfs_idmap_abort_pipe_upcall(idmap, data, ret);
601 
602 	return ret;
603 out2:
604 	kfree(data);
605 out1:
606 	complete_request_key(authkey, ret);
607 	return ret;
608 }
609 
nfs_idmap_instantiate(struct key * key,struct key * authkey,char * data,size_t datalen)610 static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
611 {
612 	return key_instantiate_and_link(key, data, datalen,
613 					id_resolver_cache->thread_keyring,
614 					authkey);
615 }
616 
nfs_idmap_read_and_verify_message(struct idmap_msg * im,struct idmap_msg * upcall,struct key * key,struct key * authkey)617 static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
618 		struct idmap_msg *upcall,
619 		struct key *key, struct key *authkey)
620 {
621 	char id_str[NFS_UINT_MAXLEN];
622 	size_t len;
623 	int ret = -ENOKEY;
624 
625 	/* ret = -ENOKEY */
626 	if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
627 		goto out;
628 	switch (im->im_conv) {
629 	case IDMAP_CONV_NAMETOID:
630 		if (strcmp(upcall->im_name, im->im_name) != 0)
631 			break;
632 		/* Note: here we store the NUL terminator too */
633 		len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
634 						    sizeof(id_str));
635 		ret = nfs_idmap_instantiate(key, authkey, id_str, len);
636 		break;
637 	case IDMAP_CONV_IDTONAME:
638 		if (upcall->im_id != im->im_id)
639 			break;
640 		len = strlen(im->im_name);
641 		ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
642 		break;
643 	default:
644 		ret = -EINVAL;
645 	}
646 out:
647 	return ret;
648 }
649 
650 static ssize_t
idmap_pipe_downcall(struct file * filp,const char __user * src,size_t mlen)651 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
652 {
653 	struct request_key_auth *rka;
654 	struct rpc_inode *rpci = RPC_I(file_inode(filp));
655 	struct idmap *idmap = (struct idmap *)rpci->private;
656 	struct idmap_legacy_upcalldata *data;
657 	struct key *authkey;
658 	struct idmap_msg im;
659 	size_t namelen_in;
660 	int ret = -ENOKEY;
661 
662 	/* If instantiation is successful, anyone waiting for key construction
663 	 * will have been woken up and someone else may now have used
664 	 * idmap_key_cons - so after this point we may no longer touch it.
665 	 */
666 	data = xchg(&idmap->idmap_upcall_data, NULL);
667 	if (data == NULL)
668 		goto out_noupcall;
669 
670 	authkey = data->authkey;
671 	rka = get_request_key_auth(authkey);
672 
673 	if (mlen != sizeof(im)) {
674 		ret = -ENOSPC;
675 		goto out;
676 	}
677 
678 	if (copy_from_user(&im, src, mlen) != 0) {
679 		ret = -EFAULT;
680 		goto out;
681 	}
682 
683 	if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
684 		ret = -ENOKEY;
685 		goto out;
686 	}
687 
688 	namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
689 	if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
690 		ret = -EINVAL;
691 		goto out;
692 	}
693 
694 	ret = nfs_idmap_read_and_verify_message(&im, &data->idmap_msg,
695 						rka->target_key, authkey);
696 	if (ret >= 0) {
697 		key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
698 		ret = mlen;
699 	}
700 
701 out:
702 	nfs_idmap_complete_pipe_upcall(data, ret);
703 out_noupcall:
704 	return ret;
705 }
706 
707 static void
idmap_pipe_destroy_msg(struct rpc_pipe_msg * msg)708 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
709 {
710 	struct idmap_legacy_upcalldata *data = container_of(msg,
711 			struct idmap_legacy_upcalldata,
712 			pipe_msg);
713 	struct idmap *idmap = data->idmap;
714 
715 	if (msg->errno)
716 		nfs_idmap_abort_pipe_upcall(idmap, data, msg->errno);
717 }
718 
719 static void
idmap_release_pipe(struct inode * inode)720 idmap_release_pipe(struct inode *inode)
721 {
722 	struct rpc_inode *rpci = RPC_I(inode);
723 	struct idmap *idmap = (struct idmap *)rpci->private;
724 	struct idmap_legacy_upcalldata *data;
725 
726 	data = xchg(&idmap->idmap_upcall_data, NULL);
727 	if (data)
728 		nfs_idmap_complete_pipe_upcall(data, -EPIPE);
729 }
730 
nfs_map_name_to_uid(const struct nfs_server * server,const char * name,size_t namelen,kuid_t * uid)731 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
732 {
733 	struct idmap *idmap = server->nfs_client->cl_idmap;
734 	__u32 id = -1;
735 	int ret = 0;
736 
737 	if (!nfs_map_string_to_numeric(name, namelen, &id))
738 		ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
739 	if (ret == 0) {
740 		*uid = make_kuid(&init_user_ns, id);
741 		if (!uid_valid(*uid))
742 			ret = -ERANGE;
743 	}
744 	trace_nfs4_map_name_to_uid(name, namelen, id, ret);
745 	return ret;
746 }
747 
nfs_map_group_to_gid(const struct nfs_server * server,const char * name,size_t namelen,kgid_t * gid)748 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
749 {
750 	struct idmap *idmap = server->nfs_client->cl_idmap;
751 	__u32 id = -1;
752 	int ret = 0;
753 
754 	if (!nfs_map_string_to_numeric(name, namelen, &id))
755 		ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
756 	if (ret == 0) {
757 		*gid = make_kgid(&init_user_ns, id);
758 		if (!gid_valid(*gid))
759 			ret = -ERANGE;
760 	}
761 	trace_nfs4_map_group_to_gid(name, namelen, id, ret);
762 	return ret;
763 }
764 
nfs_map_uid_to_name(const struct nfs_server * server,kuid_t uid,char * buf,size_t buflen)765 int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
766 {
767 	struct idmap *idmap = server->nfs_client->cl_idmap;
768 	int ret = -EINVAL;
769 	__u32 id;
770 
771 	id = from_kuid(&init_user_ns, uid);
772 	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
773 		ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
774 	if (ret < 0)
775 		ret = nfs_map_numeric_to_string(id, buf, buflen);
776 	trace_nfs4_map_uid_to_name(buf, ret, id, ret);
777 	return ret;
778 }
nfs_map_gid_to_group(const struct nfs_server * server,kgid_t gid,char * buf,size_t buflen)779 int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
780 {
781 	struct idmap *idmap = server->nfs_client->cl_idmap;
782 	int ret = -EINVAL;
783 	__u32 id;
784 
785 	id = from_kgid(&init_user_ns, gid);
786 	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
787 		ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
788 	if (ret < 0)
789 		ret = nfs_map_numeric_to_string(id, buf, buflen);
790 	trace_nfs4_map_gid_to_group(buf, ret, id, ret);
791 	return ret;
792 }
793