Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2538666yba; Mon, 6 May 2019 07:41:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqx9qeKM2+iT2g+ViTVnug8mxt3fRxfd6EhIVrucfnA4G3jdtiOtq2GRWDm6uxAqzNwO3WLS X-Received: by 2002:a63:90c7:: with SMTP id a190mr33276348pge.23.1557153711400; Mon, 06 May 2019 07:41:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557153711; cv=none; d=google.com; s=arc-20160816; b=idcKfHAOMM9V82lpsIi7JoGzLppjKFcSFhXGFQrpdx6ORHUpS0eJQImfzGufgISRXm 6YvSUjZ0rM0+Bxq9eD9IXuUaTOJXTaeh8IBIUUhWFpyK/m6GyWGRP/FweD2OZaRZlFRS hfJtP+cFR2iXi3jTU5nj/Nsrlet0ViYPsCfAKKayKq2lIuvxnujjQKZ+WY9T1yO04O8q 2bQjTPQwweDSbM9QiueM7vn+X4FwHySK2A76tOp67mbm566aqIMxWjGbFh+YT902R/eA FVb9hpUV+XSRW35eXFCPigAMySiEvkRh25qK458yENf9b8PuJ/5ZOht+gdHiq/Hm8BPw b5Fw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3vXejIL3TheOC/QoCRaV+ubojOAWLW4dCmxmJbNndk8=; b=hIaI+6gjb9Wf7TDBLgIJpSzN5LTrJGKdGIS0nQR+Dm386LlE62crMi1IZDoTad4y4l U26o9hfPVm1QhT8oM1vMZgGPOnUUv7gIy7scRqgGCSCFWlomPuRXuuJV95BWjFhnFUQd 7j6pEiSu7sWiTYIAhTnVEs3KsOGU6uW6Rqd3TaTog4yJxQXT458VkgOha9gtEay0mn+K IpM4mMknUAYVn5Eh4/Rx8jZ/rWD/VxBs3DIZ2LG7K6I63nEZUXy7WUUR7DozEtXTjIE/ s4n8TvjvvWpQ+gvfSaD6eg3dPueTe5sfeSYR1T25DUsNy5eN4yzrG7iMDUz7oOhpgJzY Z04w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uxkiASga; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w2si14623882plp.26.2019.05.06.07.41.35; Mon, 06 May 2019 07:41:51 -0700 (PDT) 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=uxkiASga; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727651AbfEFOih (ORCPT + 99 others); Mon, 6 May 2019 10:38:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:59720 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727640AbfEFOif (ORCPT ); Mon, 6 May 2019 10:38:35 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B26B6214C6; Mon, 6 May 2019 14:38:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557153514; bh=6UwhSJvLPHakxy/CgvdRH79bYTbwDNT2blP4FrTN36Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uxkiASgatAa4BnOnlR/IxQfv+qnghI93PJ4fJku6yEZnh2t+/taljSYfeaJf9PsFH HHsho1TeBfdhuSPy0xDB+n0CmME4WEuLRdP9uu+15vAJIGGAZO/wsgkXstxNF84xN4 Z8vLlRoWAyAVic7596XJhHuWpcOHkgs3TFoOfD24= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Baoquan He , Borislav Petkov , Thomas Garnier , "Kirill A. Shutemov" , "H. Peter Anvin" , Andy Lutomirski , Dave Hansen , Ingo Molnar , Kees Cook , Peter Zijlstra , Thomas Gleixner , frank.ramsay@hpe.com, herbert@gondor.apana.org.au, kirill@shutemov.name, mike.travis@hpe.com, x86-ml , yamada.masahiro@socionext.com Subject: [PATCH 5.0 118/122] x86/mm/KASLR: Fix the size of the direct mapping section Date: Mon, 6 May 2019 16:32:56 +0200 Message-Id: <20190506143104.973518031@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190506143054.670334917@linuxfoundation.org> References: <20190506143054.670334917@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Baoquan He commit ec3937107ab43f3e8b2bc9dad95710043c462ff7 upstream. kernel_randomize_memory() uses __PHYSICAL_MASK_SHIFT to calculate the maximum amount of system RAM supported. The size of the direct mapping section is obtained from the smaller one of the below two values: (actual system RAM size + padding size) vs (max system RAM size supported) This calculation is wrong since commit b83ce5ee9147 ("x86/mm/64: Make __PHYSICAL_MASK_SHIFT always 52"). In it, __PHYSICAL_MASK_SHIFT was changed to be 52, regardless of whether the kernel is using 4-level or 5-level page tables. Thus, it will always use 4 PB as the maximum amount of system RAM, even in 4-level paging mode where it should actually be 64 TB. Thus, the size of the direct mapping section will always be the sum of the actual system RAM size plus the padding size. Even when the amount of system RAM is 64 TB, the following layout will still be used. Obviously KALSR will be weakened significantly. |____|_______actual RAM_______|_padding_|______the rest_______| 0 64TB ~120TB Instead, it should be like this: |____|_______actual RAM_______|_________the rest______________| 0 64TB ~120TB The size of padding region is controlled by CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING, which is 10 TB by default. The above issue only exists when CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING is set to a non-zero value, which is the case when CONFIG_MEMORY_HOTPLUG is enabled. Otherwise, using __PHYSICAL_MASK_SHIFT doesn't affect KASLR. Fix it by replacing __PHYSICAL_MASK_SHIFT with MAX_PHYSMEM_BITS. [ bp: Massage commit message. ] Fixes: b83ce5ee9147 ("x86/mm/64: Make __PHYSICAL_MASK_SHIFT always 52") Signed-off-by: Baoquan He Signed-off-by: Borislav Petkov Reviewed-by: Thomas Garnier Acked-by: Kirill A. Shutemov Cc: "H. Peter Anvin" Cc: Andy Lutomirski Cc: Dave Hansen Cc: Ingo Molnar Cc: Kees Cook Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: frank.ramsay@hpe.com Cc: herbert@gondor.apana.org.au Cc: kirill@shutemov.name Cc: mike.travis@hpe.com Cc: thgarnie@google.com Cc: x86-ml Cc: yamada.masahiro@socionext.com Link: https://lkml.kernel.org/r/20190417083536.GE7065@MiWiFi-R3L-srv Signed-off-by: Greg Kroah-Hartman --- arch/x86/mm/kaslr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/x86/mm/kaslr.c +++ b/arch/x86/mm/kaslr.c @@ -94,7 +94,7 @@ void __init kernel_randomize_memory(void if (!kaslr_memory_enabled()) return; - kaslr_regions[0].size_tb = 1 << (__PHYSICAL_MASK_SHIFT - TB_SHIFT); + kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT); kaslr_regions[1].size_tb = VMALLOC_SIZE_TB; /*