nfsd: store net in svc_serv instead of deriving it from sv_permsocks
Subsystems (4)
PATCH v3 1/1 nfsd: store net in svc_serv instead of deriving it from sv_permsocks
nfsd() derives its struct net from the first entry of serv->sv_permsocks using list_entry() without checking that the list is non-empty. If the list is empty, list_entry() returns a bogus svc_xprt computed from the list head embedded in struct svc_serv, and the following perm_sock->xpt_net load reads past the end of the svc_serv allocation:
BUG: KASAN: slab-out-of-bounds in nfsd+0x118/0xbc0 fs/nfsd/nfssvc.c:894 Read of size 8 at addr ffff88802d423f50 by task nfsd/5988
Rather than validating the list head before referencing it, store the net in the svc_serv when it is created. svc_create_pooled() is only used by nfsd, so add a struct net argument to it and record it in a new sv_net field, then use that in nfsd().
---
v3: reword commit message, fix stray whitespace in svc_create_pooled()
prototype (Neil)
v2: store the net in svc_serv via svc_create_pooled() instead of
walking sv_permsocks under sv_lock (Jeff, Neil)
--- fs/nfsd/nfssvc.c | 5 ++--- include/linux/sunrpc/svc.h | 2 ++ net/sunrpc/svc.c | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-)
fs/nfsd/nfssvc.c +2 -3
@@ -620,7 +620,7 @@ int nfsd_create_serv(struct net *net) nfsd_max_blksize =nfsd_get_default_max_blksize ();nfsd_reset_versions (nn); serv =svc_create_pooled (nfsd_programs,ARRAY_SIZE (nfsd_programs), - &nn->nfsd_svcstats, + &nn->nfsd_svcstats, net, nfsd_max_blksize, nfsd);if (serv ==NULL ) {percpu_ref_exit (&nn->nfsd_net_ref);
@@ -890,8 +890,7 @@ nfsd(void *vrqstp) {struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;struct svc_pool *pool = rqstp->rq_pool; - struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list); -struct net *net = perm_sock->xpt_net; +struct net *net = rqstp->rq_server->sv_net;struct nfsd_net *nn =net_generic (net, nfsd_net_id);struct nfsd_thread_local_info ntli = { };bool have_mutex = false;
include/linux/sunrpc/svc.h +2 -0
@@ -72,6 +72,7 @@ enum {struct svc_serv {struct svc_program * sv_programs;/* RPC programs */ struct svc_stat * sv_stats;/* RPC statistics */ +struct net *sv_net;/* only set for pooled (nfsd) services */ spinlock_t sv_lock;unsigned int sv_nprogs;/* Number of sv_programs */ unsigned int sv_nrthreads;/* # of running server threads */
@@ -461,6 +462,7 @@ void svc_exit_thread(struct svc_rqst *);struct svc_serv *svc_create_pooled (struct svc_program *prog,unsigned int nprog,struct svc_stat *stats, +struct net *net,unsigned int bufsize,int (*threadfn)(void *data)); int svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,
net/sunrpc/svc.c +3 -0
@@ -481,6 +481,7 @@ EXPORT_SYMBOL_GPL(svc_create);* @prog: Array of RPC programs the new service will handle * @nprogs: Number of programs in the array * @stats: the stats struct if desired +* @net: network namespace the service belongs to * @bufsize: maximum message size for @prog * @threadfn: a function to service RPC requests for @prog *
@@ -489,6 +490,7 @@ EXPORT_SYMBOL_GPL(svc_create);struct svc_serv *svc_create_pooled (struct svc_program *prog,unsigned int nprogs,struct svc_stat *stats, +struct net *net,unsigned int bufsize,int (*threadfn)(void *data)) {
@@ -502,6 +504,7 @@ struct svc_serv *svc_create_pooled(struct svc_program *prog,if (!serv) goto out_err; serv->sv_is_pooled = true; + serv->sv_net = net;return serv; out_err:svc_pool_map_put ();
Signature · 1 line
2.43.0
Quoted 97 lines · Deepanshu Kartikey
nfsd()derives its struct net from the first entry ofserv->sv_permsocksusinglist_entry()without checking that the list is non-empty. If the list is empty,list_entry()returns a bogussvc_xprtcomputed from the list head embedded in structsvc_serv, and the followingperm_sock->xpt_netload reads past the end of thesvc_servallocation:BUG: KASAN: slab-out-of-bounds in nfsd+0x118/0xbc0 fs/nfsd/nfssvc.c:894 Read of size 8 at addr ffff88802d423f50 by task nfsd/5988Rather than validating the list head before referencing it, store the net in the
svc_servwhen it is created.svc_create_pooled()is only used by nfsd, so add a struct net argument to it and record it in a newsv_netfield, then use that innfsd().Fixes 88c476661719 ("nfsd: pass proper net to nfsd_destroy() from NFSd kthreads")Reported-by [email protected]Suggested-by NeilBrown <[email protected]>Tested-by [email protected]Reviewed-by NeilBrown <[email protected]>Signed-off-by Deepanshu Kartikey <[email protected]>---
v3: reword commit message, fix stray whitespace insvc_create_pooled()
prototype (Neil)
v2: store the net insvc_servviasvc_create_pooled()instead of
walkingsv_permsocksundersv_lock(Jeff, Neil)--- fs/nfsd/nfssvc.c | 5 ++--- include/linux/sunrpc/svc.h | 2 ++ net/sunrpc/svc.c | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-)fs/nfsd/nfssvc.c +2
-3@@ -620,7 +620,7 @@ int nfsd_create_serv(struct net *net) nfsd_max_blksize =nfsd_get_default_max_blksize ();nfsd_reset_versions (nn); serv =svc_create_pooled (nfsd_programs,ARRAY_SIZE (nfsd_programs), - &nn->nfsd_svcstats, + &nn->nfsd_svcstats, net, nfsd_max_blksize, nfsd);if (serv ==NULL ) {percpu_ref_exit (&nn->nfsd_net_ref);@@ -890,8 +890,7 @@ nfsd(void *vrqstp) {struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;struct svc_pool *pool = rqstp->rq_pool; - struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list); -struct net *net = perm_sock->xpt_net; +struct net *net = rqstp->rq_server->sv_net;struct nfsd_net *nn =net_generic (net, nfsd_net_id);struct nfsd_thread_local_info ntli = { };bool have_mutex = false;include/linux/sunrpc/svc.h +2
-0@@ -72,6 +72,7 @@ enum {struct svc_serv {struct svc_program * sv_programs;/* RPC programs */ struct svc_stat * sv_stats;/* RPC statistics */ +struct net *sv_net;/* only set for pooled (nfsd) services */ spinlock_t sv_lock;unsigned int sv_nprogs;/* Number of sv_programs */ unsigned int sv_nrthreads;/* # of running server threads */ @@ -461,6 +462,7 @@ void svc_exit_thread(struct svc_rqst *);struct svc_serv *svc_create_pooled (struct svc_program *prog,unsigned int nprog,struct svc_stat *stats, +struct net *net,unsigned int bufsize,int (*threadfn)(void *data)); int svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,net/sunrpc/svc.c +2
-0@@ -481,6 +481,7 @@ EXPORT_SYMBOL_GPL(svc_create);* @prog: Array of RPC programs the new service will handle * @nprogs: Number of programs in the array * @stats: the stats struct if desired +* @net: network namespace the service belongs to * @bufsize: maximum message size for @prog * @threadfn: a function to service RPC requests for @prog * @@ -489,6 +490,7 @@ EXPORT_SYMBOL_GPL(svc_create);struct svc_serv *svc_create_pooled (struct svc_program *prog,unsigned int nprogs,struct svc_stat *stats, +struct net *net,unsigned int bufsize,int (*threadfn)(void *data)) {
net/sunrpc/svc.c struct svc_serv *svc_create_pooled(struct svc_program *prog, · line 510
Quoted 7 lines
net/sunrpc/svc.c +1
-0@@ -502,6 +504,7 @@ struct svc_serv *svc_create_pooled(struct svc_program *prog,if (!serv) goto out_err; serv->sv_is_pooled = true; + serv->sv_net = net;return serv; out_err:svc_pool_map_put ();
Much nicer.
Chuck Lever
maintainerQuoted 8 lines · Deepanshu Kartikey
nfsd()derives its struct net from the first entry ofserv->sv_permsocksusinglist_entry()without checking that the list is non-empty. If the list is empty,list_entry()returns a bogussvc_xprtcomputed from the list head embedded in structsvc_serv, and the followingperm_sock->xpt_netload reads past the end of thesvc_servallocation:[...]
Applied to nfsd-testing, thanks!
[1/1] nfsd: store net in svc_serv instead of deriving it from sv_permsocks
commit: 764ffa45f3f2189cd86ec6d46d248fc0440507b6
--
Chuck Lever