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@xxxxxxxxxx> --- fs/nfsd/nfs4recover.c | 15 ++++----------- fs/nfsd/state.h | 4 +++- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/fs/nfsd/nfs4recover.c b/fs/nfsd/nfs4recover.c index 54f5e5392ef9..e2b9472e5c78 100644 --- a/fs/nfsd/nfs4recover.c +++ b/fs/nfsd/nfs4recover.c @@ -93,11 +93,11 @@ 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; dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", clname->len, clname->data); @@ -105,27 +105,20 @@ nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname) if (IS_ERR(tfm)) { status = PTR_ERR(tfm); 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; } diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h index 8adc2550129e..b7d4c6d6f3d4 100644 --- a/fs/nfsd/state.h +++ b/fs/nfsd/state.h @@ -33,10 +33,11 @@ */ #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> #include "nfsfh.h" #include "nfsd.h" @@ -379,11 +380,12 @@ struct nfsd4_sessionid { clientid_t clientid; u32 sequence; 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 * -------------------------------------------------------------------------- * | NFSD4_ACTIVE | Confirmed, active | Default | -- 2.50.1.565.gc32cd1483b-goog