sctp: clean up __sctp_connect function
Subsystems (3)
Xin Long
maintainerv2 cover sctp: clean up __sctp_connect function
This patchset is to factor out some common code for sctp_sendmsg_new_asoc() and __sctp_connect() into 2 new functioins.
v1->v2:
- add the patch 1/5 to avoid a slab-out-of-bounds warning.
- add some code comment for the check change in patch 2/5.
- remove unused 'addrcnt' as Marcelo noticed in patch 3/5.
Xin Long (5):
sctp: only copy the available addr data in sctp_transport_init
sctp: check addr_size with sa_family_t size in
__sctp_setsockopt_connectx
sctp: clean up __sctp_connect
sctp: factor out sctp_connect_new_asoc
sctp: factor out sctp_connect_add_peer
net/sctp/socket.c | 376 ++++++++++++++++++++------------------------------- net/sctp/transport.c | 2 +- 2 files changed, 147 insertions(+), 231 deletions(-)
Signature · 1 line
2.1.0
Xin Long
maintainerPATCH v2 1/5 sctp: only copy the available addr data in sctp_transport_init
'addr' passed to sctp_transport_init is not always a whole size of union sctp_addr, like the path:
sctp_sendmsg() ->
sctp_sendmsg_new_asoc() ->
sctp_assoc_add_peer() ->
sctp_transport_new() -> sctp_transport_init()
In the next patches, we will also pass the address length of data only to sctp_assoc_add_peer().
So sctp_transport_init() should copy the only available data from addr to peer->ipaddr, instead of 'peer->ipaddr = *addr' which may cause slab-out-of-bounds.
--- net/sctp/transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/sctp/transport.c +1 -1
@@ -43,8 +43,8 @@ static struct sctp_transport *sctp_transport_init(struct net *net, gfp_t gfp) {/* Copy in the address. */ - peer->ipaddr = *addr; peer->af_specific =sctp_get_af_specific (addr->sa.sa_family); +memcpy (&peer->ipaddr, addr, peer->af_specific->sockaddr_len);memset (&peer->saddr,0 ,sizeof (union sctp_addr )); peer->sack_generation =0 ;
Signature · 1 line
2.1.0
Xin Long
maintainerPATCH v2 2/5 sctp: check addr_size with sa_family_t size in __sctp_setsockopt_connectx
Now __sctp_connect() is called by __sctp_setsockopt_connectx() and sctp_inet_connect(), the latter has done addr_size check with size of sa_family_t.
In the next patch to clean up __sctp_connect(), we will remove addr_size check with size of sa_family_t from __sctp_connect() for the 1st address.
So before doing that, __sctp_setsockopt_connectx() should do this check first, as sctp_inet_connect() does.
--- net/sctp/socket.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/sctp/socket.c +2 -1
@@ -1311,7 +1311,8 @@ static int __sctp_setsockopt_connectx(struct sock *sk,pr_debug ("%s: sk:%p addrs:%p addrs_size:%d\n" , __func__, sk, addrs, addrs_size); -if (unlikely (addrs_size <=0 )) +/* make sure the 1st addr's sa_family is accessible later */ +if (unlikely (addrs_size <sizeof (sa_family_t)))return -EINVAL ; kaddrs =memdup_user (addrs, addrs_size);
Signature · 1 line
2.1.0
Xin Long
maintainerPATCH v2 3/5 sctp: clean up __sctp_connect
__sctp_connect is doing quit similar things as sctp_sendmsg_new_asoc. To factor out common functions, this patch is to clean up their code to make them look more similar:
1. create the asoc and add a peer with the 1st addr.
2. add peers with the other addrs into this asoc one by one.
while at it, also remove the unused 'addrcnt'.
--- net/sctp/socket.c | 209 +++++++++++++++++++----------------------------------- 1 file changed, 73 insertions(+), 136 deletions(-)
net/sctp/socket.c +73 -136
@@ -1049,153 +1049,105 @@ static int sctp_setsockopt_bindx(struct sock *sk,* Common routine for handling connect() and sctp_connectx(). * Connect will come in with just a single address. */ -static int __sctp_connect (struct sock *sk, -struct sockaddr *kaddrs, -int addrs_size,int flags, -sctp_assoc_t *assoc_id) +static int __sctp_connect (struct sock *sk,struct sockaddr *kaddrs, +int addrs_size,int flags,sctp_assoc_t *assoc_id) { -struct net *net =sock_net (sk); -struct sctp_sock *sp; -struct sctp_endpoint *ep; -struct sctp_association *asoc =NULL ; -struct sctp_association *asoc2; +struct sctp_association *old, *asoc; +struct sctp_sock *sp =sctp_sk (sk); +struct sctp_endpoint *ep = sp->ep;struct sctp_transport *transport; -union sctp_addr to; +struct net *net =sock_net (sk); +void *addr_buf = kaddrs; +union sctp_addr *daddr;enum sctp_scope scope; +struct sctp_af *af; +int walk_size, err;long timeo; -int err =0 ; -int addrcnt =0 ; -int walk_size =0 ; -union sctp_addr *sa_addr =NULL ; -void *addr_buf; -unsigned short port; - sp =sctp_sk (sk); - ep = sp->ep; - -/* connect() cannot be done on a socket that is already in ESTABLISHED -* state - UDP-style peeled off socket or a TCP-style socket that -* is already connected. -* It cannot be done even on a TCP-style listening socket. -*/ if (sctp_sstate (sk,ESTABLISHED ) ||sctp_sstate (sk,CLOSING ) || - (sctp_style (sk,TCP ) &&sctp_sstate (sk,LISTENING ))) { - err = -EISCONN ; - goto out_free; + (sctp_style (sk,TCP ) &&sctp_sstate (sk,LISTENING ))) +return -EISCONN ; + + daddr = addr_buf; + af =sctp_get_af_specific (daddr->sa.sa_family); +if (!af || af->sockaddr_len > addrs_size) +return -EINVAL ; + + err =sctp_verify_addr (sk, daddr, af->sockaddr_len); +if (err) +return err; + + asoc =sctp_endpoint_lookup_assoc (ep, daddr, &transport); +if (asoc) +return asoc->state >=SCTP_STATE_ESTABLISHED ? -EISCONN + : -EALREADY ; + +if (sctp_endpoint_is_peeled_off (ep, daddr)) +return -EADDRNOTAVAIL ; + +if (!ep->base.bind_addr.port) { +if (sctp_autobind (sk)) +return -EAGAIN ; + }else { +if (ep->base.bind_addr.port <inet_prot_sock (net) && + !ns_capable (net->user_ns,CAP_NET_BIND_SERVICE )) +return -EACCES ; } -/* Walk through the addrs buffer and count the number of addresses. */ - addr_buf = kaddrs; -while (walk_size < addrs_size) { -struct sctp_af *af; + scope =sctp_scope (daddr); + asoc =sctp_association_new (ep, sk, scope,GFP_KERNEL ); +if (!asoc) +return -ENOMEM ; -if (walk_size +sizeof (sa_family_t) > addrs_size) { - err = -EINVAL ; - goto out_free; - } + err =sctp_assoc_set_bind_addr_from_ep (asoc, scope,GFP_KERNEL ); +if (err <0 ) + goto out_free; - sa_addr = addr_buf; - af =sctp_get_af_specific (sa_addr->sa.sa_family); + transport =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL ,SCTP_UNKNOWN ); +if (!transport) { + err = -ENOMEM ; + goto out_free; + } -/* If the address family is not supported or if this address -* causes the address buffer to overflow return EINVAL. -*/ -if (!af || (walk_size + af->sockaddr_len) > addrs_size) { - err = -EINVAL ; + addr_buf += af->sockaddr_len; + walk_size = af->sockaddr_len; +while (walk_size < addrs_size) { + err = -EINVAL ; +if (walk_size +sizeof (sa_family_t) > addrs_size) goto out_free; - } - port =ntohs (sa_addr->v4.sin_port); - -/* Save current address so we can work with it */ -memcpy (&to, sa_addr, af->sockaddr_len); + daddr = addr_buf; + af =sctp_get_af_specific (daddr->sa.sa_family); +if (!af || af->sockaddr_len + walk_size > addrs_size) + goto out_free; - err =sctp_verify_addr (sk, &to, af->sockaddr_len); -if (err) +if (asoc->peer.port !=ntohs (daddr->v4.sin_port)) goto out_free; -/* Make sure the destination port is correctly set -* in all addresses. -*/ -if (asoc && asoc->peer.port && asoc->peer.port != port) { - err = -EINVAL ; + err =sctp_verify_addr (sk, daddr, af->sockaddr_len); +if (err) goto out_free; - } -/* Check if there already is a matching association on the -* endpoint (other than the one created here). -*/ - asoc2 =sctp_endpoint_lookup_assoc (ep, &to, &transport); -if (asoc2 && asoc2 != asoc) { -if (asoc2->state >=SCTP_STATE_ESTABLISHED ) - err = -EISCONN ; -else - err = -EALREADY ; + old =sctp_endpoint_lookup_assoc (ep, daddr, &transport); +if (old && old != asoc) { + err = old->state >=SCTP_STATE_ESTABLISHED ? -EISCONN + : -EALREADY ; goto out_free; } -/* If we could not find a matching association on the endpoint, -* make sure that there is no peeled-off association matching -* the peer address even on another socket. -*/ -if (sctp_endpoint_is_peeled_off (ep, &to)) { +if (sctp_endpoint_is_peeled_off (ep, daddr)) { err = -EADDRNOTAVAIL ; goto out_free; } -if (!asoc) { -/* If a bind() or sctp_bindx() is not called prior to -* an sctp_connectx() call, the system picks an -* ephemeral port and will choose an address set -* equivalent to binding with a wildcard address. -*/ -if (!ep->base.bind_addr.port) { -if (sctp_autobind (sk)) { - err = -EAGAIN ; - goto out_free; - } - }else { -/* -* If an unprivileged user inherits a 1-many -* style socket with open associations on a -* privileged port, it MAY be permitted to -* accept new associations, but it SHOULD NOT -* be permitted to open new associations. -*/ -if (ep->base.bind_addr.port < -inet_prot_sock (net) && - !ns_capable (net->user_ns, -CAP_NET_BIND_SERVICE )) { - err = -EACCES ; - goto out_free; - } - } - - scope =sctp_scope (&to); - asoc =sctp_association_new (ep, sk, scope,GFP_KERNEL ); -if (!asoc) { - err = -ENOMEM ; - goto out_free; - } - - err =sctp_assoc_set_bind_addr_from_ep (asoc, scope, -GFP_KERNEL ); -if (err <0 ) { - goto out_free; - } - - } - -/* Prime the peer's transport structures. */ - transport =sctp_assoc_add_peer (asoc, &to,GFP_KERNEL , + transport =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL ,SCTP_UNKNOWN );if (!transport) { err = -ENOMEM ; goto out_free; } - addrcnt++; - addr_buf += af->sockaddr_len; + addr_buf += af->sockaddr_len; walk_size += af->sockaddr_len; }
@@ -1209,39 +1161,24 @@ static int __sctp_connect(struct sock *sk, } err =sctp_primitive_ASSOCIATE (net, asoc,NULL ); -if (err <0 ) { +if (err <0 ) goto out_free; - }/* Initialize sk's dport and daddr for getpeername() */ inet_sk (sk)->inet_dport =htons (asoc->peer.port); - sp->pf->to_sk_daddr (sa_addr, sk); + sp->pf->to_sk_daddr (daddr, sk); sk->sk_err =0 ; - timeo =sock_sndtimeo (sk, flags &O_NONBLOCK ); -if (assoc_id) *assoc_id = asoc->assoc_id; - err =sctp_wait_for_connect (asoc, &timeo); -/* Note: the asoc may be freed after the return of -* sctp_wait_for_connect. -*/ - -/* Don't free association on exit. */ - asoc =NULL ; + timeo =sock_sndtimeo (sk, flags &O_NONBLOCK ); +return sctp_wait_for_connect (asoc, &timeo); out_free:pr_debug ("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n" , __func__, asoc, kaddrs, err); - -if (asoc) { -/* sctp_primitive_ASSOCIATE may have added this association -* To the hash table, try to unhash it, just in case, its a noop -* if it wasn't hashed so we're safe -*/ -sctp_association_free (asoc); - } +sctp_association_free (asoc);return err; }
Signature · 1 line
2.1.0
Xin Long
maintainerPATCH v2 4/5 sctp: factor out sctp_connect_new_asoc
In this function factored out from sctp_sendmsg_new_asoc() and __sctp_connect(), it creates the asoc and adds a peer with the 1st addr.
--- net/sctp/socket.c | 160 ++++++++++++++++++++++++++---------------------------- 1 file changed, 76 insertions(+), 84 deletions(-)
net/sctp/socket.c +75 -83
@@ -1044,6 +1044,73 @@ static int sctp_setsockopt_bindx(struct sock *sk,return err; } +static int sctp_connect_new_asoc (struct sctp_endpoint *ep, +const union sctp_addr *daddr, +const struct sctp_initmsg *init, +struct sctp_transport **tp) +{ +struct sctp_association *asoc; +struct sock *sk = ep->base.sk; +struct net *net =sock_net (sk); +enum sctp_scope scope; +int err; + +if (sctp_endpoint_is_peeled_off (ep, daddr)) +return -EADDRNOTAVAIL ; + +if (!ep->base.bind_addr.port) { +if (sctp_autobind (sk)) +return -EAGAIN ; + }else { +if (ep->base.bind_addr.port <inet_prot_sock (net) && + !ns_capable (net->user_ns,CAP_NET_BIND_SERVICE )) +return -EACCES ; + } + + scope =sctp_scope (daddr); + asoc =sctp_association_new (ep, sk, scope,GFP_KERNEL ); +if (!asoc) +return -ENOMEM ; + + err =sctp_assoc_set_bind_addr_from_ep (asoc, scope,GFP_KERNEL ); +if (err <0 ) + goto free; + + *tp =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL ,SCTP_UNKNOWN ); +if (!*tp) { + err = -ENOMEM ; + goto free; + } + +if (!init) +return 0 ; + +if (init->sinit_num_ostreams) { +__u16 outcnt = init->sinit_num_ostreams; + + asoc->c.sinit_num_ostreams = outcnt; +/* outcnt has been changed, need to re-init stream */ + err =sctp_stream_init (&asoc->stream, outcnt,0 ,GFP_KERNEL ); +if (err) + goto free; + } + +if (init->sinit_max_instreams) + asoc->c.sinit_max_instreams = init->sinit_max_instreams; + +if (init->sinit_max_attempts) + asoc->max_init_attempts = init->sinit_max_attempts; + +if (init->sinit_max_init_timeo) + asoc->max_init_timeo +msecs_to_jiffies (init->sinit_max_init_timeo); + +return 0 ; +free: +sctp_association_free (asoc); +return err; +} +/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) * * Common routine for handling connect() and sctp_connectx().
@@ -1056,10 +1123,8 @@ static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,struct sctp_sock *sp =sctp_sk (sk);struct sctp_endpoint *ep = sp->ep;struct sctp_transport *transport; -struct net *net =sock_net (sk);void *addr_buf = kaddrs;union sctp_addr *daddr; -enum sctp_scope scope;struct sctp_af *af;int walk_size, err;long timeo;
@@ -1082,32 +1147,10 @@ static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,return asoc->state >=SCTP_STATE_ESTABLISHED ? -EISCONN : -EALREADY ; -if (sctp_endpoint_is_peeled_off (ep, daddr)) -return -EADDRNOTAVAIL ; - -if (!ep->base.bind_addr.port) { -if (sctp_autobind (sk)) -return -EAGAIN ; - }else { -if (ep->base.bind_addr.port <inet_prot_sock (net) && - !ns_capable (net->user_ns,CAP_NET_BIND_SERVICE )) -return -EACCES ; - } - - scope =sctp_scope (daddr); - asoc =sctp_association_new (ep, sk, scope,GFP_KERNEL ); -if (!asoc) -return -ENOMEM ; - - err =sctp_assoc_set_bind_addr_from_ep (asoc, scope,GFP_KERNEL ); -if (err <0 ) - goto out_free; - - transport =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL ,SCTP_UNKNOWN ); -if (!transport) { - err = -ENOMEM ; - goto out_free; - } + err =sctp_connect_new_asoc (ep, daddr,NULL , &transport); +if (err) +return err; + asoc = transport->asoc; addr_buf += af->sockaddr_len; walk_size = af->sockaddr_len;
@@ -1160,7 +1203,7 @@ static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs, goto out_free; } - err =sctp_primitive_ASSOCIATE (net, asoc,NULL ); + err =sctp_primitive_ASSOCIATE (sock_net (sk), asoc,NULL );if (err <0 ) goto out_free;
@@ -1597,9 +1640,7 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, struct sctp_transport **tp) {struct sctp_endpoint *ep =sctp_sk (sk)->ep; -struct net *net =sock_net (sk);struct sctp_association *asoc; -enum sctp_scope scope;struct cmsghdr *cmsg;__be32 flowinfo =0 ;struct sctp_af *af;
@@ -1614,20 +1655,6 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, sctp_sstate(sk, CLOSING)))return -EADDRNOTAVAIL ; -if (sctp_endpoint_is_peeled_off (ep, daddr)) -return -EADDRNOTAVAIL ; - -if (!ep->base.bind_addr.port) { -if (sctp_autobind (sk)) -return -EAGAIN ; - }else { -if (ep->base.bind_addr.port <inet_prot_sock (net) && - !ns_capable (net->user_ns,CAP_NET_BIND_SERVICE )) -return -EACCES ; - } - - scope =sctp_scope (daddr); -/* Label connection socket for first association 1-to-many * style for client sequence socket()->sendmsg(). This * needs to be done before sctp_assoc_add_peer() as that will
@@ -1643,45 +1670,10 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,if (err <0 )return err; - asoc =sctp_association_new (ep, sk, scope,GFP_KERNEL ); -if (!asoc) -return -ENOMEM ; - -if (sctp_assoc_set_bind_addr_from_ep (asoc, scope,GFP_KERNEL ) <0 ) { - err = -ENOMEM ; - goto free; - } - -if (cmsgs->init) { -struct sctp_initmsg *init = cmsgs->init; - -if (init->sinit_num_ostreams) { -__u16 outcnt = init->sinit_num_ostreams; - - asoc->c.sinit_num_ostreams = outcnt; -/* outcnt has been changed, need to re-init stream */ - err =sctp_stream_init (&asoc->stream, outcnt,0 , -GFP_KERNEL ); -if (err) - goto free; - } - -if (init->sinit_max_instreams) - asoc->c.sinit_max_instreams = init->sinit_max_instreams; - -if (init->sinit_max_attempts) - asoc->max_init_attempts = init->sinit_max_attempts; - -if (init->sinit_max_init_timeo) - asoc->max_init_timeo -msecs_to_jiffies (init->sinit_max_init_timeo); - } - - *tp =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL ,SCTP_UNKNOWN ); -if (!*tp) { - err = -ENOMEM ; - goto free; - } + err =sctp_connect_new_asoc (ep, daddr, cmsgs->init, tp); +if (err) +return err; + asoc = (*tp)->asoc;if (!cmsgs->addrs_msg)return 0 ;
Signature · 1 line
2.1.0
Xin Long
maintainerPATCH v2 5/5 sctp: factor out sctp_connect_add_peer
In this function factored out from sctp_sendmsg_new_asoc() and __sctp_connect(), it adds a peer with the other addr into the asoc after this asoc is created with the 1st addr.
--- net/sctp/socket.c | 76 +++++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 45 deletions(-)
net/sctp/socket.c +31 -45
@@ -1111,6 +1111,33 @@ static int sctp_connect_new_asoc(struct sctp_endpoint *ep,return err; } +static int sctp_connect_add_peer (struct sctp_association *asoc, +union sctp_addr *daddr,int addr_len) +{ +struct sctp_endpoint *ep = asoc->ep; +struct sctp_association *old; +struct sctp_transport *t; +int err; + + err =sctp_verify_addr (ep->base.sk, daddr, addr_len); +if (err) +return err; + + old =sctp_endpoint_lookup_assoc (ep, daddr, &t); +if (old && old != asoc) +return old->state >=SCTP_STATE_ESTABLISHED ? -EISCONN + : -EALREADY ; + +if (sctp_endpoint_is_peeled_off (ep, daddr)) +return -EADDRNOTAVAIL ; + + t =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL ,SCTP_UNKNOWN ); +if (!t) +return -ENOMEM ; + +return 0 ; +} +/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) * * Common routine for handling connect() and sctp_connectx().
@@ -1119,10 +1146,10 @@ static int sctp_connect_new_asoc(struct sctp_endpoint *ep,static int __sctp_connect (struct sock *sk,struct sockaddr *kaddrs,int addrs_size,int flags,sctp_assoc_t *assoc_id) { -struct sctp_association *old, *asoc;struct sctp_sock *sp =sctp_sk (sk);struct sctp_endpoint *ep = sp->ep;struct sctp_transport *transport; +struct sctp_association *asoc;void *addr_buf = kaddrs;union sctp_addr *daddr;struct sctp_af *af;
@@ -1167,29 +1194,10 @@ static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,if (asoc->peer.port !=ntohs (daddr->v4.sin_port)) goto out_free; - err =sctp_verify_addr (sk, daddr, af->sockaddr_len); + err =sctp_connect_add_peer (asoc, daddr, af->sockaddr_len);if (err) goto out_free; - old =sctp_endpoint_lookup_assoc (ep, daddr, &transport); -if (old && old != asoc) { - err = old->state >=SCTP_STATE_ESTABLISHED ? -EISCONN - : -EALREADY ; - goto out_free; - } - -if (sctp_endpoint_is_peeled_off (ep, daddr)) { - err = -EADDRNOTAVAIL ; - goto out_free; - } - - transport =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL , -SCTP_UNKNOWN ); -if (!transport) { - err = -ENOMEM ; - goto out_free; - } - addr_buf += af->sockaddr_len; walk_size += af->sockaddr_len; }
@@ -1683,8 +1691,6 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,/* sendv addr list parse */ for_each_cmsghdr (cmsg, cmsgs->addrs_msg) { -struct sctp_transport *transport; -struct sctp_association *old;union sctp_addr _daddr;int dlen;
@@ -1718,30 +1724,10 @@ static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, daddr->v6.sin6_port =htons (asoc->peer.port);memcpy (&daddr->v6.sin6_addr,CMSG_DATA (cmsg), dlen); } - err =sctp_verify_addr (sk, daddr,sizeof (*daddr)); -if (err) - goto free; - - old =sctp_endpoint_lookup_assoc (ep, daddr, &transport); -if (old && old != asoc) { -if (old->state >=SCTP_STATE_ESTABLISHED ) - err = -EISCONN ; -else - err = -EALREADY ; - goto free; - } -if (sctp_endpoint_is_peeled_off (ep, daddr)) { - err = -EADDRNOTAVAIL ; - goto free; - } - - transport =sctp_assoc_add_peer (asoc, daddr,GFP_KERNEL , -SCTP_UNKNOWN ); -if (!transport) { - err = -ENOMEM ; + err =sctp_connect_add_peer (asoc, daddr,sizeof (*daddr)); +if (err) goto free; - } }return 0 ;
Signature · 1 line
2.1.0
Marcelo Ricardo Leitner
maintainerRe: [PATCHv2 net-next 1/5] sctp: only copy the available addr data in sctp_transport_init
In reply to Xin LongQuoted 25 lines · Xin Long
'addr' passed to
sctp_transport_initis not always a whole size of unionsctp_addr, like the path:
sctp_sendmsg()->
sctp_sendmsg_new_asoc()->
sctp_assoc_add_peer()->
sctp_transport_new()->sctp_transport_init()In the next patches, we will also pass the address length of data only to
sctp_assoc_add_peer().So
sctp_transport_init()should copy the only available data from addr topeer->ipaddr, instead of 'peer->ipaddr= *addr' which may cause slab-out-of-bounds.Signed-off-by Xin Long <[email protected]>--- net/sctp/transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/sctp/transport.c +0
-0
net/sctp/transport.c static struct sctp_transport *sctp_transport_init(struct net *net, · line 47
Quoted 6 lines
net/sctp/transport.c +1
-1@@ -43,8 +43,8 @@ static struct sctp_transport *sctp_transport_init(struct net *net, gfp_t gfp) {/* Copy in the address. */ - peer->ipaddr = *addr; peer->af_specific =sctp_get_af_specific (addr->sa.sa_family); +memcpy (&peer->ipaddr, addr, peer->af_specific->sockaddr_len);
Just for the record, transports are allocated with kzalloc() and this shouldn't result in any remaining bytes of this buffer to be uninitialized.
That said, unrelated to the patch, memset below and other =0's are not necessary.
Quoted 6 lines
net/sctp/transport.c +0
-0memset (&peer->saddr,0 ,sizeof (union sctp_addr )); peer->sack_generation =0 ;Signature · 1 line
2.1.0
Quoted 9 lines · Xin Long
This patchset is to factor out some common code for
sctp_sendmsg_new_asoc()and__sctp_connect()into 2 new functioins.
v1->v2:
- add the patch 1/5 to avoid a slab-out-of-bounds warning.
- add some code comment for the check change in patch 2/5.
- remove unused 'addrcnt' as Marcelo noticed in patch 3/5.
Quoted 7 lines
Xin Long (5):
sctp: only copy the available addr data insctp_transport_init
sctp: checkaddr_sizewithsa_family_tsize in
__sctp_setsockopt_connectx
sctp: clean up__sctp_connect
sctp: factor outsctp_connect_new_asoc
sctp: factor outsctp_connect_add_peer
Series,
David Miller
maintainerFrom: Marcelo Ricardo Leitner <[email protected]>
Date: Tue, 30 Jul 2019 16:42:31 -0300
Quoted 18 lines · Xin Long
Quoted 9 lines
This patchset is to factor out some common code for
sctp_sendmsg_new_asoc()and__sctp_connect()into 2 new functioins.
v1->v2:
- add the patch 1/5 to avoid a slab-out-of-bounds warning.
- add some code comment for the check change in patch 2/5.
- remove unused 'addrcnt' as Marcelo noticed in patch 3/5.Quoted 7 lines
Xin Long (5):
sctp: only copy the available addr data insctp_transport_init
sctp: checkaddr_sizewithsa_family_tsize in
__sctp_setsockopt_connectx
sctp: clean up__sctp_connect
sctp: factor outsctp_connect_new_asoc
sctp: factor outsctp_connect_add_peer
Quoted 2 lines
Series,
Series applied, thanks.