Commit 1eed6779 authored by Xin Long's avatar Xin Long Committed by David S. Miller

sctp: fix the transport dead race check by using atomic_add_unless on refcnt

Now when __sctp_lookup_association is running in BH, it will try to
check if t->dead is set, but meanwhile other CPUs may be freeing this
transport and this assoc and if it happens that
__sctp_lookup_association checked t->dead a bit too early, it may think
that the association is still good while it was already freed.

So we fix this race by using atomic_add_unless in sctp_transport_hold.
After we get one transport from hashtable, we will hold it only when
this transport's refcnt is not 0, so that we can make sure t->asoc
cannot be freed before we hold the asoc again.

Note that sctp association is not freed using RCU so we can't use
atomic_add_unless() with it as it may just be too late for that either.

Fixes: 4f008781 ("sctp: apply rhashtable api to send/recv path")
Reported-by: default avatarVlad Yasevich <vyasevich@gmail.com>
Signed-off-by: default avatarXin Long <lucien.xin@gmail.com>
Signed-off-by: default avatarMarcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 2baaa2d1
...@@ -955,7 +955,7 @@ void sctp_transport_route(struct sctp_transport *, union sctp_addr *, ...@@ -955,7 +955,7 @@ void sctp_transport_route(struct sctp_transport *, union sctp_addr *,
void sctp_transport_pmtu(struct sctp_transport *, struct sock *sk); void sctp_transport_pmtu(struct sctp_transport *, struct sock *sk);
void sctp_transport_free(struct sctp_transport *); void sctp_transport_free(struct sctp_transport *);
void sctp_transport_reset_timers(struct sctp_transport *); void sctp_transport_reset_timers(struct sctp_transport *);
void sctp_transport_hold(struct sctp_transport *); int sctp_transport_hold(struct sctp_transport *);
void sctp_transport_put(struct sctp_transport *); void sctp_transport_put(struct sctp_transport *);
void sctp_transport_update_rto(struct sctp_transport *, __u32); void sctp_transport_update_rto(struct sctp_transport *, __u32);
void sctp_transport_raise_cwnd(struct sctp_transport *, __u32, __u32); void sctp_transport_raise_cwnd(struct sctp_transport *, __u32, __u32);
......
...@@ -935,15 +935,22 @@ static struct sctp_association *__sctp_lookup_association( ...@@ -935,15 +935,22 @@ static struct sctp_association *__sctp_lookup_association(
struct sctp_transport **pt) struct sctp_transport **pt)
{ {
struct sctp_transport *t; struct sctp_transport *t;
struct sctp_association *asoc = NULL;
rcu_read_lock();
t = sctp_addrs_lookup_transport(net, local, peer); t = sctp_addrs_lookup_transport(net, local, peer);
if (!t || t->dead) if (!t || !sctp_transport_hold(t))
return NULL; goto out;
sctp_association_hold(t->asoc); asoc = t->asoc;
sctp_association_hold(asoc);
*pt = t; *pt = t;
return t->asoc; sctp_transport_put(t);
out:
rcu_read_unlock();
return asoc;
} }
/* Look up an association. protected by RCU read lock */ /* Look up an association. protected by RCU read lock */
...@@ -955,9 +962,7 @@ struct sctp_association *sctp_lookup_association(struct net *net, ...@@ -955,9 +962,7 @@ struct sctp_association *sctp_lookup_association(struct net *net,
{ {
struct sctp_association *asoc; struct sctp_association *asoc;
rcu_read_lock();
asoc = __sctp_lookup_association(net, laddr, paddr, transportp); asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
rcu_read_unlock();
return asoc; return asoc;
} }
......
...@@ -296,9 +296,9 @@ void sctp_transport_route(struct sctp_transport *transport, ...@@ -296,9 +296,9 @@ void sctp_transport_route(struct sctp_transport *transport,
} }
/* Hold a reference to a transport. */ /* Hold a reference to a transport. */
void sctp_transport_hold(struct sctp_transport *transport) int sctp_transport_hold(struct sctp_transport *transport)
{ {
atomic_inc(&transport->refcnt); return atomic_add_unless(&transport->refcnt, 1, 0);
} }
/* Release a reference to a transport and clean up /* Release a reference to a transport and clean up
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment