Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1519218ybb; Sat, 11 Apr 2020 05:30:25 -0700 (PDT) X-Google-Smtp-Source: APiQypLvQtRhgk1/lwd5J7K1eq1XjIyc1ToHpqGyFBjdNNM4xM7MmI/KfSyEv5Q6UtXy4FbReK8E X-Received: by 2002:aed:26c6:: with SMTP id q64mr3290322qtd.147.1586608224887; Sat, 11 Apr 2020 05:30:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586608224; cv=none; d=google.com; s=arc-20160816; b=sacmI9b/9gW4Zkmb86IIllViDivVNLvd5AflmQ460Mlwg0sSJ1iqXeJMJOcFv96Cus lPp7zPGRDklZBHUnr0AitBuDu1H1Xyfyuza5mS+4Q5r2ANGOosgaepR38WJJapPd7cPw Fuuo8RFAxiQ1K8GmKxsAEfYUWmL6OJDOtOUOUaNqYA5p22vuKH+RP50QCgOOgWuUnyTJ K8RLcnmQFYeJFLnEOFCOh1pC+SrfcW7eEFAff6mFepqZ4wbdHOWcnmLNto3X9Vvurcxt 8D2vZsMtwqhzPLiScaXuWnt3Uz5kegwLfJx1wSqn73VjVJ0VfTRGKndylxaky6pW+vyy FhMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0YOyGp7SPziVeLxosg7qZS8dJaZy3Fnck9e0tsB3Sug=; b=Bo0dBETz8VjR6r4Vic1jezDn5Bue3bmIT1zJ80YIwnDRVMs++3icVb+8l4dnprEvpb NdY2x8DkeCPor3fuTci2YR4xsFMsWEJOepf59xd831Yc6GpFF1hYLddRv40V+NC5ZPqm tuBUh1AlqwirQwDYLBVi6O5toCOKzkpTSgVruRv8fR1ZTHRXl/EhxudCWEp2WoFM6ozw M8PeRyIZYbMf2B3BhHc6CMA5Wu/tGb8vZalY6lAcRJ6tb9A5nJzcSuKdVz5/Yxv0mMR1 NMalCrjNzXOkpJL5kklSupY7/cWO0JA+mC4tOCmcCLbH7ZaLr/7fDVPpAHYlAuacvc28 m9uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=q+kNN04F; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q34si2652439qtb.216.2020.04.11.05.30.10; Sat, 11 Apr 2020 05:30:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=q+kNN04F; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727899AbgDKM1h (ORCPT + 99 others); Sat, 11 Apr 2020 08:27:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:47186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727004AbgDKMN6 (ORCPT ); Sat, 11 Apr 2020 08:13:58 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 864BD20787; Sat, 11 Apr 2020 12:13:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1586607238; bh=CmYp6LY6QUK3hkLWDrNmhADYefltpUV5ADFsUoxWmLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q+kNN04F4UOvxMXxnaxz3tFnaQu0Ul15KIHEaVXyMIayvyvAq+5wXYSBz6N6NxQd/ /z+w5jzoU5cJTvXRUNmeUpHxZx9+EQGygveAavWkD0UAAIi4NHIuyElN6fl6J/ygql lTpi6cSDjpV5Kn2A5+rtHX+8QqOVtl4pnIpm0SFM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qiujun Huang , Marcelo Ricardo Leitner , "David S. Miller" , syzbot+cea71eec5d6de256d54d@syzkaller.appspotmail.com Subject: [PATCH 4.14 03/38] sctp: fix refcount bug in sctp_wfree Date: Sat, 11 Apr 2020 14:08:47 +0200 Message-Id: <20200411115438.064353891@linuxfoundation.org> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200411115437.795556138@linuxfoundation.org> References: <20200411115437.795556138@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qiujun Huang [ Upstream commit 5c3e82fe159622e46e91458c1a6509c321a62820 ] We should iterate over the datamsgs to move all chunks(skbs) to newsk. The following case cause the bug: for the trouble SKB, it was in outq->transmitted list sctp_outq_sack sctp_check_transmitted SKB was moved to outq->sacked list then throw away the sack queue SKB was deleted from outq->sacked (but it was held by datamsg at sctp_datamsg_to_asoc So, sctp_wfree was not called here) then migrate happened sctp_for_each_tx_datachunk( sctp_clear_owner_w); sctp_assoc_migrate(); sctp_for_each_tx_datachunk( sctp_set_owner_w); SKB was not in the outq, and was not changed to newsk finally __sctp_outq_teardown sctp_chunk_put (for another skb) sctp_datamsg_put __kfree_skb(msg->frag_list) sctp_wfree (for SKB) SKB->sk was still oldsk (skb->sk != asoc->base.sk). Reported-and-tested-by: syzbot+cea71eec5d6de256d54d@syzkaller.appspotmail.com Signed-off-by: Qiujun Huang Acked-by: Marcelo Ricardo Leitner Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sctp/socket.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -175,29 +175,44 @@ static void sctp_clear_owner_w(struct sc skb_orphan(chunk->skb); } +#define traverse_and_process() \ +do { \ + msg = chunk->msg; \ + if (msg == prev_msg) \ + continue; \ + list_for_each_entry(c, &msg->chunks, frag_list) { \ + if ((clear && asoc->base.sk == c->skb->sk) || \ + (!clear && asoc->base.sk != c->skb->sk)) \ + cb(c); \ + } \ + prev_msg = msg; \ +} while (0) + static void sctp_for_each_tx_datachunk(struct sctp_association *asoc, + bool clear, void (*cb)(struct sctp_chunk *)) { + struct sctp_datamsg *msg, *prev_msg = NULL; struct sctp_outq *q = &asoc->outqueue; + struct sctp_chunk *chunk, *c; struct sctp_transport *t; - struct sctp_chunk *chunk; list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) list_for_each_entry(chunk, &t->transmitted, transmitted_list) - cb(chunk); + traverse_and_process(); list_for_each_entry(chunk, &q->retransmit, transmitted_list) - cb(chunk); + traverse_and_process(); list_for_each_entry(chunk, &q->sacked, transmitted_list) - cb(chunk); + traverse_and_process(); list_for_each_entry(chunk, &q->abandoned, transmitted_list) - cb(chunk); + traverse_and_process(); list_for_each_entry(chunk, &q->out_chunk_list, list) - cb(chunk); + traverse_and_process(); } /* Verify that this is a valid address. */ @@ -8280,9 +8295,9 @@ static void sctp_sock_migrate(struct soc * paths won't try to lock it and then oldsk. */ lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); - sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w); + sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w); sctp_assoc_migrate(assoc, newsk); - sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w); + sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w); /* If the association on the newsk is already closed before accept() * is called, set RCV_SHUTDOWN flag.