From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-sn1nam02on0126.outbound.protection.outlook.com ([104.47.36.126]:27641 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032284AbeCAPcK (ORCPT ); Thu, 1 Mar 2018 10:32:10 -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: return error if the asoc has been peeled off in sctp_wait_for_sndbuf Date: Thu, 1 Mar 2018 15:25:03 +0000 Message-ID: <20180301152116.1486-245-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-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 4.1 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 a0ff660058b88d12625a783ce9e5c1371c87951f ] After commit cea0cc80a677 ("sctp: use the right sk after waking up from wait_buf sleep"), it may change to lock another sk if the asoc has been peeled off in sctp_wait_for_sndbuf. However, the asoc's new sk could be already closed elsewhere, as it's in the sendmsg context of the old sk that can't avoid the new sk's closing. If the sk's last one refcnt is held by this asoc, later on after putting this asoc, the new sk will be freed, while under it's own lock. This patch is to revert that commit, but fix the old issue by returning error under the old sk's lock. Fixes: cea0cc80a677 ("sctp: use the right sk after waking up from wait_buf = sleep") Reported-by: syzbot+ac6ea7baa4432811eb50@syzkaller.appspotmail.com Signed-off-by: Xin Long Acked-by: Neil Horman Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/sctp/socket.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index eb75f69ca372..c44e3d208804 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -83,7 +83,7 @@ static int sctp_writeable(struct sock *sk); static void sctp_wfree(struct sk_buff *skb); static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo= _p, - size_t msg_len, struct sock **orig_sk); + size_t msg_len); static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p); static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p)= ; static int sctp_wait_for_accept(struct sock *sk, long timeo); @@ -1946,7 +1946,7 @@ static int sctp_sendmsg(struct sock *sk, struct msghd= r *msg, size_t msg_len) timeo =3D sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); if (!sctp_wspace(asoc)) { /* sk can be changed by peel off when waiting for buf. */ - err =3D sctp_wait_for_sndbuf(asoc, &timeo, msg_len, &sk); + err =3D sctp_wait_for_sndbuf(asoc, &timeo, msg_len); if (err) { if (err =3D=3D -ESRCH) { /* asoc is already dead. */ @@ -6977,12 +6977,12 @@ void sctp_sock_rfree(struct sk_buff *skb) =20 /* Helper function to wait for space in the sndbuf. */ static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo= _p, - size_t msg_len, struct sock **orig_sk) + size_t msg_len) { struct sock *sk =3D asoc->base.sk; - int err =3D 0; long current_timeo =3D *timeo_p; DEFINE_WAIT(wait); + int err =3D 0; =20 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, *timeo_p, msg_len); @@ -7012,17 +7012,13 @@ static int sctp_wait_for_sndbuf(struct sctp_associa= tion *asoc, long *timeo_p, current_timeo =3D schedule_timeout(current_timeo); BUG_ON(sk !=3D asoc->base.sk); lock_sock(sk); - if (sk !=3D asoc->base.sk) { - release_sock(sk); - sk =3D asoc->base.sk; - lock_sock(sk); - } + if (sk !=3D asoc->base.sk) + goto do_error; =20 *timeo_p =3D current_timeo; } =20 out: - *orig_sk =3D sk; finish_wait(&asoc->wait, &wait); =20 /* Release the association's refcnt. */ --=20 2.14.1