Skip to contentSkip to conversation

ipv4: ping: Fix potential use-after-free bug

PATCH In review Liang He 2 participants 3 messages Sep 16, 2022 – Sep 19, 2022

Subsystems (2)
lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH 1/1 ipv4: ping: Fix potential use-after-free bug

In ping_unhash(), we should move sock_put(sk) after any possible access point as the put function may free the object.

Fixes c319b4d76b9e ("net: ipv4: add IPPROTO_ICMP socket kind")

---

I have found other places containing similar code patterns.

 net/ipv4/ping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ping.c +1 -1
index b83c2bd9d722..f90c86d37ffc 100644
@@ -157,10 +157,10 @@ void ping_unhash(struct sock *sk)
 	spin_lock(&ping_table.lock);
 	if (sk_hashed(sk)) {
 		hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
-		sock_put(sk);
 		isk->inet_num = 0;
 		isk->inet_sport = 0;
 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
+		sock_put(sk);
 	}
 	spin_unlock(&ping_table.lock);
 }
Signature · 1 line
2.25.1
#1 lore raw
Quoted 2 lines · Liang He

In ping_unhash(), we should move sock_put(sk) after any possible access point as the put function may free the object.

unhash handlers are called from sk_common_release which still has a reference on the sock, so not really going to hit a UAF.

I do agree that it does not read correctly to 'put' a reference then continue using the object. ie., the put should be moved to the end like you have here. This is more of a tidiness exercise than a need to backport to stable kernels.

Quoted 26 lines
Fixes c319b4d76b9e ("net: ipv4: add IPPROTO_ICMP socket kind")

---

I have found other places containing similar code patterns.

 net/ipv4/ping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ping.c +1 -1
index b83c2bd9d722..f90c86d37ffc 100644
@@ -157,10 +157,10 @@ void ping_unhash(struct sock *sk)
 	spin_lock(&ping_table.lock);
 	if (sk_hashed(sk)) {
 		hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
-		sock_put(sk);
 		isk->inet_num = 0;
 		isk->inet_sport = 0;
 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
+		sock_put(sk);
 	}
 	spin_unlock(&ping_table.lock);
 }
author
#2 lore raw
Quoted 7 lines · David Ahern
Quoted 2 lines · Liang He

In ping_unhash(), we should move sock_put(sk) after any possible access point as the put function may free the object.

unhash handlers are called from sk_common_release which still has a reference on the sock, so not really going to hit a UAF.

Thanks for this valuable lesson.

Quoted 5 lines

I do agree that it does not read correctly to 'put' a reference then continue using the object. ie., the put should be moved to the end like you have here. This is more of a tidiness exercise than a need to backport to stable kernels.

OK, thanks.

Quoted 26 lines
Quoted 26 lines
Fixes c319b4d76b9e ("net: ipv4: add IPPROTO_ICMP socket kind")

---

I have found other places containing similar code patterns.

 net/ipv4/ping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ping.c +1 -1
index b83c2bd9d722..f90c86d37ffc 100644
@@ -157,10 +157,10 @@ void ping_unhash(struct sock *sk)
 	spin_lock(&ping_table.lock);
 	if (sk_hashed(sk)) {
 		hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
-		sock_put(sk);
 		isk->inet_num = 0;
 		isk->inet_sport = 0;
 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
+		sock_put(sk);
 	}
 	spin_unlock(&ping_table.lock);
 }