nfsd/nfsd3_proc_readdir: check before encoding on temporary page
Subsystems (2)
PATCH 1/1 nfsd/nfsd3_proc_readdir: check before encoding on temporary page
After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger than PAGE_SIZE. So if the request and the directory are large enough, we can run out of pages in rq_respages. Then the temporary page we are encoding on is a random page, Oops happen.
Listing a directory with 30000 files in it can trigger the panic.
Fixing this by ensuring the temporary page resides in rq_respages. And when counting how many bytes left currently from buffer to the end of the page which buffer is pointing to, aka len1, do not assume that curr_page_addr is at the beginning of the same page. Also, update resp->count by going through all pages. Because the pages may not be sequential, the old way is not safe.
--- fs/nfsd/nfs3proc.c | 17 +++++++++++++++-- fs/nfsd/nfs3xdr.c | 8 +++++--- 2 files changed, 20 insertions(+), 5 deletions(-)
fs/nfsd/nfs3proc.c +15 -2
@@ -442,7 +442,9 @@ nfsd3_proc_readdir(struct svc_rqst *rqstp)struct nfsd3_readdirargs *argp = rqstp->rq_argp;struct nfsd3_readdirres *resp = rqstp->rq_resp;__be32 nfserr; -int count; +int count =0 ; +struct page **p; +caddr_t page_addr =NULL ; dprintk("nfsd: READDIR(3) %s %d bytes at %d\n", SVCFH_fmt(&argp->fh),
@@ -462,7 +464,18 @@ nfsd3_proc_readdir(struct svc_rqst *rqstp) nfserr =nfsd_readdir (rqstp, &resp->fh, (loff_t *) &argp->cookie, &resp->common, nfs3svc_encode_entry);memcpy (resp->verf, argp->verf,8 ); - resp->count = resp->buffer - argp->buffer; + count =0 ; +for (p = rqstp->rq_respages +1 ; p < rqstp->rq_next_page; p++) { + page_addr =page_address (*p); + +if (((caddr_t )resp->buffer >= page_addr) && + ((caddr_t )resp->buffer < page_addr +PAGE_SIZE )) { + count += (caddr_t )resp->buffer - page_addr; +break ; + } + count +=PAGE_SIZE ; + } + resp->count = count >>2 ;if (resp->offset) {loff_t offset = argp->cookie;
fs/nfsd/nfs3xdr.c +5 -3
@@ -953,6 +953,7 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,break ; } +if ((caddr_t)(cd->buffer + elen) < (curr_page_addr +PAGE_SIZE )) {/* encode entry in current page */
@@ -961,11 +962,12 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,if (plus) p =encode_entryplus_baggage (cd, p, name, namlen, ino); num_entry_words = p - cd->buffer; - }else if (*(page+1 ) !=NULL ) { + }else if (*(page+1 ) !=NULL && (page+1 ) < cd->rqstp->rq_next_page) {/* temporarily encode entry into next page, then move back to * current and next page in rq_respages[] */ __be32 *p1, *tmp;int len1, len2; +caddr_t tmp_page_addr =NULL ;/* grab next page for temporary storage of entry */ p1 = tmp =page_address (*(page+1 ));
@@ -977,7 +979,8 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,/* determine entry word length and lengths to go in pages */ num_entry_words = p1 - tmp; - len1 = curr_page_addr +PAGE_SIZE - (caddr_t )cd->buffer; + tmp_page_addr = (caddr_t)((unsigned long )cd->buffer &PAGE_MASK ); + len1 = tmp_page_addr +PAGE_SIZE - (caddr_t )cd->buffer;if ((num_entry_words <<2 ) < len1) {/* the actual number of words in the entry is less * than elen and can still fit in the current page
@@ -1026,7 +1029,6 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen, cd->buffer = p; cd->common.err = nfs_ok;return 0 ; - }int
Signature · 1 line
2.21.0
Quoted 16 lines · Murphy Zhou
After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger thanPAGE_SIZE. So if the request and the directory are large enough, we can run out of pages inrq_respages. Then the temporary page we are encoding on is a random page, Oops happen.Listing a directory with 30000 files in it can trigger the panic.
Fixing this by ensuring the temporary page resides in
rq_respages. And when counting how many bytes left currently from buffer to the end of the page which buffer is pointing to, aka len1, do not assume thatcurr_page_addris at the beginning of the same page. Also, updateresp->countby going through all pages. Because the pages may not be sequential, the old way is not safe.
Quoted 2 lines
Fixes f875a79 "nfsd: allow nfsv3 readdir request to be larger"Signed-off-by Murphy Zhou <[email protected]>
Hi,
thanks for finding these problems and submitting a patch. Clearly I should have tested better :-(
Quoted 29 lines
--- fs/nfsd/nfs3proc.c | 17 +++++++++++++++-- fs/nfsd/nfs3xdr.c | 8 +++++--- 2 files changed, 20 insertions(+), 5 deletions(-)fs/nfsd/nfs3proc.c +7
-2@@ -442,7 +442,9 @@ nfsd3_proc_readdir(struct svc_rqst *rqstp)struct nfsd3_readdirargs *argp = rqstp->rq_argp;struct nfsd3_readdirres *resp = rqstp->rq_resp;__be32 nfserr; -int count; +int count =0 ; +struct page **p; +caddr_t page_addr =NULL ; dprintk("nfsd: READDIR(3) %s %d bytes at %d\n", SVCFH_fmt(&argp->fh),@@ -462,7 +464,18 @@ nfsd3_proc_readdir(struct svc_rqst *rqstp) nfserr =nfsd_readdir (rqstp, &resp->fh, (loff_t *) &argp->cookie, &resp->common, nfs3svc_encode_entry);memcpy (resp->verf, argp->verf,8 ); - resp->count = resp->buffer - argp->buffer; + count =0 ; +for (p = rqstp->rq_respages +1 ; p < rqstp->rq_next_page; p++) { + page_addr =page_address (*p); +
fs/nfsd/nfs3proc.c nfsd3_proc_readdir(struct svc_rqst *rqstp) · line 480
Quoted 10 lines
fs/nfsd/nfs3proc.c +8
-0+if (((caddr_t )resp->buffer >= page_addr) && + ((caddr_t )resp->buffer < page_addr +PAGE_SIZE )) { + count += (caddr_t )resp->buffer - page_addr; +break ; + } + count +=PAGE_SIZE ; + } + resp->count = count >>2 ;if (resp->offset) {loff_t offset = argp->cookie;
This section makes perfect sense. You have copied code from nfsd3_proc_readdirplus() int nfsd3_proc_readdir(). This is needed because readdir doesn't limit replies to PAGE_SIZE any more.
fs/nfsd/nfs3xdr.c encode_entry(struct readdir_cd *ccd, const char *name, int namlen, · line 956
Quoted 10 lines
fs/nfsd/nfs3xdr.c +1
-0@@ -953,6 +953,7 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,break ; } +
Adding blank lines is best avoided.
fs/nfsd/nfs3xdr.c encode_entry(struct readdir_cd *ccd, const char *name, int namlen, · line 965
Quoted 9 lines
fs/nfsd/nfs3xdr.c +1
-1if ((caddr_t)(cd->buffer + elen) < (curr_page_addr +PAGE_SIZE )) {/* encode entry in current page */ @@ -961,11 +962,12 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,if (plus) p =encode_entryplus_baggage (cd, p, name, namlen, ino); num_entry_words = p - cd->buffer; - }else if (*(page+1 ) !=NULL ) { + }else if (*(page+1 ) !=NULL && (page+1 ) < cd->rqstp->rq_next_page) {
Adding the test against rq_next_page looks correct. nfs3svc_decode_readdirplusargs increments ->rq_next_page over enough pages to cover the request count.
However nfs4svc_decode_readdirargs doesn't! So it will not advance rq_next_page properly. We need to fix that to make it like nfs3svc_decode_readdirplusargs().
But I don't think this test should be needed. svc_alloc_arg() NULL- terminates the ->rq_pages array which ->rq_next_page points in to.
So I think this test is correct as it stands, butnfs4svc_decode_readdirargs() needs to be fixed.
Quoted 9 lines
fs/nfsd/nfs3xdr.c +1
-0/* temporarily encode entry into next page, then move back to * current and next page in rq_respages[] */ __be32 *p1, *tmp;int len1, len2; +caddr_t tmp_page_addr =NULL ;/* grab next page for temporary storage of entry */ p1 = tmp =page_address (*(page+1 ));
fs/nfsd/nfs3xdr.c encode_entry(struct readdir_cd *ccd, const char *name, int namlen, · line 983
Quoted 6 lines
fs/nfsd/nfs3xdr.c +2
-1@@ -977,7 +979,8 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,/* determine entry word length and lengths to go in pages */ num_entry_words = p1 - tmp; - len1 = curr_page_addr +PAGE_SIZE - (caddr_t )cd->buffer; + tmp_page_addr = (caddr_t)((unsigned long )cd->buffer &PAGE_MASK ); + len1 = tmp_page_addr +PAGE_SIZE - (caddr_t )cd->buffer;
I think tmp_page_addr will always be the same as curr_page_addr. At least, it will when nfs4svc_decode_readdirargs() is fixed.
Could you please revert the changed you've made to nfs3xdr.c (keeping the change to nfsd3_proc_readdir()), and fix nfs4svc_decode_readdirargs to be more like nfs4svc_decode_readdirplusargs, and see if that fixed the problem?
Thanks a lot,
NeilBrown
Quoted 13 lines
fs/nfsd/nfs3xdr.c +0
-1if ((num_entry_words <<2 ) < len1) {/* the actual number of words in the entry is less * than elen and can still fit in the current page @@ -1026,7 +1029,6 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen, cd->buffer = p; cd->common.err = nfs_ok;return 0 ; - }int Signature · 1 line
2.21.0
Hi,
Thanks for the review!
Quoted 100 lines · NeilBrown
Quoted 16 lines · Murphy Zhou
After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger thanPAGE_SIZE. So if the request and the directory are large enough, we can run out of pages inrq_respages. Then the temporary page we are encoding on is a random page, Oops happen.Listing a directory with 30000 files in it can trigger the panic.
Fixing this by ensuring the temporary page resides in
rq_respages. And when counting how many bytes left currently from buffer to the end of the page which buffer is pointing to, aka len1, do not assume thatcurr_page_addris at the beginning of the same page. Also, updateresp->countby going through all pages. Because the pages may not be sequential, the old way is not safe.Quoted 2 lines
Fixes f875a79 "nfsd: allow nfsv3 readdir request to be larger"Signed-off-by Murphy Zhou <[email protected]>Hi,
thanks for finding these problems and submitting a patch. Clearly I should have tested better :-(Quoted 29 lines
--- fs/nfsd/nfs3proc.c | 17 +++++++++++++++-- fs/nfsd/nfs3xdr.c | 8 +++++--- 2 files changed, 20 insertions(+), 5 deletions(-)fs/nfsd/nfs3proc.c +7
-2@@ -442,7 +442,9 @@ nfsd3_proc_readdir(struct svc_rqst *rqstp)struct nfsd3_readdirargs *argp = rqstp->rq_argp;struct nfsd3_readdirres *resp = rqstp->rq_resp;__be32 nfserr; -int count; +int count =0 ; +struct page **p; +caddr_t page_addr =NULL ; dprintk("nfsd: READDIR(3) %s %d bytes at %d\n", SVCFH_fmt(&argp->fh),@@ -462,7 +464,18 @@ nfsd3_proc_readdir(struct svc_rqst *rqstp) nfserr =nfsd_readdir (rqstp, &resp->fh, (loff_t *) &argp->cookie, &resp->common, nfs3svc_encode_entry);memcpy (resp->verf, argp->verf,8 ); - resp->count = resp->buffer - argp->buffer; + count =0 ; +for (p = rqstp->rq_respages +1 ; p < rqstp->rq_next_page; p++) { + page_addr =page_address (*p); +fs/nfsd/nfs3proc.c nfsd3_proc_readdir(struct svc_rqst *rqstp) · line 480
Quoted 10 lines
fs/nfsd/nfs3proc.c +8
-0+if (((caddr_t )resp->buffer >= page_addr) && + ((caddr_t )resp->buffer < page_addr +PAGE_SIZE )) { + count += (caddr_t )resp->buffer - page_addr; +break ; + } + count +=PAGE_SIZE ; + } + resp->count = count >>2 ;if (resp->offset) {loff_t offset = argp->cookie;This section makes perfect sense. You have copied code from
nfsd3_proc_readdirplus()intnfsd3_proc_readdir(). This is needed because readdir doesn't limit replies toPAGE_SIZEany more.fs/nfsd/nfs3xdr.c encode_entry(struct readdir_cd *ccd, const char *name, int namlen, · line 956
Quoted 10 lines
fs/nfsd/nfs3xdr.c +1
-0@@ -953,6 +953,7 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,break ; } +Adding blank lines is best avoided.
fs/nfsd/nfs3xdr.c encode_entry(struct readdir_cd *ccd, const char *name, int namlen, · line 965
Quoted 9 lines
fs/nfsd/nfs3xdr.c +1
-1if ((caddr_t)(cd->buffer + elen) < (curr_page_addr +PAGE_SIZE )) {/* encode entry in current page */ @@ -961,11 +962,12 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,if (plus) p =encode_entryplus_baggage (cd, p, name, namlen, ino); num_entry_words = p - cd->buffer; - }else if (*(page+1 ) !=NULL ) { + }else if (*(page+1 ) !=NULL && (page+1 ) < cd->rqstp->rq_next_page) {Adding the test against
rq_next_pagelooks correct.nfs3svc_decode_readdirplusargsincrements ->rq_next_page over enough pages to cover the request count.
Howevernfs4svc_decode_readdirargsdoesn't! So it will not advancerq_next_pageproperly. We need to fix that to make it likenfs3svc_decode_readdirplusargs().
Quoted 5 lines
But I don't think this test should be needed.
svc_alloc_arg()NULL- terminates the ->rq_pages array which ->rq_next_page points in to.So I think this test is correct as it stands, but
nfs4svc_decode_readdirargs()needs to be fixed.
Agreed. If we set the page pointers right. These checks wont be necessary.
Quoted 20 lines
Quoted 10 lines
fs/nfsd/nfs3xdr.c +1
-0/* temporarily encode entry into next page, then move back to * current and next page in rq_respages[] */ __be32 *p1, *tmp;int len1, len2; +caddr_t tmp_page_addr =NULL ;/* grab next page for temporary storage of entry */ p1 = tmp =page_address (*(page+1 ));fs/nfsd/nfs3xdr.c encode_entry(struct readdir_cd *ccd, const char *name, int namlen, · line 983
Quoted 6 lines
fs/nfsd/nfs3xdr.c +2
-1@@ -977,7 +979,8 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen,/* determine entry word length and lengths to go in pages */ num_entry_words = p1 - tmp; - len1 = curr_page_addr +PAGE_SIZE - (caddr_t )cd->buffer; + tmp_page_addr = (caddr_t)((unsigned long )cd->buffer &PAGE_MASK ); + len1 = tmp_page_addr +PAGE_SIZE - (caddr_t )cd->buffer;I think
tmp_page_addrwill always be the same ascurr_page_addr. At least, it will whennfs4svc_decode_readdirargs()is fixed.
Quoted 4 lines
Could you please revert the changed you've made to
nfs3xdr.c(keeping the change tonfsd3_proc_readdir()), and fixnfs4svc_decode_readdirargsto be more likenfs4svc_decode_readdirplusargs, and see if that fixed the problem?
Sure. A quick test on 3000 files passed. I'll post v2 if test on 30,000 files pass.
Thanks very much for reviewing!
M
Quoted 18 lines
Thanks a lot,
NeilBrownQuoted 13 lines
fs/nfsd/nfs3xdr.c +0
-2if ((num_entry_words <<2 ) < len1) {/* the actual number of words in the entry is less * than elen and can still fit in the current page @@ -1026,7 +1029,6 @@ encode_entry(struct readdir_cd *ccd, const char *name, int namlen, cd->buffer = p; cd->common.err = nfs_ok;return 0 ; - }int --2.21.0