Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp68509imu; Tue, 8 Jan 2019 14:50:54 -0800 (PST) X-Google-Smtp-Source: ALg8bN5WGJMRlQnINNDxX4qmSsySA48fnHfviAA7w+XM8gf1l2/6GkGRyAl9XWz5kTB6+b+uLJuH X-Received: by 2002:a17:902:7d90:: with SMTP id a16mr3443332plm.249.1546987854616; Tue, 08 Jan 2019 14:50:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546987854; cv=none; d=google.com; s=arc-20160816; b=Oq40UiEdMDhqbKun7RzsanzX1Ego7BBV09HTE6cHrjw/pc6/ctkfzMB2dBBqGbSiJh d3AhcNUJgi6O5OnXw0/YnwxQthq/TK/SUZudE4WbToxteQ/L4O50amp8pL7V5HF1jhq3 KJ/5GEXRiYzlBWhau1x2Dbh2O2fQZBhr1isUignS6ZIL5jHMRXeqcO93vgB5CTYtcHu+ 5ThcFcKAicEbYBzm9Ogpo3ELzeeAFIKRe5SjQWw4Q6nLn4zyABdYKNv370nZxqhO4VIK 1AXul2iEhMly4XxNA5/FWQvd5rtXenShAYAfdvKvtbRTi1avI7ei4hFjEJOdkmvP63Wh zt2g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5Rx/crerOMaKjtHx6sAi6D8lsI4vECToCZPt7h0uFxQ=; b=J3U3ENuKcWdpvDhAN8C8DvjBpry2iI/Z1Oi8q4PrWEtLDv+HduF2MAdECrZUAZtYx6 /3f0sm8IiJnk9du+RISIso4x9J3RezX/G9b2cxny8bbxmNActna2EwOoUe6T9L5Yqynz MN2XniPPVEEpEO0CDgPawlPSxSG5mgQzLU+7r+09hZz4cv6OVZLuFg6pV9s/OlYde44t 9RMH2/3Wp6L8Fo0v22qy2/tk5kwdkIeHO2UszXDCuGuzkr1zYkNvfAVPvjS9odUrV5Bg n8N9/GsmblGRNRt3Oyt1jpgPoJq3sRG8e1vZLTuUsaXe521kb1Lwda2nJd76nwWH4lfw yOYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nwlKDcIH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r14si26765651pgk.75.2019.01.08.14.50.39; Tue, 08 Jan 2019 14:50:54 -0800 (PST) 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=nwlKDcIH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730708AbfAHT6o (ORCPT + 99 others); Tue, 8 Jan 2019 14:58:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:36932 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730399AbfAHT3a (ORCPT ); Tue, 8 Jan 2019 14:29:30 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 454C52183F; Tue, 8 Jan 2019 19:29:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1546975769; bh=f1MsrB64D5ogQt73dYUl6yOvYVFSpjYIz/QsTbS/Beo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nwlKDcIHJf7IOvgdlFWzXbGQBcJK8NUa75jzdLJhQ/ZPFfOdkujF6u6Qj2iMw0ng2 zb0DJVvHxuvcomJFbyK7tf4nnXAdjdClkoNHqkJAlCkxySyawV3pEIp00Plp5yMd+6 Mqe5x6/r6gall2F9qGWy0grxKX8N2bVf4oT7xgac= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Eric Sandeen , Eric Sandeen , "Darrick J . Wong" , Sasha Levin , linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 4.20 110/117] iomap: don't search past page end in iomap_is_partially_uptodate Date: Tue, 8 Jan 2019 14:26:18 -0500 Message-Id: <20190108192628.121270-110-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190108192628.121270-1-sashal@kernel.org> References: <20190108192628.121270-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Sandeen [ Upstream commit 3cc31fa65d85610574c0f6a474e89f4c419923d5 ] iomap_is_partially_uptodate() is intended to check wither blocks within the selected range of a not-uptodate page are uptodate; if the range we care about is up to date, it's an optimization. However, the iomap implementation continues to check all blocks up to from+count, which is beyond the page, and can even be well beyond the iop->uptodate bitmap. I think the worst that will happen is that we may eventually find a zero bit and return "not partially uptodate" when it would have otherwise returned true, and skip the optimization. Still, it's clearly an invalid memory access that must be fixed. So: fix this by limiting the search to within the page as is done in the non-iomap variant, block_is_partially_uptodate(). Zorro noticed thiswhen KASAN went off for 512 byte blocks on a 64k page system: BUG: KASAN: slab-out-of-bounds in iomap_is_partially_uptodate+0x1a0/0x1e0 Read of size 8 at addr ffff800120c3a318 by task fsstress/22337 Reported-by: Zorro Lang Signed-off-by: Eric Sandeen Signed-off-by: Eric Sandeen Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong Signed-off-by: Sasha Levin --- fs/iomap.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fs/iomap.c b/fs/iomap.c index d6bc98ae8d35..ce837d962d47 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -492,16 +492,29 @@ iomap_readpages(struct address_space *mapping, struct list_head *pages, } EXPORT_SYMBOL_GPL(iomap_readpages); +/* + * iomap_is_partially_uptodate checks whether blocks within a page are + * uptodate or not. + * + * Returns true if all blocks which correspond to a file portion + * we want to read within the page are uptodate. + */ int iomap_is_partially_uptodate(struct page *page, unsigned long from, unsigned long count) { struct iomap_page *iop = to_iomap_page(page); struct inode *inode = page->mapping->host; - unsigned first = from >> inode->i_blkbits; - unsigned last = (from + count - 1) >> inode->i_blkbits; + unsigned len, first, last; unsigned i; + /* Limit range to one page */ + len = min_t(unsigned, PAGE_SIZE - from, count); + + /* First and last blocks in range within page */ + first = from >> inode->i_blkbits; + last = (from + len - 1) >> inode->i_blkbits; + if (iop) { for (i = first; i <= last; i++) if (!test_bit(i, iop->uptodate)) -- 2.19.1