Skip to contentSkip to conversation

net: sctp: use sk_copy_sanitize for accept sockets

RFC PATCH In review Daniel Borkmann 1 participant 2 messages Dec 24, 2013 – Dec 25, 2013

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

RFC PATCH 3/3 net: sctp: use sk_copy_sanitize for accept sockets

Wang reported an issue that lksctp's test_getname_v6 seems to fail.

The issue is that we do not copy sk_v6_rcv_saddr over to the new socket, although the comment above says so regarding rcv_saddr.

Commit 914e1c8b6980 ("sctp: Inherit all socket options from parent correctly.") originally moved that over to sctp_copy_sock(), but after commit efe4208f47f9 ("ipv6: make lookups simpler and faster") this no longer holds and the actual value of sk_v6_rcv_saddr was no longer being migrated.

The issue seems to be deeper than that though. We always used to do a partial copy of socket members in accept/peeloff case. This means that also newly added members to the kernel's socket representation weren't inherited to spawned sockets, for example, Eric points out that settings of SO_MAX_PACING_RATE would be broken on SCTP as well.

So lets get rid of this restriction by using sk_copy_sanitize(). With this patch, the lksctp test suite also passes again for IPv6.

Fixes efe4208f47f9 ("ipv6: make lookups simpler and faster")
---
 include/net/sctp/sctp.h |   4 +-
 net/sctp/ipv6.c         |  11 +++---
 net/sctp/protocol.c     |  13 +++---
 net/sctp/socket.c       | 102 +++++++++++++++++++++++++++++-------------------
 4 files changed, 75 insertions(+), 55 deletions(-)
include/net/sctp/sctp.h +2 -2
index 610a8c8..8422bb5 100644
@@ -105,8 +105,8 @@ void sctp_data_ready(struct sock *sk, int len);
 unsigned int sctp_poll(struct file *file, struct socket *sock,
 		poll_table *wait);
 void sctp_sock_rfree(struct sk_buff *skb);
-void sctp_copy_sock(struct sock *newsk, struct sock *sk,
-		    struct sctp_association *asoc);
+struct sock *sctp_clone_lock(const struct sock *sk, const gfp_t priority,
+			     struct sctp_association *asoc);
 extern struct percpu_counter sctp_sockets_allocated;
 int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
 
net/sctp/ipv6.c +5 -6
index 14191ab..27879c5 100644
@@ -639,13 +639,10 @@ static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
 	struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
 	struct sctp6_sock *newsctp6sk;
 
-	newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot);
+	newsk = sctp_clone_lock(sk, GFP_KERNEL, asoc);
 	if (!newsk)
 		goto out;
 
-	sock_init_data(NULL, newsk);
-
-	sctp_copy_sock(newsk, sk, asoc);
 	sock_reset_flag(sk, SOCK_ZAPPED);
 
 	newsctp6sk = (struct sctp6_sock *)newsk;
@@ -654,7 +651,6 @@ static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
 	sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
 
 	newnp = inet6_sk(newsk);
-
 	memcpy(newnp, np, sizeof(struct ipv6_pinfo));
 
 	/* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
@@ -668,8 +664,11 @@ static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
 		sk_common_release(newsk);
 		newsk = NULL;
 	}
-
 out:
+	if (newsk) {
+		bh_unlock_sock(newsk);
+		sock_put(newsk);
+	}
 	return newsk;
 }
 
net/sctp/protocol.c +6 -7
index 19bd4c5..1058a77 100644
@@ -544,20 +544,16 @@ static int sctp_v4_is_ce(const struct sk_buff *skb)
 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
 					     struct sctp_association *asoc)
 {
-	struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
-			sk->sk_prot);
+	struct sock *newsk;
 	struct inet_sock *newinet;
 
+	newsk = sctp_clone_lock(sk, GFP_KERNEL, asoc);
 	if (!newsk)
 		goto out;
 
-	sock_init_data(NULL, newsk);
-
-	sctp_copy_sock(newsk, sk, asoc);
 	sock_reset_flag(newsk, SOCK_ZAPPED);
 
 	newinet = inet_sk(newsk);
-
 	newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
 
 	sk_refcnt_debug_inc(newsk);
@@ -566,8 +562,11 @@ static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
 		sk_common_release(newsk);
 		newsk = NULL;
 	}
-
 out:
+	if (newsk) {
+		bh_unlock_sock(newsk);
+		sock_put(newsk);
+	}
 	return newsk;
 }
 
net/sctp/socket.c +62 -40

132 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 18 lines · Daniel Borkmann

Wang reported an issue that lksctp's test_getname_v6 seems to fail.

The issue is that we do not copy sk_v6_rcv_saddr over to the new socket, although the comment above says so regarding rcv_saddr.

Commit 914e1c8b6980 ("sctp: Inherit all socket options from parent correctly.") originally moved that over to sctp_copy_sock(), but after commit efe4208f47f9 ("ipv6: make lookups simpler and faster") this no longer holds and the actual value of sk_v6_rcv_saddr was no longer being migrated.

The issue seems to be deeper than that though. We always used to do a partial copy of socket members in accept/peeloff case. This means that also newly added members to the kernel's socket representation weren't inherited to spawned sockets, for example, Eric points out that settings of SO_MAX_PACING_RATE would be broken on SCTP as well.

Quoted 8 lines

So lets get rid of this restriction by using sk_copy_sanitize(). With this patch, the lksctp test suite also passes again for IPv6.

Fixes efe4208f47f9 ("ipv6: make lookups simpler and faster")

---

One more thing for the non-RFC later on:

net/sctp/ipv6.c static struct sock *sctp_v6_create_accept_sk(struct sock *sk, · line 670

Quoted 4 lines

out:

net/sctp/ipv6.c +3 -0
+	if (newsk) {
+		bh_unlock_sock(newsk);
+		sock_put(newsk);

This also needs to be done in error path.

Quoted 3 lines
net/sctp/ipv6.c +1 -0
+	}
  	return newsk;
  }