From patchwork Fri Aug 18 19:49:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 110440 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp1344811qge; Fri, 18 Aug 2017 12:51:27 -0700 (PDT) X-Received: by 10.84.218.4 with SMTP id q4mr10705581pli.202.1503085887705; Fri, 18 Aug 2017 12:51:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503085887; cv=none; d=google.com; s=arc-20160816; b=EyRnxxKacy6BNgHt7KFENf7555G3yQmklyfRTFA3mz0ULhhH7yo6Uqi4zk5g6TptMS R/96oy7evmdz+cbwlq6j0IYrIbbk2P463DT1rGTgJ9XxICewItQpdzh9n8qKQtW7QDvM l49ubimfmLW+G7aAXBK2QuqY6L/XEj4HCEEgGt/oyM/Hsp/TQaH7lWSrF0htvpSN1s4b zvA7FBQEZk8nNDsAGpUrGc2aSSverCWpI3JCY4fKlkoeuEPLBAE1DxYpBFM1pyAFlGRP 2E7EQGRezI42IuQjRl0t39NR/joTvihD66ftb2pt89F1UcGLMlYIk+DaZf9X6qxlEh+e KYSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Rwo9TEYAsA4KxC3bCYqWzn9OnCn2P3VPU5r9FjRhiEA=; b=seLNA1x7Kg2ZLvm3+aJAWaOzMssSmreNwLh/5VkaWtecEhHs91xYVyJ3mG+mjbsaXW 6eqoGBoP44UUzofLNG/FeeCFt/zEGVNn+dfqRppmwN5XcQu6KJnhi2/Tu+mx8j/oFGZr jMZ9yyF6hYsjCiyBXIu+QDdi0xVY0uZqS8kY/NEJEHfdTGF4LRrQEJalxdnJ/zMuVtxu yZ6KZFl8EBCPAKMyLvDy34ZwiUfJ3jMwyxWlwBSo5A8McUXzh3B0FdhsRkzz+RPBlOiw lQz8W5YOSJFGLWqJi4/geenxtkdiEaBkrkj72e64AgpSnMNBRNdyYkfdzSxnonPNXYBe 0AfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=NnBGZvgk; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b75si138759pfc.166.2017.08.18.12.51.27; Fri, 18 Aug 2017 12:51:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=NnBGZvgk; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752583AbdHRTud (ORCPT + 2 others); Fri, 18 Aug 2017 15:50:33 -0400 Received: from mail-wm0-f41.google.com ([74.125.82.41]:35465 "EHLO mail-wm0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752517AbdHRTub (ORCPT ); Fri, 18 Aug 2017 15:50:31 -0400 Received: by mail-wm0-f41.google.com with SMTP id m85so7252876wma.0 for ; Fri, 18 Aug 2017 12:50:29 -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=7vGcgqXCsgtxMz0rRVsD3pMyLvs1NmPg1vYVgGUO2Zo=; b=NnBGZvgk35Nk2BfUIzazsC2jso+Me4nxe3QpxgaCyQnIU3BRoUGw6h/q7psz7ps6yQ zy01siXJf1nFoaldHZLErtopgMX7NCjkOj0r2lxdldopHV5TogwQKVxoSAMGQjpTfS5P phxR4buX4EwudE5jsG9Lynz8JvTF6Yf6eb5GM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7vGcgqXCsgtxMz0rRVsD3pMyLvs1NmPg1vYVgGUO2Zo=; b=UZ+z8bqw/VRkWfZksu0luWFVvFE5TwcHOVykE9mm/DwYml3C+FBx3dwuhvz4UmXZQd ZoKIfrJwAzuXFBxBYxYGXHWzPFrBT2FTE/3YtcByhjp02E+hPU4q9SwMTwKX3jCA7r9u Zbh/hopILBLszd+1YXxK3irfrK6Ohv20ad0VX4JWdCqDgedBsFQheTvHw7J6GeYJw5ZB S93i9Ed4nE4UnSXoi2SPRR1hxx3cLqrzr9/c4v0Q5pFjoDQZV9hRF+EpBEDyLv8P+mV4 /vcpqTqvKl7uA/ePrHkA4n7cXplYRjGPLbaTuEP69VImJrmYEtROgrQ0uNTwEkjKM0vl caNQ== X-Gm-Message-State: AHYfb5gCwWwc/53ITWC9BSrSI/OTlTEaHNh5frKRo/wbfIf6F56qPwaZ uLWCRRlYK3F90kwTNIsaog== X-Received: by 10.28.150.87 with SMTP id y84mr1972047wmd.67.1503085828357; Fri, 18 Aug 2017 12:50:28 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id k29sm5571168wrk.56.2017.08.18.12.50.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Aug 2017 12:50:27 -0700 (PDT) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner , "H . Peter Anvin" Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Russell King , Matt Fleming Subject: [PATCH 11/14] arm: efi: split zImage code and data into separate PE/COFF sections Date: Fri, 18 Aug 2017 20:49:44 +0100 Message-Id: <20170818194947.19347-12-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170818194947.19347-1-ard.biesheuvel@linaro.org> References: <20170818194947.19347-1-ard.biesheuvel@linaro.org> Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org To prevent unintended modifications to the kernel text (malicious or otherwise) while running the EFI stub, describe the kernel image as two separate sections: a .text section with read-execute permissions, covering .text, .rodata, .piggytext and the GOT sections (which the stub does not care about anyway), and a .data section with read-write permissions, covering .data and .bss. This relies on the firmware to actually take the section permission flags into account, but this is something that is currently being implemented in EDK2, which means we will likely start seeing it in the wild between one and two years from now. Cc: Russell King Cc: Matt Fleming Signed-off-by: Ard Biesheuvel --- arch/arm/boot/compressed/efi-header.S | 32 +++++++++++++++++++++++--------- arch/arm/boot/compressed/vmlinux.lds.S | 30 +++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 16 deletions(-) -- 2.11.0 -- To unsubscribe from this list: send the line "unsubscribe linux-efi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S index 542e1ad432ae..c94a88ae834d 100644 --- a/arch/arm/boot/compressed/efi-header.S +++ b/arch/arm/boot/compressed/efi-header.S @@ -54,20 +54,22 @@ coff_header: IMAGE_FILE_EXECUTABLE_IMAGE | \ IMAGE_FILE_LINE_NUMS_STRIPPED @ Characteristics +#define __pecoff_code_size (__pecoff_data_start - __efi_start) + optional_header: .short PE_OPT_MAGIC_PE32 @ PE32 format .byte 0x02 @ MajorLinkerVersion .byte 0x14 @ MinorLinkerVersion - .long _end - __efi_start @ SizeOfCode - .long 0 @ SizeOfInitializedData + .long __pecoff_code_size @ SizeOfCode + .long __pecoff_data_size @ SizeOfInitializedData .long 0 @ SizeOfUninitializedData .long efi_stub_entry - start @ AddressOfEntryPoint .long start_offset @ BaseOfCode - .long 0 @ BaseOfData + .long __pecoff_data_start - start @ BaseOfData extra_header_fields: .long 0 @ ImageBase - .long SZ_512 @ SectionAlignment + .long SZ_4K @ SectionAlignment .long SZ_512 @ FileAlignment .short 0 @ MajorOsVersion .short 0 @ MinorOsVersion @@ -77,7 +79,7 @@ extra_header_fields: .short 0 @ MinorSubsystemVersion .long 0 @ Win32VersionValue - .long _end - start @ SizeOfImage + .long __pecoff_end - start @ SizeOfImage .long start_offset @ SizeOfHeaders .long 0 @ CheckSum .short IMAGE_SUBSYSTEM_EFI_APPLICATION @ Subsystem @@ -98,9 +100,9 @@ extra_header_fields: section_table: .ascii ".text\0\0\0" - .long _end - __efi_start @ VirtualSize + .long __pecoff_code_size @ VirtualSize .long __efi_start @ VirtualAddress - .long _edata - __efi_start @ SizeOfRawData + .long __pecoff_code_size @ SizeOfRawData .long __efi_start @ PointerToRawData .long 0 @ PointerToRelocations .long 0 @ PointerToLineNumbers @@ -108,12 +110,24 @@ section_table: .short 0 @ NumberOfLineNumbers .long IMAGE_SCN_CNT_CODE | \ IMAGE_SCN_MEM_READ | \ - IMAGE_SCN_MEM_WRITE | \ IMAGE_SCN_MEM_EXECUTE @ Characteristics + .ascii ".data\0\0\0" + .long __pecoff_data_size @ VirtualSize + .long __pecoff_data_start - start @ VirtualAddress + .long __pecoff_data_rawsize @ SizeOfRawData + .long __pecoff_data_start - start @ PointerToRawData + .long 0 @ PointerToRelocations + .long 0 @ PointerToLineNumbers + .short 0 @ NumberOfRelocations + .short 0 @ NumberOfLineNumbers + .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ + IMAGE_SCN_MEM_READ | \ + IMAGE_SCN_MEM_WRITE @ Characteristics + .set section_count, (. - section_table) / 40 - .align 9 + .align 12 __efi_start: #endif .endm diff --git a/arch/arm/boot/compressed/vmlinux.lds.S b/arch/arm/boot/compressed/vmlinux.lds.S index 81c493156ce8..7a4c59154361 100644 --- a/arch/arm/boot/compressed/vmlinux.lds.S +++ b/arch/arm/boot/compressed/vmlinux.lds.S @@ -48,13 +48,6 @@ SECTIONS *(.rodata) *(.rodata.*) } - .data : { - /* - * The EFI stub always executes from RAM, and runs strictly before the - * decompressor, so we can make an exception for its r/w data, and keep it - */ - *(.data.efistub) - } .piggydata : { *(.piggydata) } @@ -70,6 +63,26 @@ SECTIONS /* ensure the zImage file size is always a multiple of 64 bits */ /* (without a dummy byte, ld just ignores the empty section) */ .pad : { BYTE(0); . = ALIGN(8); } + +#ifdef CONFIG_EFI_STUB + .data : ALIGN(4096) { + __pecoff_data_start = .; + /* + * The EFI stub always executes from RAM, and runs strictly before the + * decompressor, so we can make an exception for its r/w data, and keep it + */ + *(.data.efistub) + __pecoff_data_end = .; + + /* + * PE/COFF mandates a file size which is a multiple of 512 bytes if the + * section size equals or exceeds 4 KB + */ + . = ALIGN(512); + } + __pecoff_data_rawsize = . - ADDR(.data); +#endif + _edata = .; _magic_sig = ZIMAGE_MAGIC(0x016f2818); @@ -84,6 +97,9 @@ SECTIONS . = ALIGN(8); /* the stack must be 64-bit aligned */ .stack : { *(.stack) } + PROVIDE(__pecoff_data_size = ALIGN(512) - ADDR(.data)); + PROVIDE(__pecoff_end = ALIGN(512)); + .stab 0 : { *(.stab) } .stabstr 0 : { *(.stabstr) } .stab.excl 0 : { *(.stab.excl) }