From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932459AbdHWR6l (ORCPT ); Wed, 23 Aug 2017 13:58:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:47416 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932396AbdHWR6j (ORCPT ); Wed, 23 Aug 2017 13:58:39 -0400 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 Cc: Kees Cook , Eric Anholt , Stefan Wahren , Phil Elwell , linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, Matthias Reichl 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 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) --- 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); } -- 2.9.5 From mboxrd@z Thu Jan 1 00:00:00 1970 From: rostedt@goodmis.org (Steven Rostedt) Date: Wed, 23 Aug 2017 13:58:36 -0400 Subject: [PATCH] Arm: mm: ftrace: Only set text back to ro after kernel has been marked ro Message-ID: <20170823135836.52fb44fc@gandalf.local.home> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org 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 at 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 at lists.infradead.org Cc: linux-arm-kernel at lists.infradead.org Cc: stable at vger.kernel.org Fixes: 80d6b0c2ee ("ARM: mm: allow text and rodata sections to be read-only") Signed-off-by: Steven Rostedt (VMware) --- 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); } -- 2.9.5