From patchwork Thu Jun 29 08:18:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 106618 Delivered-To: patch@linaro.org Received: by 10.140.101.44 with SMTP id t41csp818444qge; Thu, 29 Jun 2017 01:19:14 -0700 (PDT) X-Received: by 10.99.165.28 with SMTP id n28mr14397283pgf.163.1498724354335; Thu, 29 Jun 2017 01:19:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1498724354; cv=none; d=google.com; s=arc-20160816; b=Y2XgmBCNIJgEg3B0zIUOiGsVz+RzxkSohehhzTN1fd6UPmebTNHeacOmcj2wnnyygC 4+WTSOzd9aO9EJ0by7b+Fj2cu8bceu4CuP+Z/MqIe7Hgw5czKyXzbPVmTlwvh1YurI68 8J/rhg75VsPusUGGw45OAu4LCMAylp5VRypyWWJfdNHPDy2hiSsbUcNooQPNE7FInoLg ocqo6egq9KAeQQsHbVGFECSpyi9WPwYtqcd7EIMc6q5eCmbEUAmBzDUBxbvugYAItvA4 EdjwCncKJ+Sos/Y2BhwrxTrjIHsZ1tGRX0GFRdg6Gn/PJg/mNeSNvzNyiSBCJd9P7QN9 B4ig== 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=300BPPqbjjuYis0JelbMHLzC+dwmISgjtEdpql+yasc=; b=GJ7eyMU0wLQMnhDdaIZZntZYusTYOlUFmeCaokCf05EIEuVe68SzEodETZDLJGtZMf Nri9u7U6hNEl7u6IsClWEabg7ppFVHZESwbJA/mroWF/SY907ZTEtmqdJl5J36rLG8vT +W0JHv5J819wkiM0z5KFVbAlXHfG9vvRYR5X43ZZSKM1SDUcyXZDctB56oL5KGwojcif H5AZA5Z1dgqQmiVaIk2mVfwq4E42vaey7gMKxqULlyB/QcSKpTPkF9mD/FNHCf1Oo10W hTmgmN1z45yKzJRddp4+ep+Ip0ysB3lsODnDM8EeInXQ174LW3Ky0zsgacpzNfMtdBHN O+Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.b=c3WYGPnL; 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 h68si3195921pfc.73.2017.06.29.01.19.14; Thu, 29 Jun 2017 01:19:14 -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.b=c3WYGPnL; 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 S1751986AbdF2ITN (ORCPT + 2 others); Thu, 29 Jun 2017 04:19:13 -0400 Received: from mail-wm0-f45.google.com ([74.125.82.45]:35938 "EHLO mail-wm0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752462AbdF2ITM (ORCPT ); Thu, 29 Jun 2017 04:19:12 -0400 Received: by mail-wm0-f45.google.com with SMTP id 62so74837949wmw.1 for ; Thu, 29 Jun 2017 01:19:11 -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=fIGRZsUd+gDQXkO06FSHbXqFTO9GpZ0vqMT0AVtvOjc=; b=c3WYGPnLTfEL0AvnbVmExohylhK3L98Prh12OK5k0va98F5zZ71ulLjyJ5jlMf8m/H rpNk3tHiaG7Ujn+7gJYZyIcGHdqLGqlyxIV12f19M/84JWMtKA3hLAaU962axOSjqcaz fAp3IL9fuAYSPdRfeZIP+GendsTK4W5eQJc4k= 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=fIGRZsUd+gDQXkO06FSHbXqFTO9GpZ0vqMT0AVtvOjc=; b=Y7ovmNCXxQnb6TcWZLS6gO+c2+/GNioDDS5+0uPfXy6Mg+sGQ++dTeh7RvMgltQIvR NrAZeLlg7TE6Ov9b6ExC0JvUioGnF3uKMIbAG35yt2R0QB1ccpsvDtldGvWW+lAPQtu+ jbSe6Mj8jRzI2Pemdlc1E1i/tq5wnbHR/ypowylOq9ogQlRoxBKzbl7cAnyvx6RAG5+k euM6wDUJ9PgY0+855u0Kcfi1YEUJX7a+9Pw6plXi54CfCWSf77AZQT4wMZpDB3QO3DuG CCdtJq4Cp+AYkU2L52e/7WZv/+CGxAp3dlyzjeev8IkdAB1twji2MA6nPu/1aklay/sx fkOA== X-Gm-Message-State: AKS2vOyJD/IJMeUnLRMgzmEvRd8bufCLk+mHxtur2DdIWpOlrbR40C1u mSdx0pGzDuDSfsnTQdIm4g== X-Received: by 10.28.93.197 with SMTP id r188mr10032069wmb.44.1498724350781; Thu, 29 Jun 2017 01:19:10 -0700 (PDT) Received: from localhost.localdomain ([196.90.227.203]) by smtp.gmail.com with ESMTPSA id p34sm5268190wrc.66.2017.06.29.01.19.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Jun 2017 01:19:10 -0700 (PDT) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux@armlinux.org.uk Cc: matt@codeblueprint.co.uk, leif.lindholm@linaro.org, Ard Biesheuvel Subject: [PATCH v2 5/7] arm: efi: split zImage code and data into separate PE/COFF sections Date: Thu, 29 Jun 2017 08:18:47 +0000 Message-Id: <20170629081849.15081-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170629081849.15081-1-ard.biesheuvel@linaro.org> References: <20170629081849.15081-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 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.9.3 -- 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 Tested-by: Gregory CLEMENT 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 1fa62432e283..dfcc2baa0077 100644 --- a/arch/arm/boot/compressed/vmlinux.lds.S +++ b/arch/arm/boot/compressed/vmlinux.lds.S @@ -53,13 +53,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) } @@ -75,6 +68,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); @@ -89,6 +102,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) }