From nobody Mon Sep 29 22:44:03 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 BFA57C25B0D for ; Tue, 16 Aug 2022 00:22:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353143AbiHPAUr (ORCPT ); Mon, 15 Aug 2022 20:20:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357655AbiHPAN7 (ORCPT ); Mon, 15 Aug 2022 20:13:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A70D1782AE; Mon, 15 Aug 2022 13:30:25 -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 6A6A7611B1; Mon, 15 Aug 2022 20:30:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A126C433C1; Mon, 15 Aug 2022 20:30:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660595423; bh=bZ788B6z466vmlS6LVITTCvYlqYe/U/IhX4N2/r73Is=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EoroEjCmFOVGV5KfVxdfwd8dedhu8kGHYMjmIBcB3rNjCQDHO37n2Jdj6IRIvWkAM hbmfwogzabzDD1HBNa4J4DBdfsJi7+K92bZCrUNfVr6e105oYWbWcm/74S4V2JT8wQ Tv8jhaf22ppsSVJoXNCYukFvI7XqnB5Q2i5ukdM0= 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.19 0727/1157] lib/test_hmm: avoid accessing uninitialized pages Date: Mon, 15 Aug 2022 20:01:23 +0200 Message-Id: <20220815180508.560058174@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@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 cfe632047839..f2c3015c5c82 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -732,7 +732,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)) @@ -741,7 +741,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