Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp7034ybt; Tue, 23 Jun 2020 13:50:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgKbOmOZCFgWtf+e4mD8tdtw8F7Yii3rG8XMOkCc37SI03e3AwVQICLJl/65PzARo+jU9y X-Received: by 2002:a17:906:b15:: with SMTP id u21mr16911288ejg.520.1592945425924; Tue, 23 Jun 2020 13:50:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592945425; cv=none; d=google.com; s=arc-20160816; b=V5cR/jcF5VxTcUcSugQArXmrPxgzCau7v/v/Mv09CRcF15FiMiM1HnPW2TEr3pvqRO VEL6fh+qnEs1pjD5M9/txHuDR7wqMJhsg4c7H78LzcUEeuNBxeiq1S+owArFhFXW1k1n Tw7rOTjDDAsjU3IcJYvGxsqPnO+UuSTzODl3INez0qnVHhC0cwSiRJc3obXSLEJCnE9L OAPg4u6Olfolqm1/vsSpzHDwSVwd7mLJlLKIb0hNupL7vOeQKPCy1UglF3HYqo5hH1o+ 9zAOn0F0VSjOkxO7pqGH93Riq2yg5/cIo2/iXZBb8f64HAYTiUuHmwqW3pqcJQQy6GeQ eqqQ== 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=8jpIXsjqYwMDpcYoAAX+dGHsc2WnK1GEcD0ghVCu3Bs=; b=v/PdtFJV0KKCbH8S8++np2hIneQbwz6jBPzKRBn4HbohL3hTjqI/kPpABI5RXXrFd7 q7xAsFBVNFooh2p1pEQ5+NZz2CzFGS9GY1OTCtHjaBqdBu+j5JajUq/1Eh6C1402QFcb tsz4bTbfg3esVJOUh2kH1vDAwmKcXRwYk9rw0JtMkVpb21hKAPXHr1w7QeAwob6Nt7Ae nducrBMIDV2NRJhLWGHzn16TWL6JXt80C/PHFZIoEIXVHB5fLVDTZ84IhcyTT6pX27hQ 6Me7PC1AQjuXdBYhLFuAcWihNLL7+18t0ymR4ddXPmdv1vXhB+/WJ1AhY187IDGCnNwS O2uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qJqxqVFQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c16si13101723edv.438.2020.06.23.13.50.02; Tue, 23 Jun 2020 13:50:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qJqxqVFQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392717AbgFWUs3 (ORCPT + 99 others); Tue, 23 Jun 2020 16:48:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:47368 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392932AbgFWUsU (ORCPT ); Tue, 23 Jun 2020 16:48:20 -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 8D4F721548; Tue, 23 Jun 2020 20:48:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592945301; bh=9qEEOibyO+UeBbgruNVcxalCX8lLeefGOc9hf40Qzps=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qJqxqVFQYWrOsKujaBTCMCyvhVC3JGIx+2H/eyMolpLpKBbZg+ysYaJbcM4nJD61h 14HbDr2iHy2qjF7kpEqTfrEUIIB1Kq/rC69wsPWIaVzNFCEkiGNLjSivKHcRxsT7R6 SW4uMLV/ZOfquR2OKDpH/hnMXD2Kt1HQSlELBudc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bob Peterson , Andreas Gruenbacher , Sasha Levin Subject: [PATCH 4.14 100/136] gfs2: fix use-after-free on transaction ail lists Date: Tue, 23 Jun 2020 21:59:16 +0200 Message-Id: <20200623195308.699308038@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200623195303.601828702@linuxfoundation.org> References: <20200623195303.601828702@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: Bob Peterson [ Upstream commit 83d060ca8d90fa1e3feac227f995c013100862d3 ] Before this patch, transactions could be merged into the system transaction by function gfs2_merge_trans(), but the transaction ail lists were never merged. Because the ail flushing mechanism can run separately, bd elements can be attached to the transaction's buffer list during the transaction (trans_add_meta, etc) but quickly moved to its ail lists. Later, in function gfs2_trans_end, the transaction can be freed (by gfs2_trans_end) while it still has bd elements queued to its ail lists, which can cause it to either lose track of the bd elements altogether (memory leak) or worse, reference the bd elements after the parent transaction has been freed. Although I've not seen any serious consequences, the problem becomes apparent with the previous patch's addition of: gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); to function gfs2_trans_free(). This patch adds logic into gfs2_merge_trans() to move the merged transaction's ail lists to the sdp transaction. This prevents the use-after-free. To do this properly, we need to hold the ail lock, so we pass sdp into the function instead of the transaction itself. Signed-off-by: Bob Peterson Signed-off-by: Andreas Gruenbacher Signed-off-by: Sasha Levin --- fs/gfs2/log.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fs/gfs2/log.c b/fs/gfs2/log.c index a3208511f35aa..f30418911e1bd 100644 --- a/fs/gfs2/log.c +++ b/fs/gfs2/log.c @@ -804,8 +804,10 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, * @new: New transaction to be merged */ -static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new) +static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new) { + struct gfs2_trans *old = sdp->sd_log_tr; + WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags)); old->tr_num_buf_new += new->tr_num_buf_new; @@ -817,6 +819,11 @@ static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new) list_splice_tail_init(&new->tr_databuf, &old->tr_databuf); list_splice_tail_init(&new->tr_buf, &old->tr_buf); + + spin_lock(&sdp->sd_ail_lock); + list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list); + list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list); + spin_unlock(&sdp->sd_ail_lock); } static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) @@ -828,7 +835,7 @@ static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) gfs2_log_lock(sdp); if (sdp->sd_log_tr) { - gfs2_merge_trans(sdp->sd_log_tr, tr); + gfs2_merge_trans(sdp, tr); } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) { gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags)); sdp->sd_log_tr = tr; -- 2.25.1