From nobody Sun Dec 14 08:03:53 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEA4DC00140 for ; Mon, 15 Aug 2022 19:14:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239874AbiHOTOX (ORCPT ); Mon, 15 Aug 2022 15:14:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343877AbiHOTLL (ORCPT ); Mon, 15 Aug 2022 15:11:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79F1847B92; Mon, 15 Aug 2022 11:36:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BEACB610A4; Mon, 15 Aug 2022 18:36:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7E89C433B5; Mon, 15 Aug 2022 18:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588613; bh=AoWZxuR6WXPi8Pqjbv7dEunLrV46Cyhu9vllKRjqP4s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f8cJ2wSiItzkzeAtjpykZeLIaFg6n0s/AW9e39IORCKVcPugAvyAfzMHm/B05H9Gk GdpwL2CdmUJEl74gWo1jw3sA6zQWb0movJtjjtO29xjym4+oC00LPmiMp+82JPKyu+ KpDu1tGgcPmLprob4iyC7s9Kk4YnCS3bKVhH8XUg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miaohe Lin , Jerome Glisse , Alistair Popple , Jason Gunthorpe , Ralph Campbell , Andrew Morton , Sasha Levin Subject: [PATCH 5.15 429/779] lib/test_hmm: avoid accessing uninitialized pages Date: Mon, 15 Aug 2022 20:01:13 +0200 Message-Id: <20220815180355.612757882@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Miaohe Lin [ Upstream commit ed913b055a74b723976f8e885a3395162a0371e6 ] If make_device_exclusive_range() fails or returns pages marked for exclusive access less than required, remaining fields of pages will left uninitialized. So dmirror_atomic_map() will access those yet uninitialized fields of pages. To fix it, do dmirror_atomic_map() iff all pages are marked for exclusive access (we will break if mapped is less than required anyway) so we won't access those uninitialized fields of pages. Link: https://lkml.kernel.org/r/20220609130835.35110-1-linmiaohe@huawei.com Fixes: b659baea7546 ("mm: selftests for exclusive device memory") Signed-off-by: Miaohe Lin Cc: Jerome Glisse Cc: Alistair Popple Cc: Jason Gunthorpe Cc: Ralph Campbell Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- lib/test_hmm.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index ac794e354069..a89cb4281c9d 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -731,7 +731,7 @@ static int dmirror_exclusive(struct dmirror *dmirror, =20 mmap_read_lock(mm); for (addr =3D start; addr < end; addr =3D next) { - unsigned long mapped; + unsigned long mapped =3D 0; int i; =20 if (end < addr + (ARRAY_SIZE(pages) << PAGE_SHIFT)) @@ -740,7 +740,13 @@ static int dmirror_exclusive(struct dmirror *dmirror, next =3D addr + (ARRAY_SIZE(pages) << PAGE_SHIFT); =20 ret =3D make_device_exclusive_range(mm, addr, next, pages, NULL); - mapped =3D dmirror_atomic_map(addr, next, pages, dmirror); + /* + * Do dmirror_atomic_map() iff all pages are marked for + * exclusive access to avoid accessing uninitialized + * fields of pages. + */ + if (ret =3D=3D (next - addr) >> PAGE_SHIFT) + mapped =3D dmirror_atomic_map(addr, next, pages, dmirror); for (i =3D 0; i < ret; i++) { if (pages[i]) { unlock_page(pages[i]); --=20 2.35.1