From patchwork Thu Mar 9 08:25:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9612659 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 64678602B4 for ; Thu, 9 Mar 2017 08:26:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 25DF928614 for ; Thu, 9 Mar 2017 08:26:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 16AEE28632; Thu, 9 Mar 2017 08:26:16 +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 D258628649 for ; Thu, 9 Mar 2017 08:26:09 +0000 (UTC) Received: (qmail 11684 invoked by uid 550); 9 Mar 2017 08:25:45 -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 10099 invoked from network); 9 Mar 2017 08:25:39 -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=UUM0sDiIify2ZveeXDOHh6dCra16W29L7hWXJcSbjPNDAxbNCXmh9HnfUioTtXuECM u7E3h51qnKMHR0f5rih7eglLtomWX1w6DGg2W4vYkEuYZI4lGFP/71CKZfTeUTEYMEhc /xyWk0pXsxEtdag23910qq/SLAA9QFbtjVEII= 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=DAvcXCkUPlgRSxLf733nxn1iSCue4mlzRM8+H/7lr8DY3xw9abRvwD4IJnadLT9TZC lvCv4kQhvTcg0VL0qKqiUohzRm9audRe7ex1vFbhOCMJRKg83cv9mA3dON6jwtXca9tO +ucr8IvgVZKwh9gpMGNVolCZRnNPAom9nUQpWibk2zTroPtai2Ojbsb9LwhpaAlvmxOP ItUxKBj5hIIgwjpFMTA3aumpPhf0hJf7krQ05WaMjgs4M/wuQFcDFe/VLrhScej/sY4e M7t9OfOobyvh6yIgqmthRoxWeDx6e7ydGFknNYqNbvNxCB4dPS1B74yl40+d59WesZHd 7WOA== X-Gm-Message-State: AMke39mnGGLRYcCtbAdzkN9VFIvGzb4JTOFR+zaK0mD7EhCtEPqCo4bPtXxGW9h7s1RmAfmA X-Received: by 10.223.165.138 with SMTP id g10mr9790486wrc.105.1489047928479; Thu, 09 Mar 2017 00:25:28 -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, kvmarm@lists.cs.columbia.edu, marc.zyngier@arm.com, Ard Biesheuvel Date: Thu, 9 Mar 2017 09:25:06 +0100 Message-Id: <1489047912-642-5-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1489047912-642-1-git-send-email-ard.biesheuvel@linaro.org> References: <1489047912-642-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [kernel-hardening] [PATCH v5 04/10] 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);