NFSv4: Clean up initialisation of uniquified client id strings
authorTrond Myklebust <trond.myklebust@hammerspace.com>
Wed, 7 Oct 2020 22:24:17 +0000 (18:24 -0400)
committerAnna Schumaker <Anna.Schumaker@Netapp.com>
Fri, 9 Oct 2020 14:04:36 +0000 (10:04 -0400)
When the user sets a uniquifier, then ensure we copy the string
so that calls to strlen() etc are atomic with calls to snprintf().

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
fs/nfs/nfs4proc.c

index d09fd32..50a96ca 100644 (file)
@@ -6093,9 +6093,22 @@ static void nfs4_init_boot_verifier(const struct nfs_client *clp,
        memcpy(bootverf->data, verf, sizeof(bootverf->data));
 }
 
+static size_t
+nfs4_get_uniquifier(char *buf, size_t buflen)
+{
+       buf[0] = '\0';
+
+       if (nfs4_client_id_uniquifier[0] != '\0')
+               strscpy(buf, nfs4_client_id_uniquifier, buflen);
+
+       return strlen(buf);
+}
+
 static int
 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
 {
+       char buf[NFS4_CLIENT_ID_UNIQ_LEN];
+       size_t buflen;
        size_t len;
        char *str;
 
@@ -6109,8 +6122,11 @@ nfs4_init_nonuniform_client_string(struct nfs_client *clp)
                strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
                1;
        rcu_read_unlock();
-       if (nfs4_client_id_uniquifier[0] != '\0')
-               len += strlen(nfs4_client_id_uniquifier) + 1;
+
+       buflen = nfs4_get_uniquifier(buf, sizeof(buf));
+       if (buflen)
+               len += buflen + 1;
+
        if (len > NFS4_OPAQUE_LIMIT + 1)
                return -EINVAL;
 
@@ -6124,10 +6140,9 @@ nfs4_init_nonuniform_client_string(struct nfs_client *clp)
                return -ENOMEM;
 
        rcu_read_lock();
-       if (nfs4_client_id_uniquifier[0] != '\0')
+       if (buflen)
                scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
-                         clp->cl_rpcclient->cl_nodename,
-                         nfs4_client_id_uniquifier,
+                         clp->cl_rpcclient->cl_nodename, buf,
                          rpc_peeraddr2str(clp->cl_rpcclient,
                                           RPC_DISPLAY_ADDR));
        else
@@ -6141,51 +6156,24 @@ nfs4_init_nonuniform_client_string(struct nfs_client *clp)
        return 0;
 }
 
-static int
-nfs4_init_uniquifier_client_string(struct nfs_client *clp)
-{
-       size_t len;
-       char *str;
-
-       len = 10 + 10 + 1 + 10 + 1 +
-               strlen(nfs4_client_id_uniquifier) + 1 +
-               strlen(clp->cl_rpcclient->cl_nodename) + 1;
-
-       if (len > NFS4_OPAQUE_LIMIT + 1)
-               return -EINVAL;
-
-       /*
-        * Since this string is allocated at mount time, and held until the
-        * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
-        * about a memory-reclaim deadlock.
-        */
-       str = kmalloc(len, GFP_KERNEL);
-       if (!str)
-               return -ENOMEM;
-
-       scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
-                       clp->rpc_ops->version, clp->cl_minorversion,
-                       nfs4_client_id_uniquifier,
-                       clp->cl_rpcclient->cl_nodename);
-       clp->cl_owner_id = str;
-       return 0;
-}
-
 static int
 nfs4_init_uniform_client_string(struct nfs_client *clp)
 {
+       char buf[NFS4_CLIENT_ID_UNIQ_LEN];
+       size_t buflen;
        size_t len;
        char *str;
 
        if (clp->cl_owner_id != NULL)
                return 0;
 
-       if (nfs4_client_id_uniquifier[0] != '\0')
-               return nfs4_init_uniquifier_client_string(clp);
-
        len = 10 + 10 + 1 + 10 + 1 +
                strlen(clp->cl_rpcclient->cl_nodename) + 1;
 
+       buflen = nfs4_get_uniquifier(buf, sizeof(buf));
+       if (buflen)
+               len += buflen + 1;
+
        if (len > NFS4_OPAQUE_LIMIT + 1)
                return -EINVAL;
 
@@ -6198,9 +6186,14 @@ nfs4_init_uniform_client_string(struct nfs_client *clp)
        if (!str)
                return -ENOMEM;
 
-       scnprintf(str, len, "Linux NFSv%u.%u %s",
-                       clp->rpc_ops->version, clp->cl_minorversion,
-                       clp->cl_rpcclient->cl_nodename);
+       if (buflen)
+               scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
+                         clp->rpc_ops->version, clp->cl_minorversion,
+                         buf, clp->cl_rpcclient->cl_nodename);
+       else
+               scnprintf(str, len, "Linux NFSv%u.%u %s",
+                         clp->rpc_ops->version, clp->cl_minorversion,
+                         clp->cl_rpcclient->cl_nodename);
        clp->cl_owner_id = str;
        return 0;
 }