Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5228627ybl; Tue, 27 Aug 2019 01:09:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqyrIDYLciwPhj1SuraYOgqnr7PuLLH7qv7NByxyz8iK5DWdW8PUigmUV/hCLqs8blaSqHTY X-Received: by 2002:a62:83c9:: with SMTP id h192mr24158260pfe.57.1566893386982; Tue, 27 Aug 2019 01:09:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566893386; cv=none; d=google.com; s=arc-20160816; b=rvOQVTcHGnmi5/LzPH3cqaYLy06j0MzPSRm3OOi8sS856plPKof1t9sonJHaOwog1J TsI0r4opXHAziVBVSlg+NWa+c0c5foGU5nAcXRPcdhwjyK89RQlEs0mHBYQNLfjxRU7N oflSJFfjmljHEYT3sGDcNFm4CI9/KMArP4wzTjvMSK75tqPIH/qEuR6/Gu0y6h0ZoguU S3t4Aih00yjk/kUG1PkoyxhhB35fwyLnbEgQ1BRvRWEIkg+RumLrtmzybmX3zdfnrsFE iErYxUN7cE64uaGyIPTxrpinoY0C3zos2RlAzfAps9E8/zI1+VFt9Q5lSo7z90D+ZllB 0sYA== 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=bjvtVZlHCLvLdjQ/R8AWP1nseUgmvY+isGJDDnOYYyA=; b=YpPuTxUeSKMCa/slY3Xd9eFTBn5Lr0PZK9PcteMkoc7KeTVHHgTq1DnSeODO8aWvfF IMy7QD7nNd0kyQAxWiDG38W8rF82xjMsB4WCH/BSURqnz8I2NMzN9QEpouAxVck3iXe4 BiKiXqyeAMO15x9L/UN4dVUKbGqFT7kARzfptQ/BO6nDmq5IKFX0paato2CHsbdRpKvl UuWFdKPtz9a7e3r2DDXK44vCtuqxgt/eTF8Vcfd0kVbQTC695KeXkZe0Qweete3DbMxC z1o6VAfrZV/+gccrktkyeDAV9VcCUHKj7qsCbShEqmQ/TLlaOBHnu55PwAcdyHgU/O3m HFVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tlN1DvM2; 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 q135si12725651pfc.125.2019.08.27.01.09.31; Tue, 27 Aug 2019 01:09:46 -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=tlN1DvM2; 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 S1732833AbfH0II3 (ORCPT + 99 others); Tue, 27 Aug 2019 04:08:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:35858 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731725AbfH0IFz (ORCPT ); Tue, 27 Aug 2019 04:05:55 -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 85BA2217F5; Tue, 27 Aug 2019 08:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566893154; bh=KF+Rar/Xwb/7SurNA4OoNg8WMNqOuGPJd8iPfo9AliQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tlN1DvM2Q3bpotko2qBBNOXghcppeJLoHkEI38Kp5dwsmrTwpWCiRQjn6GvOL4CDe 6/s/jJcLVmj4QNi2p9gQA+tpXkm5Yhvs5IUivZykxJyFSC41u+nv5yVyf3MPaJ8I6O EJFB+iyYm4Qe3wdaAHfBhIJU6CHxOUZz3wcEPPxw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikulas Patocka , Mike Snitzer Subject: [PATCH 5.2 138/162] dm integrity: fix a crash due to BUG_ON in __journal_read_write() Date: Tue, 27 Aug 2019 09:51:06 +0200 Message-Id: <20190827072743.423260210@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827072738.093683223@linuxfoundation.org> References: <20190827072738.093683223@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: Mikulas Patocka commit 5729b6e5a1bcb0bbc28abe82d749c7392f66d2c7 upstream. Fix a crash that was introduced by the commit 724376a04d1a. The crash is reported here: https://gitlab.com/cryptsetup/cryptsetup/issues/468 When reading from the integrity device, the function dm_integrity_map_continue calls find_journal_node to find out if the location to read is present in the journal. Then, it calculates how many sectors are consecutively stored in the journal. Then, it locks the range with add_new_range and wait_and_add_new_range. The problem is that during wait_and_add_new_range, we hold no locks (we don't hold ic->endio_wait.lock and we don't hold a range lock), so the journal may change arbitrarily while wait_and_add_new_range sleeps. The code then goes to __journal_read_write and hits BUG_ON(journal_entry_get_sector(je) != logical_sector); because the journal has changed. In order to fix this bug, we need to re-check the journal location after wait_and_add_new_range. We restrict the length to one block in order to not complicate the code too much. Fixes: 724376a04d1a ("dm integrity: implement fair range locks") Cc: stable@vger.kernel.org # v4.19+ Signed-off-by: Mikulas Patocka Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-integrity.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) --- a/drivers/md/dm-integrity.c +++ b/drivers/md/dm-integrity.c @@ -1940,7 +1940,22 @@ offload_to_thread: queue_work(ic->wait_wq, &dio->work); return; } + if (journal_read_pos != NOT_FOUND) + dio->range.n_sectors = ic->sectors_per_block; wait_and_add_new_range(ic, &dio->range); + /* + * wait_and_add_new_range drops the spinlock, so the journal + * may have been changed arbitrarily. We need to recheck. + * To simplify the code, we restrict I/O size to just one block. + */ + if (journal_read_pos != NOT_FOUND) { + sector_t next_sector; + unsigned new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector); + if (unlikely(new_pos != journal_read_pos)) { + remove_range_unlocked(ic, &dio->range); + goto retry; + } + } } spin_unlock_irq(&ic->endio_wait.lock);