Skip to contentSkip to conversation

NFSd: Fix filehandle leaks in exp_pseudoroot() and nfsd4_path()

PATCH Trond Myklebust 2 participants 2 messages Sep 2, 2009 – Sep 3, 2009 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

NFSd: Fix filehandle leaks in exp_pseudoroot() and nfsd4_path()

From: Trond Myklebust <[email protected]>

All the callers of exp_pseudoroot() appear to expect the filehandle it returns to be uninitialised if the function returns an error. This is not the case if the error is due to the check_nfsd_access() call failing.

In addition, nfsd4_path() currently allocates a temporary filehandle, then fails to free it before the function exits.

---
Note: this patch is still untested, but I can't see how it can be wrong. The leak is obvious by code inspection, and doing an extra fh_put() in the error case is obviously harmless.

Also note: there is apparently a similar leak in fh_compose() itself, but I didn't want to spend the entire afternoon fixing all occurrences of this bug...

 fs/nfsd/export.c  |    2 ++
 fs/nfsd/nfs4xdr.c |   15 ++++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
fs/nfsd/export.c +2 -0
index d946264..984a5eb 100644
@@ -1341,6 +1341,8 @@ exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
 	if (rv)
 		goto out;
 	rv = check_nfsd_access(exp, rqstp);
+	if (rv)
+		fh_put(fhp);
 out:
 	exp_put(exp);
 	return rv;
fs/nfsd/nfs4xdr.c +10 -5
index 1b07183..370c27e 100644
@@ -1599,7 +1599,8 @@ static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
 static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
 {
 	struct svc_fh tmp_fh;
-	char *path, *rootpath;
+	char *path = NULL, *rootpath;
+	size_t rootlen;
 
 	fh_init(&tmp_fh, NFS4_FHSIZE);
 	*stat = exp_pseudoroot(rqstp, &tmp_fh);
@@ -1609,14 +1610,18 @@ static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *
 
 	path = exp->ex_pathname;
 
-	if (strncmp(path, rootpath, strlen(rootpath))) {
+	rootlen = strlen(rootpath);
+	if (strncmp(path, rootpath, rootlen)) {
 		dprintk("nfsd: fs_locations failed;"
 			"%s is not contained in %s\n", path, rootpath);
 		*stat = nfserr_notsupp;
-		return NULL;
+		path = NULL;
+		goto out;
 	}
-
-	return path + strlen(rootpath);
+	path += rootlen;
+out:
+	fh_put(&tmp_fh);
+	return path;
 }
 
 /*
Signature · 6 lines
Trond Myklebust
Linux NFS client maintainer

NetApp
[email protected]
www.netapp.com

A message that is not in the archive (sent off-list or not yet received): 1251924512.26601.59.camel-rJ7iovZKK19ZJLDQqaL3InhyD016LWXt@public.gmane.org

#2 lore raw
Quoted 6 lines · Trond Myklebust

NFSd: Fix filehandle leaks in exp_pseudoroot() and nfsd4_path()

From: Trond Myklebust <[email protected]>

All the callers of exp_pseudoroot() appear to expect the filehandle it returns to be uninitialised if the function returns an error.

There are three callers:

- nfsd4_putrootfh treats failure and success cases no
differently: it leaves the result in current_fh, and leaves freeing to the caller (nfsd4_proc_compound()) in either case.
- nfsd4_lookupp puts the result in a temporary variable and returns doing nothing on failure. Oops! Thanks, this fixes that.
- nfsd4_path(), as you say, stores it in a temporary variable and never frees it, success or failure.

I agree that fixing nfsd4_path() to release in both cases, and exp_pseudoroot to release on failure, makes sense. Applied. Thanks, Trond!

Quoted 11 lines

This is not
the case if the error is due to the check_nfsd_access() call failing.

In addition, nfsd4_path() currently allocates a temporary filehandle, then fails to free it before the function exits.

---
Note: this patch is still untested, but I can't see how it can be wrong. The leak is obvious by code inspection, and doing an extra fh_put() in the error case is obviously harmless.

Yes. Works for me.

Quoted 3 lines

Also note: there is apparently a similar leak in fh_compose() itself, but I didn't want to spend the entire afternoon fixing all occurrences of this bug...

I think you're right. I'll fix fh_compose()....

--b.

Quoted 64 lines
 fs/nfsd/export.c  |    2 ++
 fs/nfsd/nfs4xdr.c |   15 ++++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
fs/nfsd/export.c +2 -0
index d946264..984a5eb 100644
@@ -1341,6 +1341,8 @@ exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
 	if (rv)
 		goto out;
 	rv = check_nfsd_access(exp, rqstp);
+	if (rv)
+		fh_put(fhp);
 out:
 	exp_put(exp);
 	return rv;
fs/nfsd/nfs4xdr.c +10 -5
index 1b07183..370c27e 100644
@@ -1599,7 +1599,8 @@ static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
 static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
 {
 	struct svc_fh tmp_fh;
-	char *path, *rootpath;
+	char *path = NULL, *rootpath;
+	size_t rootlen;
 
 	fh_init(&tmp_fh, NFS4_FHSIZE);
 	*stat = exp_pseudoroot(rqstp, &tmp_fh);
@@ -1609,14 +1610,18 @@ static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *
 
 	path = exp->ex_pathname;
 
-	if (strncmp(path, rootpath, strlen(rootpath))) {
+	rootlen = strlen(rootpath);
+	if (strncmp(path, rootpath, rootlen)) {
 		dprintk("nfsd: fs_locations failed;"
 			"%s is not contained in %s\n", path, rootpath);
 		*stat = nfserr_notsupp;
-		return NULL;
+		path = NULL;
+		goto out;
 	}
-
-	return path + strlen(rootpath);
+	path += rootlen;
+out:
+	fh_put(&tmp_fh);
+	return path;
 }
 
 /*
Signature · 7 lines
-- 
Trond Myklebust
Linux NFS client maintainer

NetApp
[email protected]
www.netapp.com