linux/fs/nfs/idmap.c
<<
>>
Prefs
   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
  55static const struct cred *id_resolver_cache;
  56static struct key_type key_type_id_resolver_legacy;
  57
  58struct idmap_legacy_upcalldata {
  59        struct rpc_pipe_msg pipe_msg;
  60        struct idmap_msg idmap_msg;
  61        struct key_construction *key_cons;
  62        struct idmap *idmap;
  63};
  64
  65struct idmap {
  66        struct rpc_pipe         *idmap_pipe;
  67        struct idmap_legacy_upcalldata *idmap_upcall_data;
  68        struct mutex            idmap_mutex;
  69};
  70
  71/**
  72 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
  73 * @fattr: fully initialised struct nfs_fattr
  74 * @owner_name: owner name string cache
  75 * @group_name: group name string cache
  76 */
  77void nfs_fattr_init_names(struct nfs_fattr *fattr,
  78                struct nfs4_string *owner_name,
  79                struct nfs4_string *group_name)
  80{
  81        fattr->owner_name = owner_name;
  82        fattr->group_name = group_name;
  83}
  84
  85static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  86{
  87        fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  88        kfree(fattr->owner_name->data);
  89}
  90
  91static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  92{
  93        fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  94        kfree(fattr->group_name->data);
  95}
  96
  97static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  98{
  99        struct nfs4_string *owner = fattr->owner_name;
 100        kuid_t uid;
 101
 102        if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
 103                return false;
 104        if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
 105                fattr->uid = uid;
 106                fattr->valid |= NFS_ATTR_FATTR_OWNER;
 107        }
 108        return true;
 109}
 110
 111static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
 112{
 113        struct nfs4_string *group = fattr->group_name;
 114        kgid_t gid;
 115
 116        if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
 117                return false;
 118        if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
 119                fattr->gid = gid;
 120                fattr->valid |= NFS_ATTR_FATTR_GROUP;
 121        }
 122        return true;
 123}
 124
 125/**
 126 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
 127 * @fattr: a fully initialised nfs_fattr structure
 128 */
 129void nfs_fattr_free_names(struct nfs_fattr *fattr)
 130{
 131        if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
 132                nfs_fattr_free_owner_name(fattr);
 133        if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
 134                nfs_fattr_free_group_name(fattr);
 135}
 136
 137/**
 138 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
 139 * @server: pointer to the filesystem nfs_server structure
 140 * @fattr: a fully initialised nfs_fattr structure
 141 *
 142 * This helper maps the cached NFSv4 owner/group strings in fattr into
 143 * their numeric uid/gid equivalents, and then frees the cached strings.
 144 */
 145void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
 146{
 147        if (nfs_fattr_map_owner_name(server, fattr))
 148                nfs_fattr_free_owner_name(fattr);
 149        if (nfs_fattr_map_group_name(server, fattr))
 150                nfs_fattr_free_group_name(fattr);
 151}
 152
 153static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
 154{
 155        unsigned long val;
 156        char buf[16];
 157
 158        if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
 159                return 0;
 160        memcpy(buf, name, namelen);
 161        buf[namelen] = '\0';
 162        if (kstrtoul(buf, 0, &val) != 0)
 163                return 0;
 164        *res = val;
 165        return 1;
 166}
 167
 168static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
 169{
 170        return snprintf(buf, buflen, "%u", id);
 171}
 172
 173static struct key_type key_type_id_resolver = {
 174        .name           = "id_resolver",
 175        .instantiate    = user_instantiate,
 176        .match          = user_match,
 177        .revoke         = user_revoke,
 178        .destroy        = user_destroy,
 179        .describe       = user_describe,
 180        .read           = user_read,
 181};
 182
 183static int nfs_idmap_init_keyring(void)
 184{
 185        struct cred *cred;
 186        struct key *keyring;
 187        int ret = 0;
 188
 189        printk(KERN_NOTICE "NFS: Registering the %s key type\n",
 190                key_type_id_resolver.name);
 191
 192        cred = prepare_kernel_cred(NULL);
 193        if (!cred)
 194                return -ENOMEM;
 195
 196        keyring = keyring_alloc(".id_resolver",
 197                                GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
 198                                (KEY_POS_ALL & ~KEY_POS_SETATTR) |
 199                                KEY_USR_VIEW | KEY_USR_READ,
 200                                KEY_ALLOC_NOT_IN_QUOTA, NULL);
 201        if (IS_ERR(keyring)) {
 202                ret = PTR_ERR(keyring);
 203                goto failed_put_cred;
 204        }
 205
 206        ret = register_key_type(&key_type_id_resolver);
 207        if (ret < 0)
 208                goto failed_put_key;
 209
 210        ret = register_key_type(&key_type_id_resolver_legacy);
 211        if (ret < 0)
 212                goto failed_reg_legacy;
 213
 214        set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
 215        cred->thread_keyring = keyring;
 216        cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
 217        id_resolver_cache = cred;
 218        return 0;
 219
 220failed_reg_legacy:
 221        unregister_key_type(&key_type_id_resolver);
 222failed_put_key:
 223        key_put(keyring);
 224failed_put_cred:
 225        put_cred(cred);
 226        return ret;
 227}
 228
 229static void nfs_idmap_quit_keyring(void)
 230{
 231        key_revoke(id_resolver_cache->thread_keyring);
 232        unregister_key_type(&key_type_id_resolver);
 233        unregister_key_type(&key_type_id_resolver_legacy);
 234        put_cred(id_resolver_cache);
 235}
 236
 237/*
 238 * Assemble the description to pass to request_key()
 239 * This function will allocate a new string and update dest to point
 240 * at it.  The caller is responsible for freeing dest.
 241 *
 242 * On error 0 is returned.  Otherwise, the length of dest is returned.
 243 */
 244static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
 245                                const char *type, size_t typelen, char **desc)
 246{
 247        char *cp;
 248        size_t desclen = typelen + namelen + 2;
 249
 250        *desc = kmalloc(desclen, GFP_KERNEL);
 251        if (!*desc)
 252                return -ENOMEM;
 253
 254        cp = *desc;
 255        memcpy(cp, type, typelen);
 256        cp += typelen;
 257        *cp++ = ':';
 258
 259        memcpy(cp, name, namelen);
 260        cp += namelen;
 261        *cp = '\0';
 262        return desclen;
 263}
 264
 265static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
 266                                         const char *type, struct idmap *idmap)
 267{
 268        char *desc;
 269        struct key *rkey;
 270        ssize_t ret;
 271
 272        ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
 273        if (ret <= 0)
 274                return ERR_PTR(ret);
 275
 276        rkey = request_key(&key_type_id_resolver, desc, "");
 277        if (IS_ERR(rkey)) {
 278                mutex_lock(&idmap->idmap_mutex);
 279                rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
 280                                                desc, "", 0, idmap);
 281                mutex_unlock(&idmap->idmap_mutex);
 282        }
 283
 284        kfree(desc);
 285        return rkey;
 286}
 287
 288static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
 289                                 const char *type, void *data,
 290                                 size_t data_size, struct idmap *idmap)
 291{
 292        const struct cred *saved_cred;
 293        struct key *rkey;
 294        struct user_key_payload *payload;
 295        ssize_t ret;
 296
 297        saved_cred = override_creds(id_resolver_cache);
 298        rkey = nfs_idmap_request_key(name, namelen, type, idmap);
 299        revert_creds(saved_cred);
 300
 301        if (IS_ERR(rkey)) {
 302                ret = PTR_ERR(rkey);
 303                goto out;
 304        }
 305
 306        rcu_read_lock();
 307        rkey->perm |= KEY_USR_VIEW;
 308
 309        ret = key_validate(rkey);
 310        if (ret < 0)
 311                goto out_up;
 312
 313        payload = rcu_dereference(rkey->payload.data);
 314        if (IS_ERR_OR_NULL(payload)) {
 315                ret = PTR_ERR(payload);
 316                goto out_up;
 317        }
 318
 319        ret = payload->datalen;
 320        if (ret > 0 && ret <= data_size)
 321                memcpy(data, payload->data, ret);
 322        else
 323                ret = -EINVAL;
 324
 325out_up:
 326        rcu_read_unlock();
 327        key_put(rkey);
 328out:
 329        return ret;
 330}
 331
 332/* ID -> Name */
 333static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
 334                                     size_t buflen, struct idmap *idmap)
 335{
 336        char id_str[NFS_UINT_MAXLEN];
 337        int id_len;
 338        ssize_t ret;
 339
 340        id_len = snprintf(id_str, sizeof(id_str), "%u", id);
 341        ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
 342        if (ret < 0)
 343                return -EINVAL;
 344        return ret;
 345}
 346
 347/* Name -> ID */
 348static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
 349                               __u32 *id, struct idmap *idmap)
 350{
 351        char id_str[NFS_UINT_MAXLEN];
 352        long id_long;
 353        ssize_t data_size;
 354        int ret = 0;
 355
 356        data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
 357        if (data_size <= 0) {
 358                ret = -EINVAL;
 359        } else {
 360                ret = kstrtol(id_str, 10, &id_long);
 361                *id = (__u32)id_long;
 362        }
 363        return ret;
 364}
 365
 366/* idmap classic begins here */
 367
 368enum {
 369        Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
 370};
 371
 372static const match_table_t nfs_idmap_tokens = {
 373        { Opt_find_uid, "uid:%s" },
 374        { Opt_find_gid, "gid:%s" },
 375        { Opt_find_user, "user:%s" },
 376        { Opt_find_group, "group:%s" },
 377        { Opt_find_err, NULL }
 378};
 379
 380static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
 381static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
 382                                   size_t);
 383static void idmap_release_pipe(struct inode *);
 384static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
 385
 386static const struct rpc_pipe_ops idmap_upcall_ops = {
 387        .upcall         = rpc_pipe_generic_upcall,
 388        .downcall       = idmap_pipe_downcall,
 389        .release_pipe   = idmap_release_pipe,
 390        .destroy_msg    = idmap_pipe_destroy_msg,
 391};
 392
 393static struct key_type key_type_id_resolver_legacy = {
 394        .name           = "id_legacy",
 395        .instantiate    = user_instantiate,
 396        .match          = user_match,
 397        .revoke         = user_revoke,
 398        .destroy        = user_destroy,
 399        .describe       = user_describe,
 400        .read           = user_read,
 401        .request_key    = nfs_idmap_legacy_upcall,
 402};
 403
 404static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
 405{
 406        if (pipe->dentry)
 407                rpc_unlink(pipe->dentry);
 408}
 409
 410static int __nfs_idmap_register(struct dentry *dir,
 411                                     struct idmap *idmap,
 412                                     struct rpc_pipe *pipe)
 413{
 414        struct dentry *dentry;
 415
 416        dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
 417        if (IS_ERR(dentry))
 418                return PTR_ERR(dentry);
 419        pipe->dentry = dentry;
 420        return 0;
 421}
 422
 423static void nfs_idmap_unregister(struct nfs_client *clp,
 424                                      struct rpc_pipe *pipe)
 425{
 426        struct net *net = clp->cl_net;
 427        struct super_block *pipefs_sb;
 428
 429        pipefs_sb = rpc_get_sb_net(net);
 430        if (pipefs_sb) {
 431                __nfs_idmap_unregister(pipe);
 432                rpc_put_sb_net(net);
 433        }
 434}
 435
 436static int nfs_idmap_register(struct nfs_client *clp,
 437                                   struct idmap *idmap,
 438                                   struct rpc_pipe *pipe)
 439{
 440        struct net *net = clp->cl_net;
 441        struct super_block *pipefs_sb;
 442        int err = 0;
 443
 444        pipefs_sb = rpc_get_sb_net(net);
 445        if (pipefs_sb) {
 446                if (clp->cl_rpcclient->cl_dentry)
 447                        err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
 448                                                   idmap, pipe);
 449                rpc_put_sb_net(net);
 450        }
 451        return err;
 452}
 453
 454int
 455nfs_idmap_new(struct nfs_client *clp)
 456{
 457        struct idmap *idmap;
 458        struct rpc_pipe *pipe;
 459        int error;
 460
 461        idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
 462        if (idmap == NULL)
 463                return -ENOMEM;
 464
 465        pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
 466        if (IS_ERR(pipe)) {
 467                error = PTR_ERR(pipe);
 468                kfree(idmap);
 469                return error;
 470        }
 471        error = nfs_idmap_register(clp, idmap, pipe);
 472        if (error) {
 473                rpc_destroy_pipe_data(pipe);
 474                kfree(idmap);
 475                return error;
 476        }
 477        idmap->idmap_pipe = pipe;
 478        mutex_init(&idmap->idmap_mutex);
 479
 480        clp->cl_idmap = idmap;
 481        return 0;
 482}
 483
 484void
 485nfs_idmap_delete(struct nfs_client *clp)
 486{
 487        struct idmap *idmap = clp->cl_idmap;
 488
 489        if (!idmap)
 490                return;
 491        nfs_idmap_unregister(clp, idmap->idmap_pipe);
 492        rpc_destroy_pipe_data(idmap->idmap_pipe);
 493        clp->cl_idmap = NULL;
 494        kfree(idmap);
 495}
 496
 497static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
 498                              struct super_block *sb)
 499{
 500        int err = 0;
 501
 502        switch (event) {
 503        case RPC_PIPEFS_MOUNT:
 504                err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
 505                                                clp->cl_idmap,
 506                                                clp->cl_idmap->idmap_pipe);
 507                break;
 508        case RPC_PIPEFS_UMOUNT:
 509                if (clp->cl_idmap->idmap_pipe) {
 510                        struct dentry *parent;
 511
 512                        parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
 513                        __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
 514                        /*
 515                         * Note: This is a dirty hack. SUNRPC hook has been
 516                         * called already but simple_rmdir() call for the
 517                         * directory returned with error because of idmap pipe
 518                         * inside. Thus now we have to remove this directory
 519                         * here.
 520                         */
 521                        if (rpc_rmdir(parent))
 522                                printk(KERN_ERR "NFS: %s: failed to remove "
 523                                        "clnt dir!\n", __func__);
 524                }
 525                break;
 526        default:
 527                printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
 528                        event);
 529                return -ENOTSUPP;
 530        }
 531        return err;
 532}
 533
 534static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
 535{
 536        struct nfs_net *nn = net_generic(net, nfs_net_id);
 537        struct dentry *cl_dentry;
 538        struct nfs_client *clp;
 539        int err;
 540
 541restart:
 542        spin_lock(&nn->nfs_client_lock);
 543        list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
 544                /* Wait for initialisation to finish */
 545                if (clp->cl_cons_state == NFS_CS_INITING) {
 546                        atomic_inc(&clp->cl_count);
 547                        spin_unlock(&nn->nfs_client_lock);
 548                        err = nfs_wait_client_init_complete(clp);
 549                        nfs_put_client(clp);
 550                        if (err)
 551                                return NULL;
 552                        goto restart;
 553                }
 554                /* Skip nfs_clients that failed to initialise */
 555                if (clp->cl_cons_state < 0)
 556                        continue;
 557                smp_rmb();
 558                if (clp->rpc_ops != &nfs_v4_clientops)
 559                        continue;
 560                cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
 561                if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
 562                    ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
 563                        continue;
 564                atomic_inc(&clp->cl_count);
 565                spin_unlock(&nn->nfs_client_lock);
 566                return clp;
 567        }
 568        spin_unlock(&nn->nfs_client_lock);
 569        return NULL;
 570}
 571
 572static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
 573                            void *ptr)
 574{
 575        struct super_block *sb = ptr;
 576        struct nfs_client *clp;
 577        int error = 0;
 578
 579        if (!try_module_get(THIS_MODULE))
 580                return 0;
 581
 582        while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
 583                error = __rpc_pipefs_event(clp, event, sb);
 584                nfs_put_client(clp);
 585                if (error)
 586                        break;
 587        }
 588        module_put(THIS_MODULE);
 589        return error;
 590}
 591
 592#define PIPEFS_NFS_PRIO         1
 593
 594static struct notifier_block nfs_idmap_block = {
 595        .notifier_call  = rpc_pipefs_event,
 596        .priority       = SUNRPC_PIPEFS_NFS_PRIO,
 597};
 598
 599int nfs_idmap_init(void)
 600{
 601        int ret;
 602        ret = nfs_idmap_init_keyring();
 603        if (ret != 0)
 604                goto out;
 605        ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
 606        if (ret != 0)
 607                nfs_idmap_quit_keyring();
 608out:
 609        return ret;
 610}
 611
 612void nfs_idmap_quit(void)
 613{
 614        rpc_pipefs_notifier_unregister(&nfs_idmap_block);
 615        nfs_idmap_quit_keyring();
 616}
 617
 618static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
 619                                     struct idmap_msg *im,
 620                                     struct rpc_pipe_msg *msg)
 621{
 622        substring_t substr;
 623        int token, ret;
 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
 651out:
 652        return ret;
 653}
 654
 655static bool
 656nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
 657                struct idmap_legacy_upcalldata *data)
 658{
 659        if (idmap->idmap_upcall_data != NULL) {
 660                WARN_ON_ONCE(1);
 661                return false;
 662        }
 663        idmap->idmap_upcall_data = data;
 664        return true;
 665}
 666
 667static void
 668nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
 669{
 670        struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
 671
 672        kfree(idmap->idmap_upcall_data);
 673        idmap->idmap_upcall_data = NULL;
 674        complete_request_key(cons, ret);
 675}
 676
 677static void
 678nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
 679{
 680        if (idmap->idmap_upcall_data != NULL)
 681                nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
 682}
 683
 684static int nfs_idmap_legacy_upcall(struct key_construction *cons,
 685                                   const char *op,
 686                                   void *aux)
 687{
 688        struct idmap_legacy_upcalldata *data;
 689        struct rpc_pipe_msg *msg;
 690        struct idmap_msg *im;
 691        struct idmap *idmap = (struct idmap *)aux;
 692        struct key *key = cons->key;
 693        int ret = -ENOMEM;
 694
 695        /* msg and im are freed in idmap_pipe_destroy_msg */
 696        data = kzalloc(sizeof(*data), GFP_KERNEL);
 697        if (!data)
 698                goto out1;
 699
 700        msg = &data->pipe_msg;
 701        im = &data->idmap_msg;
 702        data->idmap = idmap;
 703        data->key_cons = cons;
 704
 705        ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
 706        if (ret < 0)
 707                goto out2;
 708
 709        ret = -EAGAIN;
 710        if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
 711                goto out2;
 712
 713        ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
 714        if (ret < 0)
 715                nfs_idmap_abort_pipe_upcall(idmap, ret);
 716
 717        return ret;
 718out2:
 719        kfree(data);
 720out1:
 721        complete_request_key(cons, ret);
 722        return ret;
 723}
 724
 725static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
 726{
 727        return key_instantiate_and_link(key, data, datalen,
 728                                        id_resolver_cache->thread_keyring,
 729                                        authkey);
 730}
 731
 732static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
 733                struct idmap_msg *upcall,
 734                struct key *key, struct key *authkey)
 735{
 736        char id_str[NFS_UINT_MAXLEN];
 737        size_t len;
 738        int ret = -ENOKEY;
 739
 740        /* ret = -ENOKEY */
 741        if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
 742                goto out;
 743        switch (im->im_conv) {
 744        case IDMAP_CONV_NAMETOID:
 745                if (strcmp(upcall->im_name, im->im_name) != 0)
 746                        break;
 747                /* Note: here we store the NUL terminator too */
 748                len = sprintf(id_str, "%d", im->im_id) + 1;
 749                ret = nfs_idmap_instantiate(key, authkey, id_str, len);
 750                break;
 751        case IDMAP_CONV_IDTONAME:
 752                if (upcall->im_id != im->im_id)
 753                        break;
 754                len = strlen(im->im_name);
 755                ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
 756                break;
 757        default:
 758                ret = -EINVAL;
 759        }
 760out:
 761        return ret;
 762}
 763
 764static ssize_t
 765idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
 766{
 767        struct rpc_inode *rpci = RPC_I(file_inode(filp));
 768        struct idmap *idmap = (struct idmap *)rpci->private;
 769        struct key_construction *cons;
 770        struct idmap_msg im;
 771        size_t namelen_in;
 772        int ret = -ENOKEY;
 773
 774        /* If instantiation is successful, anyone waiting for key construction
 775         * will have been woken up and someone else may now have used
 776         * idmap_key_cons - so after this point we may no longer touch it.
 777         */
 778        if (idmap->idmap_upcall_data == NULL)
 779                goto out_noupcall;
 780
 781        cons = idmap->idmap_upcall_data->key_cons;
 782
 783        if (mlen != sizeof(im)) {
 784                ret = -ENOSPC;
 785                goto out;
 786        }
 787
 788        if (copy_from_user(&im, src, mlen) != 0) {
 789                ret = -EFAULT;
 790                goto out;
 791        }
 792
 793        if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
 794                ret = -ENOKEY;
 795                goto out;
 796        }
 797
 798        namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
 799        if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
 800                ret = -EINVAL;
 801                goto out;
 802}
 803
 804        ret = nfs_idmap_read_and_verify_message(&im,
 805                        &idmap->idmap_upcall_data->idmap_msg,
 806                        cons->key, cons->authkey);
 807        if (ret >= 0) {
 808                key_set_timeout(cons->key, nfs_idmap_cache_timeout);
 809                ret = mlen;
 810        }
 811
 812out:
 813        nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
 814out_noupcall:
 815        return ret;
 816}
 817
 818static void
 819idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
 820{
 821        struct idmap_legacy_upcalldata *data = container_of(msg,
 822                        struct idmap_legacy_upcalldata,
 823                        pipe_msg);
 824        struct idmap *idmap = data->idmap;
 825
 826        if (msg->errno)
 827                nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
 828}
 829
 830static void
 831idmap_release_pipe(struct inode *inode)
 832{
 833        struct rpc_inode *rpci = RPC_I(inode);
 834        struct idmap *idmap = (struct idmap *)rpci->private;
 835
 836        nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
 837}
 838
 839int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
 840{
 841        struct idmap *idmap = server->nfs_client->cl_idmap;
 842        __u32 id = -1;
 843        int ret = 0;
 844
 845        if (!nfs_map_string_to_numeric(name, namelen, &id))
 846                ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
 847        if (ret == 0) {
 848                *uid = make_kuid(&init_user_ns, id);
 849                if (!uid_valid(*uid))
 850                        ret = -ERANGE;
 851        }
 852        return ret;
 853}
 854
 855int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
 856{
 857        struct idmap *idmap = server->nfs_client->cl_idmap;
 858        __u32 id = -1;
 859        int ret = 0;
 860
 861        if (!nfs_map_string_to_numeric(name, namelen, &id))
 862                ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
 863        if (ret == 0) {
 864                *gid = make_kgid(&init_user_ns, id);
 865                if (!gid_valid(*gid))
 866                        ret = -ERANGE;
 867        }
 868        return ret;
 869}
 870
 871int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
 872{
 873        struct idmap *idmap = server->nfs_client->cl_idmap;
 874        int ret = -EINVAL;
 875        __u32 id;
 876
 877        id = from_kuid(&init_user_ns, uid);
 878        if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
 879                ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
 880        if (ret < 0)
 881                ret = nfs_map_numeric_to_string(id, buf, buflen);
 882        return ret;
 883}
 884int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
 885{
 886        struct idmap *idmap = server->nfs_client->cl_idmap;
 887        int ret = -EINVAL;
 888        __u32 id;
 889
 890        id = from_kgid(&init_user_ns, gid);
 891        if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
 892                ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
 893        if (ret < 0)
 894                ret = nfs_map_numeric_to_string(id, buf, buflen);
 895        return ret;
 896}
 897