Skip to contentSkip to conversation

smc: move unhash as early as possible in smc_release()

PATCH In review Cong Wang 3 participants 5 messages Jan 6, 2019 – Jan 7, 2019

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 smc: move unhash as early as possible in smc_release()

In smc_release() we release smc->clcsock before unhash the smc sock, but a parallel smc_diag_dump() may be still reading smc->clcsock, therefore this could cause a use-after-free as reported by syzbot.

Reported-and-tested-by [email protected]
Fixes 51f1de79ad8e ("net/smc: replace sock_put worker by socket refcounting")
Cc Ursula Braun <[email protected]>
---
 net/smc/af_smc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/smc/af_smc.c +3 -1
index c4da4a78d369..c4e56602e0c6 100644
@@ -146,6 +146,9 @@ static int smc_release(struct socket *sock)
 		sock_set_flag(sk, SOCK_DEAD);
 		sk->sk_shutdown |= SHUTDOWN_MASK;
 	}
+
+	sk->sk_prot->unhash(sk);
+
 	if (smc->clcsock) {
 		if (smc->use_fallback && sk->sk_state == SMC_LISTEN) {
 			/* wake up clcsock accept */
@@ -170,7 +173,6 @@ static int smc_release(struct socket *sock)
 		smc_conn_free(&smc->conn);
 	release_sock(sk);
 
-	sk->sk_prot->unhash(sk);
 	sock_put(sk); /* final sock_put */
 out:
 	return rc;
Signature · 1 line
2.20.1
maintainer
#1 lore raw

From: Cong Wang <[email protected]>
Date: Sat, 5 Jan 2019 23:45:26 -0800

Quoted 9 lines

In smc_release() we release smc->clcsock before unhash the smc sock, but a parallel smc_diag_dump() may be still reading smc->clcsock, therefore this could cause a use-after-free as reported by syzbot.

Reported-and-tested-by [email protected]
Fixes 51f1de79ad8e ("net/smc: replace sock_put worker by socket refcounting")
Cc Ursula Braun <[email protected]>

We have two patches messing around in here, seeming to deal with release visibility and ordering problems in two different ways.

https://patchwork.ozlabs.org/patch/1020608/
https://patchwork.ozlabs.org/patch/1021044/

Reported-by [email protected]
Reported-by [email protected]
#2 lore raw
Quoted 13 lines · David Miller

From: Cong Wang <[email protected]>
Date: Sat, 5 Jan 2019 23:45:26 -0800

Quoted 9 lines

In smc_release() we release smc->clcsock before unhash the smc sock, but a parallel smc_diag_dump() may be still reading smc->clcsock, therefore this could cause a use-after-free as reported by syzbot.

Reported-and-tested-by [email protected]
Fixes 51f1de79ad8e ("net/smc: replace sock_put worker by socket refcounting")
Cc Ursula Braun <[email protected]>
Quoted 9 lines

We have two patches messing around in here, seeming to deal with release visibility and ordering problems in two different ways.

https://patchwork.ozlabs.org/patch/1020608/
https://patchwork.ozlabs.org/patch/1021044/

Reported-by [email protected]
Reported-by [email protected]

The patches from Myungho Jung fix a closing problem for listening SMC-sockets. The patch by Cong Wang fixes a closing problem for SMC-sockets when smc_diag_dump() is running in parallel. Both are valuable in my eyes.

author
#3 lore raw
In reply to David Miller
Quoted 14 lines · David Miller

From: Cong Wang <[email protected]>
Date: Sat, 5 Jan 2019 23:45:26 -0800

Quoted 9 lines

In smc_release() we release smc->clcsock before unhash the smc sock, but a parallel smc_diag_dump() may be still reading smc->clcsock, therefore this could cause a use-after-free as reported by syzbot.

Reported-and-tested-by [email protected]
Fixes 51f1de79ad8e ("net/smc: replace sock_put worker by socket refcounting")
Cc Ursula Braun <[email protected]>
Quoted 8 lines

We have two patches messing around in here, seeming to deal with release visibility and ordering problems in two different ways.

https://patchwork.ozlabs.org/patch/1020608/
https://patchwork.ozlabs.org/patch/1021044/

Reported-by [email protected]
Reported-by [email protected]

Isn't Myungho's patch already merged as commit 78abe3d0dfad? :)

So I believe the syzbot reported this bug on top of that commit.

Thanks.

maintainer
#4 lore raw

From: Cong Wang <[email protected]>
Date: Mon, 7 Jan 2019 11:25:12 -0800

Quoted 3 lines

Isn't Myungho's patch already merged as commit 78abe3d0dfad? :)

So I believe the syzbot reported this bug on top of that commit.

Aha, now it makes sense.

Yeah I'll apply your patch Cong.