Return-Path: Received: from mail-sn1nam02on0136.outbound.protection.outlook.com ([104.47.36.136]:55810 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932599AbeARVEh (ORCPT ); Thu, 18 Jan 2018 16:04:37 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Xin Long , "David S . Miller" , Sasha Levin Subject: [added to the 4.1 stable tree] sctp: do not free asoc when it is already dead in sctp_sendmsg Date: Thu, 18 Jan 2018 21:01:08 +0000 Message-ID: <20180118205908.3220-197-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Xin Long This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit ca3af4dd28cff4e7216e213ba3b671fbf9f84758 ] Now in sctp_sendmsg sctp_wait_for_sndbuf could schedule out without holding sock sk. It means the current asoc can be freed elsewhere, like when receiving an abort packet. If the asoc is just created in sctp_sendmsg and sctp_wait_for_sndbuf returns err, the asoc will be freed again due to new_asoc is not nil. An use-after-free issue would be triggered by this. This patch is to fix it by setting new_asoc with nil if the asoc is already dead when cpu schedules back, so that it will not be freed again in sctp_sendmsg. v1->v2: set new_asoc as nil in sctp_sendmsg instead of sctp_wait_for_sndbuf. Suggested-by: Neil Horman Reported-by: Dmitry Vyukov Signed-off-by: Xin Long Acked-by: Neil Horman Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/sctp/socket.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index cb7193ed4284..5ad280d102c3 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -1948,8 +1948,14 @@ static int sctp_sendmsg(struct sock *sk, struct msgh= dr *msg, size_t msg_len) timeo =3D sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); if (!sctp_wspace(asoc)) { err =3D sctp_wait_for_sndbuf(asoc, &timeo, msg_len); - if (err) + if (err) { + if (err =3D=3D -ESRCH) { + /* asoc is already dead. */ + new_asoc =3D NULL; + err =3D -EPIPE; + } goto out_free; + } } =20 /* If an address is passed with the sendto/sendmsg call, it is used @@ -6989,10 +6995,11 @@ static int sctp_wait_for_sndbuf(struct sctp_associa= tion *asoc, long *timeo_p, for (;;) { prepare_to_wait_exclusive(&asoc->wait, &wait, TASK_INTERRUPTIBLE); + if (asoc->base.dead) + goto do_dead; if (!*timeo_p) goto do_nonblock; - if (sk->sk_err || asoc->state >=3D SCTP_STATE_SHUTDOWN_PENDING || - asoc->base.dead) + if (sk->sk_err || asoc->state >=3D SCTP_STATE_SHUTDOWN_PENDING) goto do_error; if (signal_pending(current)) goto do_interrupted; @@ -7018,6 +7025,10 @@ static int sctp_wait_for_sndbuf(struct sctp_associat= ion *asoc, long *timeo_p, =20 return err; =20 +do_dead: + err =3D -ESRCH; + goto out; + do_error: err =3D -EPIPE; goto out; --=20 2.11.0