Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2314709imc; Tue, 12 Mar 2019 11:11:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqzF4UxP2SS85/9XUWDjCqj8UeYcY4NFT3MMIGXDLhZhI/6iRnxSxDTlff61Kq2Bf/PuWnyI X-Received: by 2002:a62:aa08:: with SMTP id e8mr39568459pff.139.1552414303797; Tue, 12 Mar 2019 11:11:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552414303; cv=none; d=google.com; s=arc-20160816; b=Fs9Yez+VD0IByErUXK5xktQyU/Swnz3xrLsIWywmlb271vNf3km0eJRUh+2L2lQSxH Fv2LQWX++Yb9Guhmb6DMzZ09DJfFvbMxuKiX51E7DHMrauWN/Y1vfidrt09jsrbcHeys FIdQURd0fLGZGsa2YkVfbN6mnHCKYGST91bVbXA1tuWSIBIY1fFdqxmL5+XR4hHFyTdQ jtQ+x8Mz8PUqEn9QBOrF9JMY4Dc1RpFxxG+qsiaCefm6Uze0hihG1rDICTRKFv8Mw0Ph MNQGqZFhpapQTA1C3yTwkvWQRf5jA7WEEIybEmKdT39zgdf6SpqG+vcA6l7sxaiokDCl Sizw== 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=uEMnEMlUjVjlVH9q9kbSuL+bTauaXjoZ5yQBcTMyagM=; b=1Lccm3pdWbIgcc32z8DdjIj9yyGi8GbD/7J8yYWg5Xqc1TRo+zOyWWGJH/VA2D51hq pX62QnU0NenhHfhLBMNOj4qOiyCmrjV9VeGZYW2wJwlYqkQpDB8f4cs4cDTeuEswJLc0 imXd7j/Spjp0LiBGHaiPYDS1j/vKPKK8aG7mnB0+8P8IgiH9+SvpAOr2fRvFSWfhw55R MRr4q6/yLDc1kMl95i6ZMsWXj8zdRVUn1AulM7DL4Ns5hDVt9jiSaRgrLh6q9ktN4Uzu xosoEuxJRjW80A86zmHUpq8TjN2UzamLEr52pfAw1ugIJPknjUvTjaufny+T47abjln2 a7FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tONQavGc; 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 g10si7925110pgk.395.2019.03.12.11.11.27; Tue, 12 Mar 2019 11:11:43 -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=tONQavGc; 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 S1727289AbfCLRMH (ORCPT + 99 others); Tue, 12 Mar 2019 13:12:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:46096 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727198AbfCLRLz (ORCPT ); Tue, 12 Mar 2019 13:11:55 -0400 Received: from localhost (unknown [104.133.8.98]) (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 C35F7214AF; Tue, 12 Mar 2019 17:11:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410714; bh=GZzUkDJHEhqkjwKctnrrftjMWzbgG35xzNM8uFndx6c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tONQavGcNCj+DJtpNH76i69RMnlJ+JTtjuFh4+INf/NJJ2IotHYf7busnIq0L423C a38f96JZXTQO1FCES3H/oGMvL6xDU2QvFD7CpqNHULiCx53fixwVsV6puVgmMLTO9a pD3aZG+bbP5pzJNKERSp3bwo5i8Of4d6gkSLKdGE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chandan Rajendra , "Darrick J. Wong" , Christoph Hellwig , Dave Chinner , Sasha Levin Subject: [PATCH 4.20 029/171] iomap: fix a use after free in iomap_dio_rw Date: Tue, 12 Mar 2019 10:06:49 -0700 Message-Id: <20190312170350.500092622@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312170347.868927101@linuxfoundation.org> References: <20190312170347.868927101@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 4ea899ead2786a30aaa8181fefa81a3df4ad28f6 ] Introduce a local wait_for_completion variable to avoid an access to the potentially freed dio struture after dropping the last reference count. Also use the chance to document the completion behavior to make the refcounting clear to the reader of the code. Fixes: ff6a9292e6 ("iomap: implement direct I/O") Reported-by: Chandan Rajendra Reported-by: Darrick J. Wong Signed-off-by: Christoph Hellwig Tested-by: Chandan Rajendra Tested-by: Darrick J. Wong Reviewed-by: Dave Chinner Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong Signed-off-by: Sasha Levin --- fs/iomap.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/fs/iomap.c b/fs/iomap.c index 7793e9e42ad5..83d9a196fe3e 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -1806,6 +1806,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, loff_t pos = iocb->ki_pos, start = pos; loff_t end = iocb->ki_pos + count - 1, ret = 0; unsigned int flags = IOMAP_DIRECT; + bool wait_for_completion = is_sync_kiocb(iocb); struct blk_plug plug; struct iomap_dio *dio; @@ -1825,7 +1826,6 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, dio->end_io = end_io; dio->error = 0; dio->flags = 0; - dio->wait_for_completion = is_sync_kiocb(iocb); dio->submit.iter = iter; dio->submit.waiter = current; @@ -1880,7 +1880,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, dio_warn_stale_pagecache(iocb->ki_filp); ret = 0; - if (iov_iter_rw(iter) == WRITE && !dio->wait_for_completion && + if (iov_iter_rw(iter) == WRITE && !wait_for_completion && !inode->i_sb->s_dio_done_wq) { ret = sb_init_dio_done_wq(inode->i_sb); if (ret < 0) @@ -1896,7 +1896,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, if (ret <= 0) { /* magic error code to fall back to buffered I/O */ if (ret == -ENOTBLK) { - dio->wait_for_completion = true; + wait_for_completion = true; ret = 0; } break; @@ -1918,8 +1918,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, if (dio->flags & IOMAP_DIO_WRITE_FUA) dio->flags &= ~IOMAP_DIO_NEED_SYNC; + /* + * We are about to drop our additional submission reference, which + * might be the last reference to the dio. There are three three + * different ways we can progress here: + * + * (a) If this is the last reference we will always complete and free + * the dio ourselves. + * (b) If this is not the last reference, and we serve an asynchronous + * iocb, we must never touch the dio after the decrement, the + * I/O completion handler will complete and free it. + * (c) If this is not the last reference, but we serve a synchronous + * iocb, the I/O completion handler will wake us up on the drop + * of the final reference, and we will complete and free it here + * after we got woken by the I/O completion handler. + */ + dio->wait_for_completion = wait_for_completion; if (!atomic_dec_and_test(&dio->ref)) { - if (!dio->wait_for_completion) + if (!wait_for_completion) return -EIOCBQUEUED; for (;;) { @@ -1936,9 +1952,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, __set_current_state(TASK_RUNNING); } - ret = iomap_dio_complete(dio); - - return ret; + return iomap_dio_complete(dio); out_free_dio: kfree(dio); -- 2.19.1