Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1471895ybg; Thu, 11 Jun 2020 10:40:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrepZnPeCawhNpdCPYdLh546XKrZFEDfjvCkO/1ybbS4cZ7KsEUIsqPSaCk64tEc2nKHDJ X-Received: by 2002:a17:906:945a:: with SMTP id z26mr9771005ejx.448.1591897229142; Thu, 11 Jun 2020 10:40:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591897229; cv=none; d=google.com; s=arc-20160816; b=HRQycHSdOmFd7utG2BRrVOyQsJq83vJblj2X00RpbKomiW2I3ogkIYGnYvnvP9gtHh ZJCPF/Q1R3wRs5jvRosR+Ga3knbHosNFzJigkcaowLgqOoH2vP85wqCdFdAH+NYfodJb /KNYhCEtTI4QbqaFqtgaCpAWxaaKJk/Cst3V4aN3nYu05BM13EK3XBiRGu9Y7OqrjCnb 9xfM89TtyUTi+tpXPLxjTMb8i+5r/Fk+lBtrAN9f1UqJIyzncvjJadP3FIJ4nBkIAviR YIONks9mLu3PKw2NCnftR0V+jUfoWTNfF3696kOxLwRP/UxBDkk5I4v1bwdDfKMhrron bEaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=dCrUsFjr5nTo7YMbuo6naT5rMcveBqItH9g4ZRhLhDU=; b=j9SXra6+NM17bbD3ywoZynrhuhR0ilkfsyrsFff3uz8okA8cLYJoDaArUYxsZ1cYl6 /i+QbMfruiMsbJMT1aT2RfSxmjzk4R/UZG9S4FpR0COxpdVSeQ53D6EBGQVMC2RDGV6r aZdzmouX2vCDwNsvPVDM1cd7y9Ma7FPLd1XtGH3rejTkW3L11AuX2a61BvEzhGfDA1Cc 17VQUR6LRxCXxbeMpmNQybcGnD412XcFt1Y6PQ9ct7gspEiKMRRujUw0tdu6aZ5/p/5S BxB8/UTfh+JGRhZ5Yt4igVIPnhANjHMSgujXiBPDOYyqcLsR7idYE9gIO17doeBl5MUi m9Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O8m6jMDw; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bf20si1963756edb.66.2020.06.11.10.40.05; Thu, 11 Jun 2020 10:40:29 -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=@redhat.com header.s=mimecast20190719 header.b=O8m6jMDw; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728464AbgFKPLd (ORCPT + 99 others); Thu, 11 Jun 2020 11:11:33 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:24662 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728456AbgFKPLc (ORCPT ); Thu, 11 Jun 2020 11:11:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591888290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=dCrUsFjr5nTo7YMbuo6naT5rMcveBqItH9g4ZRhLhDU=; b=O8m6jMDw27VvzgMnrCywY+48NrqsF0zDdaPR5phGelg39ulmvkYHm5LxlDF125l4momsoQ dekWIL7Pvwg1ncFeSnugt594vlkspkXSs6YYI0Nfmw01J26F/70n0px1xnlUu9KbnqESnl icEzVCWGvGgrTsI2OdhJR1vAp9tPJ5A= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-134-hZKXzjXEOfSX68ojmbc1Yw-1; Thu, 11 Jun 2020 11:11:26 -0400 X-MC-Unique: hZKXzjXEOfSX68ojmbc1Yw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 83D3580572E; Thu, 11 Jun 2020 15:11:25 +0000 (UTC) Received: from bfoster (dhcp-41-2.bos.redhat.com [10.18.41.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4B54610013D7; Thu, 11 Jun 2020 15:11:24 +0000 (UTC) Date: Thu, 11 Jun 2020 11:11:22 -0400 From: Brian Foster To: Dave Chinner Cc: Yu Kuai , darrick.wong@oracle.com, linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com Subject: Re: [PATCH] xfs: fix use-after-free on CIL context on shutdown Message-ID: <20200611151122.GA57603@bfoster> References: <20200611013952.2589997-1-yukuai3@huawei.com> <20200611022848.GQ2040@dread.disaster.area> <20200611024503.GR2040@dread.disaster.area> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200611024503.GR2040@dread.disaster.area> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 11, 2020 at 12:45:03PM +1000, Dave Chinner wrote: > > From: Dave Chinner > > xlog_wait() on the CIL context can reference a freed context if the > waiter doesn't get scheduled before the CIL context is freed. This > can happen when a task is on the hard throttle and the CIL push > aborts due to a shutdown. This was detected by generic/019: > > thread 1 thread 2 > > __xfs_trans_commit > xfs_log_commit_cil > > xlog_wait > schedule > xlog_cil_push_work > wake_up_all > > xlog_cil_committed > kmem_free > > remove_wait_queue > spin_lock_irqsave --> UAF > > Fix it by moving the wait queue to the CIL rather than keeping it in > in the CIL context that gets freed on push completion. Because the > wait queue is now independent of the CIL context and we might have > multiple contexts in flight at once, only wake the waiters on the > push throttle when the context we are pushing is over the hard > throttle size threshold. > > Fixes: 0e7ab7efe7745 ("xfs: Throttle commits on delayed background CIL push") > Reported-by: Yu Kuai > Signed-off-by: Dave Chinner > --- Looks reasonable: Reviewed-by: Brian Foster > fs/xfs/xfs_log_cil.c | 10 +++++----- > fs/xfs/xfs_log_priv.h | 2 +- > 2 files changed, 6 insertions(+), 6 deletions(-) > > diff --git a/fs/xfs/xfs_log_cil.c b/fs/xfs/xfs_log_cil.c > index b43f0e8f43f2e..9ed90368ab311 100644 > --- a/fs/xfs/xfs_log_cil.c > +++ b/fs/xfs/xfs_log_cil.c > @@ -671,7 +671,8 @@ xlog_cil_push_work( > /* > * Wake up any background push waiters now this context is being pushed. > */ > - wake_up_all(&ctx->push_wait); > + if (ctx->space_used >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log)) > + wake_up_all(&cil->xc_push_wait); > > /* > * Check if we've anything to push. If there is nothing, then we don't > @@ -743,13 +744,12 @@ xlog_cil_push_work( > > /* > * initialise the new context and attach it to the CIL. Then attach > - * the current context to the CIL committing lsit so it can be found > + * the current context to the CIL committing list so it can be found > * during log forces to extract the commit lsn of the sequence that > * needs to be forced. > */ > INIT_LIST_HEAD(&new_ctx->committing); > INIT_LIST_HEAD(&new_ctx->busy_extents); > - init_waitqueue_head(&new_ctx->push_wait); > new_ctx->sequence = ctx->sequence + 1; > new_ctx->cil = cil; > cil->xc_ctx = new_ctx; > @@ -937,7 +937,7 @@ xlog_cil_push_background( > if (cil->xc_ctx->space_used >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log)) { > trace_xfs_log_cil_wait(log, cil->xc_ctx->ticket); > ASSERT(cil->xc_ctx->space_used < log->l_logsize); > - xlog_wait(&cil->xc_ctx->push_wait, &cil->xc_push_lock); > + xlog_wait(&cil->xc_push_wait, &cil->xc_push_lock); > return; > } > > @@ -1216,12 +1216,12 @@ xlog_cil_init( > INIT_LIST_HEAD(&cil->xc_committing); > spin_lock_init(&cil->xc_cil_lock); > spin_lock_init(&cil->xc_push_lock); > + init_waitqueue_head(&cil->xc_push_wait); > init_rwsem(&cil->xc_ctx_lock); > init_waitqueue_head(&cil->xc_commit_wait); > > INIT_LIST_HEAD(&ctx->committing); > INIT_LIST_HEAD(&ctx->busy_extents); > - init_waitqueue_head(&ctx->push_wait); > ctx->sequence = 1; > ctx->cil = cil; > cil->xc_ctx = ctx; > diff --git a/fs/xfs/xfs_log_priv.h b/fs/xfs/xfs_log_priv.h > index ec22c7a3867f1..75a62870b63af 100644 > --- a/fs/xfs/xfs_log_priv.h > +++ b/fs/xfs/xfs_log_priv.h > @@ -240,7 +240,6 @@ struct xfs_cil_ctx { > struct xfs_log_vec *lv_chain; /* logvecs being pushed */ > struct list_head iclog_entry; > struct list_head committing; /* ctx committing list */ > - wait_queue_head_t push_wait; /* background push throttle */ > struct work_struct discard_endio_work; > }; > > @@ -274,6 +273,7 @@ struct xfs_cil { > wait_queue_head_t xc_commit_wait; > xfs_lsn_t xc_current_sequence; > struct work_struct xc_push_work; > + wait_queue_head_t xc_push_wait; /* background push throttle */ > } ____cacheline_aligned_in_smp; > > /* >