Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2303036imc; Tue, 12 Mar 2019 10:58:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqzlYx+HNyQYiOb7C9t49jLRxO97DFCzw0nh0j4iAG46JdsWlK/1l0Dqkd/EjufFEarPSLJM X-Received: by 2002:a62:be0b:: with SMTP id l11mr40672163pff.52.1552413536843; Tue, 12 Mar 2019 10:58:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552413536; cv=none; d=google.com; s=arc-20160816; b=nfOueSFde+rUn6rbyeNe1vKikpKZ/zb4/qjUSS4OEnocKK3GU8vxZHffiiPeDIqgFx T8kD3qHJfV9rAo3gx2uyW8Fyej28sbDSfnujGZ1rzqAmOWJsIJOTDOgZsb4QGy6UK/Ho utb80ixCBF4Eq1VsAr3ZIlfNFg9jqXq9nVOtbkEJNdcy4M1HwUxH1XKg3c1g+LtSRwgj kvlJkQs7Fc4QZloWSbtpL9MZ5iQK2T6AG74oe7JEL9IyECm6ofEIRmCT3C3DjaByv3XV esFh3hS4UmXhP7sgQdLyH2ErJfIr6R8bw4FOlVNk4I5/Rj/WvXVgL6j0xKph7aAQOSBi OsoA== 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=z4uLVbLZ4xt/DboqpU4G3wjjaSmmlGErahpyAwuo3Yc=; b=wvk3m4kWdGMhELrQIlMIAA0ihMUUuSF42cXtoKvUW99G6Rat9JBEg1lkRwi5uajOPu h5qR/7DVVnlQN4lu0JzL7HC5aeGxfRXnhoj5/TW/UJ1YVW5MadSH30aOrub7/o3QkUz1 /oS+sjcG7M+HK4P2YXCG+UyYQMQRt3cIZ6xo8P7ZwsZm8aIK6afaN22b+7xnK0lNHqXg KLXnzC7CJNGzOnaYoMm/9vptrZF+hygtVYNzyNlHFOApv08FRZZuM1TuvSvmmzKczyoa 6brjEQhy0CA400MhgsA1LiRPb8MH6jri0gb+Tc0QD2M70Z68eA12AkTvvmqdiVNSYTpP Pihw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Elx8xKDh; 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 d25si7857172pgv.468.2019.03.12.10.58.41; Tue, 12 Mar 2019 10:58:56 -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=Elx8xKDh; 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 S1728595AbfCLR4i (ORCPT + 99 others); Tue, 12 Mar 2019 13:56:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:51660 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727280AbfCLRNz (ORCPT ); Tue, 12 Mar 2019 13:13: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 5DBB02183F; Tue, 12 Mar 2019 17:13:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410834; bh=U9Uw8wpY87Tv2vSxDLxeT4phTAvcHw8CnJFZyIsKAlA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Elx8xKDhDlacoEtV73MNi6DLjZ5qlxE9bGQeOmY6z1MbWF1ZJ0u56hFGnWOejSBMH KAkq6EQaDKJo4B5dmOJGm0u0vAj4bwt98XN5iFIPUdML7Cg+XPKn01Bn9wgAocatKK 8TOkmQt9r6W7+9M5VvaCOipXa+sFBvWPyf1jp/Jc= 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.19 023/149] iomap: fix a use after free in iomap_dio_rw Date: Tue, 12 Mar 2019 10:07:21 -0700 Message-Id: <20190312170351.658458023@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312170349.421581206@linuxfoundation.org> References: <20190312170349.421581206@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.19-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 2e3e64012db7..fac45206418a 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -1787,6 +1787,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; @@ -1806,7 +1807,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; @@ -1861,7 +1861,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) @@ -1877,7 +1877,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; @@ -1899,8 +1899,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 (;;) { @@ -1917,9 +1933,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