From patchwork Wed Mar 30 16:38:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 64723 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp2710178lbc; Wed, 30 Mar 2016 10:04:49 -0700 (PDT) X-Received: by 10.98.18.71 with SMTP id a68mr14894650pfj.41.1459357489461; Wed, 30 Mar 2016 10:04:49 -0700 (PDT) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id fc1si7673753pab.126.2016.03.30.10.04.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Mar 2016 10:04:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1alJWY-0001fE-EZ; Wed, 30 Mar 2016 17:03:18 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1alJ9W-0008En-2d for linux-arm-kernel@lists.infradead.org; Wed, 30 Mar 2016 16:40:22 +0000 Received: by mail-wm0-x230.google.com with SMTP id 127so104426635wmu.1 for ; Wed, 30 Mar 2016 09:39:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kKv1YeERqyKZaRKCyf9ZqBrMxBJI2GNtcEFNSj5oVAM=; b=VuOvitbAPpqJ5C62Np9DsVXC6UvH3Q2BkRBpL5HePKmlLdGm5mZjGAj9bN/yW1BsYh 8ggOzb9I+y2+cIhxqQRkcDY5UomwsJlYrmscvBZohU0pCw5IFl8pD0w+Ilu9IxlUhpd7 1eYAOyqtMu3iVyBdjySUB+NMpG04G1Jo+6gZ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kKv1YeERqyKZaRKCyf9ZqBrMxBJI2GNtcEFNSj5oVAM=; b=i0Aa2sT3hz9Jli0JkQC8WRd7IV6XXyAbTYokVQxVj9/wFqFmI4RFCUIMt8zyBVOe/t 1Udeo61SXUn/34fL95SJwUM/z6eGVnXaOSSOCHegnp6qIqbX8H1YNREIrlDQ8WqaVWl2 tvJyJH4muEOducg6XQVKO3Om+NfL+9mK6dQH19yFYxJmsWnSKtmCHkmkP8bytI2X4m1g MR1enPFkBPxIhUj7HnlBd6/+3xrpmY95UhSs61ZvKGGaZ90828CX5WFv7tbvl8MG5TfC n+UEwJumZsdfWCGTyVk9GAqyV5M2bmeZeVPdV/HxXwu3hyuQLz96C1envIO9c9F4eoxd xYCQ== X-Gm-Message-State: AD7BkJKYV3etljtmy9mGdxmIHs7Ezhv4v8VqsclcHcs6PFM8UBEoWl3PC7Wi7SDHGdnChu62 X-Received: by 10.194.104.202 with SMTP id gg10mr11657268wjb.103.1459355948179; Wed, 30 Mar 2016 09:39:08 -0700 (PDT) Received: from localhost.localdomain ([195.55.142.58]) by smtp.gmail.com with ESMTPSA id l124sm5233151wmf.11.2016.03.30.09.39.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 30 Mar 2016 09:39:07 -0700 (PDT) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, matt@codeblueprint.co.uk, mark.rutland@arm.com, linux@arm.linux.org.uk Subject: [PATCH v2 1/5] ARM: efi: apply strict permissons for UEFI Runtime Services regions Date: Wed, 30 Mar 2016 18:38:49 +0200 Message-Id: <1459355933-13529-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1459355933-13529-1-git-send-email-ard.biesheuvel@linaro.org> References: <1459355933-13529-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160330_093931_162469_2C655ACB X-CRM114-Status: GOOD ( 15.94 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:230 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel , catalin.marinas@arm.com, will.deacon@arm.com, leif.lindholm@linaro.org, sai.praneeth.prakhya@intel.com, pjones@redhat.com, Russell King MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine. Cc: Russell King Signed-off-by: Ard Biesheuvel --- arch/arm/include/asm/efi.h | 1 + arch/arm/kernel/efi.c | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+) -- 2.5.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/arch/arm/include/asm/efi.h b/arch/arm/include/asm/efi.h index e0eea72deb87..b0c341d7ceee 100644 --- a/arch/arm/include/asm/efi.h +++ b/arch/arm/include/asm/efi.h @@ -22,6 +22,7 @@ void efi_init(void); int efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md); +int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); #define efi_call_virt(f, ...) \ ({ \ diff --git a/arch/arm/kernel/efi.c b/arch/arm/kernel/efi.c index ff8a9d8acfac..9f43ba012d10 100644 --- a/arch/arm/kernel/efi.c +++ b/arch/arm/kernel/efi.c @@ -11,6 +11,41 @@ #include #include +static int __init set_permissions(pte_t *ptep, pgtable_t token, + unsigned long addr, void *data) +{ + efi_memory_desc_t *md = data; + pte_t pte = *ptep; + + if (md->attribute & EFI_MEMORY_RO) + pte = set_pte_bit(pte, __pgprot(L_PTE_RDONLY)); + if (md->attribute & EFI_MEMORY_XP) + pte = set_pte_bit(pte, __pgprot(L_PTE_XN)); + set_pte_ext(ptep, pte, PTE_EXT_NG); + return 0; +} + +int __init efi_set_mapping_permissions(struct mm_struct *mm, + efi_memory_desc_t *md) +{ + unsigned long base, size; + + base = md->virt_addr; + size = md->num_pages << EFI_PAGE_SHIFT; + + /* + * We can only use apply_to_page_range() if we can guarantee that the + * entire region was mapped using pages. This should be the case if the + * region does not cover any naturally aligned SECTION_SIZE sized + * blocks. + */ + if (round_down(base + size, SECTION_SIZE) < + round_up(base, SECTION_SIZE) + SECTION_SIZE) + return apply_to_page_range(mm, base, size, set_permissions, md); + + return 0; +} + int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md) { struct map_desc desc = { @@ -34,5 +69,11 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md) desc.type = MT_DEVICE; create_mapping_late(mm, &desc, true); + + /* + * If stricter permissions were specified, apply them now. + */ + if (md->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP)) + return efi_set_mapping_permissions(mm, md); return 0; }