Skip to content

Commit ed9ab73

Browse files
jtlaytonchucklever
authored andcommitted
nfsd: move init of percpu reply_cache_stats counters back to nfsd_init_net
Commit f5f9d4a ("nfsd: move reply cache initialization into nfsd startup") moved the initialization of the reply cache into nfsd startup, but didn't account for the stats counters, which can be accessed before nfsd is ever started. The result can be a NULL pointer dereference when someone accesses /proc/fs/nfsd/reply_cache_stats while nfsd is still shut down. This is a regression and a user-triggerable oops in the right situation: - non-x86_64 arch - /proc/fs/nfsd is mounted in the namespace - nfsd is not started in the namespace - unprivileged user calls "cat /proc/fs/nfsd/reply_cache_stats" Although this is easy to trigger on some arches (like aarch64), on x86_64, calling this_cpu_ptr(NULL) evidently returns a pointer to the fixed_percpu_data. That struct looks just enough like a newly initialized percpu var to allow nfsd_reply_cache_stats_show to access it without Oopsing. Move the initialization of the per-net+per-cpu reply-cache counters back into nfsd_init_net, while leaving the rest of the reply cache allocations to be done at nfsd startup time. Kudos to Eirik who did most of the legwork to track this down. Cc: [email protected] # v6.3+ Fixes: f5f9d4a ("nfsd: move reply cache initialization into nfsd startup") Reported-and-tested-by: Eirik Fuller <[email protected]> Closes: https://bugzilla.redhat.com/show_bug.cgi?id=2215429 Signed-off-by: Jeff Layton <[email protected]> Signed-off-by: Chuck Lever <[email protected]>
1 parent 00a87e5 commit ed9ab73

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

fs/nfsd/cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ enum {
8080

8181
int nfsd_drc_slab_create(void);
8282
void nfsd_drc_slab_free(void);
83+
int nfsd_net_reply_cache_init(struct nfsd_net *nn);
84+
void nfsd_net_reply_cache_destroy(struct nfsd_net *nn);
8385
int nfsd_reply_cache_init(struct nfsd_net *);
8486
void nfsd_reply_cache_shutdown(struct nfsd_net *);
8587
int nfsd_cache_lookup(struct svc_rqst *);

fs/nfsd/nfscache.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,23 @@ void nfsd_drc_slab_free(void)
148148
kmem_cache_destroy(drc_slab);
149149
}
150150

151-
static int nfsd_reply_cache_stats_init(struct nfsd_net *nn)
151+
/**
152+
* nfsd_net_reply_cache_init - per net namespace reply cache set-up
153+
* @nn: nfsd_net being initialized
154+
*
155+
* Returns zero on succes; otherwise a negative errno is returned.
156+
*/
157+
int nfsd_net_reply_cache_init(struct nfsd_net *nn)
152158
{
153159
return nfsd_percpu_counters_init(nn->counter, NFSD_NET_COUNTERS_NUM);
154160
}
155161

156-
static void nfsd_reply_cache_stats_destroy(struct nfsd_net *nn)
162+
/**
163+
* nfsd_net_reply_cache_destroy - per net namespace reply cache tear-down
164+
* @nn: nfsd_net being freed
165+
*
166+
*/
167+
void nfsd_net_reply_cache_destroy(struct nfsd_net *nn)
157168
{
158169
nfsd_percpu_counters_destroy(nn->counter, NFSD_NET_COUNTERS_NUM);
159170
}
@@ -169,17 +180,13 @@ int nfsd_reply_cache_init(struct nfsd_net *nn)
169180
hashsize = nfsd_hashsize(nn->max_drc_entries);
170181
nn->maskbits = ilog2(hashsize);
171182

172-
status = nfsd_reply_cache_stats_init(nn);
173-
if (status)
174-
goto out_nomem;
175-
176183
nn->nfsd_reply_cache_shrinker.scan_objects = nfsd_reply_cache_scan;
177184
nn->nfsd_reply_cache_shrinker.count_objects = nfsd_reply_cache_count;
178185
nn->nfsd_reply_cache_shrinker.seeks = 1;
179186
status = register_shrinker(&nn->nfsd_reply_cache_shrinker,
180187
"nfsd-reply:%s", nn->nfsd_name);
181188
if (status)
182-
goto out_stats_destroy;
189+
return status;
183190

184191
nn->drc_hashtbl = kvzalloc(array_size(hashsize,
185192
sizeof(*nn->drc_hashtbl)), GFP_KERNEL);
@@ -195,9 +202,6 @@ int nfsd_reply_cache_init(struct nfsd_net *nn)
195202
return 0;
196203
out_shrinker:
197204
unregister_shrinker(&nn->nfsd_reply_cache_shrinker);
198-
out_stats_destroy:
199-
nfsd_reply_cache_stats_destroy(nn);
200-
out_nomem:
201205
printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
202206
return -ENOMEM;
203207
}
@@ -217,7 +221,6 @@ void nfsd_reply_cache_shutdown(struct nfsd_net *nn)
217221
rp, nn);
218222
}
219223
}
220-
nfsd_reply_cache_stats_destroy(nn);
221224

222225
kvfree(nn->drc_hashtbl);
223226
nn->drc_hashtbl = NULL;

fs/nfsd/nfsctl.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,9 @@ static __net_init int nfsd_init_net(struct net *net)
15051505
retval = nfsd_idmap_init(net);
15061506
if (retval)
15071507
goto out_idmap_error;
1508+
retval = nfsd_net_reply_cache_init(nn);
1509+
if (retval)
1510+
goto out_repcache_error;
15081511
nn->nfsd_versions = NULL;
15091512
nn->nfsd4_minorversions = NULL;
15101513
nfsd4_init_leases_net(nn);
@@ -1513,6 +1516,8 @@ static __net_init int nfsd_init_net(struct net *net)
15131516

15141517
return 0;
15151518

1519+
out_repcache_error:
1520+
nfsd_idmap_shutdown(net);
15161521
out_idmap_error:
15171522
nfsd_export_shutdown(net);
15181523
out_export_error:
@@ -1521,9 +1526,12 @@ static __net_init int nfsd_init_net(struct net *net)
15211526

15221527
static __net_exit void nfsd_exit_net(struct net *net)
15231528
{
1529+
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1530+
1531+
nfsd_net_reply_cache_destroy(nn);
15241532
nfsd_idmap_shutdown(net);
15251533
nfsd_export_shutdown(net);
1526-
nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
1534+
nfsd_netns_free_versions(nn);
15271535
}
15281536

15291537
static struct pernet_operations nfsd_net_ops = {

0 commit comments

Comments
 (0)