From patchwork Tue May 30 18:36:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 100729 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp352694qge; Tue, 30 May 2017 11:37:23 -0700 (PDT) X-Received: by 10.98.73.214 with SMTP id r83mr25351489pfi.12.1496169443419; Tue, 30 May 2017 11:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496169443; cv=none; d=google.com; s=arc-20160816; b=Rr1Q03VBJ4H7MDCpjMistqhAwBbpZYtof8fby10l9fESI289IiA5DiEi6fKaFkONDY y0apfkitDzguQbdS7F1GN7+8eDIaPfJOmsXSJbvHzagggigKRrdrgmUNM63qiszO8PdD qDFHb+vaRfjOSvOMPMZmuIhHbH98ziB4vsuTgn3vLYYwMHyuk92ijXcYYM5xCXofKwuz XgO6B+W9idk6IQ/YqVtIw951rCwUdWQkWna9M9X8Getoj040jWhaVaf1Z3jYjDezu7tP 6p8svjcOFwo2IrIstbIp0GygvboCxso5NwqlKNeX9lSiA3Cyr0JfC/mJ68St38DhAb3l /r3w== 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=SqZkN1lRxkEX0086HFJlTOX64vyyKviGgbkOSCC2PTY=; b=s5foAJsGy4Ljm9HkEzdAUSlbH92iE2R7z+TKqLF/UsEgVWrweduXeV25IsdOdm+Odr Z+OsBF+lLjupjYhNhCWe3lIITZejhvf7PJz2JLiinuSf2weEJhWV/sBEx5otdHHpObT6 TzhEOZ6OAFRezFUVMqaO0xK1VYRZCrLBUk//qGom88GHgyWxOvAbX8ndwLSoL6eeYFlA EpHyva46kSb90McLPbtDYb7F9HXn+YjgJazrcfn6+cMFZtr+Kc2ib4txVrgtQx5J6oCF /f8nU0qN2OcRC4ImkhqWixxKtzF5eFxmTNuHG5UxsD0pl8v3hq0v7BPawKho/kjW1vTR aLSQ== ARC-Authentication-Results: i=1; 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-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 33si44904691plf.190.2017.05.30.11.37.23; Tue, 30 May 2017 11:37:23 -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; 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 S1750821AbdE3ShR (ORCPT + 2 others); Tue, 30 May 2017 14:37:17 -0400 Received: from mail-wm0-f46.google.com ([74.125.82.46]:36019 "EHLO mail-wm0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751218AbdE3ShI (ORCPT ); Tue, 30 May 2017 14:37:08 -0400 Received: by mail-wm0-f46.google.com with SMTP id 7so99024063wmo.1 for ; Tue, 30 May 2017 11:37:07 -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=Rh13DzP8RS2xlmnfFIZlhniU6tQQooT74gnOuizD2f0=; b=U8Eqo0tLB1/nSBNR3OfWG5B9lbQlkV7AqFB2wiws2DaFsZWFX800DZ1o2WqSYehbJs egN2VBi0hvULCzpKh2enrDynsMWlsCOsb3k6nEBW9IGCA4u84Ag09Oo2hgCwQfl4diG1 0gUc5FNuEF8zP7t1TmpKrNn8ZruAQXl0cNNeE= 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=Rh13DzP8RS2xlmnfFIZlhniU6tQQooT74gnOuizD2f0=; b=Jdmf2b36+bkltgXqCl/V2EiNBHPBGgsADfelMKX+GHe6vqVJJTN9V3LO6uHeBXnx6/ jEIkJFCb4wgsOVf2XH6lzwHRqu+G9/D08QuzPXeI+Ep7c0fNuj4VgyPYH/RVHYU9hO1H DChIHen2sfvKx7cRJFa174+kgzQFwy5QOC85xHEFgh15fQhM3SFo/Iwofx17aGMP62/a 1kdHVIz1PuHWjCLGyx55NysTGVKNRbj9rRvmnZLbiN0S5TvwfsvlHyv4yH9XdZDq0FdJ y7UMhESrNqVNMEdy/PaZ8/2psV2V4Lzwhnt5ROAaT+0wtWu2uVPjZ7nqRoYJSFR4wxnX t7UA== X-Gm-Message-State: AODbwcBkvy1R0O1klSV6hHPMM+3tlcBHG+LlYCN9S1Ggdsm0GpR0OE2M m746bnWGpbpNB7CMz1/wfQ== X-Received: by 10.28.136.85 with SMTP id k82mr2602263wmd.55.1496169426579; Tue, 30 May 2017 11:37:06 -0700 (PDT) Received: from localhost.localdomain ([154.144.49.177]) by smtp.gmail.com with ESMTPSA id b8sm15935997wrb.7.2017.05.30.11.37.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2017 11:37:05 -0700 (PDT) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, linux@armlinux.org.uk Cc: linux-arm-kernel@lists.infradead.org, matt@codeblueprint.co.uk, leif.lindholm@linaro.org, Ard Biesheuvel Subject: [PATCH 5/7] arm: efi: split zImage code and data into separate PE/COFF sections Date: Tue, 30 May 2017 18:36:45 +0000 Message-Id: <20170530183647.28557-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170530183647.28557-1-ard.biesheuvel@linaro.org> References: <20170530183647.28557-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. Signed-off-by: Ard Biesheuvel --- arch/arm/boot/compressed/efi-header.S | 30 ++++++++++++++------ arch/arm/boot/compressed/vmlinux.lds.S | 30 +++++++++++++++----- 2 files changed, 44 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 diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S index 4699c432a2e2..3b1d95f43f2b 100644 --- a/arch/arm/boot/compressed/efi-header.S +++ b/arch/arm/boot/compressed/efi-header.S @@ -57,16 +57,16 @@ 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_data_start - __efi_start @ 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 @@ -76,7 +76,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 @@ -97,9 +97,9 @@ extra_header_fields: section_table: .ascii ".text\0\0\0" - .long _end - __efi_start @ VirtualSize + .long __pecoff_data_start - __efi_start @ VirtualSize .long __efi_start @ VirtualAddress - .long _edata - __efi_start @ SizeOfRawData + .long __pecoff_data_start - __efi_start @ SizeOfRawData .long __efi_start @ PointerToRawData .long 0 @ PointerToRelocations .long 0 @ PointerToLineNumbers @@ -107,12 +107,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) }