sctp: Fix races around sctp_inpcb_free()
sctp_close() and sctp_abort() disassociate the PCB from its socket. As a part of this, they attempt to free the PCB, which may end up lingering. Fix some bugs in this area: - For some reason, sctp_close() and sctp_abort() set SCTP_PCB_FLAGS_SOCKET_GONE using an atomic compare-and-set without the PCB lock held. This is racy since sctp_flags is normally updated without atomics, using the PCB lock to synchronize. So, the update can be lost, which can cause all sort of races with other SCTP components which look for the _GONE flag. Fix the problem simply by acquiring the PCB lock in order to set the flag. Note that we have to drop and re-acquire the lock again in sctp_inpcb_free(), but I don't see a good way around that for now. If it's a real problem, the _GONE flag could be split out of sctp_flags and into a dedicated sctp_inpcb field. - In sctp_inpcb_free(), load sctp_socket after acquiring the PCB lock, to avoid possible races with parallel sctp_inpcb_free() calls. - Add an assertion sctp_inpcb_free() to verify that _ALLGONE is not set. Reviewed by: tuexen MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D31811
This commit is contained in:
parent
bb25e36e13
commit
c17b531bed
@ -3321,19 +3321,15 @@ sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
|
||||
/* mark any iterators on the list or being processed */
|
||||
sctp_iterator_inp_being_freed(inp);
|
||||
SCTP_ITERATOR_UNLOCK();
|
||||
so = inp->sctp_socket;
|
||||
if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
|
||||
/* been here before.. eeks.. get out of here */
|
||||
SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
|
||||
#ifdef SCTP_LOG_CLOSING
|
||||
sctp_log_closing(inp, NULL, 1);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
SCTP_ASOC_CREATE_LOCK(inp);
|
||||
SCTP_INP_INFO_WLOCK();
|
||||
|
||||
SCTP_INP_WLOCK(inp);
|
||||
so = inp->sctp_socket;
|
||||
KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
|
||||
("%s: inp %p still has socket", __func__, inp));
|
||||
KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
|
||||
("%s: double free of inp %p", __func__, inp));
|
||||
if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
|
||||
inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
|
||||
/* socket is gone, so no more wakeups allowed */
|
||||
|
@ -416,24 +416,23 @@ sctp_abort(struct socket *so)
|
||||
{
|
||||
struct epoch_tracker et;
|
||||
struct sctp_inpcb *inp;
|
||||
uint32_t flags;
|
||||
|
||||
inp = (struct sctp_inpcb *)so->so_pcb;
|
||||
if (inp == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
SCTP_INP_WLOCK(inp);
|
||||
NET_EPOCH_ENTER(et);
|
||||
sctp_must_try_again:
|
||||
flags = inp->sctp_flags;
|
||||
#ifdef SCTP_LOG_CLOSING
|
||||
sctp_log_closing(inp, NULL, 17);
|
||||
#endif
|
||||
if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
|
||||
(atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
|
||||
if (((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)) {
|
||||
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP;
|
||||
#ifdef SCTP_LOG_CLOSING
|
||||
sctp_log_closing(inp, NULL, 16);
|
||||
#endif
|
||||
SCTP_INP_WUNLOCK(inp);
|
||||
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
|
||||
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
|
||||
SOCK_LOCK(so);
|
||||
@ -448,13 +447,9 @@ sctp_abort(struct socket *so)
|
||||
so->so_pcb = NULL;
|
||||
SOCK_UNLOCK(so);
|
||||
} else {
|
||||
flags = inp->sctp_flags;
|
||||
if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
|
||||
goto sctp_must_try_again;
|
||||
}
|
||||
SCTP_INP_WUNLOCK(inp);
|
||||
}
|
||||
NET_EPOCH_EXIT(et);
|
||||
return;
|
||||
}
|
||||
|
||||
static int
|
||||
@ -516,7 +511,6 @@ sctp_close(struct socket *so)
|
||||
{
|
||||
struct epoch_tracker et;
|
||||
struct sctp_inpcb *inp;
|
||||
uint32_t flags;
|
||||
|
||||
inp = (struct sctp_inpcb *)so->so_pcb;
|
||||
if (inp == NULL)
|
||||
@ -525,25 +519,26 @@ sctp_close(struct socket *so)
|
||||
/*
|
||||
* Inform all the lower layer assoc that we are done.
|
||||
*/
|
||||
SCTP_INP_WLOCK(inp);
|
||||
NET_EPOCH_ENTER(et);
|
||||
sctp_must_try_again:
|
||||
flags = inp->sctp_flags;
|
||||
#ifdef SCTP_LOG_CLOSING
|
||||
sctp_log_closing(inp, NULL, 17);
|
||||
#endif
|
||||
if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
|
||||
(atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
|
||||
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
|
||||
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP;
|
||||
if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
|
||||
(so->so_rcv.sb_cc > 0)) {
|
||||
#ifdef SCTP_LOG_CLOSING
|
||||
sctp_log_closing(inp, NULL, 13);
|
||||
#endif
|
||||
SCTP_INP_WUNLOCK(inp);
|
||||
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
|
||||
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
|
||||
} else {
|
||||
#ifdef SCTP_LOG_CLOSING
|
||||
sctp_log_closing(inp, NULL, 14);
|
||||
#endif
|
||||
SCTP_INP_WUNLOCK(inp);
|
||||
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
|
||||
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
|
||||
}
|
||||
@ -563,13 +558,9 @@ sctp_close(struct socket *so)
|
||||
so->so_pcb = NULL;
|
||||
SOCK_UNLOCK(so);
|
||||
} else {
|
||||
flags = inp->sctp_flags;
|
||||
if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
|
||||
goto sctp_must_try_again;
|
||||
}
|
||||
SCTP_INP_WUNLOCK(inp);
|
||||
}
|
||||
NET_EPOCH_EXIT(et);
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
|
Loading…
Reference in New Issue
Block a user