minor filehandle-related patches for 2.6.32
Subsystems (2)
I've collected some minor patches for 2.6.32 addressing leaks found by Trond, with some cleanup. (Mainly: I find fh_compose too long to read easily.)
--b.
PATCH 1/6 nfsd: move some of fh_compose into helper functions
--- fs/nfsd/nfsfh.c | 83 +++++++++++++++++++++++++++++------------------------- 1 files changed, 45 insertions(+), 38 deletions(-)
fs/nfsd/nfsfh.c +45 -38
@@ -397,6 +397,40 @@ static inline void _fh_update_old(struct dentry *dentry, fh->ofh_dirino =0 ; } +static bool is_root_export (struct svc_export *exp) +{ +return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root; +} + +static struct super_block *exp_sb (struct svc_export *exp) +{ +return exp->ex_path.dentry->d_inode->i_sb; +} + +static bool fsid_type_ok_for_exp (u8 fsid_type,struct svc_export *exp) +{ +switch (fsid_type) { +case FSID_DEV : +if (!old_valid_dev (exp_sb (exp)->s_dev)) +return 0 ; +/* FALL THROUGH */ +case FSID_MAJOR_MINOR : +case FSID_ENCODE_DEV : +return exp_sb (exp)->s_type->fs_flags &FS_REQUIRES_DEV ; +case FSID_NUM : +return exp->ex_flags &NFSEXP_FSID ; +case FSID_UUID8 : +case FSID_UUID16 : +if (!is_root_export (exp)) +return 0 ; +/* fall through */ +case FSID_UUID4_INUM : +case FSID_UUID16_INUM : +return exp->ex_uuid !=NULL ; + } +return 1 ; +} +__be32 fh_compose (struct svc_fh *fhp,struct svc_export *exp,struct dentry *dentry,struct svc_fh *ref_fh)
@@ -414,8 +448,7 @@ fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,struct inode * inode = dentry->d_inode;struct dentry *parent = dentry->d_parent;__u32 *datap; -dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev; -int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root); +dev_t ex_dev =exp_sb (exp)->s_dev; dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n", MAJOR(ex_dev), MINOR(ex_dev),
@@ -447,49 +480,24 @@ fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, goto retry; } -/* Need to check that this type works for this -* export point. As the fsid -> filesystem mapping -* was guided by user-space, there is no guarantee -* that the filesystem actually supports that fsid -* type. If it doesn't we loop around again without -* ref_fh set. +/* +* As the fsid -> filesystem mapping was guided by +* user-space, there is no guarantee that the filesystem +* actually supports that fsid type. If it doesn't we +* loop around again without ref_fh set. */ -switch (fsid_type) { -case FSID_DEV : -if (!old_valid_dev (ex_dev)) - goto retry; -/* FALL THROUGH */ -case FSID_MAJOR_MINOR : -case FSID_ENCODE_DEV : -if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags - &FS_REQUIRES_DEV )) - goto retry; -break ; -case FSID_NUM : -if (! (exp->ex_flags &NFSEXP_FSID )) - goto retry; -break ; -case FSID_UUID8 : -case FSID_UUID16 : -if (!root_export) - goto retry; -/* fall through */ -case FSID_UUID4_INUM : -case FSID_UUID16_INUM : -if (exp->ex_uuid ==NULL ) - goto retry; -break ; - } +if (!fsid_type_ok_for_exp (fsid_type, exp)) + goto retry; }else if (exp->ex_flags &NFSEXP_FSID ) { fsid_type =FSID_NUM ; }else if (exp->ex_uuid) {if (fhp->fh_maxsize >=64 ) { -if (root_export) +if (is_root_export (exp)) fsid_type =FSID_UUID16 ;else fsid_type =FSID_UUID16_INUM ; }else { -if (root_export) +if (is_root_export (exp)) fsid_type =FSID_UUID8 ;else fsid_type =FSID_UUID4_INUM ;
@@ -639,8 +647,7 @@ enum fsid_source fsid_source(struct svc_fh *fhp)case FSID_DEV :case FSID_ENCODE_DEV :case FSID_MAJOR_MINOR : -if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags - &FS_REQUIRES_DEV ) +if (exp_sb (fhp->fh_export)->s_type->fs_flags &FS_REQUIRES_DEV )return FSIDSOURCE_DEV ;break ;case FSID_NUM :
Signature · 1 line
1.6.0.4
PATCH 2/6 nfsd: move fsid_type choice out of fh_compose
More trivial cleanup.
--- fs/nfsd/nfsfh.c | 77 +++++++++++++++++++++++++++++------------------------- 1 files changed, 41 insertions(+), 36 deletions(-)
fs/nfsd/nfsfh.c +41 -36
@@ -431,43 +431,17 @@ static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)return 1 ; } -__be32 -fh_compose (struct svc_fh *fhp,struct svc_export *exp,struct dentry *dentry, -struct svc_fh *ref_fh) -{ -/* ref_fh is a reference file handle. -* if it is non-null and for the same filesystem, then we should compose -* a filehandle which is of the same version, where possible. -* Currently, that means that if ref_fh->fh_handle.fh_version == 0xca -* Then create a 32byte filehandle using nfs_fhbase_old -* -*/ +static void set_version_and_fsid_type (struct svc_fh *fhp,struct svc_export *exp,struct svc_fh *ref_fh) +{u8 version; -u8 fsid_type =0 ; -struct inode * inode = dentry->d_inode; -struct dentry *parent = dentry->d_parent; -__u32 *datap; -dev_t ex_dev =exp_sb (exp)->s_dev; - -dprintk ("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n" , -MAJOR (ex_dev),MINOR (ex_dev), - (long ) exp->ex_path.dentry->d_inode->i_ino, - parent->d_name.name, dentry->d_name.name, - (inode ? inode->i_ino :0 )); - -/* Choose filehandle version and fsid type based on -* the reference filehandle (if it is in the same export) -* or the export options. -*/ - retry: +u8 fsid_type; +retry: version =1 ;if (ref_fh && ref_fh->fh_export == exp) { version = ref_fh->fh_handle.fh_version; fsid_type = ref_fh->fh_handle.fh_fsid_type; -if (ref_fh == fhp) -fh_put (ref_fh); ref_fh =NULL ;switch (version) {
@@ -502,11 +476,44 @@ fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,else fsid_type =FSID_UUID4_INUM ; } - }else if (!old_valid_dev (ex_dev)) + }else if (!old_valid_dev (exp_sb (exp)->s_dev))/* for newer device numbers, we must use a newer fsid format */ fsid_type =FSID_ENCODE_DEV ;else fsid_type =FSID_DEV ; + fhp->fh_handle.fh_version = version; +if (version) + fhp->fh_handle.fh_fsid_type = fsid_type; +} + +__be32 +fh_compose (struct svc_fh *fhp,struct svc_export *exp,struct dentry *dentry, +struct svc_fh *ref_fh) +{ +/* ref_fh is a reference file handle. +* if it is non-null and for the same filesystem, then we should compose +* a filehandle which is of the same version, where possible. +* Currently, that means that if ref_fh->fh_handle.fh_version == 0xca +* Then create a 32byte filehandle using nfs_fhbase_old +* +*/ + +struct inode * inode = dentry->d_inode; +struct dentry *parent = dentry->d_parent; +__u32 *datap; +dev_t ex_dev =exp_sb (exp)->s_dev; + +dprintk ("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n" , +MAJOR (ex_dev),MINOR (ex_dev), + (long ) exp->ex_path.dentry->d_inode->i_ino, + parent->d_name.name, dentry->d_name.name, + (inode ? inode->i_ino :0 )); + +/* Choose filehandle version and fsid type based on +* the reference filehandle (if it is in the same export) +* or the export options. +*/ +set_version_and_fsid_type (fhp, exp, ref_fh);if (ref_fh == fhp)fh_put (ref_fh);
@@ -524,7 +531,7 @@ fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, fhp->fh_export = exp;cache_get (&exp->h); -if (version ==0xca ) { +if (fhp->fh_handle.fh_version ==0xca ) {/* old style filehandle please */ memset (&fhp->fh_handle.fh_base,0 ,NFS_FHSIZE ); fhp->fh_handle.fh_size =NFS_FHSIZE ;
@@ -538,15 +545,13 @@ fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,_fh_update_old (dentry, exp, &fhp->fh_handle); }else {int len; - fhp->fh_handle.fh_version =1 ; fhp->fh_handle.fh_auth_type =0 ; datap = fhp->fh_handle.fh_auth+0 ; - fhp->fh_handle.fh_fsid_type = fsid_type; -mk_fsid (fsid_type, datap, ex_dev, +mk_fsid (fhp->fh_handle.fh_fsid_type, datap, ex_dev, exp->ex_path.dentry->d_inode->i_ino, exp->ex_fsid, exp->ex_uuid); - len =key_len (fsid_type); + len =key_len (fhp->fh_handle.fh_fsid_type); datap += len/4 ; fhp->fh_handle.fh_size =4 + len;
Signature · 1 line
1.6.0.4
PATCH 3/6 NFSd: Fix filehandle leak in exp_pseudoroot() and nfsd4_path()
From: Trond Myklebust <[email protected]>
nfsd4_path() allocates a temporary filehandle and then fails to free it before the function exits, leaking reference counts to the dentry and export that it refers to.
Also, nfsd4_lookupp() puts the result of exp_pseudoroot() in a temporary filehandle which it releases on success of exp_pseudoroot() but not on failure; fix exp_pseudoroot to ensure that on failure it releases the filehandle before returning.
--- fs/nfsd/export.c | 2 ++ fs/nfsd/nfs4xdr.c | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-)
fs/nfsd/export.c +2 -0
@@ -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
@@ -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 · 1 line
1.6.0.4
PATCH 4/6 nfsd4: filehandle leak or error exit from fh_compose()
A number of callers (nfsd4_encode_fattr(), at least) don't bother to release the filehandle returned to fh_compose() if fh_compose() returns an error. So, modify fh_compose() to release the filehandle before returning an error.
--- fs/nfsd/nfsfh.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
fs/nfsd/nfsfh.c +3 -1
@@ -557,8 +557,10 @@ fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,if (inode)_fh_update (fhp, exp, dentry); -if (fhp->fh_handle.fh_fileid_type ==255 ) +if (fhp->fh_handle.fh_fileid_type ==255 ) { +fh_put (fhp);return nfserr_opnotsupp; + } }return 0 ;
Signature · 1 line
1.6.0.4
PATCH 5/6 nfsd: clean up readdirplus encoding
Make the return from compose_entry_fh() zero or an error, even thoguh the returned error isn't used, just to make the meaning of the return immediately obvious.
Move some repeated code out of main function into helper.
--- fs/nfsd/nfs3xdr.c | 72 +++++++++++++++++++++++++---------------------------- 1 files changed, 34 insertions(+), 38 deletions(-)
fs/nfsd/nfs3xdr.c +34 -38
@@ -814,17 +814,6 @@ encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,return p; } -static __be32 * -encode_entryplus_baggage (struct nfsd3_readdirres *cd,__be32 *p, -struct svc_fh *fhp) -{ - p =encode_post_op_attr (cd->rqstp, p, fhp); - *p++ = xdr_one;/* yes, a file handle follows */ - p =encode_fh (p, fhp); -fh_put (fhp); -return p; -} -static int compose_entry_fh (struct nfsd3_readdirres *cd,struct svc_fh *fhp,const char *name,int namlen)
@@ -843,22 +832,46 @@ compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,if (dchild == dparent) {/* filesystem root - cannot return filehandle for ".." */ dput (dchild); -return 1 ; +return -ENOENT ; } }else dchild =dget (dparent); }else dchild =lookup_one_len (name, dparent, namlen);if (IS_ERR (dchild)) -return 1 ; -if (d_mountpoint (dchild) || -fh_compose (fhp, exp, dchild, &cd->fh) !=0 || - !dchild->d_inode) - rv =1 ; +return -ENOENT ; + rv = -ENOENT ; +if (d_mountpoint (dchild)) + goto out; + rv =fh_compose (fhp, exp, dchild, &cd->fh); +if (rv) + goto out; +if (!dchild->d_inode) + goto out; + rv =0 ; +out:dput (dchild);return rv; } +__be32 *encode_entryplus_baggage (struct nfsd3_readdirres *cd,__be32 *p,const char *name,int namlen) +{ +struct svc_fh fh; +int err; + + err =compose_entry_fh (cd, &fh, name, namlen); +if (err) { + *p++ =0 ; + *p++ =0 ; +return p; + } + p =encode_post_op_attr (cd->rqstp, p, &fh); + *p++ = xdr_one;/* yes, a file handle follows */ + p =encode_fh (p, &fh); +fh_put (&fh); +return p; +} +/* * Encode a directory entry. This one works for both normal readdir * and readdirplus.
@@ -929,16 +942,8 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen, p =encode_entry_baggage (cd, p, name, namlen, ino); -/* throw in readdirplus baggage */ -if (plus) { -struct svc_fh fh; - -if (compose_entry_fh (cd, &fh, name, namlen) >0 ) { - *p++ =0 ; - *p++ =0 ; - }else - p =encode_entryplus_baggage (cd, p, &fh); - } +if (plus) + p =encode_entryplus_baggage (cd, p, name, namlen); num_entry_words = p - cd->buffer; }else if (cd->rqstp->rq_respages[pn+1 ] !=NULL ) {/* temporarily encode entry into next page, then move back to
@@ -951,17 +956,8 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen, p1 =encode_entry_baggage (cd, p1, name, namlen, ino); -/* throw in readdirplus baggage */ -if (plus) { -struct svc_fh fh; - -if (compose_entry_fh (cd, &fh, name, namlen) >0 ) { -/* zero out the filehandle */ - *p1++ =0 ; - *p1++ =0 ; - }else - p1 =encode_entryplus_baggage (cd, p1, &fh); - } +if (plus) + p =encode_entryplus_baggage (cd, p1, name, namlen);/* determine entry word length and lengths to go in pages */ num_entry_words = p1 - tmp;
Signature · 1 line
1.6.0.4
PATCH 6/6 nfsd: fix leak on error in nfsv3 readdir
Note the !dchild->d_inode case can leak the filehandle.
--- fs/nfsd/nfs3xdr.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
fs/nfsd/nfs3xdr.c +3 -2
@@ -825,7 +825,6 @@ compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp, dparent = cd->fh.fh_dentry; exp = cd->fh.fh_export; -fh_init (fhp,NFS3_FHSIZE );if (isdotent (name, namlen)) {if (namlen ==2 ) { dchild =dget_parent (dparent);
@@ -859,15 +858,17 @@ __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const cstruct svc_fh fh;int err; +fh_init (&fh,NFS3_FHSIZE ); err =compose_entry_fh (cd, &fh, name, namlen);if (err) { *p++ =0 ; *p++ =0 ; -return p; + goto out; } p =encode_post_op_attr (cd->rqstp, p, &fh); *p++ = xdr_one;/* yes, a file handle follows */ p =encode_fh (p, &fh); +out:fh_put (&fh);return p; }
Signature · 1 line
1.6.0.4