From patchwork Wed Aug 23 17:58:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 9918149 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 4AC25602CB for ; Wed, 23 Aug 2017 18:00:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D65A289F1 for ; Wed, 23 Aug 2017 18:00:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 31FE1289F4; Wed, 23 Aug 2017 18:00:58 +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=-2.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B8B0A289F3 for ; Wed, 23 Aug 2017 18:00:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:To:From :Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=zARiP67iDLhSu4PmgXvr6sh2rETERFPFFHaUJPzl9xc=; b=odnKOrLhW6WIxS SHzbUUv7hQhBIhDKXJlEiP+Op8AcdIgIn2tEIDtoSFMrcbBAirmG/1DYbjZwyeFPOaYK1MpZKSvj2 ibfYr3tsimaZ4LxM1D0RmM6DiBqosUS47PEU2klRDkAf5NWgIlMda+iilqOT4veAIJG/oY7WZ9u3a O5VhZ2acgjjs4i2xXDsCepBpLDsdi/8Nb5BxIMCB5J7XSS4Y1GyQ8WVGRRtfErLN6cMjyZrnV6xum W+QCVQMKnMKk0H2TFqn9x0PZTp422gsar2Vg0xnsu+LevWYJBV5YWS29TIGOEp90D+DtI8T9OQyyk 1lTudivAXn+UZ0E4se7g==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dkZxT-0004YI-81; Wed, 23 Aug 2017 18:00:51 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dkZvg-0001Bz-90; Wed, 23 Aug 2017 17:59:26 +0000 Received: from gandalf.local.home (cpe-67-246-153-56.stny.res.rr.com [67.246.153.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1CA7C2170C; Wed, 23 Aug 2017 17:58:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1CA7C2170C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=goodmis.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=rostedt@goodmis.org Date: Wed, 23 Aug 2017 13:58:36 -0400 From: Steven Rostedt To: LKML , Russell King Subject: [PATCH] Arm: mm: ftrace: Only set text back to ro after kernel has been marked ro Message-ID: <20170823135836.52fb44fc@gandalf.local.home> X-Mailer: Claws Mail 3.14.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170823_105900_972450_BD416AD8 X-CRM114-Status: GOOD ( 15.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Wahren , Kees Cook , Matthias Reichl , Phil Elwell , Eric Anholt , linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP ftrace needs to modify the kernel text in order to enable function tracing. For security reasons, the kernel text is marked to read-only (ro) at the end of system bootup. When enabling function tracing after that, ftrace calls arch specific code that needs to enable the modification of kernel text while ftrace does the update, and reset it back again when finished. The issue arises when function tracing is enabled during system bootup. The text hasn't been marked as read-only yet, but the same code to modify the kernel is executed, and when it is finished, it will cause the kernel to become read-only. This causes issues for other init code that requires modification of kernel text during system bootup. This appears to cause issue with Raspberry Pi 2. By implementing the feature that is used in x86 to deal with this issue, it fixes the problem. The solution is simple. Have a variable (kernel_set_to_readonly) get set when the system finished boot and marks the kernel to readonly. If that variable is not set, both kernel_set_to_readonly() and kernel_set_to_rw() return without doing any modifications. Those functions are used by ftrace to change the permissions of the kernel text. By not doing anything, ftrace will not mess with the permissions when it is enabled at system bootup. Link: http://lkml.kernel.org/r/20170821153402.7so2u364htvt6tnf@camel2.lan Link: https://github.com/raspberrypi/linux/issues/2166#issuecomment-323355145 Reported-by: Matthias Reichl Cc: Russell King Cc: Kees Cook Cc: Eric Anholt Cc: Stefan Wahren Cc: Phil Elwell Cc: linux-rpi-kernel@lists.infradead.org Cc: linux-arm-kernel@lists.infradead.org Cc: stable@vger.kernel.org Fixes: 80d6b0c2ee ("ARM: mm: allow text and rodata sections to be read-only") Signed-off-by: Steven Rostedt (VMware) Tested-by: Matthias Reichl Acked-by: Kees Cook --- arch/arm/mm/init.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c index ad80548..fd75f38 100644 --- a/arch/arm/mm/init.c +++ b/arch/arm/mm/init.c @@ -745,19 +745,29 @@ static int __mark_rodata_ro(void *unused) return 0; } +static int kernel_set_to_readonly; + void mark_rodata_ro(void) { + kernel_set_to_readonly = 1; + stop_machine(__mark_rodata_ro, NULL, NULL); } void set_kernel_text_rw(void) { + if (!kernel_set_to_readonly) + return; + set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false, current->active_mm); } void set_kernel_text_ro(void) { + if (!kernel_set_to_readonly) + return; + set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true, current->active_mm); }