Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp915685iog; Fri, 17 Jun 2022 17:23:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sM0DLQk9qkPa3mEIJZ7RSPR5MsXIyGWrLn/kWKYNfrP40wrdnxg44xLZ5cXsa04Ue67fdk X-Received: by 2002:a05:6402:4001:b0:435:b20:d7ae with SMTP id d1-20020a056402400100b004350b20d7aemr15332632eda.90.1655511825899; Fri, 17 Jun 2022 17:23:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655511825; cv=none; d=google.com; s=arc-20160816; b=px//YhbZpI9cS5Y/lnQ3g9bVhWfipntz8NfxEcjq3DLl0fW1TG5CAMHWx4wrTcpNhT zDGOn7tvDTspL62jXCzXBdSE361mav5Pu7JEiWveO8++ojTHnT6CwtFZ27jXbk0BDvd3 DrN7EHlkd3uv04ynqa19991PZexYQQbugoUUSR2dHsKDi+jj4rPnG8k6DlofU/6dnYA1 4/TJHK/JGMVdU0t+mguSavXziXFmF5vV2xRtkLXLM+fuJ3A5qJtYssw/IBPyLc9HFv9+ 2ET8qh9oBrD+8yqszFviviir8KGc5r0PHwa29hiQpIcs3dIeHaCTYhHIoTFl8oq9o5Pp AkFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=UrOYsC1ew/w6p73+nsWTtgf6LHyGTERsTIG20+T+qIA=; b=B1sYfjYiLuuIzbN/US50z3AHhRBbnjR6cu4ePhX6b+jjP8NYkoQg2ednLv8x9WxQWX nq9tQuyaMR3aWvPkXYjnMklT0bKOuv10KUjAWKQgfx9JjWABB96wVhkVZ3x3UcPC4u3e 61iOKiaBA5KAZqxitnyN24HWd+NvYeHxdwD5tKPdi6zAeGI7CQhA1vgY1cSURvLdK8HU jyReL94r2oT82wx9pd/JpgVwFEe/I98ygw8pMFVGqtbkr0wgvDwwFOg8IklFBloK4a/e xJKPLuc0+c6gNDEWSCJJuLpQDWLgCDWpwxPr4hVSqsnfWixF2EHP0fqBek2sIPUZQeGt S3aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20210112.gappssmtp.com header.s=20210112 header.b=bWeTqjlm; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x2-20020a1709065ac200b006ff1e01c729si2989390ejs.1009.2022.06.17.17.23.20; Fri, 17 Jun 2022 17:23:45 -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=@shutemov-name.20210112.gappssmtp.com header.s=20210112 header.b=bWeTqjlm; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242164AbiFRAUO (ORCPT + 99 others); Fri, 17 Jun 2022 20:20:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236705AbiFRAUN (ORCPT ); Fri, 17 Jun 2022 20:20:13 -0400 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 689DF590B4 for ; Fri, 17 Jun 2022 17:20:12 -0700 (PDT) Received: by mail-lj1-x231.google.com with SMTP id y29so6277707ljd.7 for ; Fri, 17 Jun 2022 17:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=UrOYsC1ew/w6p73+nsWTtgf6LHyGTERsTIG20+T+qIA=; b=bWeTqjlmkUrmkwzI2B0BHdb/N/1umG3v250Prir35ImXG/+7Kc4GXIoLBzUiI8ngcH P0FKVfhaobo5HBD0tlJ/ZPBxPcHY9wChnOSGVnuUg95vJjia3SgK2yo/LBtG/koNQDEd Wk1ZjPRD4toLKKfVofO+e+1dl64Dvs0MN/bes+90C/KkJnzvkFV32WMI+86FSRkwNxBS r/d1ci66ssdEuvretCbaeHf+UCBdmLcWSt2gCFv5uyy7UqJYrnBY4M7WxHZIIDnd656h PDBf4Z0bMhVtD9cQuNCPHLRjhMXDfeI9mxAwdHMRP8aijs/Jx09kZDoMB1M+IurYve1Y FVUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=UrOYsC1ew/w6p73+nsWTtgf6LHyGTERsTIG20+T+qIA=; b=UgFdgU+lGAmboS7eYAQq0+nSjh0XIkM5aEkgvicDG4/u7AzRPTtxTO33ABmWaMTVcM oA3luuKiJOXYPwkT+Ee4iL6oxOo3KQac7myicm8TDRh5YtGF14AKpbpDmxmOs5llEyAt KHzzm2QqA+lkKjMgwh3GzXT7/mWbujAZPTQhcAkhW/mHf6V4fvrK8F3tY3OQhPMLU577 /KYlYhNwWptbaDNHk/Cx14fOl46KMuM1KwGpjan8tM02hjqCYAVN6xHrtlIhszIGt95Q yAcyTcyFbZsrlP/I0cmW1SbUVbrezgLxPdmC2d9f+ZKKI5K4UvsZ4p+In5cAKqWeeHGe eXLw== X-Gm-Message-State: AJIora8RIFKYtn9lNnmqdUKgfjKnhEemWch6tiul2RTRt7MG+LpL/lAe VI7MNmk7Yjiyxee73nWOAzpQFA== X-Received: by 2002:a2e:a549:0:b0:25a:44ab:5aa with SMTP id e9-20020a2ea549000000b0025a44ab05aamr6535706ljn.248.1655511610719; Fri, 17 Jun 2022 17:20:10 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id o4-20020ac25e24000000b00479471cda69sm815354lfg.156.2022.06.17.17.20.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jun 2022 17:20:10 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id 6DC4D103719; Sat, 18 Jun 2022 03:22:20 +0300 (+03) Date: Sat, 18 Jun 2022 03:22:20 +0300 From: "Kirill A. Shutemov" To: Yuntao Wang Cc: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Baoquan He , x86@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] x86/mm: Fix possible index overflow when creating page table mapping Message-ID: <20220618002220.panwk4zwnigayboy@box.shutemov.name> References: <20220616135510.1784995-1-ytcoode@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220616135510.1784995-1-ytcoode@gmail.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, 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 On Thu, Jun 16, 2022 at 09:55:10PM +0800, Yuntao Wang wrote: > There are two issues in phys_p4d_init(): > > - The __kernel_physical_mapping_init() does not do boundary-checking for > paddr_end and passes it directly to phys_p4d_init(), phys_p4d_init() does > not do bounds checking either, so if the physical memory to be mapped is > large enough, 'p4d_page + p4d_index(vaddr)' will wrap around to the > beginning entry of the P4D table and its data will be overwritten. > > - The for loop body will be executed only when 'vaddr < vaddr_end' > evaluates to true, but if that condition is true, 'paddr >= paddr_end' > will evaluate to false, thus the 'if (paddr >= paddr_end) {}' block will > never be executed and become dead code. > > To fix these issues, use 'i < PTRS_PER_P4D' instead of 'vaddr < vaddr_end' > as the for loop condition, this also make it more consistent with the logic > of the phys_{pud,pmt,pte}_init() functions. Hm. I don't see why you changed phys_p4d_init(), but not __kernel_physical_mapping_init(). It does exactly the same thing, just pgd_index() is hidden a bit deeper than p4d_index(). -- Kirill A. Shutemov