do not leave dangling sk pointers in pf->create functions
Subsystems (10)
v2 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.
While the first patch in the series should be enough to handle this scenario Eric Dumazet suggested that it would be a good idea to refactor the code for the af_packet implementation to avoid the error path, which leaves a dangling pointer, because it may be better for some tools like kmemleak. I went a bit further and tried to actually fix all the implementations, which could potentially leave a dangling sk pointer.
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 (8):
net: explicitly clear the sk pointer, when pf->create fails
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()
inet6: do not leave a dangling sk pointer in inet6_create()
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 | 7 ++++++- 9 files changed, 46 insertions(+), 44 deletions(-)
Signature · 1 line
2.39.5
PATCH v2 1/8 net: explicitly clear the sk pointer, when pf->create fails
We have recently noticed the exact same KASAN splat as in commit 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket creation fails"). The problem is that commit did not fully address the problem, as some pf->create implementations do not use sk_common_release in their error paths.
For example, we can use the same reproducer as in the above commit, but changing ping to arping. arping uses AF_PACKET socket and if packet_create fails, it will just sk_free the allocated sk object.
While we could chase all the pf->create implementations and make sure they NULL the freed sk object on error from the socket, we can't guarantee future protocols will not make the same mistake.
So it is easier to just explicitly NULL the sk pointer upon return from pf->create in __sock_create. We do know that pf->create always releases the allocated sk object on error, so if the pointer is not NULL, it is definitely dangling.
--- net/core/sock.c | 3 --- net/socket.c | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-)
net/core/sock.c +0 -3
@@ -3819,9 +3819,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
net/socket.c +6 -1
@@ -1574,8 +1574,13 @@ int __sock_create(struct net *net, int family, int type, int protocol,rcu_read_unlock (); err = pf->create (net, sock, protocol, kern); -if (err <0 ) +if (err <0 ) { +/* ->create should release the allocated sock->sk object on error +* but it may leave the dangling pointer +*/ + sock->sk =NULL ; goto out_module_put; + }/* * Now to bump the refcnt of the [loadable] module that owns this
Signature · 1 line
2.39.5
Re: [PATCH v2 1/8] net: explicitly clear the sk pointer, when pf->create fails
From: Ignat Korchagin <[email protected]>
Date: Mon, 7 Oct 2024 22:34:55 +0100
Quoted 14 lines
We have recently noticed the exact same KASAN splat as in commit
6cd4a78d962b("net: do not leave a dangling sk pointer, when socket creation fails"). The problem is that commit did not fully address the problem, as somepf->createimplementations do not usesk_common_releasein their error paths.For example, we can use the same reproducer as in the above commit, but changing ping to arping. arping uses
AF_PACKETsocket and ifpacket_createfails, it will justsk_freethe allocated sk object.While we could chase all the
pf->createimplementations and make sure they NULL the freed sk object on error from the socket, we can't guarantee future protocols will not make the same mistake.
Quoted 7 lines
So it is easier to just explicitly NULL the sk pointer upon return from
pf->createin__sock_create. We do know thatpf->createalways releases the allocated sk object on error, so if the pointer is not NULL, it is definitely dangling.Fixes 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket creation fails")Signed-off-by Ignat Korchagin <[email protected]>
Jakub Kicinski
maintainerRe: [PATCH v2 1/8] net: explicitly clear the sk pointer, when pf->create fails
In reply to Ignat KorchaginQuoted 6 lines · On Mon, 7 Oct 2024 22:34:55 +0100 Ignat Korchagin wrote:
net/socket.c +0
-0@@ -1574,8 +1574,13 @@ int __sock_create(struct net *net, int family, int type, int protocol,rcu_read_unlock ();
net/socket.c int __sock_create(struct net *net, int family, int type, int protocol, · line 1583
Quoted 10 lines
net/socket.c +6
-1err = pf->create (net, sock, protocol, kern); -if (err <0 ) +if (err <0 ) { +/* ->create should release the allocated sock->sk object on error +* but it may leave the dangling pointer +*/ + sock->sk =NULL ; goto out_module_put; + }
This chunk is already in net, as part of the fix you posted earlier. Please resend the cleanup portion with the other patches for net-next on Friday (IOW after net -> net-next merge).
Signature · 1 line
pw-bot: cr
PATCH v2 2/8 af_packet: avoid erroring out after sock_init_data() in packet_create()
In reply to Ignat KorchaginAfter 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
@@ -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
PATCH v2 3/8 Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create()
In reply to Ignat Korchaginbt_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
@@ -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
PATCH v2 4/8 Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc()
In reply to Ignat Korchaginbt_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
@@ -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
PATCH v2 5/8 net: af_can: do not leave a dangling sk pointer in can_create()
In reply to Ignat KorchaginOn 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
@@ -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
Re: [PATCH v2 5/8] net: af_can: do not leave a dangling sk pointer in can_create()
Hi Ignat,
Thanks for the patch.
Quoted 3 lines · Ignat Korchagin
On error
can_create()frees the allocated sk object, butsock_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.
I was about to suggest that this should be backported to stable, but after reading the cover letter, I now understand that this patch is more an improvement to avoid false positives on kmemleak & Cie. Maybe the description could be a bit more nuanced? After patch 1/8 of this series, it seems that the use-after-free is not possible anymore.
Quoted 1 line
Signed-off-by Ignat Korchagin <[email protected]>
See above comment as notwithstanding. This said:
Quoted 20 lines
--- net/can/af_can.c | 1 + 1 file changed, 1 insertion(+)net/can/af_can.c +1
-1@@ -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: --2.39.5
Re: [PATCH v2 5/8] net: af_can: do not leave a dangling sk pointer in can_create()
Quoted 10 lines · Vincent MAILHOL
Hi Ignat,
Thanks for the patch.
Quoted 3 lines · Ignat Korchagin
On error
can_create()frees the allocated sk object, butsock_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.
Quoted 5 lines
I was about to suggest that this should be backported to stable, but after reading the cover letter, I now understand that this patch is more an improvement to avoid false positives on kmemleak & Cie. Maybe the description could be a bit more nuanced? After patch 1/8 of this series, it seems that the use-after-free is not possible anymore.
If we go with Kuniyuki's suggestion in the cover email to replace the explicit NULL with DEBUG_NET_WARN_ON_ONCE(sock->sk) in __sock_create() then use-after-free would be possible again except we will easily catch it. But I will review the description, when I respin the patches to net-next as requested by Jakub.
Quoted 5 lines
Quoted 1 line
Signed-off-by Ignat Korchagin <[email protected]>See above comment as notwithstanding. This said:
Reviewed-by Vincent Mailhol <[email protected]>
Thank you.
Quoted 21 lines
Quoted 21 lines
--- net/can/af_can.c | 1 + 1 file changed, 1 insertion(+)net/can/af_can.c +1
-1@@ -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: --2.39.5
PATCH v2 6/8 net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()
In reply to Ignat Korchaginsock_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
@@ -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
Re: [PATCH v2 6/8] net: ieee802154: do not leave a dangling sk pointer in ieee802154_create()
Hi Ignat,
[email protected] wrote on Mon, 7 Oct 2024 22:35:00 +0100:
Quoted 8 lines
sock_init_data()attaches the allocated sk object to the provided sock object. Ifieee802154_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.
Signed-off-by Ignat Korchagin <[email protected]>
Thanks,
Miquèl
PATCH v2 7/8 net: inet: do not leave a dangling sk pointer in inet_create()
In reply to Ignat Korchaginsock_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
@@ -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
PATCH v2 8/8 inet6: do not leave a dangling sk pointer in inet6_create()
In reply to Ignat Korchaginsock_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
@@ -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
Quoted 1 line
[PATCH v2 0/8] do not leave dangling sk pointers in
pf->createfunctions
For the future patches, please specify the target tree, net or net-next.
From: Ignat Korchagin <[email protected]>
Date: Mon, 7 Oct 2024 22:34:54 +0100
Quoted 11 lines
Some protocol family
create()implementations have an error path after allocating the sk object and callingsock_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 callingsock_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.
Quoted 6 lines
While the first patch in the series should be enough to handle this scenario Eric Dumazet suggested that it would be a good idea to refactor the code for the
af_packetimplementation to avoid the error path, which leaves a dangling pointer, because it may be better for some tools like kmemleak. I went a bit further and tried to actually fix all the implementations, which could potentially leave a dangling sk pointer.
I feel patch 2-8 are net-next materials as the first patch is enough to fix the issue.
Also, once all protocols have moved sock_init_data() after the last failure point, we can change the patch 1's part to
err = pf->create(net, sock, protocol, kern);
if (err) {
DEBUG_NET_WARN_ON_ONCE(sock->sk);
goto out_module_put;
}for the future protocols.