Skip to content
/ linux Public

Commit 2829e80

Browse files
chucklevergregkh
authored andcommitted
NFSD: Defer sub-object cleanup in export put callbacks
commit 48db892 upstream. svc_export_put() calls path_put() and auth_domain_put() immediately when the last reference drops, before the RCU grace period. RCU readers in e_show() and c_show() access both ex_path (via seq_path/d_path) and ex_client->name (via seq_escape) without holding a reference. If cache_clean removes the entry and drops the last reference concurrently, the sub-objects are freed while still in use, producing a NULL pointer dereference in d_path. Commit 2530766 ("nfsd: fix UAF when access ex_uuid or ex_stats") moved kfree of ex_uuid and ex_stats into the call_rcu callback, but left path_put() and auth_domain_put() running before the grace period because both may sleep and call_rcu callbacks execute in softirq context. Replace call_rcu/kfree_rcu with queue_rcu_work(), which defers the callback until after the RCU grace period and executes it in process context where sleeping is permitted. This allows path_put() and auth_domain_put() to be moved into the deferred callback alongside the other resource releases. Apply the same fix to expkey_put(), which has the identical pattern with ek_path and ek_client. A dedicated workqueue scopes the shutdown drain to only NFSD export release work items; flushing the shared system_unbound_wq would stall on unrelated work from other subsystems. nfsd_export_shutdown() uses rcu_barrier() followed by flush_workqueue() to ensure all deferred release callbacks complete before the export caches are destroyed. Reported-by: Misbah Anjum N <misanjum@linux.ibm.com> Closes: https://lore.kernel.org/linux-nfs/dcd371d3a95815a84ba7de52cef447b8@linux.ibm.com/ Fixes: c224edc ("nfsd: no need get cache ref when protected by rcu") Fixes: 1b10f0b ("SUNRPC: no need get cache ref when protected by rcu") Cc: stable@vger.kernel.org Reviwed-by: Jeff Layton <jlayton@kernel.org> Reviewed-by: NeilBrown <neil@brown.name> Tested-by: Olga Kornievskaia <okorniev@redhat.com> Signed-off-by: Chuck Lever <chuck.lever@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4aea1dc commit 2829e80

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

fs/nfsd/export.c

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,30 @@
3636
* second map contains a reference to the entry in the first map.
3737
*/
3838

39+
static struct workqueue_struct *nfsd_export_wq;
40+
3941
#define EXPKEY_HASHBITS 8
4042
#define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
4143
#define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
4244

43-
static void expkey_put(struct kref *ref)
45+
static void expkey_release(struct work_struct *work)
4446
{
45-
struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
47+
struct svc_expkey *key = container_of(to_rcu_work(work),
48+
struct svc_expkey, ek_rwork);
4649

4750
if (test_bit(CACHE_VALID, &key->h.flags) &&
4851
!test_bit(CACHE_NEGATIVE, &key->h.flags))
4952
path_put(&key->ek_path);
5053
auth_domain_put(key->ek_client);
51-
kfree_rcu(key, ek_rcu);
54+
kfree(key);
55+
}
56+
57+
static void expkey_put(struct kref *ref)
58+
{
59+
struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
60+
61+
INIT_RCU_WORK(&key->ek_rwork, expkey_release);
62+
queue_rcu_work(nfsd_export_wq, &key->ek_rwork);
5263
}
5364

5465
static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
@@ -353,11 +364,13 @@ static void export_stats_destroy(struct export_stats *stats)
353364
EXP_STATS_COUNTERS_NUM);
354365
}
355366

356-
static void svc_export_release(struct rcu_head *rcu_head)
367+
static void svc_export_release(struct work_struct *work)
357368
{
358-
struct svc_export *exp = container_of(rcu_head, struct svc_export,
359-
ex_rcu);
369+
struct svc_export *exp = container_of(to_rcu_work(work),
370+
struct svc_export, ex_rwork);
360371

372+
path_put(&exp->ex_path);
373+
auth_domain_put(exp->ex_client);
361374
nfsd4_fslocs_free(&exp->ex_fslocs);
362375
export_stats_destroy(exp->ex_stats);
363376
kfree(exp->ex_stats);
@@ -369,9 +382,8 @@ static void svc_export_put(struct kref *ref)
369382
{
370383
struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
371384

372-
path_put(&exp->ex_path);
373-
auth_domain_put(exp->ex_client);
374-
call_rcu(&exp->ex_rcu, svc_export_release);
385+
INIT_RCU_WORK(&exp->ex_rwork, svc_export_release);
386+
queue_rcu_work(nfsd_export_wq, &exp->ex_rwork);
375387
}
376388

377389
static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
@@ -1478,6 +1490,36 @@ const struct seq_operations nfs_exports_op = {
14781490
.show = e_show,
14791491
};
14801492

1493+
/**
1494+
* nfsd_export_wq_init - allocate the export release workqueue
1495+
*
1496+
* Called once at module load. The workqueue runs deferred svc_export and
1497+
* svc_expkey release work scheduled by queue_rcu_work() in the cache put
1498+
* callbacks.
1499+
*
1500+
* Return values:
1501+
* %0: workqueue allocated
1502+
* %-ENOMEM: allocation failed
1503+
*/
1504+
int nfsd_export_wq_init(void)
1505+
{
1506+
nfsd_export_wq = alloc_workqueue("nfsd_export", WQ_UNBOUND, 0);
1507+
if (!nfsd_export_wq)
1508+
return -ENOMEM;
1509+
return 0;
1510+
}
1511+
1512+
/**
1513+
* nfsd_export_wq_shutdown - drain and free the export release workqueue
1514+
*
1515+
* Called once at module unload. Per-namespace teardown in
1516+
* nfsd_export_shutdown() has already drained all deferred work.
1517+
*/
1518+
void nfsd_export_wq_shutdown(void)
1519+
{
1520+
destroy_workqueue(nfsd_export_wq);
1521+
}
1522+
14811523
/*
14821524
* Initialize the exports module.
14831525
*/
@@ -1539,6 +1581,9 @@ nfsd_export_shutdown(struct net *net)
15391581

15401582
cache_unregister_net(nn->svc_expkey_cache, net);
15411583
cache_unregister_net(nn->svc_export_cache, net);
1584+
/* Drain deferred export and expkey release work. */
1585+
rcu_barrier();
1586+
flush_workqueue(nfsd_export_wq);
15421587
cache_destroy_net(nn->svc_expkey_cache, net);
15431588
cache_destroy_net(nn->svc_export_cache, net);
15441589
svcauth_unix_purge(net);

fs/nfsd/export.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <linux/sunrpc/cache.h>
99
#include <linux/percpu_counter.h>
10+
#include <linux/workqueue.h>
1011
#include <uapi/linux/nfsd/export.h>
1112
#include <linux/nfs4.h>
1213

@@ -75,7 +76,7 @@ struct svc_export {
7576
u32 ex_layout_types;
7677
struct nfsd4_deviceid_map *ex_devid_map;
7778
struct cache_detail *cd;
78-
struct rcu_head ex_rcu;
79+
struct rcu_work ex_rwork;
7980
unsigned long ex_xprtsec_modes;
8081
struct export_stats *ex_stats;
8182
};
@@ -92,7 +93,7 @@ struct svc_expkey {
9293
u32 ek_fsid[6];
9394

9495
struct path ek_path;
95-
struct rcu_head ek_rcu;
96+
struct rcu_work ek_rwork;
9697
};
9798

9899
#define EX_ISSYNC(exp) (!((exp)->ex_flags & NFSEXP_ASYNC))
@@ -110,6 +111,8 @@ __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
110111
/*
111112
* Function declarations
112113
*/
114+
int nfsd_export_wq_init(void);
115+
void nfsd_export_wq_shutdown(void);
113116
int nfsd_export_init(struct net *);
114117
void nfsd_export_shutdown(struct net *);
115118
void nfsd_export_flush(struct net *);

fs/nfsd/nfsctl.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2252,9 +2252,12 @@ static int __init init_nfsd(void)
22522252
if (retval)
22532253
goto out_free_pnfs;
22542254
nfsd_lockd_init(); /* lockd->nfsd callbacks */
2255+
retval = nfsd_export_wq_init();
2256+
if (retval)
2257+
goto out_free_lockd;
22552258
retval = register_pernet_subsys(&nfsd_net_ops);
22562259
if (retval < 0)
2257-
goto out_free_lockd;
2260+
goto out_free_export_wq;
22582261
retval = register_cld_notifier();
22592262
if (retval)
22602263
goto out_free_subsys;
@@ -2283,6 +2286,8 @@ static int __init init_nfsd(void)
22832286
unregister_cld_notifier();
22842287
out_free_subsys:
22852288
unregister_pernet_subsys(&nfsd_net_ops);
2289+
out_free_export_wq:
2290+
nfsd_export_wq_shutdown();
22862291
out_free_lockd:
22872292
nfsd_lockd_shutdown();
22882293
nfsd_drc_slab_free();
@@ -2303,6 +2308,7 @@ static void __exit exit_nfsd(void)
23032308
nfsd4_destroy_laundry_wq();
23042309
unregister_cld_notifier();
23052310
unregister_pernet_subsys(&nfsd_net_ops);
2311+
nfsd_export_wq_shutdown();
23062312
nfsd_drc_slab_free();
23072313
nfsd_lockd_shutdown();
23082314
nfsd4_free_slabs();

0 commit comments

Comments
 (0)