4 * UID and GID to name mapping for clients.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
36 #include <linux/types.h>
37 #include <linux/parser.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>
52 #include "nfs4idmap.h"
53 #include "nfs4trace.h"
55 #define NFS_UINT_MAXLEN 11
57 static const struct cred *id_resolver_cache;
58 static struct key_type key_type_id_resolver_legacy;
60 struct idmap_legacy_upcalldata {
61 struct rpc_pipe_msg pipe_msg;
62 struct idmap_msg idmap_msg;
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;
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
80 void nfs_fattr_init_names(struct nfs_fattr *fattr,
81 struct nfs4_string *owner_name,
82 struct nfs4_string *group_name)
84 fattr->owner_name = owner_name;
85 fattr->group_name = group_name;
88 static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
90 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
91 kfree(fattr->owner_name->data);
94 static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
96 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
97 kfree(fattr->group_name->data);
100 static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
102 struct nfs4_string *owner = fattr->owner_name;
105 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
107 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
109 fattr->valid |= NFS_ATTR_FATTR_OWNER;
114 static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
116 struct nfs4_string *group = fattr->group_name;
119 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
121 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
123 fattr->valid |= NFS_ATTR_FATTR_GROUP;
129 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
130 * @fattr: a fully initialised nfs_fattr structure
132 void nfs_fattr_free_names(struct nfs_fattr *fattr)
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);
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
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.
148 void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
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);
156 int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
161 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
163 memcpy(buf, name, namelen);
165 if (kstrtoul(buf, 0, &val) != 0)
170 EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
172 static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
174 return snprintf(buf, buflen, "%u", id);
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,
188 int nfs_idmap_init(void)
194 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
195 key_type_id_resolver.name);
197 cred = prepare_kernel_cred(NULL);
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;
211 ret = register_key_type(&key_type_id_resolver);
215 ret = register_key_type(&key_type_id_resolver_legacy);
217 goto failed_reg_legacy;
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;
226 unregister_key_type(&key_type_id_resolver);
234 void nfs_idmap_quit(void)
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);
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.
247 * On error 0 is returned. Otherwise, the length of dest is returned.
249 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
250 const char *type, size_t typelen, char **desc)
253 size_t desclen = typelen + namelen + 2;
255 *desc = kmalloc(desclen, GFP_KERNEL);
260 memcpy(cp, type, typelen);
264 memcpy(cp, name, namelen);
270 static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
271 const char *type, struct idmap *idmap)
277 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
281 rkey = request_key(&key_type_id_resolver, desc, "");
283 mutex_lock(&idmap->idmap_mutex);
284 rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
286 mutex_unlock(&idmap->idmap_mutex);
289 set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
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)
299 const struct cred *saved_cred;
301 const struct user_key_payload *payload;
304 saved_cred = override_creds(id_resolver_cache);
305 rkey = nfs_idmap_request_key(name, namelen, type, idmap);
306 revert_creds(saved_cred);
314 rkey->perm |= KEY_USR_VIEW;
316 ret = key_validate(rkey);
320 payload = user_key_payload_rcu(rkey);
321 if (IS_ERR_OR_NULL(payload)) {
322 ret = PTR_ERR(payload);
326 ret = payload->datalen;
327 if (ret > 0 && ret <= data_size)
328 memcpy(data, payload->data, ret);
340 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
341 size_t buflen, struct idmap *idmap)
343 char id_str[NFS_UINT_MAXLEN];
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);
355 static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
356 __u32 *id, struct idmap *idmap)
358 char id_str[NFS_UINT_MAXLEN];
363 data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
364 if (data_size <= 0) {
367 ret = kstrtol(id_str, 10, &id_long);
369 *id = (__u32)id_long;
374 /* idmap classic begins here */
377 Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
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 }
388 static int nfs_idmap_legacy_upcall(struct key *, void *);
389 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
391 static void idmap_release_pipe(struct inode *);
392 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
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,
401 static struct key_type key_type_id_resolver_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,
410 .request_key = nfs_idmap_legacy_upcall,
413 static void nfs_idmap_pipe_destroy(struct dentry *dir,
414 struct rpc_pipe_dir_object *pdo)
416 struct idmap *idmap = pdo->pdo_data;
417 struct rpc_pipe *pipe = idmap->idmap_pipe;
420 rpc_unlink(pipe->dentry);
425 static int nfs_idmap_pipe_create(struct dentry *dir,
426 struct rpc_pipe_dir_object *pdo)
428 struct idmap *idmap = pdo->pdo_data;
429 struct rpc_pipe *pipe = idmap->idmap_pipe;
430 struct dentry *dentry;
432 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
434 return PTR_ERR(dentry);
435 pipe->dentry = dentry;
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,
445 nfs_idmap_new(struct nfs_client *clp)
448 struct rpc_pipe *pipe;
451 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
455 rpc_init_pipe_dir_object(&idmap->idmap_pdo,
456 &nfs_idmap_pipe_dir_object_ops,
459 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
461 error = PTR_ERR(pipe);
464 idmap->idmap_pipe = pipe;
465 mutex_init(&idmap->idmap_mutex);
467 error = rpc_add_pipe_dir_object(clp->cl_net,
468 &clp->cl_rpcclient->cl_pipedir_objects,
471 goto err_destroy_pipe;
473 clp->cl_idmap = idmap;
476 rpc_destroy_pipe_data(idmap->idmap_pipe);
483 nfs_idmap_delete(struct nfs_client *clp)
485 struct idmap *idmap = clp->cl_idmap;
489 clp->cl_idmap = NULL;
490 rpc_remove_pipe_dir_object(clp->cl_net,
491 &clp->cl_rpcclient->cl_pipedir_objects,
493 rpc_destroy_pipe_data(idmap->idmap_pipe);
497 static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
498 struct idmap_msg *im,
499 struct rpc_pipe_msg *msg)
504 im->im_type = IDMAP_TYPE_GROUP;
505 token = match_token(desc, nfs_idmap_tokens, &substr);
509 im->im_type = IDMAP_TYPE_USER;
512 im->im_conv = IDMAP_CONV_NAMETOID;
513 ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
517 im->im_type = IDMAP_TYPE_USER;
520 im->im_conv = IDMAP_CONV_IDTONAME;
521 ret = match_int(&substr, &im->im_id);
532 msg->len = sizeof(struct idmap_msg);
539 nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
540 struct idmap_legacy_upcalldata *data)
542 if (idmap->idmap_upcall_data != NULL) {
546 idmap->idmap_upcall_data = data;
551 nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
553 struct key *authkey = idmap->idmap_upcall_data->authkey;
555 kfree(idmap->idmap_upcall_data);
556 idmap->idmap_upcall_data = NULL;
557 complete_request_key(authkey, ret);
562 nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
564 if (idmap->idmap_upcall_data != NULL)
565 nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
568 static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
570 struct idmap_legacy_upcalldata *data;
571 struct request_key_auth *rka = get_request_key_auth(authkey);
572 struct rpc_pipe_msg *msg;
573 struct idmap_msg *im;
574 struct idmap *idmap = (struct idmap *)aux;
575 struct key *key = rka->target_key;
581 /* msg and im are freed in idmap_pipe_destroy_msg */
583 data = kzalloc(sizeof(*data), GFP_KERNEL);
587 msg = &data->pipe_msg;
588 im = &data->idmap_msg;
590 data->authkey = key_get(authkey);
592 ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
597 if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
600 ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
602 nfs_idmap_abort_pipe_upcall(idmap, ret);
608 complete_request_key(authkey, ret);
612 static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
614 return key_instantiate_and_link(key, data, datalen,
615 id_resolver_cache->thread_keyring,
619 static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
620 struct idmap_msg *upcall,
621 struct key *key, struct key *authkey)
623 char id_str[NFS_UINT_MAXLEN];
628 if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
630 switch (im->im_conv) {
631 case IDMAP_CONV_NAMETOID:
632 if (strcmp(upcall->im_name, im->im_name) != 0)
634 /* Note: here we store the NUL terminator too */
635 len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
637 ret = nfs_idmap_instantiate(key, authkey, id_str, len);
639 case IDMAP_CONV_IDTONAME:
640 if (upcall->im_id != im->im_id)
642 len = strlen(im->im_name);
643 ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
653 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
655 struct request_key_auth *rka;
656 struct rpc_inode *rpci = RPC_I(file_inode(filp));
657 struct idmap *idmap = (struct idmap *)rpci->private;
663 /* If instantiation is successful, anyone waiting for key construction
664 * will have been woken up and someone else may now have used
665 * idmap_key_cons - so after this point we may no longer touch it.
667 if (idmap->idmap_upcall_data == NULL)
670 authkey = idmap->idmap_upcall_data->authkey;
671 rka = get_request_key_auth(authkey);
673 if (mlen != sizeof(im)) {
678 if (copy_from_user(&im, src, mlen) != 0) {
683 if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
688 namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
689 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
694 ret = nfs_idmap_read_and_verify_message(&im,
695 &idmap->idmap_upcall_data->idmap_msg,
696 rka->target_key, authkey);
698 key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
703 nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
709 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
711 struct idmap_legacy_upcalldata *data = container_of(msg,
712 struct idmap_legacy_upcalldata,
714 struct idmap *idmap = data->idmap;
717 nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
721 idmap_release_pipe(struct inode *inode)
723 struct rpc_inode *rpci = RPC_I(inode);
724 struct idmap *idmap = (struct idmap *)rpci->private;
726 nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
729 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
731 struct idmap *idmap = server->nfs_client->cl_idmap;
735 if (!nfs_map_string_to_numeric(name, namelen, &id))
736 ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
738 *uid = make_kuid(&init_user_ns, id);
739 if (!uid_valid(*uid))
742 trace_nfs4_map_name_to_uid(name, namelen, id, ret);
746 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
748 struct idmap *idmap = server->nfs_client->cl_idmap;
752 if (!nfs_map_string_to_numeric(name, namelen, &id))
753 ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
755 *gid = make_kgid(&init_user_ns, id);
756 if (!gid_valid(*gid))
759 trace_nfs4_map_group_to_gid(name, namelen, id, ret);
763 int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
765 struct idmap *idmap = server->nfs_client->cl_idmap;
769 id = from_kuid(&init_user_ns, uid);
770 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
771 ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
773 ret = nfs_map_numeric_to_string(id, buf, buflen);
774 trace_nfs4_map_uid_to_name(buf, ret, id, ret);
777 int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
779 struct idmap *idmap = server->nfs_client->cl_idmap;
783 id = from_kgid(&init_user_ns, gid);
784 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
785 ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
787 ret = nfs_map_numeric_to_string(id, buf, buflen);
788 trace_nfs4_map_gid_to_group(buf, ret, id, ret);