Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3258142pxb; Mon, 18 Apr 2022 20:54:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvn69FM8TyfDj0eJ9VQiCBxiBTqKI67r1w0TBZ60jdvT7d2M7s1E4gBoyRWTOzAvQ+rXAU X-Received: by 2002:aa7:c790:0:b0:41d:7e0f:f15c with SMTP id n16-20020aa7c790000000b0041d7e0ff15cmr15155073eds.129.1650340461601; Mon, 18 Apr 2022 20:54:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650340461; cv=none; d=google.com; s=arc-20160816; b=03uFmHoTLX9gkaA64lupD6GFNPZ2wuuje69SgTlghsRg1t8SYVy8fzPMgvplfN4g0o PaXiJiy6L0BGTu1WJMEMX03qj8pyJA1DA0N5hcyiQhABXUh0Z0NAKFpuTEFq9tBlqdwk 9E2cuF6EOJodA8bUveaORWyNIUvlCqC8pwKmBj/6OeekcQqndwKL0+fo7Bx9d0D9tZX8 GFCf8ZqoMJ6wAXYs1Sz3SjTa/BRr5T4x2fEwhbefpNl/P1zT0bnQUXf+Wcn47g5WM7c1 eQFx/QSh3e/vVepqKGjVHnZ2RSD9SxoGKNvhltvkc5XiNOxhwm9eW/8IUPiFIn9AEdma +m3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZJqGWrgazWaY+s5qub7QLaC1OWhQsy03bIjDKzSI4xQ=; b=W0ASXZ7ww7f6onGJQGdsZ3WICr6d4gSwKck6gxqoCKn8s9xCDkoBEOQBO53HOvPTjC oyJLYxdR5pUgWEFUT8snrFpAg1mZQ/qAKMmuCsFawe/chHsgmB3TwkIzbL3V9Ll7PsXW va5Qv7yCJeMUKr80wLNXovUBlOHRdEEYEWKGvEOHL8GrRJRzqtQAbaBAiy+/70a2rGpJ COww/1bOeJCAdCtZs+d1V9AaHxpA8d5zeyLtv7r5uO7UAq/3IjMStUTy8JfT21LQTQWH ZhyyuaHKGuAwh9DSNNskXcURNgB3u5xcMY0qXdTLPA7hIhWEOY5poXMM82Rc7hqMpfjZ 4CqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lXgO8G9F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e15-20020a50d4cf000000b0041d8c06dc6esi7533815edj.413.2022.04.18.20.53.57; Mon, 18 Apr 2022 20:54:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lXgO8G9F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239760AbiDRMmk (ORCPT + 99 others); Mon, 18 Apr 2022 08:42:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239779AbiDRMd2 (ORCPT ); Mon, 18 Apr 2022 08:33:28 -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 9313C1B78C; Mon, 18 Apr 2022 05:25:59 -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 31E9C60FB6; Mon, 18 Apr 2022 12:25:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21E64C385A1; Mon, 18 Apr 2022 12:25:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650284758; bh=Axpk3aHDU/KFEP/kw1BzLaJ460vI15II9oA+dh++VWA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lXgO8G9FXrA1qp/XzWa230EuomJtMI+5wOXyMpH1RBCzXkOAynrhUOY8dEFEUNi8K 8uQPh+WwRAXa8JN1iEjbcCZWA2gOCnSX82ObtVDpwGWrgs0TWRnOTWXqUTpizE7rBe E+aGQR55RAkdq2zU7KECYEdEEZP3lnuCPn6A3gZo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Patrick Wang , Catalin Marinas , Andrew Morton , Linus Torvalds Subject: [PATCH 5.17 178/219] mm: kmemleak: take a full lowmem check in kmemleak_*_phys() Date: Mon, 18 Apr 2022 14:12:27 +0200 Message-Id: <20220418121211.860028614@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121203.462784814@linuxfoundation.org> References: <20220418121203.462784814@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Patrick Wang commit 23c2d497de21f25898fbea70aeb292ab8acc8c94 upstream. The kmemleak_*_phys() apis do not check the address for lowmem's min boundary, while the caller may pass an address below lowmem, which will trigger an oops: # echo scan > /sys/kernel/debug/kmemleak Unable to handle kernel paging request at virtual address ff5fffffffe00000 Oops [#1] Modules linked in: CPU: 2 PID: 134 Comm: bash Not tainted 5.18.0-rc1-next-20220407 #33 Hardware name: riscv-virtio,qemu (DT) epc : scan_block+0x74/0x15c ra : scan_block+0x72/0x15c epc : ffffffff801e5806 ra : ffffffff801e5804 sp : ff200000104abc30 gp : ffffffff815cd4e8 tp : ff60000004cfa340 t0 : 0000000000000200 t1 : 00aaaaaac23954cc t2 : 00000000000003ff s0 : ff200000104abc90 s1 : ffffffff81b0ff28 a0 : 0000000000000000 a1 : ff5fffffffe01000 a2 : ffffffff81b0ff28 a3 : 0000000000000002 a4 : 0000000000000001 a5 : 0000000000000000 a6 : ff200000104abd7c a7 : 0000000000000005 s2 : ff5fffffffe00ff9 s3 : ffffffff815cd998 s4 : ffffffff815d0e90 s5 : ffffffff81b0ff28 s6 : 0000000000000020 s7 : ffffffff815d0eb0 s8 : ffffffffffffffff s9 : ff5fffffffe00000 s10: ff5fffffffe01000 s11: 0000000000000022 t3 : 00ffffffaa17db4c t4 : 000000000000000f t5 : 0000000000000001 t6 : 0000000000000000 status: 0000000000000100 badaddr: ff5fffffffe00000 cause: 000000000000000d scan_gray_list+0x12e/0x1a6 kmemleak_scan+0x2aa/0x57e kmemleak_write+0x32a/0x40c full_proxy_write+0x56/0x82 vfs_write+0xa6/0x2a6 ksys_write+0x6c/0xe2 sys_write+0x22/0x2a ret_from_syscall+0x0/0x2 The callers may not quite know the actual address they pass(e.g. from devicetree). So the kmemleak_*_phys() apis should guarantee the address they finally use is in lowmem range, so check the address for lowmem's min boundary. Link: https://lkml.kernel.org/r/20220413122925.33856-1-patrick.wang.shcn@gmail.com Signed-off-by: Patrick Wang Acked-by: Catalin Marinas Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/kmemleak.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1132,7 +1132,7 @@ EXPORT_SYMBOL(kmemleak_no_scan); void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, int min_count, gfp_t gfp) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_alloc(__va(phys), size, min_count, gfp); } EXPORT_SYMBOL(kmemleak_alloc_phys); @@ -1146,7 +1146,7 @@ EXPORT_SYMBOL(kmemleak_alloc_phys); */ void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_free_part(__va(phys), size); } EXPORT_SYMBOL(kmemleak_free_part_phys); @@ -1158,7 +1158,7 @@ EXPORT_SYMBOL(kmemleak_free_part_phys); */ void __ref kmemleak_not_leak_phys(phys_addr_t phys) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_not_leak(__va(phys)); } EXPORT_SYMBOL(kmemleak_not_leak_phys); @@ -1170,7 +1170,7 @@ EXPORT_SYMBOL(kmemleak_not_leak_phys); */ void __ref kmemleak_ignore_phys(phys_addr_t phys) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_ignore(__va(phys)); } EXPORT_SYMBOL(kmemleak_ignore_phys);