Skip to contentSkip to conversation

sock_diag: invert socket destroy broadcast check

PATCH In review Willem de Bruijn 2 participants 4 messages Jun 24, 2016 – Jul 13, 2016

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

PATCH 1/1 sock_diag: invert socket destroy broadcast check

From: Willem de Bruijn <[email protected]>

Socket destruction is only broadcast for a socket sk if a diag listener is registered and sk is not a kernel socket.

Invert the test to not even check for listeners for kernel sockets.

The sock_diag_has_destroy_listeners invocation dereferences sock_net(sk), which for kernel sockets can be invalid as they do not take a reference on the network namespace.

Fixes b922622ec6ef ("sock_diag: don't broadcast kernel sockets")
Fixes 26abe14379f8 ("net: Modify sk_alloc to not reference count the

netns of kernel sockets.")

---

This patch fixes this immediate codepath. A broader issue of live kernel sockets pointing to deleted namespaces may persist.

I observed skbs queued on a device queue in another namespace from a kernel socket in SOCK_DEAD state with dangling sock_net(sk). Socket refcnt is zero, but sk_wmem_alloc is not. (This was on an older kernel, have not yet tried to reproduce on net).

It seems that we may need to reintroduce namespace reference counting for kernel sockets (with two-stage deletion to avoid the circular reference), scrub packets between namespaces, or reparent kernel sockets to init_net on namespace destruction.

---
 net/core/sock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/sock.c +1 -1
index 08bf97e..ba082b4 100644
@@ -1473,7 +1473,7 @@ void sk_destruct(struct sock *sk)
 
 static void __sk_free(struct sock *sk)
 {
-	if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt))
+	if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
 		sock_diag_broadcast_destroy(sk);
 	else
 		sk_destruct(sk);
Signature · 1 line
2.8.0.rc3.226.g39d4020
#1 lore raw
Quoted 10 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Socket destruction is only broadcast for a socket sk if a diag listener is registered and sk is not a kernel socket.

Invert the test to not even check for listeners for kernel sockets.

The sock_diag_has_destroy_listeners invocation dereferences sock_net(sk), which for kernel sockets can be invalid as they do not take a reference on the network namespace.

No. That isn't so. A kernel socket for a network namespace must be destroyed in the network namespace teardown.

Therefore sock_net(sk) should always be valid. If sock_net(sk) is not valid something very broken is going on.

Eric

Quoted 36 lines
Fixes b922622ec6ef ("sock_diag: don't broadcast kernel sockets")
Fixes 26abe14379f8 ("net: Modify sk_alloc to not reference count the

netns of kernel sockets.")

---

This patch fixes this immediate codepath. A broader issue of live kernel sockets pointing to deleted namespaces may persist.

I observed skbs queued on a device queue in another namespace from a kernel socket in SOCK_DEAD state with dangling sock_net(sk). Socket refcnt is zero, but sk_wmem_alloc is not. (This was on an older kernel, have not yet tried to reproduce on net).

It seems that we may need to reintroduce namespace reference counting for kernel sockets (with two-stage deletion to avoid the circular reference), scrub packets between namespaces, or reparent kernel sockets to init_net on namespace destruction.

---
 net/core/sock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/sock.c +1 -1
index 08bf97e..ba082b4 100644
@@ -1473,7 +1473,7 @@ void sk_destruct(struct sock *sk)
 
 static void __sk_free(struct sock *sk)
 {
-	if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt))
+	if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
 		sock_diag_broadcast_destroy(sk);
 	else
 		sk_destruct(sk);
#2 lore raw
Quoted 7 lines · Eric W. Biederman
Quoted 5 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Socket destruction is only broadcast for a socket sk if a diag listener is registered and sk is not a kernel socket.

Quoted 8 lines
Quoted 5 lines

Invert the test to not even check for listeners for kernel sockets.

The sock_diag_has_destroy_listeners invocation dereferences sock_net(sk), which for kernel sockets can be invalid as they do not take a reference on the network namespace.

No. That isn't so. A kernel socket for a network namespace must be destroyed in the network namespace teardown.

The issue would be an skbuff with a reference to that sk and sock_wfree as destructor escaping the network namespace with that reference intact.

Both macvlan and veth scrub packets before passing them between namespaces. That alone should prevent this.

I'll try to reproduce with a short dedicated test on the current net branch.

Quoted 4 lines
Quoted 4 lines

I observed skbs queued on a device queue in another namespace from a kernel socket in SOCK_DEAD state with dangling sock_net(sk). Socket refcnt is zero, but sk_wmem_alloc is not. (This was on an older kernel, have not yet tried to reproduce on net).

#3 lore raw
Quoted 9 lines · Willem de Bruijn
Quoted 7 lines · Eric W. Biederman
Quoted 5 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Socket destruction is only broadcast for a socket sk if a diag listener is registered and sk is not a kernel socket.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Invert the test to not even check for listeners for kernel sockets.

The sock_diag_has_destroy_listeners invocation dereferences sock_net(sk), which for kernel sockets can be invalid as they do not take a reference on the network namespace.

No. That isn't so. A kernel socket for a network namespace must be destroyed in the network namespace teardown.

I spent some more time looking at this.

inet_ctl_sock_destroy does not destroy the socket if there are still skbuff with a reference on it (or its sk_wmem_alloc). Skbs are orphaned when they leave the namespace through dev_forward_skb, but not when sent out a physical nic (correctly, that would break TSQ).

The bug happened with macvlan on top of bonding on top of a physical nic. The macvlan lives in a temporary namespace. After the macvlan and network namespace are destroyed, the physical device has a TCP RST skb from net.ipv4->tcp_sk queued for tx completion.

I have not able to reproduce this exact scenario, likely because tx completion handling is on the order of microseconds and not easily slowed sufficiently for testing. Using a tap device with skb_orphan commented out, I can cause the issue. Commenting out skb_orrphan is clearly a gross hack. The point I wanted to verify is that underlying device is not stopped --and its queues cleaned of skb-- when the macvlan device is destroyed.

Network namespace teardown is complex. Am I missing a step that does prevents the above, or does this indeed sound feasible in principle (if very unlikely in practice)?