]> www.infradead.org Git - users/hch/misc.git/commitdiff
nfsd: Eliminate an allocation in nfs4_make_rec_clidname()
authorEric Biggers <ebiggers@kernel.org>
Mon, 4 Aug 2025 22:47:00 +0000 (22:47 +0000)
committerChuck Lever <chuck.lever@oracle.com>
Sun, 21 Sep 2025 23:24:50 +0000 (19:24 -0400)
Since MD5 digests are fixed-size, make nfs4_make_rec_clidname() store
the digest in a stack buffer instead of a dynamically allocated buffer.
Use MD5_DIGEST_SIZE instead of a hard-coded value, both in
nfs4_make_rec_clidname() and in the definition of HEXDIR_LEN.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
fs/nfsd/nfs4recover.c
fs/nfsd/state.h

index 54f5e5392ef9d925c245defd2b181904c2d823e0..e2b9472e5c78c9f03731090ffdfb26eb5de38fe0 100644 (file)
@@ -95,7 +95,7 @@ nfs4_reset_creds(const struct cred *original)
 static int
 nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
-       struct xdr_netobj cksum;
+       u8 digest[MD5_DIGEST_SIZE];
        struct crypto_shash *tfm;
        int status;
 
@@ -107,23 +107,16 @@ nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
                goto out_no_tfm;
        }
 
-       cksum.len = crypto_shash_digestsize(tfm);
-       cksum.data = kmalloc(cksum.len, GFP_KERNEL);
-       if (cksum.data == NULL) {
-               status = -ENOMEM;
-               goto out;
-       }
-
        status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
-                                        cksum.data);
+                                        digest);
        if (status)
                goto out;
 
-       sprintf(dname, "%*phN", 16, cksum.data);
+       static_assert(HEXDIR_LEN == 2 * MD5_DIGEST_SIZE + 1);
+       sprintf(dname, "%*phN", MD5_DIGEST_SIZE, digest);
 
        status = 0;
 out:
-       kfree(cksum.data);
        crypto_free_shash(tfm);
 out_no_tfm:
        return status;
index b6ac0f37e9cdfcfddde5861c8c0c51bad60101b7..1e736f4024263ffa9c93bcc9ec48f44566a8cc77 100644 (file)
@@ -35,6 +35,7 @@
 #ifndef _NFSD4_STATE_H
 #define _NFSD4_STATE_H
 
+#include <crypto/md5.h>
 #include <linux/idr.h>
 #include <linux/refcount.h>
 #include <linux/sunrpc/svc_xprt.h>
@@ -391,7 +392,8 @@ struct nfsd4_sessionid {
        u32             reserved;
 };
 
-#define HEXDIR_LEN     33 /* hex version of 16 byte md5 of cl_name plus '\0' */
+/* Length of MD5 digest as hex, plus terminating '\0' */
+#define HEXDIR_LEN     (2 * MD5_DIGEST_SIZE + 1)
 
 /*
  *       State                Meaning                  Where set