From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934818AbcKDNAY (ORCPT ); Fri, 4 Nov 2016 09:00:24 -0400 Received: from charlotte.tuxdriver.com ([70.61.120.58]:47175 "EHLO smtp.tuxdriver.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934676AbcKDNAW (ORCPT ); Fri, 4 Nov 2016 09:00:22 -0400 Date: Fri, 4 Nov 2016 08:59:58 -0400 From: Neil Horman To: Marcelo Ricardo Leitner Cc: Andrey Konovalov , Vlad Yasevich , "David S. Miller" , linux-sctp@vger.kernel.org, netdev , LKML , syzkaller , Kostya Serebryany , Alexander Potapenko , Eric Dumazet , Dmitry Vyukov Subject: Re: net/sctp: use-after-free in __sctp_connect Message-ID: <20161104125958.GA13691@hmsreliant.think-freely.org> References: <20161019165754.GD2958@localhost.localdomain> <20161103175220.GG8514@localhost.localdomain> <20161103183533.GH8514@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20161103183533.GH8514@localhost.localdomain> User-Agent: Mutt/1.7.1 (2016-10-04) X-Spam-Score: -2.9 (--) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 03, 2016 at 04:35:33PM -0200, Marcelo Ricardo Leitner wrote: > On Thu, Nov 03, 2016 at 07:02:47PM +0100, Andrey Konovalov wrote: > > On Thu, Nov 3, 2016 at 6:52 PM, Marcelo Ricardo Leitner > > wrote: > > > On Thu, Nov 03, 2016 at 06:11:01PM +0100, Andrey Konovalov wrote: > > >> On Wed, Nov 2, 2016 at 11:42 PM, Andrey Konovalov wrote: > > >> > On Wed, Oct 19, 2016 at 6:57 PM, Marcelo Ricardo Leitner > > >> > wrote: > > >> >> On Wed, Oct 19, 2016 at 02:25:24PM +0200, Andrey Konovalov wrote: > > >> >>> Hi, > > >> >>> > > >> >>> I've got the following error report while running the syzkaller fuzzer: > > >> >>> > > >> >>> ================================================================== > > >> >>> BUG: KASAN: use-after-free in __sctp_connect+0xabe/0xbf0 at addr > > >> >>> ffff88006b1dc610 > > >> >> > > >> >> Seems this is the same that Dmitry Vyukov had reported back in Jan 13th. > > >> >> So far I couldn't identify the reason. > > >> >> "Good" to know it's still there, thanks for reporting it. > > >> > > >> Hi Marcelo, > > >> > > > > > > Hi > > > > > >> So I've looked at the code. > > >> As far as I understand, the problem is a race condition between > > >> setsockopt(SCTP_SOCKOPT_CONNECTX) and shutdown on an sctp socket. > > >> setsockopt() calls sctp_wait_for_connect(), which exits the for loop > > >> on the sk->sk_shutdown & RCV_SHUTDOWN if clause, and then frees asoc > > >> with sctp_association_put() and returns err = 0. > > >> Then __sctp_connect() checks that err == 0 and reads asoc->assoc_id > > >> from the freed asoc. > > > > > > Suddenly this seems familiar. Your description makes sense, thanks for > > > looking deeper into this, Andrey. > > > > > > This fix should do it, can you please try it? I'll post it properly > > > if it works. > > > > Yes, it fixes the issue. > > > > Tested-by: Andrey Konovalov > > > > Thanks for the fix! > > Ahm this other fix looks better: do the read before calling > sctp_wait_for_connect() as that id won't change in this call and the > application shouldn't trust this number if an error is returned, so > there should be no issues by returning it in such situation. > > Can you please confirm this one also works? Thanks! > > ---8<--- > > diff --git a/net/sctp/socket.c b/net/sctp/socket.c > index 6cdc61c21438..be1d9bb98230 100644 > --- a/net/sctp/socket.c > +++ b/net/sctp/socket.c > @@ -1214,9 +1214,12 @@ static int __sctp_connect(struct sock *sk, > > timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); > > - err = sctp_wait_for_connect(asoc, &timeo); > - if ((err == 0 || err == -EINPROGRESS) && assoc_id) > + if (assoc_id) > *assoc_id = asoc->assoc_id; > + err = sctp_wait_for_connect(asoc, &timeo); > + /* Note: the asoc may be freed after the return of > + * sctp_wait_for_connect. > + */ > > /* Don't free association on exit. */ > asoc = NULL; > Agreed, this version looks better. Please repost it with a proper changelog and I'll ack it. Neil From mboxrd@z Thu Jan 1 00:00:00 1970 From: Neil Horman Date: Fri, 04 Nov 2016 12:59:58 +0000 Subject: Re: net/sctp: use-after-free in __sctp_connect Message-Id: <20161104125958.GA13691@hmsreliant.think-freely.org> List-Id: References: <20161019165754.GD2958@localhost.localdomain> <20161103175220.GG8514@localhost.localdomain> <20161103183533.GH8514@localhost.localdomain> In-Reply-To: <20161103183533.GH8514@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Marcelo Ricardo Leitner Cc: Andrey Konovalov , Vlad Yasevich , "David S. Miller" , linux-sctp@vger.kernel.org, netdev , LKML , syzkaller , Kostya Serebryany , Alexander Potapenko , Eric Dumazet , Dmitry Vyukov On Thu, Nov 03, 2016 at 04:35:33PM -0200, Marcelo Ricardo Leitner wrote: > On Thu, Nov 03, 2016 at 07:02:47PM +0100, Andrey Konovalov wrote: > > On Thu, Nov 3, 2016 at 6:52 PM, Marcelo Ricardo Leitner > > wrote: > > > On Thu, Nov 03, 2016 at 06:11:01PM +0100, Andrey Konovalov wrote: > > >> On Wed, Nov 2, 2016 at 11:42 PM, Andrey Konovalov wrote: > > >> > On Wed, Oct 19, 2016 at 6:57 PM, Marcelo Ricardo Leitner > > >> > wrote: > > >> >> On Wed, Oct 19, 2016 at 02:25:24PM +0200, Andrey Konovalov wrote: > > >> >>> Hi, > > >> >>> > > >> >>> I've got the following error report while running the syzkaller fuzzer: > > >> >>> > > >> >>> ================================= > > >> >>> BUG: KASAN: use-after-free in __sctp_connect+0xabe/0xbf0 at addr > > >> >>> ffff88006b1dc610 > > >> >> > > >> >> Seems this is the same that Dmitry Vyukov had reported back in Jan 13th. > > >> >> So far I couldn't identify the reason. > > >> >> "Good" to know it's still there, thanks for reporting it. > > >> > > >> Hi Marcelo, > > >> > > > > > > Hi > > > > > >> So I've looked at the code. > > >> As far as I understand, the problem is a race condition between > > >> setsockopt(SCTP_SOCKOPT_CONNECTX) and shutdown on an sctp socket. > > >> setsockopt() calls sctp_wait_for_connect(), which exits the for loop > > >> on the sk->sk_shutdown & RCV_SHUTDOWN if clause, and then frees asoc > > >> with sctp_association_put() and returns err = 0. > > >> Then __sctp_connect() checks that err = 0 and reads asoc->assoc_id > > >> from the freed asoc. > > > > > > Suddenly this seems familiar. Your description makes sense, thanks for > > > looking deeper into this, Andrey. > > > > > > This fix should do it, can you please try it? I'll post it properly > > > if it works. > > > > Yes, it fixes the issue. > > > > Tested-by: Andrey Konovalov > > > > Thanks for the fix! > > Ahm this other fix looks better: do the read before calling > sctp_wait_for_connect() as that id won't change in this call and the > application shouldn't trust this number if an error is returned, so > there should be no issues by returning it in such situation. > > Can you please confirm this one also works? Thanks! > > ---8<--- > > diff --git a/net/sctp/socket.c b/net/sctp/socket.c > index 6cdc61c21438..be1d9bb98230 100644 > --- a/net/sctp/socket.c > +++ b/net/sctp/socket.c > @@ -1214,9 +1214,12 @@ static int __sctp_connect(struct sock *sk, > > timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); > > - err = sctp_wait_for_connect(asoc, &timeo); > - if ((err = 0 || err = -EINPROGRESS) && assoc_id) > + if (assoc_id) > *assoc_id = asoc->assoc_id; > + err = sctp_wait_for_connect(asoc, &timeo); > + /* Note: the asoc may be freed after the return of > + * sctp_wait_for_connect. > + */ > > /* Don't free association on exit. */ > asoc = NULL; > Agreed, this version looks better. Please repost it with a proper changelog and I'll ack it. Neil