Skip to contentSkip to conversation

NFSD: Keep an evicted NFSv4.0 openowner off close_lru

PATCH In review Chuck Lever 2 participants 2 messages Dec 2

Subsystems (2)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

PATCH 1/1 NFSD: Keep an evicted NFSv4.0 openowner off close_lru

An NFSv4.0 OPEN that evicts an unconfirmed openowner can race with a CLOSE on that owner. release_openowner() takes the owner off nn->close_lru only when oo_last_closed_stid is set. That field stays NULL until the CLOSE reaches move_to_close_lru(), which then adds the unhashed owner to the list.

__destroy_client() finds close_lru entries only through cl_openowners, so it misses the stranded owner and frees the nfs4_client. The laundromat later reads the freed client through oo_owner.so_client, and can go on to take cl_lock in it:

  BUG: KASAN: slab-use-after-free in is_client_expired
  Read of size 8 by task kworker/u19:2/84
  Workqueue: nfsd4 laundromat_main
   is_client_expired
   nfs4_laundromat
   laundromat_main

  Freed by task 84:
   kmem_cache_free
   free_client
   __destroy_client
   expire_client
   nfs4_process_client_reaplist
   nfs4_laundromat

Any client permitted to OPEN on an NFSv4.0 export can provoke the use-after-free. NFSv4.1 and later create openowners confirmed.

After the list add in move_to_close_lru(), test under cl_lock whether the owner is hashed, and if not, remove it with release_last_closed_stateid(). release_openowner() unhashes the owner under cl_lock before it reads oo_last_closed_stid under client_lock. move_to_close_lru() sets oo_last_closed_stid under client_lock before its test under cl_lock. At least one of the two therefore removes the owner, and release_last_closed_stateid() drops the stateid reference only once.

Until that test, a fresh oo_time keeps the laundromat off the owner, and the CLOSE pins the client through cl_rpc_users. Testing inside the existing client_lock section would need a second path that skips the list add and puts the stateid by hand.

Fixes 3234975f477f ("nfsd: Remove nfs4_lock_state(): nfsd4_open and nfsd4_open_confirm")
---
 fs/nfsd/nfs4state.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +14 -2
index 6ddb35f5ae5a..c40b48125202 100644
@@ -6145,8 +6145,9 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
 {
 	struct nfs4_ol_stateid *last;
 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
-	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
-						nfsd_net_id);
+	struct nfs4_client *clp = s->st_stid.sc_client;
+	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
+	bool unhashed;
 
 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
 
@@ -6178,6 +6179,17 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
 	spin_unlock(&nn->client_lock);
 	if (last)
 		nfs4_put_stid(&last->st_stid);
+
+	/*
+	 * A release_openowner() that reads oo_last_closed_stid before the
+	 * store above leaves @oo on close_lru unhashed. __destroy_client()
+	 * reaches close_lru entries only through cl_openowners.
+	 */
+	spin_lock(&clp->cl_lock);
+	unhashed = nfs4_openowner_unhashed(oo);
+	spin_unlock(&clp->cl_lock);
+	if (unhashed)
+		release_last_closed_stateid(oo);
 }
 
 static noinline_for_stack struct nfs4_file *
Signature · 1 line
2.56.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 73 lines · Chuck Lever

An NFSv4.0 OPEN that evicts an unconfirmed openowner can race with a CLOSE on that owner. release_openowner() takes the owner off nn->close_lru only when oo_last_closed_stid is set. That field stays NULL until the CLOSE reaches move_to_close_lru(), which then adds the unhashed owner to the list.

__destroy_client() finds close_lru entries only through cl_openowners, so it misses the stranded owner and frees the nfs4_client. The laundromat later reads the freed client through oo_owner.so_client, and can go on to take cl_lock in it:

  BUG: KASAN: slab-use-after-free in is_client_expired
  Read of size 8 by task kworker/u19:2/84
  Workqueue: nfsd4 laundromat_main
   is_client_expired
   nfs4_laundromat
   laundromat_main

  Freed by task 84:
   kmem_cache_free
   free_client
   __destroy_client
   expire_client
   nfs4_process_client_reaplist
   nfs4_laundromat

Any client permitted to OPEN on an NFSv4.0 export can provoke the use-after-free. NFSv4.1 and later create openowners confirmed.

After the list add in move_to_close_lru(), test under cl_lock whether the owner is hashed, and if not, remove it with release_last_closed_stateid(). release_openowner() unhashes the owner under cl_lock before it reads oo_last_closed_stid under client_lock. move_to_close_lru() sets oo_last_closed_stid under client_lock before its test under cl_lock. At least one of the two therefore removes the owner, and release_last_closed_stateid() drops the stateid reference only once.

Until that test, a fresh oo_time keeps the laundromat off the owner, and the CLOSE pins the client through cl_rpc_users. Testing inside the existing client_lock section would need a second path that skips the list add and puts the stateid by hand.

Fixes 3234975f477f ("nfsd: Remove nfs4_lock_state(): nfsd4_open and nfsd4_open_confirm")
---
 fs/nfsd/nfs4state.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +7 -2
index 6ddb35f5ae5a..c40b48125202 100644
@@ -6145,8 +6145,9 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
 {
 	struct nfs4_ol_stateid *last;
 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
-	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
-						nfsd_net_id);
+	struct nfs4_client *clp = s->st_stid.sc_client;
+	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
+	bool unhashed;
 
 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
 
@@ -6178,6 +6179,17 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
 	spin_unlock(&nn->client_lock);
 	if (last)
 		nfs4_put_stid(&last->st_stid);
+
+	/*
+	 * A release_openowner() that reads oo_last_closed_stid before the
+	 * store above leaves @oo on close_lru unhashed. __destroy_client()

fs/nfsd/nfs4state.c move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) · line 6195

Quoted 10 lines
fs/nfsd/nfs4state.c +7 -0
+	 * reaches close_lru entries only through cl_openowners.
+	 */
+	spin_lock(&clp->cl_lock);
+	unhashed = nfs4_openowner_unhashed(oo);
+	spin_unlock(&clp->cl_lock);
+	if (unhashed)
+		release_last_closed_stateid(oo);
 }
 
 static noinline_for_stack struct nfs4_file *