Commit 715f5552 authored by Xin Long's avatar Xin Long Committed by David S. Miller

sctp: hold the transport before using it in sctp_hash_cmp

Since commit 4f008781 ("sctp: apply rhashtable api to send/recv
path"), sctp uses transport rhashtable with .obj_cmpfn sctp_hash_cmp,
in which it compares the members of the transport with the rhashtable
args to check if it's the right transport.

But sctp uses the transport without holding it in sctp_hash_cmp, it can
cause a use-after-free panic. As after it gets transport from hashtable,
another CPU may close the sk and free the asoc. In sctp_association_free,
it frees all the transports, meanwhile, the assoc's refcnt may be reduced
to 0, assoc can be destroyed by sctp_association_destroy.

So after that, transport->assoc is actually an unavailable memory address
in sctp_hash_cmp. Although sctp_hash_cmp is under rcu_read_lock, it still
can not avoid this, as assoc is not freed by RCU.

This patch is to hold the transport before checking it's members with
sctp_transport_hold, in which it checks the refcnt first, holds it if
it's not 0.

Fixes: 4f008781 ("sctp: apply rhashtable api to send/recv path")
Signed-off-by: default avatarXin Long <lucien.xin@gmail.com>
Acked-by: default avatarMarcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3e1be7ad
...@@ -796,27 +796,34 @@ struct sctp_hash_cmp_arg { ...@@ -796,27 +796,34 @@ struct sctp_hash_cmp_arg {
static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg, static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
const void *ptr) const void *ptr)
{ {
struct sctp_transport *t = (struct sctp_transport *)ptr;
const struct sctp_hash_cmp_arg *x = arg->key; const struct sctp_hash_cmp_arg *x = arg->key;
const struct sctp_transport *t = ptr; struct sctp_association *asoc;
struct sctp_association *asoc = t->asoc; int err = 1;
const struct net *net = x->net;
if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr)) if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
return 1; return err;
if (!net_eq(sock_net(asoc->base.sk), net)) if (!sctp_transport_hold(t))
return 1; return err;
asoc = t->asoc;
if (!net_eq(sock_net(asoc->base.sk), x->net))
goto out;
if (x->ep) { if (x->ep) {
if (x->ep != asoc->ep) if (x->ep != asoc->ep)
return 1; goto out;
} else { } else {
if (x->laddr->v4.sin_port != htons(asoc->base.bind_addr.port)) if (x->laddr->v4.sin_port != htons(asoc->base.bind_addr.port))
return 1; goto out;
if (!sctp_bind_addr_match(&asoc->base.bind_addr, if (!sctp_bind_addr_match(&asoc->base.bind_addr,
x->laddr, sctp_sk(asoc->base.sk))) x->laddr, sctp_sk(asoc->base.sk)))
return 1; goto out;
} }
return 0; err = 0;
out:
sctp_transport_put(t);
return err;
} }
static inline u32 sctp_hash_obj(const void *data, u32 len, u32 seed) static inline u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
......
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