Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp567203ybz; Fri, 24 Apr 2020 05:34:21 -0700 (PDT) X-Google-Smtp-Source: APiQypIT8ZP6ojHWYEYglDtKiFOqnhQJNzxHny0EbhFmW8o2/NBRXeQRqNCLHFxoW3gUqEcwts2H X-Received: by 2002:a17:906:e5a:: with SMTP id q26mr6520381eji.261.1587731661840; Fri, 24 Apr 2020 05:34:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587731661; cv=none; d=google.com; s=arc-20160816; b=MFPEA68iNFPqL9B4wLtvvYnDRiA8ipUlthMTYjz6cPA2wNNVOW203eyxQ/0qWyRnr5 AFqaopiyDOByRgnfqxB9RDL2KuuBgZt3zlxRwPs7qL3Jmgr2gXNaSSp788a1KD3chZo6 RhyuJfLkaLAOPnKBvc/eZno6UURTsmL7GSMYeuAMUQgmhtM9JCbDFXRdJcZDaQTKUteU buVgU8vyBypqt2KB9b3EaeuYwk3CMnelyylkSnfztPBjyCO7TW3lu38UnQROA2PzskBn Sx/5iFB5ChRphuRZxInb2dRZxaXbkuYSesJybNNyhXrm4RxJdO9VYiu+WpbENTaX6z9L UcvQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2awecuP7jUks+ml9BUoqyTf6thlyNZibp7/Z7BVt9OI=; b=rPfVcNDFswdwEEpSCeLG4RuU6TlLgB2UnZdPw8COpSL4GHcMc3isMmEA2I0ZixPv1/ 7lR5tohznm1hW3r4BOoUOOtyjdpkBVmZrVMZBkvvBb0v7B5mRX4C99OPdQTq550UAm/q fBEjSk8g95Rz4dw0Ncr6rqCefZXvKKj+Z58wGZKmokahh2TulyAfDmS/Tw9s+wKnv196 IzkbBS+AQ6G2ZVDleojI2GeBX6cmn57TYXtsupctH6bJSDLCPq8mweV+X/aetnyUpgQT jacZapFJmmlGQyiborx3NKbJcwGTMDNBokpQYtSUGSJAea8qd76GtZ72XBWfE0x/1Ycn VBaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="tm04s82/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v16si2919466eds.400.2020.04.24.05.33.57; Fri, 24 Apr 2020 05:34:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="tm04s82/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727017AbgDXMcV (ORCPT + 99 others); Fri, 24 Apr 2020 08:32:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:52356 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727772AbgDXMW7 (ORCPT ); Fri, 24 Apr 2020 08:22:59 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6378A21655; Fri, 24 Apr 2020 12:22:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587730978; bh=HzlJRxvoWLWrTG8oY8QmwnmlLgFsVoN2D29ndbKHzCw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tm04s82/GByCVOB6vsPM/EQh5eNNz/7eTVlQWA/SUj3IDzNZbzVMm+Wm7R8t8RtHk ZQuupQMrrnpp4swwOIAc6y+fqV3Te27SmZji+G6BfJshW1gJIUrN51FnG5UHMLUQjI KegSZj5+/YgvGfJQAv9iUAUM9rlsaFIr7372wiFE= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ard Biesheuvel , Jiri Slaby , Ingo Molnar , Sasha Levin , linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, x86@kernel.org Subject: [PATCH AUTOSEL 5.6 18/38] efi/x86: Don't remap text<->rodata gap read-only for mixed mode Date: Fri, 24 Apr 2020 08:22:16 -0400 Message-Id: <20200424122237.9831-18-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200424122237.9831-1-sashal@kernel.org> References: <20200424122237.9831-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ard Biesheuvel [ Upstream commit f6103162008dfd37567f240b50e5e1ea7cf2e00c ] Commit d9e3d2c4f10320 ("efi/x86: Don't map the entire kernel text RW for mixed mode") updated the code that creates the 1:1 memory mapping to use read-only attributes for the 1:1 alias of the kernel's text and rodata sections, to protect it from inadvertent modification. However, it failed to take into account that the unused gap between text and rodata is given to the page allocator for general use. If the vmap'ed stack happens to be allocated from this region, any by-ref output arguments passed to EFI runtime services that are allocated on the stack (such as the 'datasize' argument taken by GetVariable() when invoked from efivar_entry_size()) will be referenced via a read-only mapping, resulting in a page fault if the EFI code tries to write to it: BUG: unable to handle page fault for address: 00000000386aae88 #PF: supervisor write access in kernel mode #PF: error_code(0x0003) - permissions violation PGD fd61063 P4D fd61063 PUD fd62063 PMD 386000e1 Oops: 0003 [#1] SMP PTI CPU: 2 PID: 255 Comm: systemd-sysv-ge Not tainted 5.6.0-rc4-default+ #22 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0008:0x3eaeed95 Code: ... <89> 03 be 05 00 00 80 a1 74 63 b1 3e 83 c0 48 e8 44 d2 ff ff eb 05 RSP: 0018:000000000fd73fa0 EFLAGS: 00010002 RAX: 0000000000000001 RBX: 00000000386aae88 RCX: 000000003e9f1120 RDX: 0000000000000001 RSI: 0000000000000000 RDI: 0000000000000001 RBP: 000000000fd73fd8 R08: 00000000386aae88 R09: 0000000000000000 R10: 0000000000000002 R11: 0000000000000000 R12: 0000000000000000 R13: ffffc0f040220000 R14: 0000000000000000 R15: 0000000000000000 FS: 00007f21160ac940(0000) GS:ffff9cf23d500000(0000) knlGS:0000000000000000 CS: 0008 DS: 0018 ES: 0018 CR0: 0000000080050033 CR2: 00000000386aae88 CR3: 000000000fd6c004 CR4: 00000000003606e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: Modules linked in: CR2: 00000000386aae88 ---[ end trace a8bfbd202e712834 ]--- Let's fix this by remapping text and rodata individually, and leave the gaps mapped read-write. Fixes: d9e3d2c4f10320 ("efi/x86: Don't map the entire kernel text RW for mixed mode") Reported-by: Jiri Slaby Tested-by: Jiri Slaby Signed-off-by: Ard Biesheuvel Signed-off-by: Ingo Molnar Link: https://lore.kernel.org/r/20200409130434.6736-10-ardb@kernel.org Signed-off-by: Sasha Levin --- arch/x86/platform/efi/efi_64.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c index a472940638820..a20914b38e6ae 100644 --- a/arch/x86/platform/efi/efi_64.c +++ b/arch/x86/platform/efi/efi_64.c @@ -202,7 +202,7 @@ virt_to_phys_or_null_size(void *va, unsigned long size) int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) { - unsigned long pfn, text, pf; + unsigned long pfn, text, pf, rodata; struct page *page; unsigned npages; pgd_t *pgd = efi_mm.pgd; @@ -256,7 +256,7 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) efi_scratch.phys_stack = page_to_phys(page + 1); /* stack grows down */ - npages = (__end_rodata_aligned - _text) >> PAGE_SHIFT; + npages = (_etext - _text) >> PAGE_SHIFT; text = __pa(_text); pfn = text >> PAGE_SHIFT; @@ -266,6 +266,14 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) return 1; } + npages = (__end_rodata - __start_rodata) >> PAGE_SHIFT; + rodata = __pa(__start_rodata); + pfn = rodata >> PAGE_SHIFT; + if (kernel_map_pages_in_pgd(pgd, pfn, rodata, npages, pf)) { + pr_err("Failed to map kernel rodata 1:1\n"); + return 1; + } + return 0; } -- 2.20.1