From patchwork Thu Mar 9 20:52:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9614167 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 1846C60414 for ; Thu, 9 Mar 2017 20:54:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0DF5E28334 for ; Thu, 9 Mar 2017 20:54:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 023652866A; Thu, 9 Mar 2017 20:54:25 +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 10B2928334 for ; Thu, 9 Mar 2017 20:54:24 +0000 (UTC) Received: (qmail 29956 invoked by uid 550); 9 Mar 2017 20:54:23 -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 27716 invoked from network); 9 Mar 2017 20:53:10 -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=mAHHInkC8hGGXWoZi1ok1eXqTU5vc7Mwhf7OcMXoUN4=; b=dEQuZnFgCzxucKSmgf3RGNey2se1p+qmVa/mrPvSOCAB6a79XYh05jK3F1dNvuF5vB jxa4DIDqfywCDpKIifL8jmguItr0GzDCEhyhFLJZcMSXmo5z89xD0zeNBN8nuh4lUk23 nI4RAxrStwEVzht2vkObfRamPLJxXHr/bJ3w0= 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=mAHHInkC8hGGXWoZi1ok1eXqTU5vc7Mwhf7OcMXoUN4=; b=F89Lfhv3p9ymKANm5QOxBfrCW5FYoLXt6LQue8BaVkqc2kwvfMtB/hFTpZmKbzg2NF UJGsHkbdfVbR1EdUhghEXcn4fJk1lOUzgUYnwrl5NaL54r26c7QVziVFnJSDXHhESHjp DeK/1gyN5i7nEEVQFLBZyXuUivwdEsZ/EIKxlKVMv57h7SFchu1FY6ZQiOmYz7Rx2eUh /pjYdPnGdV1wV339EnviuFlBa4sv1iQwJLU16au9Tys1pv9fpZGRpySdS+nWwRkD5GNY z9PZZ1xgCOmB2xPZAjXXsnMm01akUmueRHP5yykfAjzcTXjf+2MGTj6HCx7KOzRBpWGW Bi8A== X-Gm-Message-State: AMke39n8AptpXyNPzqajcOpp+1U4OLsqJeelnMzU5Xe3vnmDlvDJHDxYXDQAOoI9h1gluK/L X-Received: by 10.28.93.68 with SMTP id r65mr12986539wmb.133.1489092778828; Thu, 09 Mar 2017 12:52:58 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, keescook@chromium.org, labbott@fedoraproject.org Cc: kernel-hardening@lists.openwall.com, will.deacon@arm.com, catalin.marinas@arm.com, Ard Biesheuvel Date: Thu, 9 Mar 2017 21:52:02 +0100 Message-Id: <1489092729-16871-5-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1489092729-16871-1-git-send-email-ard.biesheuvel@linaro.org> References: <1489092729-16871-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [kernel-hardening] [PATCH v6 04/11] arm64: mmu: map .text as read-only from the outset X-Virus-Scanned: ClamAV using ClamSMTP Now that alternatives patching code no longer relies on the primary mapping of .text being writable, we can remove the code that removes the writable permissions post-init time, and map it read-only from the outset. To preserve the existing behavior under rodata=off, which is relied upon by external debuggers to manage software breakpoints (as pointed out by Mark), add an early_param() check for rodata=, and use RWX permissions if it set to 'off'. Reviewed-by: Laura Abbott Reviewed-by: Kees Cook Reviewed-by: Mark Rutland Signed-off-by: Ard Biesheuvel --- arch/arm64/mm/mmu.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index df377fbe464e..300e98e8cd63 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -416,9 +416,6 @@ void mark_rodata_ro(void) { unsigned long section_size; - section_size = (unsigned long)_etext - (unsigned long)_text; - update_mapping_prot(__pa_symbol(_text), (unsigned long)_text, - section_size, PAGE_KERNEL_ROX); /* * mark .rodata as read only. Use __init_begin rather than __end_rodata * to cover NOTES and EXCEPTION_TABLE. @@ -451,6 +448,12 @@ static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end, vm_area_add_early(vma); } +static int __init parse_rodata(char *arg) +{ + return strtobool(arg, &rodata_enabled); +} +early_param("rodata", parse_rodata); + /* * Create fine-grained mappings for the kernel. */ @@ -458,7 +461,14 @@ static void __init map_kernel(pgd_t *pgd) { static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_init, vmlinux_data; - map_kernel_segment(pgd, _text, _etext, PAGE_KERNEL_EXEC, &vmlinux_text); + /* + * External debuggers may need to write directly to the text + * mapping to install SW breakpoints. Allow this (only) when + * explicitly requested with rodata=off. + */ + pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; + + map_kernel_segment(pgd, _text, _etext, text_prot, &vmlinux_text); map_kernel_segment(pgd, __start_rodata, __init_begin, PAGE_KERNEL, &vmlinux_rodata); map_kernel_segment(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC, &vmlinux_init);