From patchwork Mon Feb 6 16:24:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9558351 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B10C6602B1 for ; Mon, 6 Feb 2017 16:26:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A2AE526D05 for ; Mon, 6 Feb 2017 16:26:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9659A27F88; Mon, 6 Feb 2017 16:26:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id C02A226D05 for ; Mon, 6 Feb 2017 16:25:59 +0000 (UTC) Received: (qmail 28236 invoked by uid 550); 6 Feb 2017 16:25:26 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 28178 invoked from network); 6 Feb 2017 16:25:24 -0000 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=3g2Rw6lAepXrJ4jtGVWKiCJqVJZpnNMFl0XBrC62FMM=; b=gh2NcK3WFqkW4fiPQ+ABrd9lIVQ9tpezJE3MotlPUtCRfevV2mlSYDxY0aZS2C58gs NgXjlVImiAIzCs4MzczzRoTj8Pvr53cMBkuO8I3ankf04RjUPAVnj8o8pCN2IZZxpcPY zDAQqeuQEhghlz6sNbONXPi2Rd2BtaPa+vxrY= 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=3g2Rw6lAepXrJ4jtGVWKiCJqVJZpnNMFl0XBrC62FMM=; b=TLsfkStDSRrPF1t9PE7MfrwFxcUbz7NhWTT0U7OpzrV3HhyHaTWjpWXMzr8vzv5wbe DcwqghifvNplrZfsdRlIBeogjA64IimFF4K3bOZqw4/a5PzJMF8QC+xDtAo4tkSNBXqT foXEJRtNIgkm53EuFeGRxZ6bAtc3r9TVyGxkqeCa3rcVVf/MYjWlGqyERAY3SoNqwq9p inO0GC4fhiRcMr8csCz+Y8AQ9HYVukfPweWM3qV6yUe9C3i1+YUHQbh7Ot9so6L9Hbhl EDkDBCYmorZHjL4zRjYbdEJZI0fzTwbZRYHgDctEJk1kXx/5AwFyrOs3e/EXqk2DNOzF MfEA== X-Gm-Message-State: AMke39nHmsCe3/8U9WIeQsmPCCrXCyfpi1xAM6D1GlKsA9OyTXOWCinVJk3yhMLdXSQg4nFP X-Received: by 10.28.131.72 with SMTP id f69mr10265960wmd.140.1486398312660; Mon, 06 Feb 2017 08:25:12 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, will.deacon@arm.com, catalin.marinas@arm.com, mark.rutland@arm.com Cc: labbott@fedoraproject.org, kernel-hardening@lists.openwall.com, leif.lindholm@linaro.org, pjones@redhat.com, Ard Biesheuvel Date: Mon, 6 Feb 2017 16:24:35 +0000 Message-Id: <1486398275-3966-8-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1486398275-3966-1-git-send-email-ard.biesheuvel@linaro.org> References: <1486398275-3966-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [kernel-hardening] [PATCH 7/7] arm64: efi: split Image code and data into separate PE/COFF sections X-Virus-Scanned: ClamAV using ClamSMTP 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 and .init.text, and a .data section with read-write permissions, covering .init.data, .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/arm64/kernel/efi-header.S | 25 +++++++++++++++----- arch/arm64/kernel/vmlinux.lds.S | 5 ++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kernel/efi-header.S b/arch/arm64/kernel/efi-header.S index c87c23336c86..fc93ab48f69a 100644 --- a/arch/arm64/kernel/efi-header.S +++ b/arch/arm64/kernel/efi-header.S @@ -38,7 +38,7 @@ pe_header: .long PE_MAGIC coff_header: .short IMAGE_FILE_MACHINE_ARM64 // Machine - .short 1 // NumberOfSections + .short 2 // NumberOfSections .long 0 // TimeDateStamp .long 0 // PointerToSymbolTable .long 0 // NumberOfSymbols @@ -51,8 +51,8 @@ optional_header: .short PE_OPT_MAGIC_PE32PLUS // PE32+ format .byte 0x02 // MajorLinkerVersion .byte 0x14 // MinorLinkerVersion - .long _end - efi_header_end // SizeOfCode - .long 0 // SizeOfInitializedData + .long __pecoff_data_start - efi_header_end // SizeOfCode + .long __pecoff_data_size // SizeOfInitializedData .long 0 // SizeOfUninitializedData .long __efistub_entry - _head // AddressOfEntryPoint .long efi_header_end - _head // BaseOfCode @@ -98,9 +98,9 @@ extra_header_fields: // Section table section_table: .ascii ".text\0\0\0" - .long _end - efi_header_end // VirtualSize + .long __pecoff_data_start - efi_header_end // VirtualSize .long efi_header_end - _head // VirtualAddress - .long _edata - efi_header_end // SizeOfRawData + .long __pecoff_data_start - efi_header_end // SizeOfRawData .long efi_header_end - _head // PointerToRawData .long 0 // PointerToRelocations @@ -108,7 +108,20 @@ section_table: .short 0 // NumberOfRelocations .short 0 // NumberOfLineNumbers .long IMAGE_SCN_CNT_CODE | \ - IMAGE_SCN_MEM_EXECUTE | \ + IMAGE_SCN_MEM_READ | \ + IMAGE_SCN_MEM_EXECUTE // Characteristics + + .ascii ".data\0\0\0" + .long __pecoff_data_size // VirtualSize + .long __pecoff_data_start - _head // VirtualAddress + .long __pecoff_data_rawsize // SizeOfRawData + .long __pecoff_data_start - _head // 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 diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index b8deffa9e1bf..a93cc2b6f50b 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -149,6 +149,9 @@ SECTIONS ARM_EXIT_KEEP(EXIT_TEXT) } + . = ALIGN(SZ_4K); + __pecoff_data_start = .; + .init.data : { INIT_DATA INIT_SETUP(16) @@ -206,6 +209,7 @@ SECTIONS } PECOFF_EDATA_PADDING + __pecoff_data_rawsize = ABSOLUTE(. - __pecoff_data_start); _edata = .; BSS_SECTION(0, 0, 0) @@ -221,6 +225,7 @@ SECTIONS . += RESERVED_TTBR0_SIZE; #endif + __pecoff_data_size = ABSOLUTE(. - __pecoff_data_start); _end = .; STABS_DEBUG