nfsd: don't replace page in rq_pages if it's a continuation of last page
Subsystems (3)
Jeff Layton
maintainerPATCH v2 1/2 nfsd: don't replace page in rq_pages if it's a continuation of last page
The splice read calls nfsd_splice_actor to put the pages containing file data into the svc_rqst->rq_pages array. It's possible however to get a splice result that only has a partial page at the end, if (e.g.) the filesystem hands back a short read that doesn't cover the whole page.
nfsd_splice_actor will plop the partial page into its rq_pages array and return. Then later, when nfsd_splice_actor is called again, the remainder of the page may end up being filled out. At this point, nfsd_splice_actor will put the page into the array _again_ corrupting the reply. If this is done enough times, rq_next_page will overrun the array and corrupt the trailing fields -- the rq_respages and rq_next_page pointers themselves.
If we've already added the page to the array in the last pass, don't add it to the array a second time when dealing with a splice continuation. This was originally handled properly in nfsd_splice_actor, but commit 91e23b1c3982 removed the check for it.
--- fs/nfsd/vfs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
fs/nfsd/vfs.c +8 -2
@@ -941,8 +941,14 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *last_page; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ; -for (page += offset /PAGE_SIZE ; page <= last_page; page++) -svc_rqst_replace_page (rqstp, page); +for (page += offset /PAGE_SIZE ; page <= last_page; page++) { +/* +* Skip page replacement when extending the contents +* of the current page. +*/ +if (page != *(rqstp->rq_next_page -1 )) +svc_rqst_replace_page (rqstp, page); + }if (rqstp->rq_res.page_len ==0 )// first call rqstp->rq_res.page_base = offset %PAGE_SIZE ; rqstp->rq_res.page_len += sd->len;
Signature · 1 line
2.39.2
Jeff Layton
maintainerPATCH v2 2/2 sunrpc: add bounds checking to svc_rqst_replace_page
If rq_next_page ends up pointing outside the array, then we can corrupt memory when we go to change its value. Ensure that it hasn't strayed outside the array, and have svc_rqst_replace_page return -EIO without changing anything if it has.
Fix up nfsd_splice_actor (the only caller) to handle this case by either returning an error or a short splice when this happens.
--- fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)
fs/nfsd/vfs.c +17 -6
@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *page = buf->page;// may be a compound one unsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset /PAGE_SIZE ; page <= last_page; page++) {
@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,* Skip page replacement when extending the contents * of the current page. */ -if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed +=min_t (int, +PAGE_SIZE -offset_in_page (offset), + sd->len - consumed); + offset =0 ; } -if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call + rqstp->rq_res.page_base = offset %PAGE_SIZE ; + rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret; } static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
include/linux/sunrpc/svc.h +1 -1
@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void svc_rqst_free (struct svc_rqst *);void svc_exit_thread (struct svc_rqst *);
net/sunrpc/svc.c +13 -1
@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the rq_respages +* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ]; +if (WARN_ON_ONCE (rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) +return -EIO ; +if (*rqstp->rq_next_page) {if (!pagevec_space (&rqstp->rq_pvec))__pagevec_release (&rqstp->rq_pvec);
@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);
Signature · 1 line
2.39.2
Jeff Layton
maintainerRe: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 40 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout changing anything if it has.Fix up
nfsd_splice_actor(the only caller) to handle this case by either returning an error or a short splice when this happens.Signed-off-by Jeff Layton <[email protected]>--- fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +8
-2@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *page = buf->page;// may be a compound one unsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset /PAGE_SIZE ; page <= last_page; page++) {@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,* Skip page replacement when extending the contents * of the current page. */ -if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed += min_t(int, + PAGE_SIZE - offset_in_page(offset),
fs/nfsd/vfs.c nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, · line 962
Quoted 10 lines
fs/nfsd/vfs.c +5
-4+ sd->len - consumed); + offset =0 ; } -if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call + rqstp->rq_res.page_base = offset %PAGE_SIZE ;
Oops, this will get the page_base wrong if the first splice covers multiple pages. I'll need to respin the offset handling.
Quoted 53 lines
fs/nfsd/vfs.c +4
-0+ rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret; } static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void svc_rqst_free (struct svc_rqst *);void svc_exit_thread (struct svc_rqst *);net/sunrpc/svc.c +13
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the rq_respages +* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ]; +if (WARN_ON_ONCE (rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) +return -EIO ; +if (*rqstp->rq_next_page) {if (!pagevec_space (&rqstp->rq_pvec))__pagevec_release (&rqstp->rq_pvec);@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
In reply to Jeff LaytonQuoted 10 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout changing anything if it has.Fix up
nfsd_splice_actor(the only caller) to handle this case by either returning an error or a short splice when this happens.
IMO it's not worth the extra complexity to return a short splice. This is a "should never happen" scenario in a hot I/O path. Let's keep this code as simple as possible, and use unlikely() for the error cases in both nfsd_splice_actor and svc_rqst_replace_page().
Also, since "nfsd_splice_actor ... [is] the only caller", a WARN_ON stack trace is not adding value. I still think a tracepoint is more appropriate. I suggest:
trace_svc_replace_page_err(rqst);
Quoted 98 lines
Signed-off-by Jeff Layton <[email protected]>---
fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +17
-6@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *page = buf->page;// may be a compound one unsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset /PAGE_SIZE ; page <= last_page; page++) {@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,* Skip page replacement when extending the contents * of the current page. */ -if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed +=min_t (int, +PAGE_SIZE -offset_in_page (offset), + sd->len - consumed); + offset =0 ; } -if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call + rqstp->rq_res.page_base = offset %PAGE_SIZE ; + rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret; } static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void svc_rqst_free (struct svc_rqst *);void svc_exit_thread (struct svc_rqst *);net/sunrpc/svc.c +13
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the rq_respages +* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ]; +if (WARN_ON_ONCE (rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) +return -EIO ; +if (*rqstp->rq_next_page) {if (!pagevec_space (&rqstp->rq_pvec))__pagevec_release (&rqstp->rq_pvec);@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);Signature · 1 line
2.39.2
--
Chuck Lever
Jeff Layton
maintainerRe: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 9 lines · Chuck Lever III
Quoted 9 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt
memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout
changing anything if it has.
Quoted 9 lines
Quoted 3 lines
Fix up
nfsd_splice_actor(the only caller) to handle this case by either
returning an error or a short splice when this happens.IMO it's not worth the extra complexity to return a short splice. This is a "should never happen" scenario in a hot I/O path. Let's keep this code as simple as possible, and use
unlikely()for the error cases in bothnfsd_splice_actorandsvc_rqst_replace_page().
Are there any issues with just returning an error even though we have successfully spliced some of the data into the buffer? I guess the caller will just see an EIO or whatever instead of the short read in that case?
Quoted 7 lines
Also, since "
nfsd_splice_actor... [is] the only caller", aWARN_ONstack trace is not adding value. I still think a tracepoint is more appropriate. I suggest:trace_svc_replace_page_err(rqst);
Ok, I can look at adding a conditional tracepoint.
Quoted 117 lines
Quoted 112 lines
Signed-off-by Jeff Layton <[email protected]>---
fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +1
-0@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe,struct pipe_buffer *buf, struct page *page = buf->page; // may be a compound oneunsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset / PAGE_SIZE; page <= last_page; page++){
fs/nfsd/vfs.c +0
-0@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, * Skip page replacement when extending thecontents
* of the current page. */fs/nfsd/vfs.c +7
-2-if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed += min_t(int, + PAGE_SIZE -
offset_in_page(offset),fs/nfsd/vfs.c +2
-0+ sd->len - consumed); + offset =0 ;}fs/nfsd/vfs.c +2
-4-if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call
fs/nfsd/vfs.c +1
-0+ rqstp->rq_res.page_base = offset %
PAGE_SIZE;fs/nfsd/vfs.c +4
-0+ rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret;}
static int nfsd_direct_splice_actor(struct
pipe_inode_info*pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void
svc_rqst_free(struct svc_rqst *);
voidsvc_exit_thread(struct svc_rqst *);net/sunrpc/svc.c +5
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the
rq_respagesnet/sunrpc/svc.c +5
-0+* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ]; + if (WARN_ON_ONCE(rqstp->rq_next_page < begin || rqstp-Quoted 1 line
rq_next_page> end))net/sunrpc/svc.c +2
-0+return -EIO ; +if (*rqstp->rq_next_page) { if (!pagevec_space(&rqstp->rq_pvec)) __pagevec_release(&rqstp->rq_pvec);net/sunrpc/svc.c +1
-0@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);Signature · 2 lines
-- 2.39.2--
Chuck Lever
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 24 lines · Jeff Layton
Quoted 9 lines · Chuck Lever III
Quoted 9 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt
memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout
changing anything if it has.Quoted 9 lines
Quoted 3 lines
Fix up
nfsd_splice_actor(the only caller) to handle this case by either
returning an error or a short splice when this happens.IMO it's not worth the extra complexity to return a short splice. This is a "should never happen" scenario in a hot I/O path. Let's keep this code as simple as possible, and use
unlikely()for the error cases in bothnfsd_splice_actorandsvc_rqst_replace_page().
Quoted 4 lines
Are there any issues with just returning an error even though we have successfully spliced some of the data into the buffer? I guess the caller will just see an EIO or whatever instead of the short read in that case?
NFSv4 READ is probably going to truncate the XDR buffer. I'm not sure NFSv3 is so clever, so you should test it.
Quoted 7 lines
Quoted 5 lines
Also, since "
nfsd_splice_actor... [is] the only caller", aWARN_ONstack trace is not adding value. I still think a tracepoint is more appropriate. I suggest:trace_svc_replace_page_err(rqst);Ok, I can look at adding a conditional tracepoint.
I thought about that: it doesn't help much, since you have to explicitly test anyway to see whether to return an error.
Quoted 109 lines
Quoted 109 lines
Quoted 109 lines
Signed-off-by Jeff Layton <[email protected]>---
fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +1
-0@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe,struct pipe_buffer *buf, struct page *page = buf->page; // may be a compound oneunsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset / PAGE_SIZE; page <= last_page; page++){
fs/nfsd/vfs.c +0
-0@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, * Skip page replacement when extending thecontents
* of the current page. */fs/nfsd/vfs.c +7
-2-if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed += min_t(int, + PAGE_SIZE -
offset_in_page(offset),fs/nfsd/vfs.c +2
-0+ sd->len - consumed); + offset =0 ;}fs/nfsd/vfs.c +2
-4-if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call
fs/nfsd/vfs.c +1
-0+ rqstp->rq_res.page_base = offset %
PAGE_SIZE;fs/nfsd/vfs.c +4
-0+ rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret;}
static int nfsd_direct_splice_actor(struct
pipe_inode_info*pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void
svc_rqst_free(struct svc_rqst *);
voidsvc_exit_thread(struct svc_rqst *);net/sunrpc/svc.c +5
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the
rq_respagesnet/sunrpc/svc.c +5
-0+* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ]; + if (WARN_ON_ONCE(rqstp->rq_next_page < begin || rqstp-Quoted 1 line
rq_next_page> end))net/sunrpc/svc.c +2
-0+return -EIO ; +if (*rqstp->rq_next_page) { if (!pagevec_space(&rqstp->rq_pvec)) __pagevec_release(&rqstp->rq_pvec);net/sunrpc/svc.c +1
-0@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);
Quoted 11 lines
Quoted 8 lines
Quoted 3 lines
Signature · 1 line
2.39.2--
Chuck LeverSignature · 1 line
Jeff Layton <[email protected]>
--
Chuck Lever
Jeff Layton
maintainerRe: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 23 lines · Chuck Lever III
Quoted 23 lines · Jeff Layton
Quoted 18 lines · Chuck Lever III
Quoted 9 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt
memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout
changing anything if it has.Quoted 3 lines
Fix up
nfsd_splice_actor(the only caller) to handle this case by either
returning an error or a short splice when this happens.IMO it's not worth the extra complexity to return a short splice. This is a "should never happen" scenario in a hot I/O path. Let's keep this code as simple as possible, and use
unlikely()for the error cases in bothnfsd_splice_actorandsvc_rqst_replace_page().
Quoted 8 lines
Quoted 4 lines
Are there any issues with just returning an error even though we have successfully spliced some of the data into the buffer? I guess the caller will just see an EIO or whatever instead of the short read in that case?
NFSv4 READ is probably going to truncate the XDR buffer. I'm not sure NFSv3 is so clever, so you should test it.
Honestly, I don't have the cycles to do that sort of fault injection testing for this. If you think handling it as a short read is overblown, then tell me what you would like see here.
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Also, since "
nfsd_splice_actor... [is] the only caller", aWARN_ONstack trace is not adding value. I still think a tracepoint is more appropriate. I suggest:trace_svc_replace_page_err(rqst);Ok, I can look at adding a conditional tracepoint.
I thought about that: it doesn't help much, since you have to explicitly test anyway to see whether to return an error.
Fair point. I can just make it a regular tracepoint that fires inside the conditional.
Quoted 126 lines
Quoted 110 lines
Quoted 110 lines
Quoted 110 lines
Signed-off-by Jeff Layton <[email protected]>---
fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +1
-0@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe,struct pipe_buffer *buf, struct page *page = buf->page; // may be a compound oneunsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset / PAGE_SIZE; page <= last_page; page++){
fs/nfsd/vfs.c +0
-0@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, * Skip page replacement when extending thecontents
* of the current page. */fs/nfsd/vfs.c +7
-2-if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed += min_t(int, + PAGE_SIZE -
offset_in_page(offset),fs/nfsd/vfs.c +2
-0+ sd->len - consumed); + offset =0 ;}fs/nfsd/vfs.c +2
-4-if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call
fs/nfsd/vfs.c +1
-0+ rqstp->rq_res.page_base = offset %
PAGE_SIZE;fs/nfsd/vfs.c +4
-0+ rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret;}
static int nfsd_direct_splice_actor(struct
pipe_inode_info*pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void
svc_rqst_free(struct svc_rqst *);
voidsvc_exit_thread(struct svc_rqst *);net/sunrpc/svc.c +5
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the
rq_respagesnet/sunrpc/svc.c +5
-0+* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ]; + if (WARN_ON_ONCE(rqstp->rq_next_page < begin || rqstp-Quoted 1 line
rq_next_page> end))net/sunrpc/svc.c +2
-0+return -EIO ; +if (*rqstp->rq_next_page) { if (!pagevec_space(&rqstp->rq_pvec)) __pagevec_release(&rqstp->rq_pvec);net/sunrpc/svc.c +1
-0@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);Quoted 11 lines
Quoted 8 lines
Quoted 3 lines
Signature · 1 line
2.39.2--
Chuck LeverSignature · 1 line
Jeff Layton <[email protected]>--
Chuck Lever
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 33 lines · Jeff Layton
Quoted 28 lines · Chuck Lever III
Quoted 27 lines · Jeff Layton
Quoted 9 lines · Chuck Lever III
Quoted 9 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt
memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout
changing anything if it has.Quoted 9 lines
Quoted 3 lines
Fix up
nfsd_splice_actor(the only caller) to handle this case by either
returning an error or a short splice when this happens.IMO it's not worth the extra complexity to return a short splice. This is a "should never happen" scenario in a hot I/O path. Let's keep this code as simple as possible, and use
unlikely()for the error cases in bothnfsd_splice_actorandsvc_rqst_replace_page().Are there any issues with just returning an error even though we have successfully spliced some of the data into the buffer? I guess the caller will just see an EIO or whatever instead of the short read in that case?
Quoted 5 lines
Quoted 2 lines
NFSv4 READ is probably going to truncate the XDR buffer. I'm not sure NFSv3 is so clever, so you should test it.
Honestly, I don't have the cycles to do that sort of fault injection testing for this.
nfsd_splice_actor() has never returned an error, so IMO it is necessary to confirm that when svc_rqst_replace_page() returns an error, it doesn't create further problems. I don't see how we can avoid some kind of simple fault injection while developing the fix.
Tell you what, I can take it from here if you'd like.
Quoted 2 lines
If you think handling it as a short read is overblown, then tell me what you would like see here.
It's not the short reads that bugs me, it's the additional code in a hot path that is worrisome.
--
Chuck Lever
Jeff Layton
maintainerRe: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 38 lines · Chuck Lever III
Quoted 32 lines · Jeff Layton
Quoted 28 lines · Chuck Lever III
Quoted 27 lines · Jeff Layton
Quoted 9 lines · Chuck Lever III
Quoted 9 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt
memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout
changing anything if it has.Quoted 9 lines
Quoted 3 lines
Fix up
nfsd_splice_actor(the only caller) to handle this case by either
returning an error or a short splice when this happens.IMO it's not worth the extra complexity to return a short splice. This is a "should never happen" scenario in a hot I/O path. Let's keep this code as simple as possible, and use
unlikely()for the error cases in bothnfsd_splice_actorandsvc_rqst_replace_page().Are there any issues with just returning an error even though we have successfully spliced some of the data into the buffer? I guess the caller will just see an EIO or whatever instead of the short read in that case?
Quoted 5 lines
Quoted 2 lines
NFSv4 READ is probably going to truncate the XDR buffer. I'm not sure NFSv3 is so clever, so you should test it.
Honestly, I don't have the cycles to do that sort of fault injection testing for this.
Quoted 8 lines
nfsd_splice_actor()has never returned an error, so IMO it is necessary to confirm that whensvc_rqst_replace_page()returns an error, it doesn't create further problems. I don't see how we can avoid some kind of simple fault injection while developing the fix.Tell you what, I can take it from here if you'd like.
Sure! All yours!
Quoted 7 lines
Quoted 3 lines
If you think handling it as a short read is overblown, then tell me what you would like see here.
It's not the short reads that bugs me, it's the additional code in a hot path that is worrisome.
I wouldn't think tracking some extra stuff on the stack would show up much in profiles, but it's your call.
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
In reply to Jeff LaytonQuoted 83 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout changing anything if it has.Fix up
nfsd_splice_actor(the only caller) to handle this case by either returning an error or a short splice when this happens.Signed-off-by Jeff Layton <[email protected]>---
fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +17
-6@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *page = buf->page;// may be a compound one unsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset /PAGE_SIZE ; page <= last_page; page++) {@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,* Skip page replacement when extending the contents * of the current page. */ -if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed +=min_t (int, +PAGE_SIZE -offset_in_page (offset), + sd->len - consumed); + offset =0 ; } -if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call + rqstp->rq_res.page_base = offset %PAGE_SIZE ; + rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret; } static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void svc_rqst_free (struct svc_rqst *);void svc_exit_thread (struct svc_rqst *);net/sunrpc/svc.c +0
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page)
net/sunrpc/svc.c EXPORT_SYMBOL_GPL(svc_set_num_threads); · line 855
Quoted 10 lines
net/sunrpc/svc.c +9
-0+int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; + +/* +* Bounds check: make sure rq_next_page points into the rq_respages +* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ];
Would it make sense to use rq_page_end here? Just a thought.
Quoted 17 lines
net/sunrpc/svc.c +4
-0+if (WARN_ON_ONCE (rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) +return -EIO ; +if (*rqstp->rq_next_page) {if (!pagevec_space (&rqstp->rq_pvec))__pagevec_release (&rqstp->rq_pvec);@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);Signature · 1 line
2.39.2
--
Chuck Lever
Jeff Layton
maintainerRe: [PATCH v2 2/2] sunrpc: add bounds checking to svc_rqst_replace_page
Quoted 86 lines · Chuck Lever III
Quoted 86 lines · Jeff Layton
If
rq_next_pageends up pointing outside the array, then we can corrupt memory when we go to change its value. Ensure that it hasn't strayed outside the array, and havesvc_rqst_replace_pagereturn-EIOwithout changing anything if it has.Fix up
nfsd_splice_actor(the only caller) to handle this case by either returning an error or a short splice when this happens.Signed-off-by Jeff Layton <[email protected]>---
fs/nfsd/vfs.c | 23 +++++++++++++++++------ include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-)fs/nfsd/vfs.c +17
-6@@ -939,6 +939,7 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *page = buf->page;// may be a compound one unsigned offset = buf->offset;struct page *last_page; +int ret =0 , consumed =0 ; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ;for (page += offset /PAGE_SIZE ; page <= last_page; page++) {@@ -946,13 +947,23 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,* Skip page replacement when extending the contents * of the current page. */ -if (page != *(rqstp->rq_next_page -1 )) -svc_rqst_replace_page (rqstp, page); +if (page != *(rqstp->rq_next_page -1 )) { + ret =svc_rqst_replace_page (rqstp, page); +if (ret) +break ; + } + consumed +=min_t (int, +PAGE_SIZE -offset_in_page (offset), + sd->len - consumed); + offset =0 ; } -if (rqstp->rq_res.page_len ==0 )// first call - rqstp->rq_res.page_base = offset %PAGE_SIZE ; - rqstp->rq_res.page_len += sd->len; -return sd->len; +if (consumed) { +if (rqstp->rq_res.page_len ==0 )// first call + rqstp->rq_res.page_base = offset %PAGE_SIZE ; + rqstp->rq_res.page_len += consumed; +return consumed; + } +return ret; } static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,include/linux/sunrpc/svc.h +1
-1@@ -422,7 +422,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int, int (*threadfn)(void *data));struct svc_rqst *svc_rqst_alloc (struct svc_serv *serv,struct svc_pool *pool,int node); -void svc_rqst_replace_page (struct svc_rqst *rqstp, +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void svc_rqst_free (struct svc_rqst *);void svc_exit_thread (struct svc_rqst *);net/sunrpc/svc.c +3
-1@@ -843,8 +843,19 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);* When replacing a page in rq_pages, batch the release of the * replaced pages to avoid hammering the page allocator. */ -void svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) +int svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page) { +struct page **begin, **end; +
Quoted 9 lines
net/sunrpc/svc.c EXPORT_SYMBOL_GPL(svc_set_num_threads); · line 855
Quoted 6 lines
net/sunrpc/svc.c +6
-0+/* +* Bounds check: make sure rq_next_page points into the rq_respages +* part of the array. +*/ + begin = rqstp->rq_pages; + end = &rqstp->rq_pages[RPCSVC_MAXPAGES ];Would it make sense to use
rq_page_endhere? Just a thought.
That should be set to the right value, but do you trust that it wouldn't be corrupt if rq_pages got overrun? It's only 3 pointers beyond the end of the array. This should work even if rq_page_end gets clobbered.
Quoted 23 lines
Quoted 18 lines
net/sunrpc/svc.c +4
-0+if (WARN_ON_ONCE (rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) +return -EIO ; +if (*rqstp->rq_next_page) {if (!pagevec_space (&rqstp->rq_pvec))__pagevec_release (&rqstp->rq_pvec);@@ -853,6 +864,7 @@ void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)get_page (page); *(rqstp->rq_next_page++) = page; +return 0 ; }EXPORT_SYMBOL_GPL (svc_rqst_replace_page);Signature · 1 line
2.39.2--
Chuck Lever
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 22 lines · Jeff Layton
The splice read calls
nfsd_splice_actorto put the pages containing file data into thesvc_rqst->rq_pagesarray. It's possible however to get a splice result that only has a partial page at the end, if (e.g.) the filesystem hands back a short read that doesn't cover the whole page.
nfsd_splice_actorwill plop the partial page into itsrq_pagesarray and return. Then later, whennfsd_splice_actoris called again, the remainder of the page may end up being filled out. At this point,nfsd_splice_actorwill put the page into the array _again_ corrupting the reply. If this is done enough times,rq_next_pagewill overrun the array and corrupt the trailing fields -- therq_respagesandrq_next_pagepointers themselves.If we've already added the page to the array in the last pass, don't add it to the array a second time when dealing with a splice continuation. This was originally handled properly in
nfsd_splice_actor, but commit91e23b1c3982removed the check for it.
Quoted 6 lines
Fixes 91e23b1c3982 ("NFSD: Clean up nfsd_splice_actor()")Cc Al Viro <[email protected]>Reported-by Dario Lesca <[email protected]>Tested-by David Critch <[email protected]>Signed-off-by Jeff Layton <[email protected]>
I've applied this one internally for due-diligence testing. I'll wait a couple of days for review comments, so it's not likely to make v6.3-rc3 on Sunday.
Quoted 28 lines
---
fs/nfsd/vfs.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)fs/nfsd/vfs.c +8
-2@@ -941,8 +941,14 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,struct page *last_page; last_page = page + (offset + sd->len -1 ) /PAGE_SIZE ; -for (page += offset /PAGE_SIZE ; page <= last_page; page++) -svc_rqst_replace_page (rqstp, page); +for (page += offset /PAGE_SIZE ; page <= last_page; page++) { +/* +* Skip page replacement when extending the contents +* of the current page. +*/ +if (page != *(rqstp->rq_next_page -1 )) +svc_rqst_replace_page (rqstp, page); + }if (rqstp->rq_res.page_len ==0 )// first call rqstp->rq_res.page_base = offset %PAGE_SIZE ; rqstp->rq_res.page_len += sd->len;Signature · 1 line
2.39.2
--
Chuck Lever