Skip to contentSkip to conversation

do not leave dangling sk pointers in pf->create functions

PATCH Applied Ignat Korchagin 8 participants 32 messages Oct 14, 2024 – Oct 16, 2024

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v3 cover do not leave dangling sk pointers in pf->create functions

Some protocol family create() implementations have an error path after allocating the sk object and calling sock_init_data(). sock_init_data() attaches the allocated sk object to the sock object, provided by the caller.

If the create() implementation errors out after calling sock_init_data(), it releases the allocated sk object, but the caller ends up having a dangling sk pointer in its sock object on return. Subsequent manipulations on this sock object may try to access the sk pointer, because it is not NULL thus creating a use-after-free scenario.

We have implemented a stable hotfix in commit 631083143315 ("net: explicitly clear the sk pointer, when pf->create fails"), but this series aims to fix it properly by going through each of the pf->create() implementations and making sure they all don't return a sock object with a dangling pointer on error.

Changes in V3:
* retargeted the series to net-next
* dropped the hotfix patch, which was merged into net already
* replaced the hotfix code with DEBUG_NET_WARN_ON_ONCE() to catch future violations

Changes in V2:
* reverted the change introduced in 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket creation fails")
* added optional commits to all pf->create implementaions to clear the sk pointer on error after sock_init_data()

Ignat Korchagin (9):
af_packet: avoid erroring out after sock_init_data() in
packet_create()
Bluetooth: L2CAP: do not leave dangling sk pointer on error in

    l2cap_sock_create()
  Bluetooth: RFCOMM: avoid leaving dangling sk pointer in
    rfcomm_sock_alloc()
  net: af_can: do not leave a dangling sk pointer in can_create()
  net: ieee802154: do not leave a dangling sk pointer in
    ieee802154_create()

net: inet: do not leave a dangling sk pointer in inet_create()
net: inet6: do not leave a dangling sk pointer in inet6_create()
net: warn, if pf->create does not clear sock->sk on error Revert "net: do not leave a dangling sk pointer, when socket creation
fails"

 net/bluetooth/l2cap_sock.c  |  1 +
 net/bluetooth/rfcomm/sock.c | 10 +++++-----
 net/can/af_can.c            |  1 +
 net/core/sock.c             |  3 ---
 net/ieee802154/socket.c     | 12 +++++++-----
 net/ipv4/af_inet.c          | 22 ++++++++++------------
 net/ipv6/af_inet6.c         | 22 ++++++++++------------
 net/packet/af_packet.c      | 12 ++++++------
 net/socket.c                |  4 ++--
 9 files changed, 42 insertions(+), 45 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v3 1/9 af_packet: avoid erroring out after sock_init_data() in packet_create()

After sock_init_data() the allocated sk object is attached to the provided sock object. On error, packet_create() frees the sk object leaving the dangling pointer in the sock object on return. Some other code may try to use this pointer and cause use-after-free.

---
 net/packet/af_packet.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/packet/af_packet.c +6 -6
index f8942062f776..99ae27d1e4dc 100644
@@ -3421,17 +3421,17 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
 	if (sock->type == SOCK_PACKET)
 		sock->ops = &packet_ops_spkt;
 
+	po = pkt_sk(sk);
+	err = packet_alloc_pending(po);
+	if (err)
+		goto out_sk_free;
+
 	sock_init_data(sock, sk);
 
-	po = pkt_sk(sk);
 	init_completion(&po->skb_completion);
 	sk->sk_family = PF_PACKET;
 	po->num = proto;
 
-	err = packet_alloc_pending(po);
-	if (err)
-		goto out2;
-
 	packet_cached_dev_reset(po);
 
 	sk->sk_destruct = packet_sock_destruct;
@@ -3463,7 +3463,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
 	sock_prot_inuse_add(net, &packet_proto, 1);
 
 	return 0;
-out2:
+out_sk_free:
 	sk_free(sk);
 out:
 	return err;
Signature · 1 line
2.39.5
#2 lore raw

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: net/socket.c:1576
error: net/socket.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

Reviewed-by on v3 1/9
#3 lore raw

Re: [PATCH net-next v3 1/9] af_packet: avoid erroring out after sock_init_data() in packet_create()

In reply to Ignat Korchagin

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:00 +0100

Quoted 7 lines

After sock_init_data() the allocated sk object is attached to the provided sock object. On error, packet_create() frees the sk object leaving the dangling pointer in the sock object on return. Some other code may try to use this pointer and cause use-after-free.

maintainer Reviewed-by on v3 1/9
#4 lore raw

Re: [PATCH net-next v3 1/9] af_packet: avoid erroring out after sock_init_data() in packet_create()

In reply to Ignat Korchagin
Quoted 7 lines · Ignat Korchagin

After sock_init_data() the allocated sk object is attached to the provided sock object. On error, packet_create() frees the sk object leaving the dangling pointer in the sock object on return. Some other code may try to use this pointer and cause use-after-free.

Reviewed-by on v3 1/9
#5 lore raw

Re: [PATCH net-next v3 1/9] af_packet: avoid erroring out after sock_init_data() in packet_create()

In reply to Ignat Korchagin
Quoted 9 lines · Ignat Korchagin

After sock_init_data() the allocated sk object is attached to the provided sock object. On error, packet_create() frees the sk object leaving the dangling pointer in the sock object on return. Some other code may try to use this pointer and cause use-after-free.

---

Thanks.

#6 lore raw

PATCH v3 2/9 Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create()

In reply to Ignat Korchagin

bt_sock_alloc() allocates the sk object and attaches it to the provided sock object. On error l2cap_sock_alloc() frees the sk object, but the dangling pointer is still attached to the sock object, which may create use-after-free in other code.

---
 net/bluetooth/l2cap_sock.c | 1 +
 1 file changed, 1 insertion(+)
net/bluetooth/l2cap_sock.c +1 -0
index ba437c6f6ee5..18e89e764f3b 100644
@@ -1886,6 +1886,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
 	chan = l2cap_chan_create();
 	if (!chan) {
 		sk_free(sk);
+		sock->sk = NULL;
 		return NULL;
 	}
 
Signature · 1 line
2.39.5
Reviewed-by on v3 2/9
#7 lore raw

Re: [PATCH net-next v3 2/9] Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create()

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:01 +0100

Quoted 6 lines

bt_sock_alloc() allocates the sk object and attaches it to the provided sock object. On error l2cap_sock_alloc() frees the sk object, but the dangling pointer is still attached to the sock object, which may create use-after-free in other code.

Checked all bt_sock_alloc() paths and confirmed only rfcomm and l2cap need changes.

Reviewed-by
#8 lore raw

Re: [PATCH net-next v3 2/9] Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create()

Quoted 8 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:01 +0100

Quoted 5 lines

bt_sock_alloc() allocates the sk object and attaches it to the provided sock object. On error l2cap_sock_alloc() frees the sk object, but the dangling pointer is still attached to the sock object, which may create use-after-free in other code.

Quoted 6 lines
Quoted 1 line

Checked all bt_sock_alloc() paths and confirmed only rfcomm and l2cap need changes.

#9 lore raw

PATCH v3 3/9 Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc()

In reply to Ignat Korchagin

bt_sock_alloc() attaches allocated sk object to the provided sock object. If rfcomm_dlc_alloc() fails, we release the sk object, but leave the dangling pointer in the sock object, which may cause use-after-free.

Fix this by swapping calls to bt_sock_alloc() and rfcomm_dlc_alloc().

---
 net/bluetooth/rfcomm/sock.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/bluetooth/rfcomm/sock.c +5 -5
index f48250e3f2e1..355e1a1698f5 100644
@@ -274,13 +274,13 @@ static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock,
 	struct rfcomm_dlc *d;
 	struct sock *sk;
 
-	sk = bt_sock_alloc(net, sock, &rfcomm_proto, proto, prio, kern);
-	if (!sk)
+	d = rfcomm_dlc_alloc(prio);
+	if (!d)
 		return NULL;
 
-	d = rfcomm_dlc_alloc(prio);
-	if (!d) {
-		sk_free(sk);
+	sk = bt_sock_alloc(net, sock, &rfcomm_proto, proto, prio, kern);
+	if (!sk) {
+		rfcomm_dlc_free(d);
 		return NULL;
 	}
 
Signature · 1 line
2.39.5
Reviewed-by on v3 3/9
#10 lore raw

Re: [PATCH net-next v3 3/9] Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc()

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:02 +0100

Quoted 7 lines

bt_sock_alloc() attaches allocated sk object to the provided sock object. If rfcomm_dlc_alloc() fails, we release the sk object, but leave the dangling pointer in the sock object, which may cause use-after-free.

Fix this by swapping calls to bt_sock_alloc() and rfcomm_dlc_alloc().

Reviewed-by
#11 lore raw

Re: [PATCH net-next v3 3/9] Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc()

Quoted 7 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:02 +0100

Quoted 4 lines

bt_sock_alloc() attaches allocated sk object to the provided sock object. If rfcomm_dlc_alloc() fails, we release the sk object, but leave the dangling pointer in the sock object, which may cause use-after-free.

Quoted 5 lines
Quoted 3 lines

Fix this by swapping calls to bt_sock_alloc() and rfcomm_dlc_alloc().

#12 lore raw

PATCH v3 4/9 net: af_can: do not leave a dangling sk pointer in can_create()

In reply to Ignat Korchagin

On error can_create() frees the allocated sk object, but sock_init_data() has already attached it to the provided sock object. This will leave a dangling sk pointer in the sock object and may cause use-after-free later.

---
 net/can/af_can.c | 1 +
 1 file changed, 1 insertion(+)
net/can/af_can.c +1 -0
index 707576eeeb58..01f3fbb3b67d 100644
@@ -171,6 +171,7 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
 		/* release sk on errors */
 		sock_orphan(sk);
 		sock_put(sk);
+		sock->sk = NULL;
 	}
 
  errout:
Signature · 1 line
2.39.5
Reviewed-by on v3 4/9
#13 lore raw

Re: [PATCH net-next v3 4/9] net: af_can: do not leave a dangling sk pointer in can_create()

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:03 +0100

Quoted 6 lines

On error can_create() frees the allocated sk object, but sock_init_data() has already attached it to the provided sock object. This will leave a dangling sk pointer in the sock object and may cause use-after-free later.

maintainer Reviewed-by on v3 4/9
#14 lore raw

Re: [PATCH net-next v3 4/9] net: af_can: do not leave a dangling sk pointer in can_create()

In reply to Ignat Korchagin
Quoted 6 lines · Ignat Korchagin

On error can_create() frees the allocated sk object, but sock_init_data() has already attached it to the provided sock object. This will leave a dangling sk pointer in the sock object and may cause use-after-free later.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#15 lore raw

PATCH v3 5/9 net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()

In reply to Ignat Korchagin

sock_init_data() attaches the allocated sk object to the provided sock object. If ieee802154_create() fails later, the allocated sk object is freed, but the dangling pointer remains in the provided sock object, which may allow use-after-free.

Clear the sk pointer in the sock object on error.

---
 net/ieee802154/socket.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
net/ieee802154/socket.c +7 -5
index 990a83455dcf..18d267921bb5 100644
@@ -1043,19 +1043,21 @@ static int ieee802154_create(struct net *net, struct socket *sock,
 
 	if (sk->sk_prot->hash) {
 		rc = sk->sk_prot->hash(sk);
-		if (rc) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (rc)
+			goto out_sk_release;
 	}
 
 	if (sk->sk_prot->init) {
 		rc = sk->sk_prot->init(sk);
 		if (rc)
-			sk_common_release(sk);
+			goto out_sk_release;
 	}
 out:
 	return rc;
+out_sk_release:
+	sk_common_release(sk);
+	sock->sk = NULL;
+	goto out;
 }
 
 static const struct net_proto_family ieee802154_family_ops = {
Signature · 1 line
2.39.5
Reviewed-by on v3 5/9
#16 lore raw

Re: [PATCH net-next v3 5/9] net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:04 +0100

Quoted 9 lines

sock_init_data() attaches the allocated sk object to the provided sock object. If ieee802154_create() fails later, the allocated sk object is freed, but the dangling pointer remains in the provided sock object, which may allow use-after-free.

Clear the sk pointer in the sock object on error.

Reviewed-by
#17 lore raw

Re: [PATCH net-next v3 5/9] net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()

Quoted 8 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:04 +0100

Quoted 5 lines

sock_init_data() attaches the allocated sk object to the provided sock object. If ieee802154_create() fails later, the allocated sk object is freed, but the dangling pointer remains in the provided sock object, which may allow use-after-free.

Quoted 6 lines
Quoted 4 lines

Clear the sk pointer in the sock object on error.

#18 lore raw

PATCH v3 6/9 net: inet: do not leave a dangling sk pointer in inet_create()

In reply to Ignat Korchagin

sock_init_data() attaches the allocated sk object to the provided sock object. If inet_create() fails later, the sk object is freed, but the sock object retains the dangling pointer, which may create use-after-free later.

Clear the sk pointer in the sock object on error.

---
 net/ipv4/af_inet.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
net/ipv4/af_inet.c +10 -12
index b24d74616637..8095e82de808 100644
@@ -376,32 +376,30 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,
 		inet->inet_sport = htons(inet->inet_num);
 		/* Add to protocol hash chains. */
 		err = sk->sk_prot->hash(sk);
-		if (err) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (err)
+			goto out_sk_release;
 	}
 
 	if (sk->sk_prot->init) {
 		err = sk->sk_prot->init(sk);
-		if (err) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (err)
+			goto out_sk_release;
 	}
 
 	if (!kern) {
 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
-		if (err) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (err)
+			goto out_sk_release;
 	}
 out:
 	return err;
 out_rcu_unlock:
 	rcu_read_unlock();
 	goto out;
+out_sk_release:
+	sk_common_release(sk);
+	sock->sk = NULL;
+	goto out;
 }
 
 
Signature · 1 line
2.39.5
Reviewed-by on v3 6/9
#19 lore raw

Re: [PATCH net-next v3 6/9] net: inet: do not leave a dangling sk pointer in inet_create()

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:05 +0100

Quoted 8 lines

sock_init_data() attaches the allocated sk object to the provided sock object. If inet_create() fails later, the sk object is freed, but the sock object retains the dangling pointer, which may create use-after-free later.

Clear the sk pointer in the sock object on error.

Reviewed-by
#20 lore raw

Re: [PATCH net-next v3 6/9] net: inet: do not leave a dangling sk pointer in inet_create()

Quoted 8 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:05 +0100

Quoted 5 lines

sock_init_data() attaches the allocated sk object to the provided sock object. If inet_create() fails later, the sk object is freed, but the sock object retains the dangling pointer, which may create use-after-free later.

Quoted 5 lines
Quoted 3 lines

Clear the sk pointer in the sock object on error.

#21 lore raw

PATCH v3 7/9 net: inet6: do not leave a dangling sk pointer in inet6_create()

In reply to Ignat Korchagin

sock_init_data() attaches the allocated sk pointer to the provided sock object. If inet6_create() fails later, the sk object is released, but the sock object retains the dangling sk pointer, which may cause use-after-free later.

Clear the sock sk pointer on error.

---
 net/ipv6/af_inet6.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
net/ipv6/af_inet6.c +10 -12
index ba69b86f1c7d..f60ec8b0f8ea 100644
@@ -252,31 +252,29 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,
 		 */
 		inet->inet_sport = htons(inet->inet_num);
 		err = sk->sk_prot->hash(sk);
-		if (err) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (err)
+			goto out_sk_release;
 	}
 	if (sk->sk_prot->init) {
 		err = sk->sk_prot->init(sk);
-		if (err) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (err)
+			goto out_sk_release;
 	}
 
 	if (!kern) {
 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
-		if (err) {
-			sk_common_release(sk);
-			goto out;
-		}
+		if (err)
+			goto out_sk_release;
 	}
 out:
 	return err;
 out_rcu_unlock:
 	rcu_read_unlock();
 	goto out;
+out_sk_release:
+	sk_common_release(sk);
+	sock->sk = NULL;
+	goto out;
 }
 
 static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
Signature · 1 line
2.39.5
Reviewed-by on v3 7/9
#22 lore raw

Re: [PATCH net-next v3 7/9] net: inet6: do not leave a dangling sk pointer in inet6_create()

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:06 +0100

Quoted 8 lines

sock_init_data() attaches the allocated sk pointer to the provided sock object. If inet6_create() fails later, the sk object is released, but the sock object retains the dangling sk pointer, which may cause use-after-free later.

Clear the sock sk pointer on error.

Reviewed-by
#23 lore raw

Re: [PATCH net-next v3 7/9] net: inet6: do not leave a dangling sk pointer in inet6_create()

Quoted 8 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:06 +0100

Quoted 5 lines

sock_init_data() attaches the allocated sk pointer to the provided sock object. If inet6_create() fails later, the sk object is released, but the sock object retains the dangling sk pointer, which may cause use-after-free later.

Quoted 5 lines
Quoted 3 lines

Clear the sock sk pointer on error.

#24 lore raw

PATCH v3 8/9 net: warn, if pf->create does not clear sock->sk on error

In reply to Ignat Korchagin

All pf->create implementations have been fixed now to clear sock->sk on error, when they deallocate the allocated sk object.

Put a warning in place to make sure we don't break this promise in the future.

---
 net/socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/socket.c +2 -2
index 24b404299015..9a8e4452b9b2 100644
@@ -1576,9 +1576,9 @@ int __sock_create(struct net *net, int family, int type, int protocol,
 	err = pf->create(net, sock, protocol, kern);
 	if (err < 0) {
 		/* ->create should release the allocated sock->sk object on error
-		 * but it may leave the dangling pointer
+		 * and make sure sock->sk is set to NULL to avoid use-after-free
 		 */
-		sock->sk = NULL;
+		DEBUG_NET_WARN_ON_ONCE(sock->sk);
 		goto out_module_put;
 	}
 
Signature · 1 line
2.39.5
Reviewed-by on v3 8/9
#25 lore raw

Re: [PATCH net-next v3 8/9] net: warn, if pf->create does not clear sock->sk on error

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:07 +0100

Quoted 8 lines

All pf->create implementations have been fixed now to clear sock->sk on error, when they deallocate the allocated sk object.

Put a warning in place to make sure we don't break this promise in the future.

Reviewed-by
#26 lore raw

Re: [PATCH net-next v3 8/9] net: warn, if pf->create does not clear sock->sk on error

Quoted 6 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:07 +0100

Quoted 3 lines

All pf->create implementations have been fixed now to clear sock->sk on error, when they deallocate the allocated sk object.

Quoted 7 lines
Quoted 5 lines

Put a warning in place to make sure we don't break this promise in the future.

#27 lore raw

PATCH v3 9/9 Revert "net: do not leave a dangling sk pointer, when socket creation fails"

In reply to Ignat Korchagin

This reverts commit 6cd4a78d962bebbaf8beb7d2ead3f34120e3f7b2.

inet/inet6->create() implementations have been fixed to explicitly NULL the allocated sk object on error.

A warning was put in place to make sure any future changes will not leave a dangling pointer in pf->create() implementations.

So this code is now redundant.

---
 net/core/sock.c | 3 ---
 1 file changed, 3 deletions(-)
net/core/sock.c +0 -3
index 083d438d8b6f..a9391cb796a2 100644
@@ -3830,9 +3830,6 @@ void sk_common_release(struct sock *sk)
 
 	sk->sk_prot->unhash(sk);
 
-	if (sk->sk_socket)
-		sk->sk_socket->sk = NULL;
-
 	/*
 	 * In this point socket cannot receive new packets, but it is possible
 	 * that some packets are in flight because some CPU runs receiver and
Signature · 1 line
2.39.5
Reviewed-by on v3 9/9
#28 lore raw

Re: [PATCH net-next v3 9/9] Revert "net: do not leave a dangling sk pointer, when socket creation fails"

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:08 +0100

Quoted 5 lines

This reverts commit 6cd4a78d962bebbaf8beb7d2ead3f34120e3f7b2.

inet/inet6->create() implementations have been fixed to explicitly NULL the allocated sk object on error.

Quoted 7 lines

A warning was put in place to make sure any future changes will not leave a dangling pointer in pf->create() implementations.

So this code is now redundant.

Reviewed-by
#29 lore raw

Re: [PATCH net-next v3 9/9] Revert "net: do not leave a dangling sk pointer, when socket creation fails"

Quoted 11 lines · Kuniyuki Iwashima

From: Ignat Korchagin <[email protected]>
Date: Mon, 14 Oct 2024 16:38:08 +0100

Quoted 8 lines

This reverts commit 6cd4a78d962bebbaf8beb7d2ead3f34120e3f7b2.

inet/inet6->create() implementations have been fixed to explicitly NULL the allocated sk object on error.

A warning was put in place to make sure any future changes will not leave a dangling pointer in pf->create() implementations.

Quoted 6 lines
Quoted 4 lines

So this code is now redundant.

#30 lore raw
In reply to Ignat Korchagin
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 5 lines · On Mon, 14 Oct 2024 16:37:59 +0100 you wrote:

Some protocol family create() implementations have an error path after allocating the sk object and calling sock_init_data(). sock_init_data() attaches the allocated sk object to the sock object, provided by the caller.

Quoted 7 lines

If the create() implementation errors out after calling sock_init_data(), it releases the allocated sk object, but the caller ends up having a dangling sk pointer in its sock object on return. Subsequent manipulations on this sock object may try to access the sk pointer, because it is not NULL thus creating a use-after-free scenario.

[...]

Here is the summary with links:
- [net-next,v3,1/9] af_packet: avoid erroring out after sock_init_data() in packet_create()

    https://git.kernel.org/netdev/net-next/c/46f2a11cb82b
  - [net-next,v3,2/9] Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create()
    https://git.kernel.org/netdev/net-next/c/7c4f78cdb8e7
  - [net-next,v3,3/9] Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc()
    https://git.kernel.org/netdev/net-next/c/3945c799f12b
  - [net-next,v3,4/9] net: af_can: do not leave a dangling sk pointer in can_create()
    https://git.kernel.org/netdev/net-next/c/811a7ca7320c
  - [net-next,v3,5/9] net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()
    https://git.kernel.org/netdev/net-next/c/b4fcd63f6ef7
  - [net-next,v3,6/9] net: inet: do not leave a dangling sk pointer in inet_create()
    https://git.kernel.org/netdev/net-next/c/9365fa510c6f
  - [net-next,v3,7/9] net: inet6: do not leave a dangling sk pointer in inet6_create()
    https://git.kernel.org/netdev/net-next/c/9df99c395d0f
  - [net-next,v3,8/9] net: warn, if pf->create does not clear sock->sk on error
    https://git.kernel.org/netdev/net-next/c/48156296a08c
  - [net-next,v3,9/9] Revert "net: do not leave a dangling sk pointer, when socket creation fails"
    https://git.kernel.org/netdev/net-next/c/18429e6e0c2a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#31 lore raw
In reply to Ignat Korchagin
This series was applied to bluetooth/bluetooth-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to bluetooth/bluetooth-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 5 lines · On Mon, 14 Oct 2024 16:37:59 +0100 you wrote:

Some protocol family create() implementations have an error path after allocating the sk object and calling sock_init_data(). sock_init_data() attaches the allocated sk object to the sock object, provided by the caller.

Quoted 7 lines

If the create() implementation errors out after calling sock_init_data(), it releases the allocated sk object, but the caller ends up having a dangling sk pointer in its sock object on return. Subsequent manipulations on this sock object may try to access the sk pointer, because it is not NULL thus creating a use-after-free scenario.

[...]

Here is the summary with links:
- [net-next,v3,1/9] af_packet: avoid erroring out after sock_init_data() in packet_create()

    https://git.kernel.org/bluetooth/bluetooth-next/c/46f2a11cb82b
  - [net-next,v3,2/9] Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create()
    https://git.kernel.org/bluetooth/bluetooth-next/c/7c4f78cdb8e7
  - [net-next,v3,3/9] Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc()
    https://git.kernel.org/bluetooth/bluetooth-next/c/3945c799f12b
  - [net-next,v3,4/9] net: af_can: do not leave a dangling sk pointer in can_create()
    https://git.kernel.org/bluetooth/bluetooth-next/c/811a7ca7320c
  - [net-next,v3,5/9] net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()
    https://git.kernel.org/bluetooth/bluetooth-next/c/b4fcd63f6ef7
  - [net-next,v3,6/9] net: inet: do not leave a dangling sk pointer in inet_create()
    https://git.kernel.org/bluetooth/bluetooth-next/c/9365fa510c6f
  - [net-next,v3,7/9] net: inet6: do not leave a dangling sk pointer in inet6_create()
    https://git.kernel.org/bluetooth/bluetooth-next/c/9df99c395d0f
  - [net-next,v3,8/9] net: warn, if pf->create does not clear sock->sk on error
    https://git.kernel.org/bluetooth/bluetooth-next/c/48156296a08c
  - [net-next,v3,9/9] Revert "net: do not leave a dangling sk pointer, when socket creation fails"
    https://git.kernel.org/bluetooth/bluetooth-next/c/18429e6e0c2a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html